How to search all tables, all columns for a specific value on SQL Server - GoDevStack.com
How to search all tables, all columns for a specific value on SQL Server - GoDevStack.com
Solved:
/***************************************************************************************************Author: GoDevStack.comDescription: Searh all tables, all columns for a specific valueafraid to say too much. More is better, than less, every single time. Think about"what, when, where, how and why" when authoring a description.Usage: @SearchColumnValue = '%Input search value%'****************************************************************************************************/DECLARE @SearchTableName nvarchar(255), @SearchColumnName nvarchar(255), @SearchColumnValue nvarchar(255), @SearchInXML bit, @FullRowResult bit, @FullRowResultRows intSET @SearchColumnValue = '%Input search value%' /* use LIKE syntax */SET @FullRowResult = 1SET @FullRowResultRows = 3SET @SearchTableName = NULL /* NULL for all tables, uses LIKE syntax */SET @SearchColumnName = NULL /* NULL for all columns, uses LIKE syntax */SET @SearchInXML = 0 /* Searching XML data may be slow */IF OBJECT_ID('tempdb..#Results') IS NOT NULL DROP TABLE #ResultsCREATE TABLE #Results (TableName nvarchar(128), ColumnName nvarchar(128), ColumnValue nvarchar(max),ColumnType nvarchar(20))SET NOCOUNT ONDECLARE @TableName nvarchar(256) = '',@ColumnName nvarchar(128),@ColumnType nvarchar(20), @QuotedSearchStrColumnValue nvarchar(110), @QuotedSearchStrColumnName nvarchar(110)SET @QuotedSearchStrColumnValue = QUOTENAME(@SearchColumnValue,'''')DECLARE @ColumnNameTable TABLE (COLUMN_NAME nvarchar(128),DATA_TYPE nvarchar(20))WHILE @TableName IS NOT NULLBEGINSET @TableName =(SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))FROM INFORMATION_SCHEMA.TABLESWHERE TABLE_TYPE = 'BASE TABLE'AND TABLE_NAME LIKE COALESCE(@SearchTableName,TABLE_NAME)AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableNameAND OBJECTPROPERTY(OBJECT_ID(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)), 'IsMSShipped') = 0)IF @TableName IS NOT NULLBEGINDECLARE @sql VARCHAR(MAX)SET @sql = 'SELECT QUOTENAME(COLUMN_NAME),DATA_TYPEFROM INFORMATION_SCHEMA.COLUMNSWHERE TABLE_SCHEMA = PARSENAME(''' + @TableName + ''', 2)AND TABLE_NAME = PARSENAME(''' + @TableName + ''', 1)AND DATA_TYPE IN (' + CASE WHEN ISNUMERIC(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@SearchColumnValue,'%',''),'_',''),'[',''),']',''),'-','')) = 1 THEN '''tinyint'',''int'',''smallint'',''bigint'',''numeric'',''decimal'',''smallmoney'',''money'',' ELSE '' END + '''char'',''varchar'',''nchar'',''nvarchar'',''timestamp'',''uniqueidentifier''' + CASE @SearchInXML WHEN 1 THEN ',''xml''' ELSE '' END + ')AND COLUMN_NAME LIKE COALESCE(' + CASE WHEN @SearchColumnName IS NULL THEN 'NULL' ELSE '''' + @SearchColumnName + '''' END + ',COLUMN_NAME)'INSERT INTO @ColumnNameTableEXEC (@sql)WHILE EXISTS (SELECT TOP 1 COLUMN_NAME FROM @ColumnNameTable)BEGINPRINT @ColumnNameSELECT TOP 1 @ColumnName = COLUMN_NAME,@ColumnType = DATA_TYPE FROM @ColumnNameTableSET @sql = 'SELECT ''' + @TableName + ''',''' + @ColumnName + ''',' + CASE @ColumnType WHEN 'xml' THEN 'LEFT(CAST(' + @ColumnName + ' AS nvarchar(MAX)), 4096),'''WHEN 'timestamp' THEN 'master.dbo.fn_varbintohexstr('+ @ColumnName + '),'''ELSE 'LEFT(' + @ColumnName + ', 4096),''' END + @ColumnType + '''FROM ' + @TableName + ' (NOLOCK) ' +' WHERE ' + CASE @ColumnType WHEN 'xml' THEN 'CAST(' + @ColumnName + ' AS nvarchar(MAX))'WHEN 'timestamp' THEN 'master.dbo.fn_varbintohexstr('+ @ColumnName + ')'ELSE @ColumnName END + ' LIKE ' + @QuotedSearchStrColumnValueINSERT INTO #ResultsEXEC(@sql)IF @@ROWCOUNT > 0 IF @FullRowResult = 1BEGINSET @sql = 'SELECT TOP ' + CAST(@FullRowResultRows AS VARCHAR(3)) + ' ''' + @TableName + ''' AS [TableFound],''' + @ColumnName + ''' AS [ColumnFound],''FullRow>'' AS [FullRow>],*' +' FROM ' + @TableName + ' (NOLOCK) ' +' WHERE ' + CASE @ColumnType WHEN 'xml' THEN 'CAST(' + @ColumnName + ' AS nvarchar(MAX))'WHEN 'timestamp' THEN 'master.dbo.fn_varbintohexstr('+ @ColumnName + ')'ELSE @ColumnName END + ' LIKE ' + @QuotedSearchStrColumnValueEXEC(@sql)ENDDELETE FROM @ColumnNameTable WHERE COLUMN_NAME = @ColumnNameENDENDENDSET NOCOUNT OFFSELECT TableName, ColumnName, ColumnValue, ColumnType, COUNT(*) AS Count FROM #ResultsGROUP BY TableName, ColumnName, ColumnValue, ColumnType/***************************************************************************************************Author: GoDevStack.comDescription: Searh all tables, all columns for a specific valueafraid to say too much. More is better, than less, every single time. Think about"what, when, where, how and why" when authoring a description.Usage: @SearchColumnValue = '%Input search value%'****************************************************************************************************/
Hope it can be useful,
Post a Comment
Thank for leaving message