Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
SQL
DECLARE @sql VARCHAR (MAX)
DECLARE @mid AS BIGINT
DECLARE @cursor AS CURSOR

SET @SQL =

SELECT [MERCHANT_ID] from [Reports].[MONAD\karal.ramil].[REF#19414MHTNEEDED]


OPEN @cursor

FETCH @cursor INTO @mid
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @mid
SELECT MERCHANT_NUMBER
INTO #TEMP
FROM [BI].[dbo].[MERCHANT_PROFILE]
WHERE YEAR_MONTH =201503 AND THIS_MHT_TYPE ='MRCH' AND MERCHANT_NUMBER LIKE '%' + @mid
FETCH NEXT
FROM @cursor INTO @mid
END
CLOSE @cursor
DEALLOCATE @cursor

--error-------
Msg 16950, Level 16, State 2, Line 35
The variable '@cursor' does not currently have a cursor allocated to it.
Msg 16950, Level 16, State 2, Line 40
The variable '@cursor' does not currently have a cursor allocated to it.
Msg 16950, Level 16, State 2, Line 52
The variable '@cursor' does not currently have a cursor allocated to it.
Msg 16950, Level 16, State 2, Line 53
The variable '@cursor' does not currently have a cursor allocated to it.
Posted
Comments
John C Rayan 10-Apr-15 14:45pm    
The @SQL should be in single quotes.

SET @SQL = 'SELECT [MERCHANT_ID] from [Reports].[MONAD\karal.ramil].[REF#19414MHTNEEDED]'
Maciej Los 10-Apr-15 17:05pm    
Do not shout!
Philippe Mori 11-Apr-15 10:09am    
Do not use ALL CAPS title. Shouting is aggressive. People don't like that.

1 solution

1. Please do not shout. Do not use all CAPS.
2. See, https://msdn.microsoft.com/en-us/library/ms180169.aspx[^]. You need to use a FOR SELECT statement when you declare the cursor. You are missing select_statement.
3. I'm not sure if it makes a difference; however, when I have done cursors I don't use @ on the cursor name.
 
Share this answer
 
Comments
Maciej Los 10-Apr-15 17:05pm    
Short and to the point!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900