Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 delete trigger

Author  Topic 

joemama
Posting Yak Master

113 Posts

Posted - 2005-04-02 : 19:19:46
newbie help pleeeeease :)

i have a table called users

when i delete a user from that table i also what to delete and row from the tables

newsletter
orders

based on the USERID I deleted in the users table

any help would be appreciated


love,
joemama

PW
Yak Posting Veteran

95 Posts

Posted - 2005-04-02 : 19:51:35

Do you have foreign key constraints declared between users and newsletter & orders ?
If so, just add ON DELETE CASCADE to the constraint.
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2005-04-02 : 22:33:43
otherwise

create trigger tr_users_del for delete
as
delete newsletter
from newsletter n
join deleted d
on n.userid = d.userid

delete orders
from orders o
join deleted d
on o.userid = d.userid
go


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-04-03 : 05:26:17
otherwise

add the contraint

rockmoose
Go to Top of Page

joemama
Posting Yak Master

113 Posts

Posted - 2005-04-03 : 15:47:01
how do i create a foreign key?

quote:
Originally posted by rockmoose

otherwise

add the contraint

rockmoose

Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2005-04-03 : 16:29:04
Books Online has all the details, under "ALTER TABLE". You'll want to use the ADD CONSTRAINT clause. Make sure that the table you're referencing (parent table) has a primary key or unique constraint/index on the column(s) you want to reference.
Go to Top of Page
   

- Advertisement -