Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Other SQL Server 2008 Topics
 Updating Columns Dynamically

Author  Topic 

JimL
SQL Slinging Yak Ranger

1537 Posts

Posted - 2009-01-14 : 09:13:38
We have Legacy system for customer IDs it is created by using the 1st 3 char of the customer’s name combined with a 5 digit numeric code to create a unique ID.

Unfortunately companies are bought out or change their name on a regular basis. This was not too bad when dealing with in house software as I just had a SP to update every table that used the “custID” column for a given ID.

Now however we are integrating a canned package that has no such capabilities and can have new tables or columns added as the supplier sees fit.
So the question is……Is there a way to dynamically find all instances of a given column name across all tables in a DB and update a given value in all of them.

Use Mytable
Update <all tables containing column CustID>
Set CustID = @newCustID
Where CustID = @OldCustID


Jim
Users <> Logic

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2009-01-14 : 09:35:24
you'll have to go through the INFORMATION_SCHEMA.COLUMNS and build the update statements from that. then execute those statements.


___________________________________________________________________________
Causing trouble since 1980
Blog: http://weblogs.sqlteam.com/mladenp
Speed up SSMS development: www.ssmstoolspack.com <- version 1.1 out!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-14 : 10:11:45
or use

EXEC sp_Msforeachtable 'IF COL_LENGTH(''?'',''Cust_ID'')>0 Update ?
Set CustID = newcustidvalue
Where CustID = oldvalue'
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-14 : 11:20:25
Yes. I am bored again...
Same as Spirit suggested but with the addition that only tables are taken into consideration.
Views are thrown out.
SET NOCOUNT ON

DECLARE @WantedColumnName SYSNAME,
@OldID VARCHAR(20),
@NewID VARCHAR(20)

SELECT @WantedColumnName = 'CustID',
@OldID = 'ER00544',
@NewID = 'PE1343'

DECLARE @Exec TABLE
(
rowID INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED,
SQL NVARCHAR(4000)
)

INSERT @Exec
(
SQL
)
SELECT N'UPDATE ' + QUOTENAME(c.TABLE_SCHEMA) + N'.' + QUOTENAME(c.TABLE_NAME)
+ N' SET ' + QUOTENAME(@WantedColumnName) + N' = ' + QUOTENAME(@NewID, '''')
+ N' WHERE ' + QUOTENAME(@WantedColumnName) + N' = ' + QUOTENAME(@OldID, '''')
FROM INFORMATION_SCHEMA.COLUMNS AS c
INNER JOIN INFORMATION_SCHEMA.TABLES AS t ON t.TABLE_CATALOG = c.TABLE_CATALOG
AND t.TABLE_SCHEMA = c.TABLE_SCHEMA
AND t.TABLE_NAME = c.TABLE_NAME
WHERE c.COLUMN_NAME = @WantedColumnName
AND t.TABLE_TYPE = 'BASE TABLE'

DECLARE @rowID INT,
@SQL NVARCHAR(4000)

SELECT @rowID = MAX(rowID)
FROM @Exec

WHILE @rowID > 1
BEGIN
SELECT @SQL = SQL
FROM @Exec
WHERE rowID = @rowID

RAISERROR('%s', 10, 1, @SQL) WITH NOWAIT
EXEC sp_executesql @SQL

SET @rowID = @rowID - 1
END


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

JimL
SQL Slinging Yak Ranger

1537 Posts

Posted - 2009-01-15 : 07:56:06
Works Great Peso(no Point in doing Views) Thanks Alot!!!!

Hey just Curious, How would we deal with if we wanted to use it across multiple DBs or all DBs as well?

Jim
Users <> Logic
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2009-01-15 : 08:04:09
put the db name in front of the table name.

___________________________________________________________________________
Causing trouble since 1980
Blog: http://weblogs.sqlteam.com/mladenp
Speed up SSMS development: www.ssmstoolspack.com <- version 1.1 out!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-15 : 09:00:00
quote:
Originally posted by JimL

Works Great Peso(no Point in doing Views) Thanks Alot!!!!

Hey just Curious, How would we deal with if we wanted to use it across multiple DBs or all DBs as well?

Jim
Users <> Logic


use below code inside sp_Msforeachdb system stored procedure

http://blog.sqlauthority.com/2008/08/06/sql-server-query-to-find-column-from-all-tables-of-database/
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-15 : 09:08:33
Since this is a SQL Server 2008 forum, it's easier to open a query window for registered servers and "Local Server Groups".
Then execute code and SSMS will execute the code on all your registered servers for you, automatically.


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -