December 5, 2003 at 3:49 pm
I need to update a field in all tables where the field exists. Is there a generic query which I can run which would help me identify which tables contain the field name?
December 5, 2003 at 4:01 pm
Use the syscolumns table and search by name field. FOr you output do OBJECT_NAME([id]) as the id field is the numeric table reference and object_name is the function that returns the name based on id.
December 6, 2003 at 12:50 am
There are a couple of scripts on the site that do this, try ...
http://www.sqlservercentral.com/scripts/contributions/565.asp
http://www.sqlservercentral.com/scripts/contributions/511.asp
December 6, 2003 at 6:05 am
SELECT Table_Schema + '.' + Table_Name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE OBJECTPROPERTY(OBJECT_ID(Table_Name),'IsUserTable') = 1
AND Column_Name = 'YourColumnName'
--Jonathan
--Jonathan
December 7, 2003 at 12:31 pm
http://www.insidesql.de/scripts/sqlserverfaq.php?id=33
When looking at the execution plan of each, Jonathan's solution is efficient.
For the sake of 'completeness' you can also use this
SELECT
table_schema, table_name
FROM
information_schema.columns
WHERE
column_name = 'MsgDateReceived'
SELECT
s2.name
FROM
dbo.syscolumns s1
JOIN
dbo.sysobjects s2
ON
s1.id = s2.id
WHERE
s1.NAME='MsgDateReceived'
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply