Solved

Update a column throughout the DB

Posted on 2013-05-24
5
250 Views
Last Modified: 2013-05-28
In my database more tables contain a column called DataRowVersion. I would like to update the column value as 1 in all of the tables. How to perform it in single shot? Please assist.
0
Comment
Question by:Easwaran Paramasivam
[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
  • 2
5 Comments
 
LVL 25

Expert Comment

by:jogos
ID: 39196382
Find all tables that hold your column
http://blog.sqlauthority.com/2008/08/06/sql-server-query-to-find-column-from-all-tables-of-database/

use it as the base for a cursor whith in the cursorloop your update composed as a dynamic sql
http://www.softcodearticle.com/2012/12/mssql-declare-cursor-exampleexecute-sql-string/
0
 
LVL 25

Expert Comment

by:jogos
ID: 39196390
Or execute this script.  Execute it with text output box and  copy output and past it in the query window to execute it.
I added the WHERE IS NULL, but you can change that for your needs
SELECT 'update ' +  t.name + ' set DataRowVersion = 1 where DataRowVersion is null ; '
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name ='DataRowVersion'
ORDER BY schema_name, table_name;

Open in new window

0
 
LVL 16

Author Comment

by:Easwaran Paramasivam
ID: 39196532
It gives error as Incorrect syntax near 'schema_id'.
0
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39196548
step 1: execute the below update statement, it gives you a set of update statements as rows...
step 2: Now, copy them up and put them back in SSMS and execute them.

select 'UPDATE ' + T.TABLE_SCHEMA + '.' + T.TABLE_NAME + ' SET DataRowVersion = 1 where DataRowVersion is null ; ' 
from INFORMATION_SCHEMA.TABLES T
JOIN INFORMATION_SCHEMA.COLUMNS C
ON T.TABLE_NAME = C.TABLE_NAME
AND c.COLUMN_NAME ='DataRowVersion'

Open in new window

0
 
LVL 16

Author Closing Comment

by:Easwaran Paramasivam
ID: 39200920
Thanks.
0

Featured Post

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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