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 2005 Forums
 Transact-SQL (2005)
 Trouble with creating a trigger

Author  Topic 

RBTrout
Starting Member

6 Posts

Posted - 2011-03-06 : 14:21:47
I'm having trouble with triggers. I understand what they do, just having trouble creating one.

I've got a DB with 3 tables -
Customers
OrderHeader
OrderDetail

I need to create a trigger that will not allow a customer to be deleted in that customer still has an order.
THX for the help.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-06 : 14:35:30
Referential integrity should handle this and not a trigger. Why not add a foreign key between the two tables?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

RBTrout
Starting Member

6 Posts

Posted - 2011-03-06 : 14:57:31
After everything I've scoured in books and online, I agree completely. I'm taking a SQL class that wants me to do this and I'm completely stumped. I get stored procedures and functions, but triggers have me lost. I've got a mental block (on my shoulders, I think) on this.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-06 : 15:07:44
Show us what you have so far with the trigger, and we'll help you fix it.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

RBTrout
Starting Member

6 Posts

Posted - 2011-03-06 : 18:16:35
use ProductOrders
Go

create trigger order_delete
on orders
after delete
AS
If exists
(select *
From orders join customers
On Orders.CustID = Customers.CustID
Where Orders.CustID = Customers.CustID

Raiserror('Customer still has order.',11,1)
Rolback Tran
End

I usually get stuff like this, but I'm really drawing a blank.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-06 : 18:30:41
The key is to use the deleted trigger table:

if exists (select * from deleted d join customers c on d.custid = c.custid)
...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

RBTrout
Starting Member

6 Posts

Posted - 2011-03-06 : 18:53:55
Thanks! I've already read lines similar and didn't get it. I had to walk away and spend some time doing honey-dos and then come back with a fresh mind. NOW, that line makes sense and I get it.
I really appreciate the help.....especially on a Sunday. Thanks again.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-06 : 20:24:41
You're welcome, glad to help.

It's truly refreshing for someone to be interested in learning rather than us just providing the answer. Most people we see here asking questions about their SQL classes/homework just want us to do all of the work for them. So thank you for your effort here, very refreshing!

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -