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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:53:21+00:00 2026-06-05T12:53:21+00:00

I have a small problem with an update trigger under SQL Server 2008 R2.

  • 0

I have a small problem with an update trigger under SQL Server 2008 R2. I have installed a trigger to log the changes into a log table. This is working fine for me but not when I update multiple rows.

CREATE TRIGGER [dbo].[TR_CompaniesUpdated]
ON  [dbo].[Companies]
AFTER UPDATE
AS 
BEGIN
SET NOCOUNT ON;
DECLARE
    @Return NVARCHAR(MAX), -- Is the JSON of the data
    @ParameterSQL VARCHAR(MAX), -- Is the select we want to retreive in JSON format

    -- Data from of the row updated
    @ID INT,
    @StatusID SMALLINT,

    -- Old data from of the row
    @StatusID2 SMALLINT

-- Declare the cursor
DECLARE InsertedCursor CURSOR FAST_FORWARD FOR
SELECT [ID], [StatusID] FROM INSERTED

-- Create the temporary table with logs so we'll need to do only one insert after this
CREATE TABLE #tempLog(
    [ID] [int] NOT NULL,
    [Data] [nvarchar](max) NOT NULL,
    [ActionID] [int] NOT NULL,
    [DtCreated] [datetime] NOT NULL,
    [CreatedBy] [int] NOT NULL)

-- Save updated values into variables
OPEN InsertedCursor
FETCH NEXT FROM InsertedCursor
INTO @ID, @StatusID

WHILE @@fetch_status = 0
BEGIN
    -- Save old values into variables
    SELECT
        @StatusID2 = [StatusID]
    FROM DELETED
    WHERE [ID] = @ID

    SELECT * INTO #tempTable
    FROM DELETED
    WHERE [ID] = @ID

    -- Build the select only to et the changed data
    SET @ParameterSQL = 'SELECT '
    IF (ISNULL(@StatusID, '') != ISNULL(@StatusID2, '')) SET @ParameterSQL = @ParameterSQL + '[StatusID],'
    IF (@ParameterSQL != 'SELECT ')
    BEGIN
        -- Update the modified date
        UPDATE [dbo].[AssessedLevelCostCentre]
        SET [DtModified] = GETDATE()
        WHERE [ID] = @ID

        SET @ParameterSQL = SUBSTRING(@ParameterSQL, 0, LEN(@ParameterSQL)) + ' FROM #tempTable'

        -- Execute the JSON function to get the result in JSON format
        EXEC [dbo].[GetJSON] @ParameterSQL, @Return = @Return OUTPUT

        -- Insert the Change Log with the old data
        INSERT INTO #tempLog
        SELECT
            [ID] AS [ID],
            @Return AS [Data],
            [ActionID] AS [ActionID],
            CASE
                WHEN [DtModified] IS NULL
                    THEN [DtCreated]
                    ELSE [DtModified]
            END AS [DtModified],
            CASE
                WHEN [ModifiedBy] IS NULL
                    THEN [CreatedBy]
                    ELSE [ModifiedBy]
            END AS [CreatedBy]
        FROM #tempTable
    END
    ELSE
    BEGIN
        UPDATE [dbo].[AssessedLevelCostCentre]
        SET [ModifiedBy] = (SELECT [ModifiedBy] FROM #tempTable)
        WHERE [ID] = @ID
    END

    DROP TABLE #tempTable

    -- Advance the Cursor
    FETCH NEXT FROM InsertedCursor
    INTO @ID, @StatusID
END

CLOSE InsertedCursor
DEALLOCATE InsertedCursor

-- Insert the Change Log with the old data
INSERT INTO [dbo].[AssessedLevelsCostCentersLogs]
SELECT *
FROM #tempLog

DROP TABLE #tempLog
END

As you can see I have an stored procedure than returns me the JSON data, the problem is than the stored procedure get the data from a query and I can’t pass the DELETED or INSERTED tables, that’s the reason I used a temp table that seems to do the trick but adding the ID clause.

UPDATED

I updated the code, now I have a cursor but as I can see the performance of this is really slow. I’ll need this in this way cause I can’t change the application to create the JSON and I think I trigger like this will work fine. Any one have an idea or how to improve the performance of this?

  • 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-06-05T12:53:22+00:00Added an answer on June 5, 2026 at 12:53 pm

    Trigger in Sql server run once for each batch, so if you insert 5000 records in one statement, the trigger kicks off exactly once. What you have done is assume it will run for each individual record by setting the values you want to scalar variables which of course can only hold one value not 5000. You need to re-think your process to take advantage of joining to inserted and deleted not running through one record at a time.

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

Sidebar

Related Questions

I have small problem with mysql. I have data in table and I want
I have this problem. I have a table (below) of groups. It's a recursive
have small problem, and would very much appreciate help :) I should convert byte
I have a small problem with TFS. I am trying to zip files after
I have a small problem by using PDO instead of mysql_fetch_object . I tried
I have a small problem but cannot figure out why. On my django shell:
I have a small problem trying to unzip a file using the 7za command-line
I have a small problem with my grammar. I am trying to detect whether
I have a small problem on a website with a background element in CSS
I have a small problem regarding a count after grouping some elements from a

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.