Solved

Tricky SQL Query

Posted on 2008-10-02
3
236 Views
Last Modified: 2010-03-20
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?
0
Comment
Question by:NigelRocks
  • 2
3 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 22631178
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
0
 
LVL 4

Accepted Solution

by:
randy_knight earned 500 total points
ID: 22631188
One way to do it would be to query syscolumns to get a list of the tables with LOCATION_ID in them. Then cursor through the tables running an update statement for each table. Use dbo.sp_executesql to dynamically build your statement. Or you could just print the statement to the results then copy and paste.

Here's an example using a column named "body" in the msdb database.

------- code start -----------
declare  @TableName sysname, @sql nvarchar(4000)

declare curTable cursor for
     select o.name
     from msdb.dbo.syscolumns c
            inner join msdb.dbo.sysobjects o on o.id = c.id
      where c.name = 'body'
      order by 1
open curTable
while 1=1
begin
         fetch next from curTable into @TableName
         if @@fetch_status <> 0
            break
         print 'update msdb.dbo.' + @TableName + ' set body = 2'
end
close curTable
deallocate curTable
-----------end code ------------
if you wanted to  go ahead and execute it in the cursor just put the dynamic squl in the @sql variable and call dbo.sp_executesql instead of the print statement.




 
0
 
LVL 4

Assisted Solution

by:randy_knight
randy_knight earned 500 total points
ID: 22631198

One more thing.&nbsp; You said the table would have BUSINESS in the name.&nbsp; So your select statement for the cursor would look like this:

select o.name
from dbo.syscolumns c
inner join dbo.sysobjects o on o.id = c.id
where c.name = 'LOCATION_ID'
and o.name like '%BUSINESS%'
order by 1&nbsp;
&nbsp;
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question