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

  • Home
  • SEARCH
  • 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 6208295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:47:06+00:00 2026-05-24T05:47:06+00:00

I have two tables User & User Log . User log table basically logs

  • 0

I have two tables User & User Log . User log table basically logs all the changes(insert/update/delete) made to user table.

I have a trigger on User table which is as below:

ALTER TRIGGER [dbo].[TRG_UserLog]
   ON  [dbo].[Users]
   FOR INSERT,UPDATE,DELETE
AS 
-- Declare variables here
DECLARE @UserName  VARCHAR(50)
DECLARE @FirstName  VARCHAR(50)
DECLARE @LastName       VARCHAR(50)
DECLARE @Email  VARCHAR(50)
DECLARE @RoleID   INT
DECLARE @UpdatedBy VARCHAR(50)

BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for trigger here

    IF @@ROWCOUNT = 0
    BEGIN
        RETURN
    END


    IF EXISTS(SELECT * FROM INSERTED)
        --INSERTED / UPDATED
        BEGIN
                SET @UserName   = ( SELECT UserName FROM INSERTED)
                SET @FirstName  = ( SELECT FirstName FROM INSERTED)
                SET @LastName   = ( SELECT LastName FROM INSERTED)
                SET @Email      = ( SELECT Email FROM INSERTED)
                SET @RoleID     = ( SELECT RoleID FROM INSERTED) 
                SET @UpdatedBy  = ( SELECT ModifiedBy FROM INSERTED)
                INSERT INTO UserLog(UserName,FirstName,LastName,Email,RoleID,[DateTime],UpdatedBy)
                    VALUES (@UserName,@FirstName,@LastName,@Email,@RoleID,GETDATE(),@UpdatedBy)
        END
    ELSE
        -- DELETED
        BEGIN
                SET @UserName   = ( SELECT UserName FROM DELETED)
                SET @FirstName  = ( SELECT FirstName FROM DELETED)
                SET @LastName   = ( SELECT LastName FROM DELETED)
                SET @Email      = ( SELECT Email FROM DELETED)
                SET @RoleID     = ( SELECT RoleID FROM DELETED) 
                SET @UpdatedBy  = ( SELECT ModifiedBy FROM DELETED)
                INSERT INTO UserLog(UserName,FirstName,LastName,Email,RoleID,[DateTime],UpdatedBy)
                    VALUES (@UserName,@FirstName,@LastName,@Email,@RoleID,GETDATE(),@UpdatedBy)
        END
END

The above triigger is not inserting data in userlog table when insert/update or delete is made to user table. Is there anything wrong with the code?

  • 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-24T05:47:07+00:00Added an answer on May 24, 2026 at 5:47 am

    This will always be true:

    SET NOCOUNT ON;   -- this sets @@ROWCOUNT to 0
    
    IF @@ROWCOUNT = 0
    BEGIN
        RETURN
    END
    

    because calling SET NOCOUNT ON; affects @@ROWCOUNT, and since no rows are affected by that statement, it’s set to 0. So the rest of the code never runs. At the very least, you would run that code before SET NOCOUNT ON; (or store @@ROWCOUNT in a variable for later use), but as indicated by the other answer, it’s completely unnecessary anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So i have a query that is suppose to get me data from two
I have a fairly standard 'has_many :x, :through => :y' relationship with a user,
If I have users, as well as user groups (like a local astronomy group/club),
I have a small PL/SQL script that I'm using to try and copy data
Here is my scenario: I have a mobile application, which will call a method
Couple questions here: My end goal is to password protect the file logged_in.php. Note:
I am trying to reference a variable stored in a SESSION() from a SESSION()
I'm having a problem getting some data since I'm just starting to use LINQToEntities,
Here's the layout : I'm developing an internal ASP.NET Web App (in VB.NET) with

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.