Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Update multiple tables

Author  Topic 

Vack
Aged Yak Warrior

530 Posts

Posted - 2013-11-04 : 13:25:31
I have a table that holds a list of fields in it.
TableName = FieldList
In my table FieldList I have one field called Field1 which holds each table.field

field1
imitmidx_sql.item_no
bmprdstr_sql.item_no
oeordlin_sql.item_no
.....


I want to change the value in all of those fields in each table. Can a script be written to read this list and update the item_no field all at once?

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-11-04 : 13:34:32
Run a query such as this to generate the update statements, copy those update statements to an SSMS query window and run it.
SELECT 'Update ' + LEFT(field1,CHARINDEX('.',field1)-1) + ' set ' + STUFF(field1,1,CHARINDEX('.',field1),'')
+ ' = YourNewValueHere'
FROM FieldList;
Please do review the generated SQL statements to make sure that it is correct and that that is what you want to do.

You could also make those statements into a dynamic SQL string and then use EXEC or sp_executesql to execute that dynamic string.
Go to Top of Page
   

- Advertisement -