SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Trigger Syntax Help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

brandonl
Yak Posting Veteran

58 Posts

Posted - 12/20/2004 :  00:08:28  Show Profile  Send brandonl an ICQ Message  Reply with Quote
I have a table RECORDS, that has a StateID in it and a RecordID (primary key). StateID is an integer that links to a STATE table.

What I want to do is make a trigger that, anytime a record in the RECORDS table gets updated, the trigger checks if the new StateID = 8. If it is, it inserts a new record into a PILOT_TESTING table (which has a foreign key RecordID that links back to the RECORDS table). It also checks that if, in the PILOT table, that RecordID already exists, it clears out the values in that record.

I can do the select statements to find out the new stateid after the update, and can do the insert statement, and the update statements. What I need help with, is how do I accomplish what I need to do in the trigger. Do I do it with an if then, case, etc? Sorta like this?

select updated record (stateid, and recordid) from updates
if new stateid = 8 then
select from PILOT table where recordid = if of the updated record
if record exists
update record in PILOT
else
insert new record
end if
end if

Am I on the right track with this?

~BrandonL

Bustaz Kool
Flowing Fount of Yak Knowledge

USA
1600 Posts

Posted - 12/20/2004 :  01:33:50  Show Profile  Reply with Quote
create trigger u_Records
on dbo.Records
for update
as
begin
if update(StateID)
insert into dbo.Pilot_Testing (
RecordID, StateID
)
select RecordID, StateID
from inserted
where StateID = 8

delete from dbo.Pilot
where RecordID in (select RecordID from Inserted)
end

I'm not sure what you meant by "clear the values" from the Pilot table so I implemented it as a DELETE statement.

HTH

=================================================================

Happy Holidays!
Go to Top of Page

brandonl
Yak Posting Veteran

58 Posts

Posted - 12/20/2004 :  09:48:32  Show Profile  Send brandonl an ICQ Message  Reply with Quote
quote:
Originally posted by Bustaz Kool


I'm not sure what you meant by "clear the values" from the Pilot table so I implemented it as a DELETE statement.
I'll give your code a try, and see if that's a start.

By clear the values-in the PILOT table, there's a recordid and some bit fields. Basically, when someone checks off on a web form, it sets the bit value to 1, otherwise it's blank. If there's already a record in the PILOT table for that requestid, I want it to set the bit values to NULLs.

~BrandonL
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000