| Author |
Topic |
|
dhani
Posting Yak Master
132 Posts |
Posted - 2008-10-09 : 13:23:23
|
| hello all,can any one tell me, how can i write a procedure or t-sql statement that i have 2 tables (example: tempTransaction, originalTransactions)in above both tables it has columns (example: transno, amount,no.of items)so temptransaction table rows compare with originaltransaction table rows,if row(transno) not found in originaltransaction table then insert the rowif found and has updates (either in amount or no.of items) then update with temptransaction table values for particular transaction(transno)can you please provide any examples or thoughts Thanks in advance |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-09 : 13:37:07
|
| [code]CREATE PROC ApplyDeltasASINSERT INTO originalTransactionsSELECT tmp.transno, tmp.amount,tmp.[no.of items]FROM tempTransaction tmpLEFT JOIN originalTransactions orgON org.transno = tmp.transnoWHERE org.transno IS NULLUPDATE orgSET org.amount=tmp.amount,org.[no.of items]=tmp.[no.of items]FROM originalTransactions orgINNER JOIN tempTransaction tmpON org.transno = tmp.transnoWHERE org.amount<>tmp.amountOR org.[no.of items]<>tmp.[no.of items]GO[/code] |
 |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2008-10-09 : 19:39:15
|
| Hello Vishak,Thank you for your response,your answer worked me very fine, but i have some adjustments in the query, i tried it but unable to get exactly,can you please give me idea or some procedure or t-sql statement that i have 2 tables (example: tempTransaction, originalTransactions)tempTransactions table columns(transno, amount,no.of items,status)OrigTransactions table columns(transno, amount,no.of items)so tempTransaction table rows will compare with originalTransaction table rows,if row(transno) not found in originalTransaction table, then insert the row in OriginalTransaction table, and also update the status column (in tempTransaction table) to "INS"if found and it has updates (either in amount or no.of items) then update with tempTransaction table values for particular transaction(transno) in OrigTransactions table, and also update the status column (in tempTransaction table) to "UPD"if found and it has NO updates (in both tables same record) then delete from tempTransactioncan you please provide any examples or thoughts Best Regardsaswani |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-10 : 01:04:06
|
| [code]CREATE PROC ApplyDeltasASDECLARE @INSERTED_IDS table(transno int)DECLARE @UPDATED_IDS table(transno int)INSERT INTO originalTransactionsOUTPUT INSERTED.transno INTO @INSERTED_IDSSELECT tmp.transno, tmp.amount,tmp.[no.of items]FROM tempTransaction tmpLEFT JOIN originalTransactions orgON org.transno = tmp.transnoWHERE org.transno IS NULLUPDATE orgSET org.amount=tmp.amount,org.[no.of items]=tmp.[no.of items]OUTPUT INSERTED.transno INTO @UPDATED_IDSFROM originalTransactions orgINNER JOIN tempTransaction tmpON org.transno = tmp.transnoWHERE org.amount<>tmp.amountOR org.[no.of items]<>tmp.[no.of items]DELETE tmpFROM tempTransaction tmpINNER JOIN originalTransactions orgON org.transno = tmp.transnoAND org.amount=tmp.amountAND org.[no.of items]=tmp.[no.of items]UPDATE tmp SET tmp.status=CASE WHEN i.transno IS NOT NULL THEN 'INS' ELSE 'UPD' END FROM tempTransaction tmpLEFT JOIN @INSERTED_IDS iON i.transno=tmp.transnoLEFT JOIN @UPDATED_IDS uON u.transno=tmp.transnoWHERE i.transno IS NOT NULLOR u.transno IS NOT NULLGO[/code]i've assumed transno is int. if not change accordingly in two table varuables. |
 |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2008-10-10 : 10:52:58
|
| Hello Visakhthank you for your immediate responsethe code is working as belowin originalTrans table it is inserting all new values, updating the changed values in origtrans table butit is deleting all rows in the temptransaction table even i changed delete (query) to the above of insert query even there is no change can you look at once pleaseRegardsasini |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-10 : 11:49:40
|
| i think moving DELETE statement as first one above insert should solve the problem. |
 |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2008-10-10 : 12:15:46
|
sorry visakh i did a small mistake( that i created procedure but i didn't move delete part in procedure)i moved delete code above to insert statementand it is worked finethank you very much i grateful to youasini |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-10 : 12:42:30
|
quote: Originally posted by dhani sorry visakh i did a small mistake( that i created procedure but i didn't move delete part in procedure)i moved delete code above to insert statementand it is worked finethank you very much i grateful to youasini
no worries you're welcomeglad that i could help you out |
 |
|
|
|