Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6957583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:01:20+00:00 2026-05-27T15:01:20+00:00

I have the trigger in one table and would like to read UserId value

  • 0

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 error on UPDATED

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;

    DECLARE 
    @UserId INT,

    SELECT @UserId = INSERTED.UserId FROM INSERTED, DELETED

    UPDATE dbo.[User] SET CreditsLeft = CreditsLeft - 1 WHERE Id = @UserId
END
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:01:21+00:00Added an answer on May 27, 2026 at 3:01 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have a trigger to perform following operation for inserted records:
I have a trigger that stores changes made in a separate table when a
I have an auditing trigger that automatically places the time something was updated and
I would like to have multiple users that share the same tables in the
i have an orders table that has a userID column i have a user
I want to create a MySQL trigger that updates a table everytime one of
Is there a way to have the callback of one AJAX trigger another AJAX
I have this Trigger in Postgresql that I can't just get to work (does
I have a trigger to check for a single column in a table, when
If I have a trigger before the update on a table, how can I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.