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 |
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2006-03-28 : 12:50:40
|
Hi all,I have a trigger that is giving me a little grief:CREATE TRIGGER enforcezonecharges ON dbo.PS_TKT_LINFOR INSERT, UPDATEASset nocount on/*declare local variables--*/declare@ZON varchar(2),@ZIP varchar(15),@SVCFLG varchsr(2),@MSG varchar(255)select@ZON=(select svc_zone from im_item where im_item.item_no=i.item_no),@ZIP=(select ship_zip_codfrom ps_tkt_hdrwhere ps_tkt_hdr.tkt_no=i.tkt_noand ps_tkt_hdr.str_id=i.str_idand ps_tkt_hdr.sta_id=i.sta_id),@SVCFLG=(select IS_SVC_CALLfrom ps_tkt_hdrwhere ps_tkt_hdr.tkt_no=i.tkt_noand ps_tkt_hdr.str_id=i.str_idand ps_tkt_hdr.sta_id=i.sta_id)from inserted iwhere (i.subcat_cod='LABOR' or i.subcat_cod='WALAB')/*make sure zone charge has been set if service related */if @SVCFLG='Y' and (@ZON<>'00' or @ZON<>'99') and @ZON<>(select svc_zone from zip_cod_zones where zip_cod_zones.zip_cod=@ZIP)beginset @MSG='Zone charge on ticket does not match Ship to zone!'endif @SVCFLG='Y' and (@ZON='88') and @ZON<>(select svc_zone from zip_cod_zones where zip_cod_zones.zip_cod=@ZIP) and @ZON>'03'and not exists(select top 1 im_item.item_no from im_item join inserted i on im_item.item_no=i.item_no where im_item.svc_zone='77')beginset @MSG='Out of Normal Service Area: No Travel Charge on on this warranty ticket!'endif @MSG is not nullbeginrollback tranraiserror(@MSG,18,1)end This is a trigger on a ticket line table.When @ZON='88'the trigger errors out, even if a row on the ticket meets the highlighted section above. I'm sure I have that section wrong, but I just can't figure out what to do with it! If a line being inserted has a svc_zone of '88' and the zip_zone is > 3, I need to check ALL the other lines being inserted to see if at least one of them has a svc_zone of '77', and error out if it does not exist.I'll just go refill my stupid pill Rx now... There's never enough time to type code right, but always enough time for a hotfix... |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2006-03-29 : 05:52:40
|
| set the select to a variable and check that it isn't null.. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-03-29 : 06:28:48
|
your trigger did not handle the inserted as a set. Do note that inserted table may contains more than one rows. Your trigger script will need to handle this KHChoice is an illusion, created between those with power, and those without.Concordantly, while your first question may be the most pertinent, you may or may not realize it is also the most irrelevant |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2006-03-29 : 09:27:08
|
Oh man... oooooooooooohhhhhh maaaannnnn.... I hate to say it, but I knew that! You know how I knew that? I already posted this code once before, and Michael Valentine Jones already pointed that out and I already changed it! I've been trying to do an update to an old piece of code that I'm not even using!!     I think I just OD'd on the stupid pills!AndyThere's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-03-29 : 17:17:41
|
Maybe you should consider using source control tool like SourceSafe, CVS, Subversion KHChoice is an illusion, created between those with power, and those without.Concordantly, while your first question may be the most pertinent, you may or may not realize it is also the most irrelevant |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2006-03-29 : 20:28:43
|
Maybe I should use my head for something other than a hair rack...I actually got thinking about adding the second part of this trigger while traveling with my laptop. I just wasn't thinking and opened the saved .SQL script that was on my laptop without bothering to check and see if it was the right one... AndyThere's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
|
|
|
|
|