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
  • 3 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

I have two relational tables: the Profiles table which contains 3 kinds of user
I've got the following problem: I have two tables: (simplified) +--------+ +-----------+ | User
I have a simple database with two tables. Users and Configurations. A user has
I have two models, user and group. I also have a joining table groups_users.
Lets say I have two tables, one for transactions, and another table who's primary
I have two tables from two different databases. For example: Table: Articles1 - Database:
I have a PostgreSQL database with two tables named user and group. They have
Hi I have two tables (users & friends) I need to connect to the
I have two classes, namely User & UserProfile User contains(few attributes like):- userId loginId
I have two tables called lawyer & client.And they are the users of my

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.