Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys I am using the following trigger to insert data into table2 when table1 data is deleted:

SQL
CREATE TRIGGER trgInsRejctWrd
   ON  table1
   AFTER DELETE
AS

        declare @id int;
        declare @word nvarchar(50);
        declare @type nvarchar(50);
        declare @rejecter nvarchar(15);
BEGIN
            select @word=d.Suggest_SourceWord from deleted d
            select @type=d.Suggest_WordDiff from deleted d
            select @id=ISNULL(max(Reject_ID),0)+1 from table2

            insert into table2(Reject_ID,Reject_Words,Reject_DateTime,Reject_WordType)
            values(@id,@word,GETDATE(),@type)


END
GO


now it works fine when I delete single record, but when I delete multiple records it insert only
1 record. I want to insert all the deleted records from table1 into table2.
so any suggestions will be helpful.
Thanks in advance.
Posted

1 solution

Firstly your way of generating an ID for table2 is wrong. Use an IDENTITY[^] column to automatically create that ID for you.

Secondly, you are only assigning those variables once, which is why it only inserts a single record, you need to select the entire deleted table.

For example (NB this assumes that you are using an identity column for table2.Reject_id
SQL
CREATE TRIGGER trgInsRejctWrd
   ON  table1 AFTER DELETE
AS
     insert into table2
     VALUES(Suggest_SourceWord, GETDATE(), Suggest_WordDiff)
     from deleted
 
Share this answer
 
Comments
Mitul Birla 7-Jul-14 0:39am    
Hey CHill60 thanks for the reply.
I used your suggestion but it gives me error on FROM keyword,"Incorrect syntax near 'from'".
can u suggest something else.
Thanks in advance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900