| Author |
Topic |
|
TRACEYSQL
Aged Yak Warrior
594 Posts |
Posted - 2006-11-27 : 13:34:01
|
| First time writing a triggerThis needs to update reason code when new record inserted and updated records.Is this correct for both cases.Getting confused on UPDATE xas i usually do UPDATE (TABLENAME) here or UPDATE DELTEK.EMPL_LAB_INFO XDo i need to change below ?CREATE TRIGGER TR_Empl_Lab_InfoON DELTEK.EMPL_LAB_INFOAFTER UPDATE, INSERTASUPDATE xSET PERS_ACT_RSN_CD = 'NCT'FROM DELTEK.EMPL_LAB_INFO XINNER JOIN INSERTED on x.EMPL_ID = inserted.EMPL_ID and x.EFFECT_DT = inserted.EFFECT_DTWHERE x.REASON_DESC = 'NEW HIRE CONTRACT TRANSITION' |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-11-27 : 13:37:32
|
| What is DELTEK? table owner?Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
TRACEYSQL
Aged Yak Warrior
594 Posts |
Posted - 2006-11-27 : 13:41:21
|
| Yes DELTEK is the database owner |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-11-27 : 15:05:06
|
probably not relevant, but on a case-sensitive database the use of "INSERTED" and "inserted" isn't going to work, and that apart its sloppy - no offence intended.Alias it if you prefer:INNER JOIN inserted AS I ON x.EMPL_ID = I.EMPL_ID AND x.EFFECT_DT = I.EFFECT_DT So your requirement is:If a record is INSERTED or UPDATE and the REASON_DESC is set to 'NEW HIRE CONTRACT TRANSITION' then overwrite the PERS_ACT_RSN_CD with 'NCT'? If so you've got it!I would have added to the WHERE:AND COALESCE(PERS_ACT_RSN_CD, '') <> 'NCT'to avoid re-updating records that already have that field set.I also presume the PK is EMPL_ID, EFFECT_DT (i.e. given values for those two fields are unique)Kristen |
 |
|
|
TRACEYSQL
Aged Yak Warrior
594 Posts |
Posted - 2006-11-27 : 15:32:54
|
| Thanks for your guidance.x (is the empl lab info record im on)i (is the inserted record that is being inserted)So final trigger is as follows:CREATE TRIGGER TR_Empl_Lab_InfoON DELTEK.EMPL_LAB_INFOAFTER UPDATE, INSERTASUPDATE xSET PERS_ACT_RSN_CD = 'NCT'FROM DELTEK.EMPL_LAB_INFO XINNER JOIN inserted AS I ON x.EMPL_ID = I.EMPL_ID AND x.EFFECT_DT = I.EFFECT_DTWHERE x.REASON_DESC = 'NEW HIRE CONTRACT TRANSITION'AND COALESCE(PERS_ACT_RSN_CD, '') <> 'NCT' |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-11-27 : 15:46:50
|
| I reckon that's going to do the trick, but I'll leave the careful, pedantic, testing down to you!Although I'm a bit bothered that you asked in the first place: Your original code looked fine to me, was there something praying on your mind about it? Perhaps there is some nuance that might be troublesome if you speak-out a little more?But I may just be being pedantic, as defensive-programming is my style - if so just ignore me!Kristen |
 |
|
|
TRACEYSQL
Aged Yak Warrior
594 Posts |
Posted - 2006-11-28 : 08:42:01
|
| Thanks i tested it seemed to work...both in SQL and in the erp application...Asked question cause it the first trigger i have ever written and like to just check to make sure ...and i could not understand UPDATE x ...as im so used to working with UPDATE MYTABLENAME and not using xI understand the I part. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-11-28 : 09:33:50
|
| I always use the "AS" keyword, as I think it makes the SQL more readable:...FROM DELTEK.EMPL_LAB_INFO AS X...don't know if that helps at all though!Kristen |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
TRACEYSQL
Aged Yak Warrior
594 Posts |
Posted - 2006-11-28 : 10:55:06
|
| but in a triggerCREATE TRIGGER TR_Empl_Lab_InfoON DELTEK.EMPL_LAB_INFOAFTER UPDATE, INSERTASUPDATE xu do not need to do CREATE TRIGGER TR_Empl_Lab_InfoON DELTEK.EMPL_LAB_INFOAFTER UPDATE, INSERTASUPDATE DELTEK.EMPL_LAB_INFO as XThats the bit that got me confused...i was i know im on the table but im so used to put table name in there. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-11-28 : 11:37:32
|
You can't actually put the Alias there, it has to be after the FROM (AFAIK)You can doUPDATE MyTableSET MyColumn = 'FOO'WHERE MyColumn = 'BAR'UPDATE MyTableSET MyColumn = 'FOO'FROM MyTableWHERE MyColumn = 'BAR' which is overkill of course, but it leads to the syntax required when you have a JOIN:UPDATE MyTableSET MyColumn = 'FOO'FROM MyTable JOIN MyTable2 ON MyTable2.ID = MyTable.IDWHERE MyColumn = 'BAR' but I prefer to use aliases at that point, and in fact I always alias the table-to-be-updated as U (so that whatever UPDATE source code I am looking at I am less likely to mistake the table that is being updated:UPDATE USET MyColumn = 'FOO'FROM MyTable AS U JOIN MyTable2 AS T2 ON T2.ID = U.IDWHERE MyColumn = 'BAR' Kristen |
 |
|
|
TRACEYSQL
Aged Yak Warrior
594 Posts |
Posted - 2006-11-28 : 12:18:45
|
| Never seen it written like that at the top of the UPDATE..Good to learn.Thanks for your guidance |
 |
|
|
|