This is another useful tool.
This Stored Procedure will allow for searching every table and every field and return a passed value. Essentially, if you forgot where something was, you could use this as a diagnostic tool. It is not recommended for programmatic execution as you could imagine, dependent on optimization of your db structure it could take a while.
/****** Object: StoredProcedure [dbo].[SearchAllTables] Script Date: 09/27/2007 10:06:17 ******/ SET GO SET GO ALTER ( @SearchStr nvarchar(100) ) AS BEGIN
-- Copyright © 2002 Narayana Vyas Kondreddi. All rights reserved. -- Purpose: To search all columns of all tables for a given search string -- Written by: Narayana Vyas Kondreddi -- Site: http://vyaskn.tripod.com -- Tested on: SQL Server 7.0 and SQL Server 2000 -- Date modified: 28th July 2002 22:50 GMT
CREATE
SET
DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110) SET @TableName = SET @SearchStr2 =
WHILE @TableName IS BEGIN SET @ColumnName = SET @TableName = ( SELECT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = AND QUOTENAME(TABLE_SCHEMA) AND OBJECTPROPERTY( OBJECT_ID( QUOTENAME(TABLE_SCHEMA) ), ) )
WHILE BEGIN SET @ColumnName = ( SELECT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = AND TABLE_NAME = AND DATA_TYPE IN AND QUOTENAME(COLUMN_NAME) ) IF @ColumnName IS BEGIN INSERT EXEC ( 'SELECT ''' FROM ' ' WHERE ' ) END END END
SELECT ColumnName, ColumnValue FROM #Results END |
The utilization would be something like:
SearchAllTables 'MyValueToFind' |
Output would be something like:
ColumnName | Column Value |
[dbo].[MyTable].[MyField1] | |
[dbo].[MyTable].[MyField2] |
No comments:
Post a Comment