简体   繁体   中英

Update data if matching value and column in all table

I have a problem on updating value in a column named 'ST_CODE' exists in many tables throughout the database.

I am able to find out the table that contain the column "ST_CODE" with the following codes.

SELECT c.name As ColNames, t.name as TableNames 
FROM sys.columns c JOIN sys.tables t ON c.object_id = t.object_id 
WHERE c.name='ST_CODE'

Is it possible to update the value from '00000' to '11000' for all the columns named 'ST_CODE' within all the tables?

Many thanks!

Yes.. create a stored procedure and insert your tablename and column name in a temporary table.

  Insert into #temp(SELECT c.name As ColNames, t.name as TableNames 
  FROM sys.columns c JOIN sys.tables t ON c.object_id = t.object_id 
  WHERE c.name='ST_CODE')

Then loop your temp table and write update statement as you needed. Please follow this link for assistance. http://ask.sqlservercentral.com/questions/24259/how-to-update-a-dynamic-sql-table-with-a-set-varia.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM