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 not working properly

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_LIN
FOR INSERT, UPDATE
AS
set 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_cod
from ps_tkt_hdr
where ps_tkt_hdr.tkt_no=i.tkt_no
and ps_tkt_hdr.str_id=i.str_id
and ps_tkt_hdr.sta_id=i.sta_id),
@SVCFLG=(select IS_SVC_CALL
from ps_tkt_hdr
where ps_tkt_hdr.tkt_no=i.tkt_no
and ps_tkt_hdr.str_id=i.str_id
and ps_tkt_hdr.sta_id=i.sta_id)
from inserted i
where (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)
begin
set @MSG='Zone charge on ticket does not match Ship to zone!'
end
if @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')
begin
set @MSG='Out of Normal Service Area: No Travel Charge on on this warranty ticket!'
end
if @MSG is not null
begin
rollback tran
raiserror(@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..
Go to Top of Page

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



KH

Choice 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

Go to Top of Page

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!
Andy


There's never enough time to type code right,
but always enough time for a hotfix...
Go to Top of Page

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



KH

Choice 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

Go to Top of Page

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...
Andy

There's never enough time to type code right,
but always enough time for a hotfix...
Go to Top of Page
   

- Advertisement -