Saturday, June 2, 2012

SQL: Update table from another table.

SQL: Update table from another table.

1- Create a temp table with a primary key column and fields which is to be update
2- Insert data into temp table using sql script or any other tool
3- Run the update statement as given below

update ra_customer_trx_all rcta
set  rcta.COMMENTS = (select COMMENTS from RA_TEMP a where a.CUSTOMER_TRX_ID=rcta.CUSTOMER_TRX_ID)
where rcta.CUSTOMER_TRX_ID = (select CUSTOMER_TRX_ID from RA_TEMP a where a.CUSTOMER_TRX_ID=rcta.CUSTOMER_TRX_ID)

Here:

ra_customer_trx_all = is the table to be updated
RA_TEMP  = is the other temp table having primary key and new data.
rcta.COMMENTS = is the filed to be updated.
rcta.CUSTOMER_TRX_ID = is the primary key in main table.
a.CUSTOMER_TRX_ID = is the primary key in temp table

Your feedback/comment is quite important for improving this blog so feel free to leave your comments & suggestions

0 comments:

Post a Comment