| Author |
Topic |
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2004-12-20 : 15:24:29
|
Here we go again!I have a table, PS_TKT_HDR, with a column SHIP_DAT(datetime).I have a child table, PS_TKT_LIN, with a column ITEM_SHIP_DAT (datetime). When a user enters a new order, we would like to put a date into SHIP_DAT and have it update ITEM_SHIP_DAT in PS_TKT_LIN for each rowset that is connected to the ticket header. The key columns between PS_TKT_HDR and PS_TKT_LIN are TKT_NO, STA_ID, and STR_ID. This part is easy, but there is a catch! IF SHIP_DAT is blank, we don't want it to change ITEM_SHIP_DAT. Also, if a user changes SHIP_DAT at a later time, and the OLD value for SHIP_DAT DID NOT MATCH ITEM_SHIP_DAT, then we DON'T want it to change ITEM_SHIP_DAT either. Any idea how this would look in a triiger? Can this even be done this way?Andy There's never enough time to type code right, but always enough time for a hotfix... |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-12-20 : 15:40:26
|
| So how far did you get on writing the trigger? Please post what you have so far.Tara |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2004-12-20 : 16:13:22
|
| Sorry Tara!I actually haven't written anything yet. I got asked for this, then got thinking about it, and the gotCONFUSED!AndyThere's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-12-20 : 16:21:07
|
| Where did you get stuck?Tara |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2004-12-20 : 16:24:14
|
Basically,It would be something like this:create trigger tr_Itemshipdaton PS_TKT_HDRfor insert, updateasupdate tlset tl.ITEM_SHIP_DAT = th.SHIP_DATfrom inserted iinner join PS_TKT_LIN tlon i.STR_ID = tl.STR_IDand i.STA_ID= tl.STA_IDand i.TKT_NO=tl.TKT_NOinner join ps_tkt_hdr thon tl.STR_ID= th.STR_IDand tl.STA_ID=th.STA_IDand tl.TKT_NO=th.TKT_NO But the rest has got me a little puzzled...There's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2004-12-20 : 16:31:41
|
| I assume we need to write this out more like a proc, but I'm not sure how. Seems to me that we need to look at the date columns as a variable and then do our work from there...Or am I off base on this?There's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-12-20 : 16:38:45
|
| In order for us to help, we'll need to see the table structures of all tables involved in the form of CREATE TABLE statements, sample data to illustrate what the tables look like in the form of INSERT INTO statements, and the expected result using this sample data after the trigger fires. So basically we need it illustrated in the form of DDL and DML so that we can play with it on our machines.Tara |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2004-12-20 : 16:56:44
|
| ok Tara,I can write that up for you. Do you want ALL of the columns in each table, or just the relevant ones? These tables have quite a few columns in them...Also, how much sample data would you like? Couple o' rows?There's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-12-20 : 16:57:33
|
| Just the relevant columns in the tables and just enough rows to illustrate what you want.Tara |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2004-12-20 : 17:17:02
|
PS_TKT_HDR table:create table PS_TKT_HDR(STR_ID varchar(10), STA_ID varchar(10),TKT_NO varchar(15),CONSTRAINT PK_PS_TKT_HDR PRIMARY KEY CLUSTERED ( STR_ID, STA_ID, TKT_NO),SHIP_DAT datetime (8)) PS_TKT_LIN table:create table PS_TKT_LIN(STR_ID varchar(10), STA_ID varchar(10),TKT_NO varchar(15), SEQ_NO int (4),CONSTRAINT PK_PS_TKT_LIN PRIMARY KEY CLUSTERED ( STR_ID, STA_ID, TKT_NO, SEQ_NO),ITEM_SHIP_DAT datetime (8)) Insert for HDR:INSERT INTO PS_TKT_HDR(STR_ID,STA_ID,TKT_NO,SHIP_DAT)VALUES '1','1','555','08/01/04') Insert for LIN:INSERT INTO PS_TKT_LIN(STR_ID,STA_ID,TKT_NO,SEQ_NO,ITEM_SHIP_DAT)VALUES '1','1','555','1','08/01/04' This only puts one line in for the PS_TKT_LIN table, but I don't think we need more to get this working, we just need to hit all lines WHERE the HDR table and LIN table link. Basically what has to happen is:A) If the HDR SHIP_DAT is NULL, don't modify the matching ITEM_SHIP_DAT date(s).B) If the HDR SHIP_DAT is not NULL, modify the ITEM_SHIP_DAT(s).C) If the HDR SHIP_DAT gets changed, and it DID MATCH(the original value before the change) the ITEM_SHIP_DAT(s), change the ITEM_SHIP_DAT(s) as well.D) If the HDR SHIP_DAT gets chanded, and it DID NOT MATCH the ITEM_SHIP_DAT(s), leave the ITEM_SHIP_DAT(s) alone.There's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2004-12-21 : 10:24:22
|
Hey Tara,Does this look like it covers all the bases? maybe I am making this harder than it needs to be..update tlif i.ship_dat is not nulland th.SHIP_DAT=tl.ITEM_SHIP_DATset tl.ITEM_SHIP_DAT = th.SHIP_DATfrom inserted iinner join PS_TKT_LIN tlon i.STR_ID = tl.STR_IDand i.STA_ID= tl.STA_IDand i.TKT_NO=tl.TKT_NOinner join ps_tkt_hdr thon tl.STR_ID= th.STR_IDand tl.STA_ID=th.STA_IDand tl.TKT_NO=th.TKT_NO AndyThere's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2004-12-21 : 11:00:49
|
One more time:update tlset tl.ITEM_SHIP_DAT = th.SHIP_DATfrom inserted iinner join PS_TKT_LIN tlon i.STR_ID = tl.STR_IDand i.STA_ID= tl.STA_IDand i.TKT_NO=tl.TKT_NOinner join ps_tkt_hdr thon tl.STR_ID= th.STR_IDand tl.STA_ID=th.STA_IDand tl.TKT_NO=th.TKT_NOwhere i.ship_dat is not nulland th.SHIP_DAT=tl.ITEM_SHIP_DAT is this more like it?There's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2004-12-21 : 12:06:33
|
Yet another revelation:request for item ship date trigger:A) If the HDR SHIP_DAT is NULL, don't modify the matching ITEM_SHIP_DAT date(s).B) If the HDR SHIP_DAT is not NULL, modify the ITEM_SHIP_DAT(s).C) If the HDR SHIP_DAT gets changed, and it DID MATCH(the original value before the change) the ITEM_SHIP_DAT(s), change the ITEM_SHIP_DAT(s) as well.D) If the HDR SHIP_DAT gets changed, and it DID NOT MATCH the ITEM_SHIP_DAT(s), leave the ITEM_SHIP_DAT(s) alone.create trigger tr_Itemshipdaton PS_TKT_HDRfor insert, updateasset nocount onupdate tlset tl.ITEM_SHIP_DAT = th.SHIP_DATfrom inserted iinner join PS_TKT_LIN tlon i.STR_ID = tl.STR_IDand i.STA_ID= tl.STA_IDand i.TKT_NO=tl.TKT_NOinner join ps_tkt_hdr thon tl.STR_ID= th.STR_IDand tl.STA_ID=th.STA_IDand tl.TKT_NO=th.TKT_NO--requirement (A) is satisifed below as null value is ignored----below satisfies requirement (B) also, but causes problems for requirement (D), and there is no filtering for(C)--where i.ship_dat is not nullset nocount off Is this of any help?There's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-12-21 : 12:19:42
|
| You won't be able to do it all in one update statement, I believe. You are going to need to check the inserted table against the detail table to determine what to do. You'll probably need 3 IFs in here. Just don't use variables as that'll only work for the last row.Tara |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2004-12-21 : 12:47:27
|
I keep thinking the same thing, but every time I start an IF statement I get stuck. It just doesn't seem right. Now check out this:create trigger tr_Itemshipdaton PS_TKT_HDRfor insert, updateasset nocount onupdate tlset tl.ITEM_SHIP_DAT = th.SHIP_DATfrom inserted iinner join PS_TKT_LIN tlon i.STR_ID = tl.STR_IDand i.STA_ID= tl.STA_IDand i.TKT_NO=tl.TKT_NOinner join ps_tkt_hdr thon tl.STR_ID= th.STR_IDand tl.STA_ID=th.STA_IDand tl.TKT_NO=th.TKT_NO--requirement (A) is satisifed below as null value is ignored, and (B) is satisfied also --where I.SHIP_DAT IS NOT NULL-- (C) is satisfied below; (D) is satisfied also --AND I.SHIP_DAT<>TH.SHIP_DATAND TH.SHIP_DAT=TL.ITEM_SHIP_DATset nocount off does this look any better? I keep thinking it needs multiple IF statements as well, but damned if I can get it to go!AndyThere's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2004-12-21 : 12:52:56
|
Hey Tara,Here is an option another coder I know has sent. Check it out:update tl settl.ITEM_SHIP_DAT = case when th.SHIP_DAT is null then tl.ITEM_SHIP_DATwhen th.SHIP_DAT is not null and tl.ITEM_SHIP_DAT is null then th.SHIP_DAT when th.SHIP_DAT is not null and d.SHIP_DAT = tl.ITEM_SHIP_DATthen th.SHIP_DATwhen th.SHIP_DAT is not null and d.SHIP_DAT <> tl.ITEM_SHIP_DATthen tl.ITEM_SHIP_DATfrom PS_TKT_LIN tlinner join inserted ion tl.STR_ID = i.STR_IDand tl.STA_ID= i.STA_IDand tl.TKT_NO=i.TKT_NOinner join ps_tkt_hdr thon tl.STR_ID= th.STR_IDand tl.STA_ID=th.STA_IDand tl.TKT_NO=th.TKT_NOleft outer join deleted don tl.STR_ID = d.STR_IDand tl.STA_ID= d.STA_IDand tl.TKT_NO=d.TKT_NOBill-----Original Message-----From: Andrew Guattery [mailto:service@michaelsappliance.com] Sent: Tuesday, December 21, 2004 11:08 AMTo: Bill MeckSubject: trigger tribulationsYet another goodie...request for item ship date trigger:A) If the HDR SHIP_DAT is NULL, don’t modify the matching ITEM_SHIP_DAT date(s).B) If the HDR SHIP_DAT is not NULL, modify the ITEM_SHIP_DAT(s).C) If the HDR SHIP_DAT gets changed, and it DID MATCH(the original value before the change) the ITEM_SHIP_DAT(s), change the ITEM_SHIP_DAT(s) as well.D) If the HDR SHIP_DAT gets changed, and it DID NOT MATCH the ITEM_SHIP_DAT(s), leave the ITEM_SHIP_DAT(s) alone.create trigger tr_Itemshipdaton PS_TKT_HDRfor insert, updateasset nocount onupdate tlset tl.ITEM_SHIP_DAT = th.SHIP_DATfrom inserted iinner join PS_TKT_LIN tlon i.STR_ID = tl.STR_IDand i.STA_ID= tl.STA_IDand i.TKT_NO=tl.TKT_NOinner join ps_tkt_hdr thon tl.STR_ID= th.STR_IDand tl.STA_ID=th.STA_IDand tl.TKT_NO=th.TKT_NO What do you think?There's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2004-12-21 : 17:13:44
|
SO CLOSE...CREATE TRIGGER tr_itemshipdate ON [dbo].[PS_TKT_HDR] FOR INSERT, UPDATE, DELETE ASset nocount onupdate tl settl.ITEM_SHIP_DAT = case when th.SHIP_DAT is null then tl.ITEM_SHIP_DATwhen th.SHIP_DAT is not null and tl.ITEM_SHIP_DAT is null then th.SHIP_DAT when th.SHIP_DAT is not null and d.SHIP_DAT = tl.ITEM_SHIP_DATthen th.SHIP_DATwhen th.SHIP_DAT is not null and d.SHIP_DAT <> tl.ITEM_SHIP_DATthen tl.ITEM_SHIP_DATend from PS_TKT_HDR thinner join inserted ion i.STR_ID = th.STR_IDand i.STA_ID= th.STA_IDand i.TKT_NO=th.TKT_NOinner join ps_tkt_lin tlon tl.STR_ID= th.STR_IDand tl.STA_ID=th.STA_IDand tl.TKT_NO=th.TKT_NOleft outer join deleted don d.STR_ID = th.STR_IDand d.STA_ID= th.STA_IDand d.TKT_NO=th.TKT_NOwhere tl.str_id=th.str_idand tl.sta_id=tl.sta_idand tl.tkt_no=th.tkt_noset nocount off This all looks ok, but does not work. One thing I forgot is that the PS_TKT_LIN table has the sequence column SEQ_NO as part of its' key. could this have something to do with this not working? If I strip the trigger down to this:update tlset tl.ITEM_SHIP_DAT = th.SHIP_DATfrom inserted iinner join PS_TKT_HDR thon i.STR_ID = th.STR_IDand i.STA_ID=th.STA_IDand i.TKT_NO=th.TKT_NOinner join PS_TKT_LIN tlon tl.STR_ID = th.STR_IDand tl.STA_ID=th.STA_IDand tl.TKT_NO=th.TKT_NO It returns the error 'cannot locate the row for updating'...Any clues?Bewildered AndyThere's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2004-12-22 : 16:47:18
|
Tara,I found out why the original trigger wasn't working, and to fix it We need to move the trigger to the LIN table. Now I have an altogether different problem: On the PS_TKT_LIN table there is this triggercreate trigger tr_UpdateItemInvoiceWarrantyon dbo.PS_TKT_LINfor insert, updateasupdate ttset tt.PROMPT_ALPHA_1 = th.PROF_ALPHA_1from inserted iinner join PS_TKT_LIN tton i.STR_ID = tt.STR_IDand i.STA_ID = tt.STA_IDand i.TKT_NO = tt.TKT_NOand i.SEQ_NO = tt.SEQ_NOinner join IM_ITEM thon tt.ITEM_NO = th.ITEM_NOupdate ttset tt.PROMPT_ALPHA_2 = th.PROF_ALPHA_2from inserted iinner join PS_TKT_LIN tton i.STR_ID = tt.STR_IDand i.STA_ID = tt.STA_IDand i.TKT_NO = tt.TKT_NOand i.SEQ_NO = tt.SEQ_NOinner join IM_ITEM thon tt.ITEM_NO = th.ITEM_NO This trigger works fine. Now I've written this script:create trigger tr_UpdateItemShipDateon dbo.PS_TKT_LINfor insert, updateasupdate tlset tl.ITEM_SHIP_DAT = casewhen th.ship_dat is nullthen tl.item_ship_datwhen th.ship_dat is not null and tl.Item_ship_dat is nullthen th.ship_datwhen th.ship_dat is not null and tl.item_ship_dat=th.ship_datthen th.ship_datwhen th.ship_dat is not null and tl.item_ship_dat<>th.ship_datthen tl.item_ship_datendfrom inserted iinner join PS_TKT_LIN tlon i.STR_ID = tl.STR_IDand i.STA_ID = tl.STA_IDand i.TKT_NO = tl.TKT_NOand i.SEQ_NO = tl.SEQ_NOinner join PS_TKT_HDR thon tl.STR_ID = th.STR_IDand tl.STA_ID = th.STA_IDand tl.TKT_NO = th.TKT_NO When I put this script on the table, and try to save a ticket, I get this error:Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32)I know what it means, but I'm not sure what to do about it. Any ideas?Thanks again for the help!Not-quite-a-people-hater-yetAndyThere's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2004-12-26 : 16:12:54
|
| Tara,The problem all along has been the app. I put the two update triggers together (sorry, I just wasn't thinking), and they work properly when an INSERT is done outside the app. using the app, however, doesn't work. I am going to post this trigger problem in a new posting (so as not to confuse others with the original mess) and see if we can get it fixed.Thanks so much for your help, hope you had a happy holiday!AndyThere's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
|