Question : Update/Change Value in Multiple Tables SQL 2005

I am trying to make a simple change database wide.   Basically I need to change the value of a field called NOTEINDX in all tables database wide if NOTEINDX is a column in the table, and other conditions are met.

I have created a table that maps the old value of NOTEINDX to the NEWINDX value I want to replace it with database wide.

I have an UPDATE statement that works for a single table, I also have a SELECT statement that returns all tables with NOTEINDX column.  

I know that in SQL 2005 an UPDATE statement can not run on multiple tables.   Can someone please help me come up with a script or stored procedure (im a newb with sp's) that will accomplish this?

I appreciate any help.

thanks
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
//Select Statement, selects all tables in my database with NOTEINDX column
 
SELECT TABLE_NAME FROM TGA.INFORMATION_SCHEMA.Columns WHERE COLUMN_NAME = 'NOTEINDX' AND TABLE_NAME != 'ZZTEST' ORDER BY TABLE_NAME
 
 
 
//Update Statement that works for a single table
 
UPDATE SY03900 SET SY03900.NOTEINDX = ZZTEST.NEWINDX FROM SY03900, ZZTEST WHERE SY03900.NOTEINDX IN (SELECT NOTEINDX FROM TCA..SY03900)

Answer : Update/Change Value in Multiple Tables SQL 2005

Try this - please check carefully - I have altered your update statement...

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
declare @table_name varchar(500)
declare @sqlcmd varchar(500) 
declare table_name_cursor cursor for SELECT distinct TABLE_NAME FROM INFORMATION_SCHEMA.Columns WHERE COLUMN_NAME = 'NOTEINDX' AND TABLE_NAME != 'ZZTEST' ORDER BY TABLE_NAME
open table_name_cursor
fetch next from table_name_cursor into @table_name
while @@fetch_status = 0
begin
	set @sqlcmd = 'UPDATE '+@table_name+' SET NOTEINDX = ZZTEST.NEWINDX FROM ZZTEST WHERE '+@table_name+'.NOTEINDX = ZZTEST.NOTEINDX'
    exec (@sqlcmd)
	fetch next from table_name_cursor into @table_name
end
CLOSE table_name_cursor
DEALLOCATE table_name_cursor
Random Solutions  
 
programming4us programming4us