Author |
Topic |
June
Starting Member
18 Posts |
Posted - 2009-01-29 : 01:36:51
|
Hi, I am developing a Material management system where I have an Amendment table. It contains the amendments of Terms & Conditions for an order. AmendmentOrderNo IRN VendorID Description AmdFor AmdRead AmdDateHere IRN is the indent no, Description is the Term which is changed, AmdFor is the Term value befor change and AmdRead is the Term value after change. I have another table CSTerms which contains the original Terms & Conditions.CSTermsIRN CSDate VendorID Validity DeliveryPeriod PayTerms Tax ExciseDuty Discount OthersI want to set a Trigger in Amendment table which will update the CSTerms table on insertion of a new row in Amendment. But my problem is that Description could be any of the columns of CSTerms and so the trigger in Amendment should update only that column in CSTerms. How can I create such a Trigger that will update CSTerms dynamically. Thanks for any help. |
|
June
Starting Member
18 Posts |
Posted - 2009-01-30 : 02:48:58
|
where are all the fabled MVP's? |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-01-30 : 03:06:55
|
Seems like nobody understands your problem.Maybe you can elaborate using sample data?Webfred No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
June
Starting Member
18 Posts |
Posted - 2009-01-30 : 05:46:36
|
quote: Originally posted by webfred Maybe you can elaborate using sample data?Webfred
CSTermsIRN CSDate VendorID Validity DeliveryPeriod M3015 11-01-2009 8 60 days 12 week Now If Validity is changed,say from 60 Days to 40 Days, then a row will be inserted to the Amendment table.AmendmentIRN VendorID Description AmdFor AmdRead M3015 8 Validity 60 Days 40 Days Description contains the column that has been modified. AmdFor contains the value before modification and AmdRead contains the value after modification. Now I want to set a Trigger in Amendment table which will update the CSTerms table on insertion of a row in Amendment. The trigger in Amendment should update only that column in CSTerms(here Validity) which is modified and from the original value to the modified value(here from 60 Days to 40 Days).Hope this is enough...lol |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2009-01-30 : 06:18:56
|
I suspect your schema could be improved.Something like the following should work:UPDATE CSET VendorId = CASE WHEN I.[Description] = 'VendorId' THEN I.AmdRead ELSE C.VendorId END ,Validity = CASE WHEN I.[Description] = 'Validity' THEN I.AmdRead ELSE C.Validity END ,DeliveryPeriod CASE WHEN I.[Description] = 'DeliveryPeriod' THEN I.AmdRead ELSE C.DeliveryPeriod ENDFROM CSTerms C JOIN inserted I ON C.IRN = I.IRN |
|
|
June
Starting Member
18 Posts |
Posted - 2009-02-02 : 00:36:54
|
Thanks. This is what I have done..CREATE TRIGGER t_Amendment ON AmendmentFOR INSERTASDECLARE @stringval varchar(30)select @stringval = inserted.[Description] FROM insertedUPDATE CSTermsSET @stringval=inserted.AmdRead FROM CSTerms JOIN inserted ON CSTerms.IRN = inserted.IRN and CSTerms.VendorID = inserted.VendorIDBut it is not updating the CSTerms table. I think the problem lies in SET @stringval. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-02 : 00:41:18
|
shouldnt it be?UPDATE CSTermsSET inserted.AmdRead=@stringval |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-02 : 01:30:45
|
Yup. As Tara suggests, this will only work as expected when you do a single row insert. when more than one row is inserted, the inserted table will contain more than one row and first select will cause only one of row's description value to be store in variable. so probably what you need is table variable. |
|
|
June
Starting Member
18 Posts |
Posted - 2009-02-02 : 06:26:45
|
quote: Originally posted by visakh16 UPDATE CSTermsSET inserted.AmdRead=@stringval
When I tried it I got the error "cannot use column prefix inserted". quote: Using @stringval doesn't make any sense
I know. I used it only to show that nothing is working as intented. I think I will have to go the dynamic way.thanks for your time Tara and visakh. |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-02-02 : 07:53:20
|
quote: Originally posted by visakh16 shouldnt it be?UPDATE CSTermsSET inserted.AmdRead=@stringval
This makes no sense.You cannot update any column in table INSERTED.Webfred No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-02 : 09:16:36
|
didnt notice inserted. i thought you're updating CSTerms column value. but reading it again i think what you need is dynamic sql. why is your column name stored as a value in amendment? also will you be sure of values that can come inside it? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-02 : 09:32:13
|
ok. Here's one methodCREATE TRIGGER t_Amendment ON AmendmentFOR INSERTASUPDATE cSET c.CSDate=COALESCE(i.CSDate,c.CSDate),c.Validity=COALESCE(i.Validity,c.Validity),c.DeliveryPeriod=COALESCE(i.DeliveryPeriod,c.DeliveryPeriod) FROM CSTerms cJOIN (SELECT VendorID,IRN,MAX(CASE WHEN [Description]='CSDate' THEN AmdRead ELSE NULL END) AS CSDate,MAX(CASE WHEN [Description]='Validity' THEN AmdRead ELSE NULL END) AS Validity,MAX(CASE WHEN [Description]='DeliveryPeriod' THEN AmdRead ELSE NULL END) AS DeliveryPeriod FROM insertedGROUP BY VendorID,IRN)iON c.IRN = i.IRN and c.VendorID = i.VendorID |
|
|
|