Question : Cursor method: Error Msg

Whats wrong with this code? Could you pls help

----------------------------------------------------------------
go
declare @INTERNID varchar(500)
declare @STAMP2 binary(500)
declare @STAMP3_B binary(500)
declare @STAMP3_I binary(500)
declare @STAMP3_C binary(500)
declare @STAMP3_M binary(500)
declare @STAMP3_N binary(500)
declare @ID varchar(500)
declare @RMP_ID varchar(500)
declare @resultRMP varchar(100)
declare @result varchar(100)
declare @hsecs numeric
declare @date datetime
set @hsecs = 1
set @date = getdate()
exec shortstamp @date, @hsecs, @result OUTPUT

DECLARE queryRMP CURSOR FOR
SELECT _STAMP2, _ID from RMP where _Status1 = 'Generated'

DECLARE queryBUSSTRA CURSOR FOR
SELECT _STAMP3 from BUS_STRA
join RMP on RMP._STAMP2 = BUS_STRA._STAMP2 where RMP._Status1 = 'Generated'

DECLARE queryINVSTRA CURSOR FOR
SELECT _STAMP3 from INV_STRA
join RMP on RMP._STAMP2 = INV_STRA._STAMP2 where RMP._Status1 = 'Generated'

DECLARE queryCOMCLIE CURSOR FOR
SELECT _STAMP3 from COM_CLIE
join RMP on RMP._STAMP2 = COM_CLIE._STAMP2 where RMP._Status1 = 'Generated'

DECLARE queryMAJCHAL CURSOR FOR
SELECT _STAMP3 from MAJ_CHAL
join RMP on RMP._STAMP2 = MAJ_CHAL._STAMP2 where RMP._Status1 = 'Generated'

DECLARE queryNEXREM CURSOR FOR
SELECT _STAMP3 from NEX_REM
join RMP on RMP._STAMP2 = NEX_REM._STAMP2 where RMP._Status1 = 'Generated'

OPEN queryRMP
FETCH NEXT FROM queryRMP INTO @STAMP2, @RMP_ID

while @@FETCH_STATUS = 0
  BEGIN
  exec shortstamp @date, @hsecs, @result OUTPUT
  UPDATE RMP SET _ID = @result where @STAMP2 = RMP._STAMP2
  set @resultRMP = @result

  OPEN queryBUSSTRA
  FETCH NEXT FROM queryBUSSTRA INTO @STAMP3_B
  while @@FETCH_STATUS = 0
  exec shortstamp @date, @hsecs, @result OUTPUT
  UPDATE BUS_STRA
  SET _RMP_ID = @RMP_ID
  ,_ID = @result
  where @STAMP2 = BUS_STRA._STAMP2
  FETCH NEXT FROM queryBUSSTRA INTO @STAMP3_B
  CLOSE queryBUSSTRA
  DEALLOCATE queryBUSSTRA

  OPEN queryINVSTRA
  FETCH NEXT FROM queryINVSTRA INTO @STAMP3_I
  while @@FETCH_STATUS = 0
  exec shortstamp @date, @hsecs, @result OUTPUT
  UPDATE INV_STRA
  SET _RMP_ID = @RMP_ID
  ,_ID = @result
  where @STAMP2 = INV_STRA._STAMP2
  FETCH NEXT FROM queryINVSTRA INTO @STAMP3_I
  CLOSE queryINVSTRA
  DEALLOCATE queryINVSTRA

  OPEN queryCOMCLIE
  FETCH NEXT FROM queryCOMCLIE INTO @STAMP3_C
  while @@FETCH_STATUS = 0
  exec shortstamp @date, @hsecs, @result OUTPUT
  UPDATE COM_CLIE
  SET _RMP_ID = @RMP_ID
  ,_ID = @result
  where @STAMP2 = COM_CLIE._STAMP2
  FETCH NEXT FROM queryCOMCLIE INTO @STAMP3_C
  CLOSE queryCOMCLIE
  DEALLOCATE queryCOMCLIE
 
  OPEN queryMAJCHAL
  FETCH NEXT FROM queryMAJCHAL INTO @STAMP3_M
  while @@FETCH_STATUS = 0
  exec shortstamp @date, @hsecs, @result OUTPUT
  UPDATE MAJ_CHAL
  SET _RMP_ID = @RMP_ID
  ,_ID = @result
  where @STAMP2 = MAJ_CHAL._STAMP2
  FETCH NEXT FROM queryMAJCHAL INTO @STAMP3_M
  CLOSE queryMAJCHAL
  DEALLOCATE queryMAJCHAL  

  OPEN queryNEXREM
  FETCH NEXT FROM queryNEXREM INTO @STAMP3_N
  while @@FETCH_STATUS = 0
  exec shortstamp @date, @hsecs, @result OUTPUT
  UPDATE NEX_REM
  SET _RMP_ID = @RMP_ID
  ,_ID = @result
  where @STAMP2 = NEX_REM._STAMP2
  FETCH NEXT FROM queryNEXREM INTO @STAMP3_N
  CLOSE queryNEXREM
  DEALLOCATE queryNEXREM
 
FETCH NEXT FROM queryRMP INTO @STAMP2
END
CLOSE queryRMP
DEALLOCATE queryRMP

----------------------------------------------------------------------------
I get these error msgs:
Server: Msg 16915, Level 16, State 1, Line 1
A cursor with the name 'queryBUSSTRA' already exists.
Server: Msg 16915, Level 16, State 1, Line 26
A cursor with the name 'queryINVSTRA' already exists.
Server: Msg 16915, Level 16, State 1, Line 30
A cursor with the name 'queryCOMCLIE' already exists.
Server: Msg 16915, Level 16, State 1, Line 34
A cursor with the name 'queryMAJCHAL' already exists.
Server: Msg 16915, Level 16, State 1, Line 38
A cursor with the name 'queryNEXREM' already exists.

Answer : Cursor method: Error Msg

I notice that you don't actually use the information returned by the FETCH statement.  Are you just trying to update each record in the individual cursor with a distinct value returned by the shortstamp stored proc?  Assuming that each return is, in some way, unique, then you can rewrite your loops like this:

while @@FETCH_STATUS = 0
  BEGIN
  UPDATE RMP SET _ID = @result where @STAMP2 = RMP._STAMP2   -- May want to use WHERE CURRENT OF here
  set @resultRMP = @result

  DECLARE queryBUSSTRA CURSOR FOR
  SELECT _STAMP3 from BUS_STRA
  join RMP on RMP._STAMP2 = BUS_STRA._STAMP2 where RMP._Status1 = 'Generated' AND rmp._Stamp2 = @Stamp2

  OPEN queryBUSSTRA
  FETCH NEXT FROM queryBUSSTRA -- INTO @STAMP3_B -- since not used, do not need to retrieve data value
  while @@FETCH_STATUS = 0
   BEGIN
      exec shortstamp @date, @hsecs, @result OUTPUT
      UPDATE BUS_STRA
      SET _RMP_ID = @RMP_ID
      ,_ID = @result
      where CURRENT OF queryBUSSTRA   -- Updates only the record currently pointed to by the cursor
                                                                     ---@STAMP2 = BUS_STRA._STAMP2
      FETCH NEXT FROM queryBUSSTRA --INTO @STAMP3_B
   END
  CLOSE queryBUSSTRA
  DEALLOCATE queryBUSSTRA

...

etcetera.

Note the differences.  The Cursor definition query restricts based on the @Stamp2 value, limiting the number of entries examined.  The WHERE CURRENT OF option means that you will update *only* the record currently pointed to by your cursor.  I believe that this is what you are working towards.
Random Solutions  
 
programming4us programming4us