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.
Author |
Topic |
kasper
Starting Member
7 Posts |
Posted - 2006-11-21 : 15:47:39
|
Hi,Is there a way to find all database objects(icluding the stored procedures, views, etc.), that have references to the specified column of the specified table. Thanks. |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-11-21 : 22:04:10
|
One way (that will take a little effort) is the following statements. It could give you some false positives if your column is a common name like [code].use pubsselect distinct routine_name, routine_type as object from information_schema.routines where routine_definition like '%pub_id%'unionselect table_name, 'table/view' as object from information_schema.columns where column_name = 'pub_id'Be One with the OptimizerTG |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2006-11-21 : 22:56:08
|
for something like this I build a data dictionary and then search it for the keyword of interest - it's foolproof and requires no real effort :)sqlspec has a trial version (no expiration) that will do this for you, link in my sig below. it builds a chm that is searchable by any keyword. SqlSpec - a fast, cheap, and comprehensive data dictionary generator forSQL Server 2000/2005 and Analysis Server 2005 - http://www.elsasoft.org |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-11-21 : 23:57:01
|
quote: Originally posted by TG One way (that will take a little effort) is the following statements. It could give you some false positives if your column is a common name like [code].use pubsselect distinct routine_name, routine_type as object from information_schema.routines where routine_definition like '%pub_id%'unionselect table_name, 'table/view' as object from information_schema.columns where column_name = 'pub_id'Be One with the OptimizerTG
information_schema.routines routine_definition only shows the first 4000 charactere of the procedure or function code, and does not include trigger code or view definitions at all. I think it is better to do a direct search of syscomments.If you are only dealing with one database, just script all objects out to a single file, and search that in an editor.CODO ERGO SUM |
|
|
mahesh_bote
Constraint Violating Yak Guru
298 Posts |
Posted - 2006-11-22 : 04:30:25
|
try itSELECT 'TABLE ' AS ObjectType, TABLE_NAME AS ObjectName, ORDINAL_POSITION AS ObjectOrder, COLUMN_NAME AS ObjFieldName , DATA_TYPE as ObjDataType, CASE DATA_TYPE WHEN 'varchar' THEN CONVERT(varchar(4),CHARACTER_MAXIMUM_LENGTH) WHEN 'nvarchar' THEN CONVERT(varchar(4),CHARACTER_MAXIMUM_LENGTH) WHEN 'decimal' THEN CONVERT(varchar(2),NUMERIC_PRECISION)+ ', ' + CONVERT(varchar(2),NUMERIC_SCALE) ELSE 'Default' END AS ObjFieldLength FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME IN ( select [name] from sysobjects where xtype = 'U' ) UNION ALL SELECT 'STORED PROC ' AS ObjectType, SPECIFIC_NAME AS ObjectName, ORDINAL_POSITION AS ObjectOrder, PARAMETER_NAME AS ObjFieldName, DATA_TYPE as ObjDataType, CASE DATA_TYPE WHEN 'varchar' THEN CONVERT(varchar(4),CHARACTER_MAXIMUM_LENGTH) WHEN 'nvarchar' THEN CONVERT(varchar(4),CHARACTER_MAXIMUM_LENGTH) WHEN 'decimal' THEN CONVERT(varchar(2),NUMERIC_PRECISION)+ ', ' + CONVERT(varchar(2),NUMERIC_SCALE) ELSE 'Default' END AS ObjFieldLength FROM INFORMATION_SCHEMA.PARAMETERS WHERE SPECIFIC_NAME NOT IN ( select [name] from sysobjects where xtype = 'p' and status < 0 )ORDER BY 1,2,3Mahesh |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-11-22 : 08:04:57
|
quote: Originally posted by Michael Valentine Jones
quote: Originally posted by TG One way (that will take a little effort) is the following statements. It could give you some false positives if your column is a common name like [code].use pubsselect distinct routine_name, routine_type as object from information_schema.routines where routine_definition like '%pub_id%'unionselect table_name, 'table/view' as object from information_schema.columns where column_name = 'pub_id'Be One with the OptimizerTG
information_schema.routines routine_definition only shows the first 4000 charactere of the procedure or function code, and does not include trigger code or view definitions at all. I think it is better to do a direct search of syscomments.If you are only dealing with one database, just script all objects out to a single file, and search that in an editor.CODO ERGO SUM
I didn't even know that because I always use syscomments myself I was attempting to lead our young poster down the enlightened path of information_schema views rather than the system tables :)Be One with the OptimizerTG |
|
|
X002548
Not Just a Number
15586 Posts |
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2006-11-22 : 10:07:35
|
I can't remember where I got this script from (I assume it was from sqlteam but not sure) and it's an excellent search script like you're looking for:USE [databasename]DECLARE @string varchar(1000), @ShowReferences char(1)SET @string = 'mysearchterm' --> searchstringSET @ShowReferences = 'N'/****************************************************************************//* *//* TITLE: sp_FindReferences *//* *//* DATE: 18 February, 2004 *//* *//* AUTHOR: WILLIAM MCEVOY *//* *//****************************************************************************//* *//* DESCRIPTION: SEARCH SYSCOMMENTS FOR INPUT STRING, OUTPUT NAME OF OBJECT *//* *//****************************************************************************/set nocount ondeclare @errnum int , @errors char(1) , @rowcnt int , @output varchar(255)select @errnum = 0 , @errors = 'N' , @rowcnt = 0 , @output = '' /****************************************************************************//* INPUT DATA VALIDATION *//****************************************************************************//****************************************************************************//* M A I N P R O C E S S I N G *//****************************************************************************/-- Create temp table to hold resultsDECLARE @Results table( Name varchar(55), Type varchar(12), DateCreated datetime, ProcLine varchar(4000))IF (@ShowReferences = 'N')BEGIN insert into @Results select distinct 'Name' = convert(varchar(55),SO.name), 'Type' = SO.type, crdate, '' from sysobjects SO join syscomments SC on SC.id = SO.id where SC.text like '%' + @string + '%' union select distinct 'Name' = convert(varchar(55),SO.name), 'Type' = SO.type, crdate, '' from sysobjects SO where SO.name like '%' + @string + '%' union select distinct 'Name' = convert(varchar(55),SO.name), 'Type' = SO.type, crdate, '' from sysobjects SO join syscolumns SC on SC.id = SO.ID where SC.name like '%' + @string + '%' order by 2,1ENDELSEBEGIN insert into @Results select 'Name' = convert(varchar(55),SO.name), 'Type' = SO.type, crdate, 'Proc Line' = text from sysobjects SO join syscomments SC on SC.id = SO.id where SC.text like '%' + @string + '%' union select 'Name' = convert(varchar(55),SO.name), 'Type' = SO.type, crdate, 'Proc Line' = '' from sysobjects SO where SO.name like '%' + @string + '%' union select 'Name' = convert(varchar(55),SO.name), 'Type' = SO.type, crdate, 'Proc Line' = '' from sysobjects SO join syscolumns SC on SC.id = SO.ID where SC.name like '%' + @string + '%' order by 2,1ENDIF (@ShowReferences = 'N')BEGIN select Name, 'Type' = Case (Type) when 'P' then 'Procedure' when 'TR' then 'Trigger' when 'X' then 'Xtended Proc' when 'U' then 'Table' when 'C' then 'Check Constraint' when 'D' then 'Default' when 'F' then 'Foreign Key' when 'K' then 'Primary Key' when 'V' then 'View' else Type end, DateCreated from @Results order by 2,1ENDELSEBEGIN select Name, 'Type' = Case (Type) when 'P' then 'Procedure' when 'TR' then 'Trigger' when 'X' then 'Xtended Proc' when 'U' then 'Table' when 'C' then 'Check Constraint' when 'D' then 'Default' when 'F' then 'Foreign Key' when 'K' then 'Primary Key' when 'V' then 'View' else Type end, DateCreated, ProcLine from @Results order by 2,1END --Lumbago"Real programmers don't document, if it was hard to write it should be hard to understand" |
|
|
kasper
Starting Member
7 Posts |
Posted - 2006-11-27 : 08:58:48
|
Thanks a lot for information! |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-27 : 09:06:01
|
If you have some SP that does dynamic SQL, you will miss them. I just thought you should be aware of that.Peter LarssonHelsingborg, Sweden |
|
|
|
|
|
|
|