Is there a way to use parameters in a SQL Server cursor?
I have a parent-child relationship in the database. What I need to do is loop through the parent's query, and using the parent's primary key, got get its children. The issue I am having is that I need to use a parameterized cursor (pass in the key) to do this.
Is there such a thing in SQL Server or a trick to mimic this? I tried doing this, but it didn't work:
DECLARE @value VARCHAR(20);
DECLARE @someKey NUMERIC(19,0);
DECLARE main_curs
CURSOR FOR SELECT value FROM someTable where key = @someKey;
SET @someKey = 12345;
OPEN main_curs
FETCH NEXT FROM main_curs INTO @value;
CLOSE main_curs
DEALLOCATE main_curs
But it seems开发者_Go百科 that it doesn't pick up me setting the @someKey.
Any help on this would be greatly appreciated. Thanks!
UPDATE
I should include more information as I made the example seem too simple. I have multiple @someKey values that I need to use. As mentioned before, I have a parent-child relationship and I can have up to 6 children. So I am getting a list of parents and it's respective columns and iterating through it. While in the WHILE-LOOP, I wanted to get the primary key from the parent and call another cursor to get the child information (different columns returned). So I would do multiple calls to the child cursor with different @someKey values set. Hope that makes sense.
What you need is 2 cursors - one for the parent and one for the child .Make sure the child cursor is DECLARED inside the LOOP not outside.it will not work if you declare outside.
eg :
DECLARE @value VARCHAR(20);
DECLARE @someKey NUMERIC(19,0);
DECLARE main_curs
CURSOR FOR SELECT value FROM someTable where key = @someKey;
SET @someKey = 12345;
OPEN main_curs
FETCH NEXT FROM main_curs INTO @value;
while @@FETCH_STATUS = 0
BEGIN
DECLARE CHILD_CURS CURSOR FOR SELECT VALUE2 FROM CHILDTABLE WHERE value=@value;
open child_curs
fetch next from child_curs into @x,@y
close child_curs
deallocate child_curs
FETCH NEXT FROM main_curs INTO @value;
END
CLOSE main_curs
DEALLOCATE main_curs
Here is how you can declare a cursor with dynamic SQL, using the 'EXEC()' function. Surprisingly this does work. For example:
DECLARE @QuotedDatabase NVARCHAR(128) = QUOTENAME('ReportServer')
DECLARE @ObjectID INT = 389576426
DECLARE @ColumnName NVARCHAR(128)
DECLARE @ColumnType NVARCHAR(128)
DECLARE @DeclareColumnCursor NVARCHAR(4000)
SET @DeclareColumnCursor = '
DECLARE ColumnCursor CURSOR READ_ONLY FORWARD_ONLY FOR
SELECT c.Name, t.Name
FROM ' + @QuotedDatabase + '.sys.columns c
INNER JOIN ' + @QuotedDatabase + '.sys.types t
ON c.user_type_id = t.user_type_id
WHERE c.object_id = ' + CAST(@ObjectID AS NVARCHAR) + '
ORDER BY column_id'
EXEC(@DeclareColumnCursor)
OPEN ColumnCursor
FETCH NEXT FROM ColumnCursor INTO @ColumnName, @ColumnType
PRINT @ColumnName + ',' + @ColumnType
CLOSE ColumnCursor
DEALLOCATE ColumnCursor
One thing you could try is using nested cursors. An example of this is on the bottom of the page titled: Using nested cursors to produce report output.
In another place, someone suggested using a stored procedure (compiled SQL rather than an ad-hoc script) but that doesn't work either. Here's another MWE that shows the issue fairly clearly:
/* Should print:
dbNamein=master dbNameout=master
dbNamein=model dbNameout=model
dbNamein=msdb dbNameout=msdb
*/
create procedure [TestParamsWithOpenCursorStmt]
as
begin
declare @dbNameIn [nvarchar](255) = N'tempdb',
@dbNameOut [nvarchar](255),
@fs [int];
declare dbNames cursor for
select db.[name] from [master].[sys].[databases] db
where db.[name] = @dbNameIn;
while (@dbNameIn != N'msdb') begin
if @dbNameIn = N'tempdb'
set @dbNameIn = N'master'
else if @dbNameIn = N'master'
set @dbNameIn = N'model'
else if @dbNameIn = N'model'
set @dbNameIn = N'msdb';
open dbNames;
fetch next from dbNames into @dbNameOut;
set @fs = @@fetch_status;
if @fs != 0 continue;
raiserror (N'dbNamein=%s dbNameout=%s', 0, 0, @dbNameIn, @dbNameOut) with nowait;
close dbNames;
end;
deallocate dbNames;
end;
go
execute [TestParamsWithOpenCursorStmt];
It appears that the variable (and its value at the time) gets bound to the "declare ... cursor" rather than the open cursor.
you need to set @someKey = 12345;
before Cursor Declaration such as:
SET @someKey = 12345;
DECLARE main_curs
CURSOR FOR SELECT value FROM someTable where key = @someKey;
You seem to have the order of things wrong, and you're not actually doing anything inside the cursor?
DECLARE @value VARCHAR(20);
DECLARE @someKey NUMERIC(19,0);
SET @someKey = 12345; --this has to be set before its used in cursor declaration
DECLARE main_curs
CURSOR FOR SELECT value FROM someTable where key = @someKey;
OPEN main_curs
FETCH NEXT FROM main_curs INTO @value; -- first row is fetched
WHILE @@FETCH_STATUS = 0 -- start the loop
BEGIN
-- do something here with @value
FETCH NEXT FROM main_curs INTO @value; --fetch the next row
END
CLOSE main_curs
DEALLOCATE main_curs
If you want to iterate over a recursive hierarchy use CTEs, see Recursive Queries Using Common Table Expressions. You can declare your cursor over the recursive CTE, eg:
create table test (
id int not null identity(1,1) primary key,
parent_id int null,
data varchar (max));
go
insert into test (parent_id, data) values
(null, 'root'),
(1, 'child 1'),
(1, 'child 2') ,
(2, 'child of child 1'),
(4, 'child of child of child 1');
go
declare @root int = 2;
declare crs cursor for
with cte as (
select id, parent_id, data
from test
where id = @root
union all
select t.id, t.parent_id, t.data
from test t
join cte c on t.parent_id = c.id)
select id, data from cte;
open crs;
declare @id int, @data varchar(max);
fetch next from crs into @id, @data;
while @@fetch_status = 0
begin
print @data;
fetch next from crs into @id, @data;
end
close crs;
deallocate crs;
But most often the recursive CTEs can completely eliminate the need for a cursor.
精彩评论