Solved

Update a column throughout the DB

Posted on 2013-05-24
5
240 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
  • 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now