July 30, 2008 at 11:28 am
Within SQL Server I am trying to populate a cursor with no luck. After declaring a cursor you populate it with the results of a SELECT statement. The issue I am running into is that it appears that you cannot use the EXEC() function to populate the Cursor (it returns an error for me) and therefore I do not know how to concatenate a variable name with column names within the SELCECT statement to populate the cursor. The SELECT statement I am trying to use to populate the cursor is as follows (@dbname is the name of the database the cursor is running within and various db names will be passed into it through another cursor).
SELECT @dbname, PagePID, PageFID, OBJECTID FROM #DataPages WHERE [PAGETYPE] = 1
I'm not sure if anyone has come across this but any help would be greatly appreciated
Thanks!
July 30, 2008 at 12:34 pm
I'm not sure what you are trying to capture for data but if you google sp_MSforeachdb you may find out you can use this undocumented system procedure to accomplish your task. I don't know of anyway to declare a cursor and assign the select statement using dynamic sql.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
July 30, 2008 at 12:39 pm
Populate a temp table with the results of your dynamic SQL select statement, and then open the cursor on a select from the temp table.
July 30, 2008 at 9:19 pm
Jack - I was hoping to stay away from the SP but I may have no other choice.
Thanks for the reply.
July 31, 2008 at 3:52 am
Is this what you want??
DECLARE @dbname varchar(255),@query varchar(255)
DECLARE dbname CURSOR FOR
select name from master..sysdatabases where dbid > 6
OPEN dbname
FETCH NEXT FROM dbname INTO @dbname
WHILE(@@FETCH_STATUS = 0)
BEGIN
SET @query = 'SELECT '+@dbname+', PagePID, PageFID, OBJECTID FROM #DataPages WHERE [PAGETYPE] = 1'
exec (@query)
FETCH NEXT FROM dbname INTO @dbname
END
CLOSE dbname
DEALLOCATE dbname
"-=Still Learning=-"
Lester Policarpio
August 1, 2008 at 7:05 am
I accomplished this task by declaring synonyms
there is a "master" application database which has a table containing all the other databases
the cursor creates, using dynamic SQL, synonyms for every table needed in the procedure
for each database
it then invokes a stored procedure which references those synonyms
and so forth
August 1, 2008 at 9:46 pm
With the advent of VARCHAR(MAX), there's just no need for cursors anymore... for example...
DECLARE @SQL VARCHAR(MAX)
SELECT @SQL = COALESCE(@SQL+CHAR(10)+' UNION ALL --------------------'+CHAR(10),'')
+REPLACE(
' SELECT '' '' AS DbName,*
FROM [ ].sys.SysObjects
WHERE OBJECTPROPERTY(ID,''IsMsShipped'') = 0'
,' ',Name)
FROM Master.sys.SysDataBases
WHERE DBID > 4
AND Name NOT LIKE 'ReportServer$%'
PRINT @SQL
EXEC (@SQL)
--Jeff Moden
Change is inevitable... Change for the better is not.
August 5, 2008 at 2:03 pm
Jeff Moden (8/1/2008)
With the advent of VARCHAR(MAX), there's just no need for cursors anymore... for example...
DECLARE @SQL VARCHAR(MAX)
SELECT @SQL = COALESCE(@SQL+CHAR(10)+' UNION ALL --------------------'+CHAR(10),'')
+REPLACE(
' SELECT '' '' AS DbName,*
FROM [ ].sys.SysObjects
WHERE OBJECTPROPERTY(ID,''IsMsShipped'') = 0'
,' ',Name)
FROM Master.sys.SysDataBases
WHERE DBID > 4
AND Name NOT LIKE 'ReportServer$%'
PRINT @SQL
EXEC (@SQL)
I believe there is still a limit of 256 tables in a query (haven't verified that, so I could be wrong), so if you have a lot of databases, you could run into a problem.
Of course, you could modify that code to do individual inserts for each database into a temp table.
August 5, 2008 at 5:42 pm
Michael Valentine Jones (8/5/2008)
I believe there is still a limit of 256 tables in a query (haven't verified that, so I could be wrong), so if you have a lot of databases, you could run into a problem.Of course, you could modify that code to do individual inserts for each database into a temp table.
Yeah... I keep forgetting about that limit. Thanks for the reminder... and you're absolutely correct/good idea... code could be easily modified to do individual inserts.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 6, 2008 at 1:37 pm
Jeff Moden (8/5/2008)
Michael Valentine Jones (8/5/2008)
I believe there is still a limit of 256 tables in a query (haven't verified that, so I could be wrong), so if you have a lot of databases, you could run into a problem.Of course, you could modify that code to do individual inserts for each database into a temp table.
Yeah... I keep forgetting about that limit. Thanks for the reminder... and you're absolutely correct/good idea... code could be easily modified to do individual inserts.
I was just picking a few nits, but I sometimes regret showing people things like that.
A few weeks ago someone was getting some really weird errors when a query they wrote with 8000 unions in it failed. I think they overflowed some internal SQL Server counter.
April 14, 2023 at 3:40 am
Within SQL Server I am trying to populate a cursor with no luck. After declaring a cursor you populate it with the results of a SELECT statement. The issue I am running into is that it appears that you cannot use the EXEC() function to populate the Cursor (it returns an error for me) and therefore I do not know how to concatenate a variable name with column names within the SELCECT statement to populate the cursor. The SELECT statement I am trying to use to populate the cursor is as follows (@dbname is the name of the database the cursor is running within and various db names will be passed into it through another cursor). SELECT @dbname, PagePID, PageFID, OBJECTID FROM #DataPages WHERE [PAGETYPE] = 1 I'm not sure if anyone has come across this but any help would be greatly appreciated Thanks!
I have the same issue as you. I want to EXEC a stored procedure and use the returned result set in a cursor for further processing. But as you mentioned DECLARE CURSOR syntax does NOT allow user to populate the cursor with the result set returned from EXEC statement and it only works when the user provide a SELECT statement after "FOR"
DECLARE CURSOR cursor_name CURSOR FOR select_statement
April 14, 2023 at 3:40 am
This was removed by the editor as SPAM
April 14, 2023 at 3:44 am
This was removed by the editor as SPAM
April 14, 2023 at 4:03 am
So create a temporary table and insert the records there, and then create the cursor based on that?
(I guess ideally you'd create the temporary objects inside your stored procedure, and then they'd go out of scope once the stored procedure finished processing).
INSERT INTO #MyTempTable
EXEC Db.Schema.StoredProc @Param1='A';
April 17, 2023 at 1:36 am
This was removed by the editor as SPAM
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply