Solved

Update a column throughout the DB

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

12 Experts available now in Live!

Get 1:1 Help Now