Tuesday, November 24, 2009

Updating Columns in a Table from Data from Another

Let us imagine that you are attempting a migration of data from a schema in an existing third party application. Then you noticed that the tables are simply over-normalised and you have a need update a column in a target table from another table. What should you do ?

This is what I normally do :-

1) Identify the common field or fields  in both say GLCODE
2) Identify the target column to populate in your target table and the source column in the source table
say GLNAME

To achieve your objective, EXECUTE this SQL statement


UPDATE  TargetTableName SET GLNAME=(SELECT GLNAME  FROM SourceTableName WHERE GLCODE=SourceTable.GLCODE)

That's it !

No comments:

Post a Comment