SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Update multiple tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Vack
Constraint Violating Yak Guru

USA
497 Posts

Posted - 11/04/2013 :  13:25:31  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3649 Posts

Posted - 11/04/2013 :  13:34:32  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000