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,111 questions
0 comments No comments
{count} votes

1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 103.9K Reputation points MVP
    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