Trigger question

thudangdichoichu 21 Reputation points
2021-09-18T10:40:48.023+00:00

I have 2 trigger, one is for insert and another for delete..
133331-111111212121212.png

but when i run delete query, an error occurred : A cursor with the name 'x' does not exist.
while x is insert trigger cursor name !
133280-12122424234234234324234.png

here is trigger for deleted
133256-e-cvcccccccccccccccccccccccc.png

Can anyone explain for me, thanks

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,361 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114.7K Reputation points
    2021-09-18T10:53:45.553+00:00

    Try this fix:

    ā€ƒFETCH NEXT FROM xy INTO @delano

    Or redesign the triggers to avoid the cursors. (Make sure that the triggers work in case of multiple inserted and deleted rows).

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 107.2K Reputation points
    2021-09-18T13:06:37.99+00:00

    Note that in the DELETE trigger, the FETCH statement is missing at the end of the loop, so if you delete more then one row, the trigger will run forever.

    But it is complete insanity to have a cursor here. I don't know your details, but this is my guess how the DELETE trigger should look like:

    CREATE TRIGGER dbo.hyudon ON dbo.dothang AS
    UPDATE stock
    SET     souluongton += d.sl
    FROM  stock st
    JOIN   (SELECT ordered, SUM(sl) AS sl
            FROM deleted
           GROUP BY ordered) AS d ON d.orderd = st.id
    
    0 comments No comments