Solved

Tricky SQL Query

Posted on 2008-10-02
3
234 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now