SQL Server : trigger how to read value for Insert, Update, Delete

后端 未结 3 2044
予麋鹿
予麋鹿 2021-02-04 01:25

I have the trigger in one table and would like to read UserId value when a row is inserted, updated or deleted. How to do that? The code below does not work, I get

相关标签:
3条回答
  • 2021-02-04 02:12

    Here is the syntax to create a trigger:

    CREATE TRIGGER trigger_name
    ON { table | view }
    [ WITH ENCRYPTION ]
    {
        { { FOR | AFTER | INSTEAD OF } { [ INSERT ] [ , ] [ UPDATE ] [ , ] [ DELETE ] }
            [ WITH APPEND ]
            [ NOT FOR REPLICATION ]
            AS
            [ { IF UPDATE ( column )
                [ { AND | OR } UPDATE ( column ) ]
                    [ ...n ]
            | IF ( COLUMNS_UPDATED ( ) { bitwise_operator } updated_bitmask )
                    { comparison_operator } column_bitmask [ ...n ]
            } ]
            sql_statement [ ...n ]
        }
    } 
    

    If you want to use On Update you only can do it with the IF UPDATE ( column ) section. That's not possible to do what you are asking.

    0 讨论(0)
  • 2021-02-04 02:24

    There is no updated dynamic table. There is just inserted and deleted. On an UPDATE command, the old data is stored in the deleted dynamic table, and the new values are stored in the inserted dynamic table.

    Think of an UPDATE as a DELETE/INSERT combination.

    0 讨论(0)
  • 2021-02-04 02:29

    Please note that inserted, deleted means the same thing as inserted CROSS JOIN deleted and gives every combination of every row. I doubt this is what you want.

    Something like this may help get you started...

    SELECT
      CASE WHEN inserted.primaryKey IS NULL THEN 'This is a delete'
           WHEN  deleted.primaryKey IS NULL THEN 'This is an insert'
                                            ELSE 'This is an update'
      END  as Action,
      *
    FROM
      inserted
    FULL OUTER JOIN
      deleted
        ON inserted.primaryKey = deleted.primaryKey
    


    Depending on what you want to do, you then reference the table you are interested in with inserted.userID or deleted.userID, etc.


    Finally, be aware that inserted and deleted are tables and can (and do) contain more than one record.

    If you insert 10 records at once, the inserted table will contain ALL 10 records. The same applies to deletes and the deleted table. And both tables in the case of an update.


    EDIT Examplee Trigger after OPs edit.

    ALTER TRIGGER [dbo].[UpdateUserCreditsLeft] 
      ON  [dbo].[Order]
      AFTER INSERT,UPDATE,DELETE
    AS 
    BEGIN
    
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
    
      UPDATE
        User
      SET
        CreditsLeft = CASE WHEN inserted.UserID IS NULL THEN <new value for a  DELETE>
                           WHEN  deleted.UserID IS NULL THEN <new value for an INSERT>
                                                        ELSE <new value for an UPDATE>
                      END
      FROM
        User
      INNER JOIN
        (
          inserted
        FULL OUTER JOIN
          deleted
            ON inserted.UserID = deleted.UserID  -- This assumes UserID is the PK on UpdateUserCreditsLeft
        )
          ON User.UserID = COALESCE(inserted.UserID, deleted.UserID)
    
    END
    


    If the PrimaryKey of UpdateUserCreditsLeft is something other than UserID, use that in the FULL OUTER JOIN instead.

    0 讨论(0)
提交回复
热议问题