Author |
Topic |
jej1216
Starting Member
27 Posts |
Posted - 2009-02-03 : 19:10:40
|
I have a simple update with one join that works fine. This works:UPDATE PS_WTHD_TRXN_TBLSET WTHD_BASIS_AMT = T1.[PYMNT_AMT] FROM PS_PAYMENT_TBL T1JOIN PS_WTHD_TRXN_TBL T2 ON T2.BANK_SETID = T1.BANK_SETID AND T2.BANK_CD = T1.BANK_CD AND T2.BANK_ACCT_KEY = T1.BANK_ACCT_KEY AND T2.PYMNT_METHOD = T1.PYMNT_METHOD AND T2.PYMNT_ID = T1.PYMNT_ID AND T2.VENDOR_SETID = T1.REMIT_SETID AND T2.VENDOR_ID = T1.REMIT_VENDOR AND T2.PYMNT_DT = T1.PYMNT_DT WHERE T1.[PYMNT_AMT] IS NOT NULL AND T1.PYMNT_DT >= '2007-01-01' AND T1.PYMNT_DT <= '2007-12-31' AND T1.PYMNT_AMT <> T2.WTHD_BASIS_AMTI have another update that has to join 3 tables instead of just 2 as above. This 3 join update is not working. Here it is:UPDATE PS_PYMNT_VCHR_WTHDSET WTHD_DECL_DATE = T1.[PYMNT_DT] FROM PS_PAYMENT_TBL T1,PS_WTHD_TRXN_TBL T2JOIN PS_PYMNT_VCHR_WTHD T3 ON T3.BUSINESS_UNIT = T2.BUSINESS_UNIT AND T3.PROCESS_INSTANCE = T2.PROCESS_INSTANCE JOIN PS_WTHD_TRXN_TBL T2 ON T2.BANK_SETID = T1.BANK_SETID AND T2.BANK_CD = T1.BANK_CD AND T2.BANK_ACCT_KEY = T1.BANK_ACCT_KEY AND T2.PYMNT_METHOD = T1.PYMNT_METHOD AND T2.PYMNT_ID = T1.PYMNT_ID AND T2.VENDOR_SETID = T1.REMIT_SETID AND T2.VENDOR_ID = T1.REMIT_VENDOR AND T2.PYMNT_DT = T1.PYMNT_DT WHERE T1.[PYMNT_DT] >= '2007-01-01' AND T1.[PYMNT_DT] <= '2007-12-31' AND T1.PYMNT_AMT <> T2.WTHD_BASIS_AMTI get this error:Server: Msg 1011, Level 15, State 1, Line 8The correlation name 'T2' is specified multiple times in a FROM clause.I cannot see examples that I understand on how to do this 3 join statement. I tried changing the statement to:.............JOIN PS_PYMNT_VCHR_WTHD T3 ON T3.BUSINESS_UNIT = T2.BUSINESS_UNIT AND T3.PROCESS_INSTANCE = T2.PROCESS_INSTANCE AND T2.BANK_SETID = T1.BANK_SETID AND T2.BANK_CD = T1.BANK_CD AND T2.BANK_ACCT_KEY = T1.BANK_ACCT_KEY AND T2.PYMNT_METHOD = T1.PYMNT_METHOD AND T2.PYMNT_ID = T1.PYMNT_ID AND T2.VENDOR_SETID = T1.REMIT_SETID AND T2.VENDOR_ID = T1.REMIT_VENDOR AND T2.PYMNT_DT = T1.PYMNT_DT WHERE T1.[PYMNT_DT] >= '2007-01-01' AND T1.[PYMNT_DT] <= '2007-12-31' AND T1.PYMNT_AMT <> T2.WTHD_BASIS_AMTBut I just get a different error, repeated 8 times:Server: Msg 107, Level 16, State 2, Line 1The column prefix 'T1' does not match with a table name or alias name used in the query. I know my syntax is wrong - how do I write my JOINs correctly?TIA,jej1216 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
jej1216
Starting Member
27 Posts |
Posted - 2009-02-03 : 19:20:41
|
I need to tie that third table in somehow - in order to get the dataset I need to update. How can I do that?Thanks, |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2009-02-03 : 19:25:41
|
This might be what you want although it's hard to know for sure:UPDATE T3SET WTHD_DECL_DATE = T1.[PYMNT_DT] FROM PS_PYMNT_VCHR_WTHD T3JOIN PS_WTHD_TRXN_TBL T2 ON T3.BUSINESS_UNIT = T2.BUSINESS_UNITAND T3.PROCESS_INSTANCE = T2.PROCESS_INSTANCE JOIN PS_PAYMENT_TBL T1ON T2.BANK_SETID = T1.BANK_SETID AND T2.BANK_CD = T1.BANK_CDAND T2.BANK_ACCT_KEY = T1.BANK_ACCT_KEYAND T2.PYMNT_METHOD = T1.PYMNT_METHODAND T2.PYMNT_ID = T1.PYMNT_IDAND T2.VENDOR_SETID = T1.REMIT_SETIDAND T2.VENDOR_ID = T1.REMIT_VENDORAND T2.PYMNT_DT = T1.PYMNT_DT WHERE T1.[PYMNT_DT] >= '2007-01-01'AND T1.[PYMNT_DT] <= '2007-12-31'AND T1.PYMNT_AMT <> T2.WTHD_BASIS_AMTTara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
|
|
jej1216
Starting Member
27 Posts |
Posted - 2009-02-03 : 19:31:07
|
I know that to you this sounds simple, but to me I feel like the top of my head is going to blow off. I need to set the T3 Field value to a field in T1, but I have to JOIN in the T2 table to get from T1 to T3 and to get the correct dataset.If I remove the T2 table from "FROM PS_PAYMENT_TBL T1,PS_WTHD_TRXN_TBL T2" the there's nothing there to identify what T2 is in the JOIN ONs. This returns errors saying as much:UPDATE PS_PYMNT_VCHR_WTHDSET WTHD_DECL_DATE = T1.[PYMNT_DT] FROM PS_PAYMENT_TBL T1JOIN PS_PYMNT_VCHR_WTHD T3ON T3.BUSINESS_UNIT = T2.BUSINESS_UNITAND T3.PROCESS_INSTANCE = T2.PROCESS_INSTANCE JOIN PS_WTHD_TRXN_TBL T2 ON T2.BANK_SETID = T1.BANK_SETID AND T2.BANK_CD = T1.BANK_CDAND T2.BANK_ACCT_KEY = T1.BANK_ACCT_KEYAND T2.PYMNT_METHOD = T1.PYMNT_METHODAND T2.PYMNT_ID = T1.PYMNT_IDAND T2.VENDOR_SETID = T1.REMIT_SETIDAND T2.VENDOR_ID = T1.REMIT_VENDORAND T2.PYMNT_DT = T1.PYMNT_DT WHERE T1.[PYMNT_DT] >= '2007-01-01'AND T1.[PYMNT_DT] <= '2007-12-31'AND T1.PYMNT_AMT <> T2.WTHD_BASIS_AMT |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
jej1216
Starting Member
27 Posts |
Posted - 2009-02-03 : 19:48:01
|
No, I missed that last post - sorry. That works! Thanks! Just so I understand my mistake better - is it because I did not assign T3 on my UPDATE line or is it because of the order of my joins that mine failed?Thanks again - exploding head avoided. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|