question

cybersyd avatar image
cybersyd asked

Looping a script for values that changes according to a particular table

Hi everybody, How can I create a procedure in order to repeat a script for values that changes according to a particular table? for example: I've got a DESCRIPTION field in 10 tables, and I need to update all the values of DESCRIPTION according to data coming from a new table I created: TABLE NEW_DESCRIPTION is formed by 3 colums: ROW_ID NEW_DESCRIPTION OLD_DESCRIPTION 1, value_A_1, value_B_1 2, value_A_2, value_B_2 3, value_A_3, value_B_3 .,.,., .,.,., N, value_A_N, value_B_N I created a script that I need to repeat for all the row in TABLE_NEW_DESCRIPTION... update table_1 set DESCRIPTION = 'value_A_1' WHERE DESCRIPTION = 'value_B_1' update table_2 set DESCRIPTION = 'value_A_1' WHERE DESCRIPTION = 'value_B_1' update table_3 set DESCRIPTION = 'value_A_1' WHERE DESCRIPTION = 'value_B_1' update table_4 set DESCRIPTION = 'value_A_1' WHERE DESCRIPTION = 'value_B_1' update table_5 set DESCRIPTION = 'value_A_1' WHERE DESCRIPTION = 'value_B_1' update table_6 set DESCRIPTION = 'value_A_1' WHERE DESCRIPTION = 'value_B_1' update table_7 set DESCRIPTION = 'value_A_1' WHERE DESCRIPTION = 'value_B_1' update table_8 set DESCRIPTION = 'value_A_1' WHERE DESCRIPTION = 'value_B_1' update table_9 set DESCRIPTION = 'value_A_1' WHERE DESCRIPTION = 'value_B_1' update table_10 set DESCRIPTION = 'value_A_1' WHERE DESCRIPTION = 'value_B_1' the same for value_A_2, value_A_3, value_A_4 and so on. How can I loop the script in order to repeat it from row_id 1 to row_id 589?
sql-server-2008scriptloop
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ThomasRushton avatar image
ThomasRushton answered
`UPDATE` does allow you to do this in one hit per table: UPDATE table_1 SET table_1.Description = new_description.new_description FROM table1 INNER JOIN new_description ON table_1.description = new_description.old_description something like that, anyway. Untested code.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

cybersyd avatar image
cybersyd answered
Thanks Thomas, but the script is really a lot longer than the example one and I need to loop it
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.