Solved

Tricky SQL Query

Posted on 2008-10-02
3
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

724 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