I have the following query, I want use a nested cursor in my query. How to do this, because it's not running and I am new to SQL Server. Please help me
CHECK TABLE SUGGEST LAT 31.8181 LONG 71.4146
Msg 16915, Level 16, State 1, Procedure Sp_CheckCarStatusMeter, Line 266
A cursor with the name 'ShapeCursor' already exists. ELSE OPEN CURSORMsg 16905, Level 16, State 1, Procedure Sp_CheckCarStatusMeter, Line 296
The cursor is already open.
Code:
DECLARE SuggestCursor CURSOR FOR
SELECT TOP 100
rtha.car_id, rtha.latitude, rtha.longitude
FROM
Carhistory rtha
WHERE
rtha.car_id = 6142 ;
OPEN SuggestCursor;
FETCH NEXT FROM SuggestCursor INTO @CarSuggested, @carlatprevious, @carlongprevious;
WHILE (@@FETCH_STATUS = 0)
BEGIN
PRINT 'CHECK TABLE SUGGEST LAT '+@carlatprevious +' LONG '+ @carlongprevious;
DECLARE ShapeCursor CURSOR FOR
SELECT
g.ID, @carID, g.ShapeType
FROM
tblgeo AS g
WHERE
car_id @ID;
IF (SELECT CURSOR_STATUS('local','ShapeCursor')) >= -1
BEGIN
Print 'DEALLOCATE CURSOR'
--DEALLOCATE ShapeCursor
END
ELSE
Print 'ELSE OPEN CURSOR'
OPEN ShapeCursor;
FETCH NEXT FROM ShapeCursor INTO @ID, @CarIdx, @ShapeType;
WHILE (@@FETCH_STATUS = 0)
BEGIN
STRING_AGGfunction