I have a table "Table1" which I need to update based on a flag from a config table, "Table_Config". The column names on both the tables are same. The two tables look as shown below.
Table1:
Primary_Key Col1 Col2 Col3 Col4
----------------------------------------------------------------------------
ABC123 20120731 14 Microsoft 100.00
CDE456 20120731 14 IBM 250.00
URYT23 20120731 14 Oracle 150.00
Table_Config:
Primary_Key Col1 Col2 Col3 Col4
---------------------------------------------------------------------------------
UpdateFlag N N Y Y
I have an existing stored procedure that updates the columns on "Table1" by accepting the column name as parameter, something like
Exec usp_UpdateTable "Col1"
Exec usp_UpdateTable "Col2"
Exec usp_UpdateTable "Col3"
I need to call the above stored procedures only when the flag for a particular column on "table_Config" is "Y".
I am planning on implementing this by following the below approach:
- Get all column names from "Table1" and stored in a temp table. The table will now have "Col1", "Col2", "Col3", etc as rows.
- Use a cursor or a while loop to read through the rows in the temp table. This will now return "Col1", "Col2", "Col3", etc.
- For each row, check if the Flag is "Y" by reading "Table_Config", if yes call the stored procedure, Exec usp_UpdateTable <column name>
Please let me know if you guys think of a better approach.