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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:57:27+00:00 2026-06-10T16:57:27+00:00

Below is a trigger I’ve created for a table in a SQL database (We

  • 0

Below is a trigger I’ve created for a table in a SQL database (We are running SQL Server 2008). We are going to be doing updates, inserts, and deletes in bulk so we created this trigger and a “storage” table (TransactionLog) to capture all the activity. So far, this trigger works perfectly for inserts and updates. All the records wind up in the “storage” table with all appropriate values.

However, the problem occurs when we try to delete anything more than one record. The only record this trigger captures and sends to the “storage” table is the last record deleted. All others get lost.

CREATE TRIGGER [dbo].[trg_Agent_ALL] 
ON [dbo].[Agent]

FOR INSERT, UPDATE, DELETE
AS
BEGIN

--TransactionLog variables
DECLARE @tableName char(25) = 'Agent'
DECLARE @transDate datetime 
DECLARE @lastChangeOperator char(6) 
DECLARE @tableString char(255)
DECLARE @action char(1) = 'I'
DECLARE @userId char(25)

--Trigger table variables
DECLARE @sNumber char(10)
DECLARE @controlId char(3)
DECLARE @entityType char(1)
DECLARE @firstName nvarchar(50)
DECLARE @lastName nvarchar(50)
DECLARE @suffix nvarchar(10)
DECLARE @corpName nvarchar(100)


IF EXISTS (SELECT * FROM deleted)
    BEGIN
        SET @action = 
            CASE    
                WHEN EXISTS (SELECT * FROM inserted) THEN 'U'
                ELSE 'D'
            END
    END

IF @action = 'D'
    BEGIN 
        SELECT @sNumber = sNumber, @lastChangeOperator = LastChangeOperator, @transDate = LastChangeDate, @entityType = EntityType, 
        @firstName = FirstName, @lastName = LastName, @suffix = NameSuffix, @corpName = CorporateName, @controlId = ControlId
        FROM deleted

        IF @firstName IS NULL SET @firstName = 'NULL'
        IF @lastName IS NULL SET @lastName = 'NULL'
        IF @suffix IS NULL SET @suffix = 'NULL'
        IF @corpName IS NULL SET @corpName = 'NULL'
        IF @controlId IS NULL SET @controlId = 'NULL'

        SET @tableString = 'sNum:' + @sNumber + ' ' + 'EntType:' + @entityType + ' ' + 'Fname:' + @firstName + ' ' + 'Lname:' + @lastname + ' ' + 'suf:' + @suffix +
            ' ' + 'crpName:' + @corpName + ' ' + 'crlId:' + @controlId
    END
ELSE
    BEGIN
        SELECT @sNumber = SymetraNumber, @lastChangeOperator = LastChangeOperator, @transDate = LastChangeDate, @entityType = EntityType, 
        @firstName = FirstName, @lastName = LastName, @suffix = NameSuffix, @corpName = CorporateName, @controlId = ControlId
        FROM inserted

        IF @firstName IS NULL SET @firstName = 'NULL'
        IF @lastName IS NULL SET @lastName = 'NULL'
        IF @suffix IS NULL SET @suffix = 'NULL'
        IF @corpName IS NULL SET @corpName = 'NULL'
        IF @controlId IS NULL SET @controlId = 'NULL'

        SET @tableString = 'sNum:' + @sNumber + ' ' + 'EntType:' + @entityType + ' ' + 'Fname:' + @firstName + ' ' + 'Lname:' + @lastname + ' ' + 'suf:' + @suffix +
            ' ' + 'crpName:' + @corpName + 'crlId:' + @controlId
    END

INSERT INTO TransactionLog (TransactionDate, Operator, TableName, Action, TableString, UserId)
VALUES (@transDate, 'Op', @tableName, @action, @tableString, @lastChangeOperator)

END

Based on the comments below I’ve altered the SQL code in the delete section. The hard-coded values seem to be working, however the main reason I placed them in there just to show those are the values I need for those specific columns. I have variables set with these values in the code above (see the DECLARE statements). This, however, is giving me the following error message:

 Conversion failed when converting the varchar value 'P' to data type int.

This error is coming off of the EntityType attribute in the inner SELECT statement. What confuses me is that this column has a data type set to char(1), and the TableString column (the destination of the concatenated values) has a data type of nvarchar(255). No clue where the “int” is coming from…

IF @action = 'D'
    BEGIN   
        INSERT INTO TransactionLog (TransactionDate, Operator, TableName, Action, TableString, UserId)
        SELECT LastChangeDate, 'Op', 'Agent', 'D', 
        (SELECT CAST(CAST(sNumber as nvarchar) + ' ' + EntityType + ' ' + ISNULL(FirstName, ' ') + ' ' + ISNULL(LastName, ' ') + ' ' + ISNULL(NameSuffix, ' ') + ' ' + 
                ISNULL(CorporateName, ' ' ) + ' ' + ISNULL(CAST(ControlId as nvarchar), ' ') AS nvarchar) as TableString
        FROM deleted), LastChangeOperator
        FROM deleted
    END
ELSE

EDIT

By casting the sNumber and controlId fields to nvarchar I was able to move past my previous error message. Right now, however I am receiving a different error message posted below:

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

FINAL EDIT

Altering the subquery like so allowed me to return multiple deleted records into the Audit table as I was only requesting one record at a time.

IF @action = 'D'
    BEGIN 
        INSERT INTO TransactionLog (TransactionDate, Operator, TableName, Action, TableString, UserId)
            SELECT LastChangeDate, 'Op', 'Agent', 'D', 
            CAST('SymNum:' + CAST(SymetraNumber as nvarchar(30)) + ' entType:' + EntityType + ' Fname:' + ISNULL(FirstName, 'NULL') + ' Lname:' + ISNULL(LastName, 'NULL') +
             ' suff:' + ISNULL(NameSuffix, 'NULL') + ' corpName:' + ISNULL(CorporateName, 'NULL' ) + ' ctrlId:' + ISNULL(CAST(ControlId as nvarchar(30)), 'NULL') AS nvarchar(30)) as TableString
            , LastChangeOperator
        FROM deleted
    END
ELSE 
  • 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-10T16:57:29+00:00Added an answer on June 10, 2026 at 4:57 pm

    That select @sNumber = sNumber, .. syntax will overwrite the variables once for each row, so you end up only with the last row’s values.

    And then you also do an insert into ... values (...), which can only insert one row.

    You should try to rewrite it in the form:

    insert into TransactionLog ( ... )
    select sNumber, ... from deleted
    

    You can use ISNULL(lastname, 'NULL') instead of your if-statements.

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

Sidebar

Related Questions

I am newbie in SQL.I am reading Triggers created two trigger on a table.Below
I am trying to create a trigger in SQL Server 2008 which inserts a
Below is a SQL Server 2005 update trigger. For every update on the email_subscriberList
I am going to create a DDL trigger to all databases in SQL Server
When authoring an INSERT Trigger for SQL Server 2008 Express in C#: I find
I have a table trigger like below: CREATE OR REPLACE TRIGGER PAT_BUR_DOB_TRG BEFORE UPDATE
I'm trying to alter an SQL Server 2000 update trigger, but its hanging and
There are two Table Table_Trigger and Table_Trigger1 .I have created a trigger on Table_Trigger1
I created a FOR INSERT trigger that will fire after inserting. Below are the
I have the below SQL Trigger on a SQL 2005 box that is supposed

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.