Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

Cursor + Temporal table

$
0
0

Hi guys,

I am quite new with sql, so apologies in advance if I am asking a silly question. 

I need to have a cursor that reads info from a temporal table.

Could you take a look at the following query and tell me why I am having an error that says 'Must declare the table variable "@mylist".' when I do have declared it? Thanks!


SET NOCOUNT ON
DECLARE @mylist TABLE (Debtor_Code varchar(max), DummyAuthoriser int)
INSERT INTO @mylist (Debtor_Code, DummyAuthoriser)  
VALUES 
('danitest1', 1)
,('danitest2', 0)
,('danitest3', 1)

DECLARE @SQL_SCRIPT nvarchar(max)
DECLARE @Debtor_Code VARCHAR(255)
DECLARE my_cursor CURSOR

FOR SELECT Debtor_Code FROM @mylist
OPEN my_cursor
FETCH NEXT FROM my_cursor INTO @Debtor_Code
WHILE @@FETCH_STATUS = 0
BEGIN

       Set @SQL_SCRIPT = '
  select ''{Debtor_Code}'' from @mylist
       '

SET @SQL_SCRIPT = REPLACE(@SQL_SCRIPT, '{Debtor_Code}', @Debtor_Code)
EXECUTE (@SQL_SCRIPT)
    FETCH NEXT FROM my_cursor INTO @Debtor_Code
END
CLOSE my_cursor
DEALLOCATE my_cursor;
SET NOCOUNT OFF



Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>