SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


TSQL Error using Cursor to loop through sysdatabases (Syntax Error)


TSQL Error using Cursor to loop through sysdatabases (Syntax Error)

Author
Message
john monroe
john monroe
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 16
I am trying to loop through sysdatabases to get all databases that meet a naming convention and then for each one found run a select statement on another table in that database.

I am getting a syntax error on line 24: Incorrect syntax near @dataname. MSG 170 Level 15, state 1, line 24


Thanks for any help in advance.


{START SCRIPT}****************************************************************
SET QUOTED_IDENTIFIER ON

DECLARE @fillfactor varchar(2)
DECLARE @tablename varchar(30)
DECLARE @tablename_header varchar(75)
DECLARE @dataname varchar(255)
DECLARE @dataname_header varchar(75)


DECLARE datanames_cursor CURSOR FOR SELECT name FROM dbo.sysdatabases WHERE name like '%somefilter%'

Open datanames_cursor

FETCH NEXT FROM datanames_cursor INTO @dataname
WHILE (@@fetch_status <> -1)
BEGIN
IF (@@fetch_status = -2)
FETCH NEXT FROM datanames_cursor INTO @dataname
BEGIN
--PRINT @dataname
PRINT @dataname
SET @dataname = '"'+@dataname+'"'
PRINT @dataname
--USE @dataname
--SELECT UserInfo.tp_Login
--FROM Sites INNER JOIN
--UserInfo ON Sites.OwnerID = UserInfo.tp_ID

FETCH NEXT FROM datanames_cursor INTO @dataname
END

END

DEALLOCATE datanames_cursor
Koji Matsumura
Koji Matsumura
Old Hand
Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)

Group: General Forum Members
Points: 397 Visits: 1918
Although nothing is returned, your script generates no syntax (or any) error.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90870 Visits: 41151
Line 24 is the USE statement that you currently have commented out... the operand of the USE statement cannot be a variable. You will need to use dynamic SQL.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
john monroe
john monroe
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 16
I am not sure how to use dynamic SQL.

But I found a reference to using CURSOR that does not take an argument as input. So please tell me if there is a different way to do what I need to do.

Basically I want to use the output of dbo.sysdatabases to run a select statement in each database calling a specific table.

I cheated by creating a stored procedure in each DB and then using an INSERT to a temp table for the output of each SP and then adding that as the report. But I have to add the SP to each DB and there are hundreds of DB's. It would be much easier if I could just use each DB Name in sysdatabases as the argument.

Thanks for your help in advance.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90870 Visits: 41151
...and, if you have less that 250 or so databases, the following will beat the pants off your cursor Wink

--===== Declare local variables
DECLARE @SQL VARCHAR(8000) --Holds the dynamic SQL

--===== Build the dynamic SQL
SELECT @SQL = ISNULL(@SQL+' UNION ALL'+CHAR(10),'')
+ 'SELECT u.tp_Login,''' + Name + ''' FROM [' + Name + ']..Sites s INNER JOIN ['
+ Name + ']..UserInfo u ON s.OwnerID=u.tp_ID'
FROM Master.dbo.SysDatabases
WHERE Name LIKE '%somefilter%'

--===== Execute the dynamic SQL to get the result set
EXEC (@SQL)



--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Koji Matsumura
Koji Matsumura
Old Hand
Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)

Group: General Forum Members
Points: 397 Visits: 1918
search this forum on sp_MSforeachdb
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90870 Visits: 41151
Koji Matsumura (11/5/2007)
search this forum on sp_MSforeachdb


Gosh... I wouldn't use that on a bet... it's got a cursor with some steroid rage going on. I suppose it's ok for the casual "what if", but I sure wouldn't use it for production code.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
john monroe
john monroe
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 16
I forgot to mention that once I have the code running I will be using SSRS to allow users to pull updated reports. So Cursor's will not work either embedded in SP_FOREACHDB or in another location. I am going to research the dynamic SQL Option. Is there a good reference material?
john monroe
john monroe
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 16
Thanks for all the suggestions. Turns out for this application i just had a typo error.

EXEC ('USE ' + @DataName +
'INSERT INTO #TMP1 SELECT ' + @DataName + '.dbo.Sites.FullUrl,'
+ @DataName + '.dbo.UserInfo.tp_Login
FROM ' + @DataName + '.dbo.Sites INNER JOIN
UserInfo ON ' + @DataName + '.dbo.Sites.OwnerID = '
+ @DataName + '.dbo.UserInfo.tp_ID
')

So putting in single quotes with the use statemnt fixed my problem. Just in case anyone wants to know.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90870 Visits: 41151
Thanks for the feedback, John...

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search