| |

VerySource

 Forgot password?
 Register
Search
View: 647|Reply: 2

A question about cursors, please advise!

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-10 18:00:01
| Show all posts |Read mode
I wrote the following piece of code and executed it in the analyzer. The result is "the cursor named 'cursor_problemrecord' does not exist."

DECLARE @sql nvarchar (1000)
DECLARE @eid int
DECLARE @bgndate smalldatetime
DECLARE @enddate smalldatetime
DECLARE @otherwhere nvarchar (200)

DECLARE @ ma_6_1 nvarchar (2000)
DECLARE @tmpgrade int
DECLARE @tmpplaceincomp nvarchar (30)
DECLARE @tmpydate int
SET @eid = 1
SET @bgndate = convert (smalldatetime, '2005-12-1')
SET @enddate = convert (smalldatetime, '2017-1-1')
SET @otherwhere = N 'status = -3'

SET @sql = N'DECLARE cursor_problemrecord INSENSITIVE CURSOR FOR (SELECT grade, placeincomp, Year (bgndate) FROM problem_record WHERE recscid = 31 AND receid = @ eid AND (bgndate> = @ bgndate AND enddate <@enddate) '+ N' AND '+ @ otherwhere + N' ORDER BY bgndate DESC) '
EXEC sp_executesql @ sql, N '@ eid int, @ bgndate smalldatetime, @ enddate smalldatetime', @ eid, @ bgndate, @ enddate

OPEN cursor_problemrecord
FETCH NEXT FROM cursor_problemrecord
INTO @ tmpgrade, @ tmpplaceincomp, @ tmpydate
WHILE @@ FETCH_STATUS = 0
BEGIN
SET @ ma_6_1 = @ ma_6_1 + CONVERT (nvarchar (4), @ tmpydate) + N '#' + CONVERT (nvarchar (4), @ tmpgrade) + N '#' + @ tmpplaceincomp + N '@'
FETCH NEXT FROM cursor_problemrecord
INTO @ tmpgrade, @ tmpplaceincomp, @ tmpydate
END
CLOSE cursor_problemrecord
DEALLOCATE cursor_problemrecord

GO
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-4-7 22:45:01
| Show all posts
SET @sql = N'DECLARE cursor_problemrecord INSENSITIVE CURSOR FOR (SELECT grade, placeincomp, Year (bgndate) FROM problem_record WHERE recscid = 31 AND receid = @ eid AND (bgndate> = @ bgndate AND enddate <@enddate) '+ N' AND '+ @ otherwhere + N' ORDER BY bgndate DESC) '
EXEC sp_executesql @ sql, N '@ eid int, @ bgndate smalldatetime, @ enddate smalldatetime', @ eid, @ bgndate, @ enddate

The cursor is defined in a dynamic SQL statement, so the compilation here does not recognize it.

In the execution of dynamic SQL statements, the cursor is returned as a parameter.
(The cursor can only be passed out as a parameter in the stored procedure, not passed in)

EXEC sp_executesql @ sql, N '@ eid int, @ bgndate smalldatetime, @ enddate smalldatetime, cursor_problemrecord cursor output', @ eid, @ bgndate, @ enddate, cursor_problemrecord output

Try
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-4-11 15:15:01
| Show all posts
The result of the execution is "The OUTPUT option cannot be used when passing constants to stored procedures.",
Please trouble again and see what is the problem? Thanks!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list