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
 SQL Server Development (2000)
 Trigger Syntax Help

Author  Topic 

brandonl
Yak Posting Veteran

58 Posts

Posted - 2004-12-20 : 00:08:28
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
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2004-12-20 : 01:33:50
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 - 2004-12-20 : 09:48:32
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
   

- Advertisement -