Link to home
Start Free TrialLog in
Avatar of NigelRocks
NigelRocks

asked on

Tricky SQL Query

Experts,

I'm performing a small migration in SQL Server and am wondering about how to update a common field in a set of tables.

The field that exists in about 10 tables in this database is LOCATION_ID.  I want to do a global updatge that essentially says that for every LOCATION_ID field in all the tables with the word BUSINESS in the name to have the value changed from a 1 to a 2 (where the value is 1).

How would I do this?
Avatar of momi_sabag
momi_sabag
Flag of United States of America image

you will need to use a transaction if you want all tables updated together:

begin tran
begin try
update table1 set location_id=2 where location_id=1
update table2 set location_id=2 where location_id=1
update table3 set location_id=2 where location_id=1
update table4 set location_id=2 where location_id=1
update table5 set location_id=2 where location_id=1
...
commit
end try
begin catch
select error_number(), error_message()
rollback
end catch
ASKER CERTIFIED SOLUTION
Avatar of randy_knight
randy_knight

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial