Home All Groups Group Topic Archive Search About

Trigger: When deleting more then 1 row it doesn't trigger

Author
23 Mar 2006 2:25 AM
Andreas Klemt
Hello,

I have this

ALTER TRIGGER [mytableDelete] ON [dbo].[mytable]
FOR DELETE
AS
SET NOCOUNT ON
DECLARE @user_id int
SELECT @user_id=user_id FROM deleted
DELETE FROM users WHERE user_id = @pa_id

Now this works fine when I only delete one row like this
DELETE FROM mytable WHERE id=1

But if I delete more than one row at once, my trigger does not work.
DELETE FROM mytable WHERE id in (1, 3, 4, 5)

What is wrong?

Thanks for any help in advance!

Regards
Andreas Klemt

Author
23 Mar 2006 3:27 AM
Dave Frommer
This line is likely your problem as it will set @user_id to the user_id
column from ONE row in the deleted table.

SELECT @user_id=user_id FROM deleted

Also, I see the variable @pa_id in the delete statement, but, it is not
declared or given a value iin the trigger.

If you want to delete all rows from the users table where the user_id column
= any of the values in the user_id column of all rows in the deleted table,
then, thsi is what may work:

ALTER TRIGGER [mytableDelete] ON [dbo].[mytable]
FOR DELETE
AS
SET NOCOUNT ON
DELETE FROM users WHERE user_id IN (SELECT user_id FROM deleted)

Show quote
"Andreas Klemt" <aklem***@hotmail.com> wrote in message
news:es%23lXEiTGHA.1688@TK2MSFTNGP11.phx.gbl...
> Hello,
>
> I have this
>
> ALTER TRIGGER [mytableDelete] ON [dbo].[mytable]
> FOR DELETE
> AS
> SET NOCOUNT ON
> DECLARE @user_id int
> SELECT @user_id=user_id FROM deleted
> DELETE FROM users WHERE user_id = @pa_id
>
> Now this works fine when I only delete one row like this
> DELETE FROM mytable WHERE id=1
>
> But if I delete more than one row at once, my trigger does not work.
> DELETE FROM mytable WHERE id in (1, 3, 4, 5)
>
> What is wrong?
>
> Thanks for any help in advance!
>
> Regards
> Andreas Klemt
>
Author
23 Mar 2006 9:17 AM
Andreas Klemt
Hello Dave,

many thanks! That helped me.

Kind Regards,
Andreas Klemt


Show quote
"Dave Frommer" <anti@spam.com> schrieb im Newsbeitrag
news:Obx$jmiTGHA.1728@TK2MSFTNGP11.phx.gbl...
> This line is likely your problem as it will set @user_id to the user_id
> column from ONE row in the deleted table.
>
> SELECT @user_id=user_id FROM deleted
>
> Also, I see the variable @pa_id in the delete statement, but, it is not
> declared or given a value iin the trigger.
>
> If you want to delete all rows from the users table where the user_id
> column = any of the values in the user_id column of all rows in the
> deleted table, then, thsi is what may work:
>
> ALTER TRIGGER [mytableDelete] ON [dbo].[mytable]
> FOR DELETE
> AS
> SET NOCOUNT ON
> DELETE FROM users WHERE user_id IN (SELECT user_id FROM deleted)
>
> "Andreas Klemt" <aklem***@hotmail.com> wrote in message
> news:es%23lXEiTGHA.1688@TK2MSFTNGP11.phx.gbl...
>> Hello,
>>
>> I have this
>>
>> ALTER TRIGGER [mytableDelete] ON [dbo].[mytable]
>> FOR DELETE
>> AS
>> SET NOCOUNT ON
>> DECLARE @user_id int
>> SELECT @user_id=user_id FROM deleted
>> DELETE FROM users WHERE user_id = @pa_id
>>
>> Now this works fine when I only delete one row like this
>> DELETE FROM mytable WHERE id=1
>>
>> But if I delete more than one row at once, my trigger does not work.
>> DELETE FROM mytable WHERE id in (1, 3, 4, 5)
>>
>> What is wrong?
>>
>> Thanks for any help in advance!
>>
>> Regards
>> Andreas Klemt
>>
>
>

AddThis Social Bookmark Button