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 |
|
chris_cs
Posting Yak Master
223 Posts |
Posted - 2009-04-17 : 09:12:29
|
I have created a trigger which is shown below:ALTER trigger [dbo].[trg_clearNarrative_cr] on [dbo].[WORKHISTORY] for insertasdeclare @rc as intdeclare @caseid intset @rc = @@rowcountif @rc = 0 returnif @rc > 0begin if (select count(*) from inserted where transtype = 510) >= 1 begin select distinct @caseid = caseid from inserted if exists(select * from casetext where caseid = @caseid and texttype = '_B') begin delete from casetext where caseid = @caseid and texttype = '_B' end endend It doesn't delete the corresponding row from the casetext table and I'm a little bemused as to why.Can anyone see why I would be having a problem? |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2009-04-17 : 09:27:26
|
| select distinct @caseid = caseid from insertedwhat is that trying to do?select top 1 @caseid = caseid from insertedmaybe you wantdelete casetextfrom casetext cjoin (select distinct caseid from inserted) ion c.caseid = i.caseidwhere texttype = '_B'Get rid of everything else in the trigger.You can put a if exists (select * from inserted)before it if you wish.==========================================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. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-17 : 09:28:29
|
Make the code work for more than one inserted record.ALTER TRIGGER dbo.trg_clearNarrative_crON dbo.WORKHISTORYAFTER INSERTASSET NOCOUNT ONDELETE ctFROM CaseText AS ctINNER JOIN inserted AS i ON i.CaseID = ct.CaseIDWHERE i.TransType = 510 i.TextType = '_B'GO E 12°55'05.63"N 56°04'39.26" |
 |
|
|
chris_cs
Posting Yak Master
223 Posts |
Posted - 2009-04-17 : 09:47:51
|
| Thanks for the help.This is working now after using top 1 instead of distinct. I'll take your other points on board and try to tidy up the code.Thanks again |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2009-04-17 : 09:52:49
|
| That will only cater for a single caseid - if there are more in the inserted table they will be ignored.==========================================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. |
 |
|
|
chris_cs
Posting Yak Master
223 Posts |
Posted - 2009-04-17 : 10:18:43
|
| Yeah thats fine as there will only ever be one caseid. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-17 : 11:00:57
|
quote: Originally posted by chris_cs Yeah thats fine as there will only ever be one caseid.
Famous last words... E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|
|
|