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.
| Author |
Topic |
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2003-07-28 : 06:23:39
|
| I currently have two seperate triggers that run fine and look for variations of the same event. This sounds to me like a good candidate for a single trigger that combines the two operations but I couldn't get it to work.Trigger 1 is create trigger trg_RemoveFromWLOnFTAon clinic for updateas/* If Not Attended is entered then take the valueof B_DATE and put it into M_DATE for the same record */if update(counseled)beginif (select counseled from inserted) like 'Not Attended'beginupdate clinic set M_DATE = (select B_DATE from inserted)endendGOTrigger 2 is create trigger trg_RemoveFromWLOnAttendon clinic for updateas/* If Attended is entered then take the valueof B_DATE and put it into M_DATE for the associated record */DECLARE @ClinicDate DATETIME, @WLNumber VARCHAR(10) set @ClinicDate = (select B_DATE from inserted) set @WLNumber = (select PREV_CLINICNO from inserted)if update(counseled)beginif (select count(*) from inserted where inserted.counseled like 'Attended')>0beginupdate clinic set M_DATE = @ClinicDatewhere CLINICNO = @WLNumberendendI tried to combine them with a case statement but it didn't seem to like the update statements. Can this be done. Am I fixing somthing that isn't broken? Secondly I am aware that the second trigger may not work correctly if I do a bulk update. What would I need to do to fix that for this unlikely event thanks in advancesteveSteve no function beer well without |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-07-28 : 07:00:08
|
| set @WLNumber = (select PREV_CLINICNO from inserted)if update(counseled)beginif (select count(*) from inserted where inserted.counseled like 'Attended')>0beginupdate clinicset M_DATE = @ClinicDatewhere CLINICNO = @WLNumberendELSEbeginupdate clinic set M_DATE = (select B_DATE from inserted)endend- Vit |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-07-28 : 07:53:12
|
| >> if (select count(*) from inserted where inserted.counseled like 'Attended')>0should be (doesn't matter too much but is more efficient)if exists (select * from inserted where inserted.counseled like 'Attended')Also doesn't work for more than a single row update.What is the PK? WLNumber?update Clinicset M_Date = i.B_Datefrom Clinic cjoin inserted ion i.WLNumber = c.WLNumberwhere i.Counseled = 'Not Attended'update Clinicset M_Date = i.B_Datefrom Clinic cjoin inserted ion i.WLNumber = c.PREV_CLINICNOwhere i.Counseled = 'Attended'you could do it in a single queryupdate Clinicset M_Date = i.B_Datefrom Clinic cjoin inserted ion (i.WLNumber = c.WLNumberand i.Counseled = 'Not Attended')or (i.WLNumber = c.PREV_CLINICNOand i.Counseled = 'Attended')but doesn't seem worth it.==========================================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. |
 |
|
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2003-07-29 : 03:32:55
|
| Many thanks to both of yousteveSteve no function beer well without |
 |
|
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2003-07-29 : 09:20:44
|
I knew my triggers were a bit clunky but I didn't realise quite how bad they were until I saw this! I now have a working system. Thanks again for the tips guys, I'm off to sit in my hammock and drink ice cold beer mmmmmmm BEEEEER!steveSteve no function beer well without |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-07-29 : 16:26:33
|
| Cheers, Steve!! :)Take care.- Vit |
 |
|
|
|
|
|