Question : Cursor in SP, cursor not open error


I've a trigger on table SynchReceiver to redirect insert records to another two tables,
SynchMessage and SynchDeliver only if BUSSUNITNBR field has value.  

The schema for the three tables are:

SynchReceiver

ReceiverID
RetPlantID
Status
Content
BUSSUNITNBR


SynchMessage

MessageID
RetPlantID
DateTime
IsDelivered
Content

SynchDeliver

DeliverID
RetPlantID
Status
Content
MessageID
BUSSUNITNBR

And my testing script is

INSERT INTO SynchReceiver
      (RetPlantID, Status, Content, BUSSUNITNBR)
VALUES      ('101', 0, '101new', '901902')



here's my trigger with a cursor in it.  When I tested it, I got error message in query analyzer:
Cursor is not open.
The statement has been terminated.

I also tested the select statement for the cursor, which is working fine and I got two records back.  




SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


ALTER     TRIGGER [Synchreceiver_Check]
ON      [dbo].[SynchReceiver]
INSTEAD OF INSERT
AS

DECLARE
      @RetPlantID varchar(6),
      @UnitID varchar(6),
      @MaxMessageID int,
      @MaxDeliverID int,
      @MaxReceiverID int,
      @retid varchar(6)

      SELECT @UnitID = BUSSUNITNBR
      FROM inserted
      
      IF (@UnitID <> '')
      -- Message for Business Unit
            BEGIN
                  SELECT @RetPlantID = RetPlantID
                  FROM inserted
                  
                  -- Move the record over to SynchMessage
                  SELECT @MaxMessageID = Max(MessageID)
                  FROM [SynchMessage]
            
                  INSERT INTO [SynchMessage]
                        (MessageID, RetPlantID, [DateTime], IsDelivered, Content)
                  SELECT ISNULL(@MaxMessageID,0) + 1, i.RetPlantID, GETDATE(), 0, i.Content
                  FROM inserted i

                  DECLARE retplantscursor CURSOR
                  FOR
                        SELECT      RETPLANTNBR
                        FROM      RetreadPlant
                        WHERE      BUSSUNITNBR = @UnitID
                        AND      RETPLANTNBR <> @RetPlantID
                  
                  OPEN      retplantscursor
                  
                  FETCH NEXT FROM retplantscursor INTO @retid

                  WHILE (@@FETCH_STATUS = 0)
                  BEGIN      
                        -- Add reference to deliver table
                        SELECT @MaxDeliverID = Max(DeliverID)
                        FROM [SynchDeliver]
                  
                        INSERT INTO [SynchDeliver]
                              (DeliverID, RetPlantID, MessageID, BUSSUNITNBR)
                        VALUES (ISNULL(@MaxDeliverID,0) + 1, @retid, ISNULL(@MaxMessageID,0) + 1, @UnitID)

                        FETCH NEXT FROM retplantscursor INTO @retid
                  END

                  CLOSE retplants_cursor
                  DEALLOCATE retplants_cursor
            END
      ELSE
            -- Message for CIMS
            BEGIN
                  SELECT @MaxReceiverID = Max(ReceiverID)
                  FROM [SynchReceiver]
            
                  INSERT INTO [SynchReceiver]
                        (ReceiverID, RetPlantID, Status, Content, BUSSUNITNBR)
                  SELECT ISNULL(@MaxReceiverID,0) + 1, i.RetPlantID, i.Status, i.Content, NULL
                  FROM inserted i
            END




GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

Answer : Cursor in SP, cursor not open error

Your close and deallocate statements use the underscore character and your declare, open and fetches don't.

OPEN     retplantscursor

CLOSE retplants_cursor

Paul
Random Solutions  
 
programming4us programming4us