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 7413295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:40:25+00:00 2026-05-29T06:40:25+00:00

I have a trigger in SQL Server 2005 that is used to track audit

  • 0

I have a trigger in SQL Server 2005 that is used to track audit changes on specific fields. We track the changes in a separate table. This trigger works as expected for the single record entries which is how it was intended but now they want to track changes made to the column via a bulk update.

Current trigger:

CREATE TRIGGER [dbo].[trg_LogChanges]
   ON [dbo].[Test]
   FOR UPDATE
AS 

DECLARE @TableName VARCHAR(100) ,
    @UpdatedDate smalldatetime ,
    @UpdatedBy uniqueidentifier

SELECT @TableName = 'dbo.Test'

IF(SELECT COUNT(*) FROM INSERTED) = 1
    BEGIN
       IF(SELECT LastModifiedDate FROM INSERTED) Is Null
        SET @UpdatedDate = getdate()
       ELSE
        SET @UpdatedDate = (SELECT LastModifiedDate FROM INSERTED)

       IF(SELECT LastModifiedBy FROM INSERTED) Is Null
        SET @UpdatedBy = '11111111-1111-1111-1111-111111111111'
       ELSE
        SET @UpdatedBy = (SELECT LastModifiedBy FROM INSERTED)

            IF UPDATE (ActDate)
        BEGIN
        INSERT INTO dbo.LogChanges
        (
            ChangeType
            , TableName
            , RecordGuid
            , FieldName
            , OldValue
            , NewValue
            , UpdatedBy
            , UpdatedDate
        )
        SELECT 
            'U'
            , @TableName
            , d.Guid
            , 'ActDate'
            , d.ActDate
            , i.ActDate
            , @UpdatedBy
            , @UpdatedDate
        FROM INSERTED i
        INNER JOIN DELETED d
            on i.Guid = d.Guid
        WHERE 
            (d.ActDate IS NULL AND i.ActDate IS NOT NULL)
            OR (d.ActDate IS NOT NULL AND i.ActDate IS NULL)
            OR (d.ActDate <> i.ActDate)
       END
        -- this keeps going for each field that we need to get the Audit Trail on
    END
ELSE
    BEGIN
        -- now I need to track for multiple records
        -- I tried changing the WHERE clause above to see if it would work for bulk updates
        INSERT INTO...
        SELECT...
        WHERE 
    (
        (d.ActDate IS NULL AND i.ActDate IS NOT NULL)
        OR (d.ActDate IS NOT NULL AND i.ActDate IS NULL)
        OR (d.ActDate <> i.ActDate)
    )
    AND d.ActDate IN (SELECT d.ActDate FROM DELETED d)
    END

This code doesn’t work for multiple records it throws an error:

Subquery returned more than 1 value. This is not permitted when the subquery 
follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

How can I alter my current trigger to work for a bulk update. Do I have to use a cursor to do it? If so, then can someone offer some sample 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-29T06:40:26+00:00Added an answer on May 29, 2026 at 6:40 am

    How are you setting @UpdatedDate and @UpdatedBy when there is more than one record? I suspect you are doing something like this, which is what you are doing for one record.

    IF(SELECT LastModifiedBy FROM INSERTED) Is Null
      SET @UpdatedBy = '11111111-1111-1111-1111-111111111111'
    ELSE
     SET @UpdatedBy = (SELECT LastModifiedBy FROM INSERTED)
    

    The SET under ELSE will cause the error if there is more than one row in INSERTED.

    Try this instead

    INSERT INTO dbo.LogChanges
    (
    ChangeType
    , TableName
    , RecordGuid
    , FieldName
    , OldValue
    , NewValue
    , UpdatedBy
    , UpdatedDate
    )
    SELECT 
    'U'
    , @TableName
    , d.Guid
    , 'ActDate'
    , d.ActDate
    , i.ActDate
    , ISNULL(LastModifiedBy, '11111111-1111-1111-1111-111111111111')
    , ISNULL(LastModifiedDate, getdate())
    FROM INSERTED i
    INNER JOIN DELETED d
        on i.Guid = d.Guid
    WHERE (d.ActDate IS NULL AND i.ActDate IS NOT NULL)
        OR (d.ActDate IS NOT NULL AND i.ActDate IS NULL)
        OR (d.ActDate <> i.ActDate)
    

    This should work for any number of records, including just one. Note that I am not checking UPDATE(ActDate). No records will be returned if ActDate did not change.

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

Sidebar

Related Questions

I have a table in a SQL Server 2005 database with a trigger that
I have a trigger that stores changes made in a separate table when a
I am working with SQL Server 2005 and I have trigger on a table
With sql Server 2005. I have declared a trigger that get fired AFTER INSERT,
I am using SQL Server 2005. I have a site that people can vote
I am having a problem with a trigger in SQL Server 2005, I have
FYI: SQL Server 2005 I have a database user account (user_web) that has the
I use SQL Server 2005 and have the below question: On a table A
I'm using SQL Server 2005. I have a field that must either contain a
i have a website that uses nhibernate to speak to SQL Server 2005. 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.