Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
T-SQL 6
SQL Server 5
#SQL Server 5
#TSQL 4
BRH 3

Archive · View All
October 2010 2
April 2012 1
October 2011 1
September 2011 1
May 2011 1
April 2010 1
March 2010 1

Search integer value in all columns in all tables in the database

May 26 2011 3:47PM by Naomi   

A new MSDN thread question prompted me to write a new variation of my original blog post 'How to search a string value in all columns in all tables in a database'. This blog will show how to search for an integer value in all integer (smallint, tinyint, int, bigint) columns in all tables in the database. This time I will not create a separate procedure to search for an integer value in a table, but just show a complete script involving a loop through all tables in a database. This loop may execute a long time in a huge database, so test it only on a small database. There are many ways to enhance it, I just show the idea and appreciate your comments/critique.

-- First declare variables for test
USE AdventureWorks 
DECLARE @SearchValue INT 
-- Set values for variables for test
SET NOCOUNT ON;
SELECT  @SearchValue = 30

DECLARE @Columns NVARCHAR(MAX) ,
    @Cols NVARCHAR(MAX) ,
    @Table_Name SYSNAME ,
    @PkColumn NVARCHAR(MAX) ,
    @Table_Schema SYSNAME ,
    @SQL NVARCHAR(MAX) 

IF OBJECT_ID('TempDB..#Result', 'U') IS NOT NULL 
    DROP TABLE #Result 
CREATE TABLE #RESULT
    (
      [PK COLUMN] NVARCHAR(MAX) ,
      [COLUMN VALUE] BIGINT ,
      [COLUMN Name] SYSNAME ,
      [TABLE SCHEMA] SYSNAME ,
      [TABLE Name] SYSNAME
    )
DECLARE curAllTables CURSOR LOCAL FORWARD_ONLY STATIC READ_ONLY
FOR
    SELECT  Table_Schema ,
            Table_Name
    FROM    INFORMATION_SCHEMA.Tables
    WHERE TABLE_TYPE = 'BASE TABLE'
    ORDER BY Table_Schema ,
            Table_Name
    
OPEN curAllTables
WHILE 1 = 1 
    BEGIN
        FETCH curAllTables
    INTO @Table_Schema, @Table_Name    
        IF @@FETCH_STATUS <> 0  -- Loop through all tables in the database
            BREAK
    

        PRINT CHAR(13) + 'Processing ' + QUOTENAME(@Table_Schema) + '.'
            + QUOTENAME(@Table_Name)
-- Get all int columns
        SET @Columns = STUFF(( SELECT   ', ' + QUOTENAME(Column_Name)
                               FROM     INFORMATION_SCHEMA.COLUMNS
                               WHERE    DATA_TYPE LIKE '%int'
                                        AND TABLE_NAME = @Table_Name
                                        AND table_schema = @Table_Schema
                               ORDER BY COLUMN_NAME
                             FOR
                               XML PATH('')
                             ), 1, 2, '')
        IF @Columns IS NULL 
            BEGIN
                PRINT 'No int columns in the ' + QUOTENAME(@Table_Schema)
                    + '.' + QUOTENAME(@Table_Name)
                CONTINUE
            END  

-- Get columns for select statement - we need to convert all columns to bigint
        SET @Cols = STUFF(( SELECT  ', cast(' + QUOTENAME(Column_Name)
                                    + ' as bigint) as '
                                    + QUOTENAME(Column_Name)
                            FROM    INFORMATION_SCHEMA.COLUMNS
                            WHERE   DATA_TYPE LIKE '%int'
                                    AND TABLE_NAME = @Table_Name
                            ORDER BY COLUMN_NAME
                          FOR
                            XML PATH('')
                          ), 1, 2, '')
 
-- Create PK column(s)
        SET @PkColumn = STUFF(( SELECT  N' + ''|'' + ' + ' cast('
                                        + QUOTENAME(CU.COLUMN_NAME)
                                        + ' as nvarchar(max))'
                                FROM    INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
                                        INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CU ON TC.TABLE_NAME = CU.TABLE_NAME
                                                              AND TC.TABLE_SCHEMA = CU.TABLE_SCHEMA
                                                              AND Tc.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
                                WHERE   TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
                                        AND TC.TABLE_SCHEMA = @Table_Schema
                                        AND TC.TABLE_NAME = @Table_Name
                                ORDER BY CU.COLUMN_NAME
                              FOR
                                XML PATH('')
                              ), 1, 9, '') 

        IF @PkColumn IS NULL 
            SELECT  @PkColumn = 'cast(NULL as nvarchar(max))' 
    
 -- set select statement using dynamic UNPIVOT
 
        SET @SQL = 'select *, ' + QUOTENAME(@Table_Schema, '''')
            + 'as [Table Schema], ' + QUOTENAME(@Table_Name, '''')
            + ' as [Table Name]' + ' from 
  (select ' + @PkColumn + ' as [PK Column], ' + @Cols + ' from '
            + QUOTENAME(@Table_Schema) + '.' + QUOTENAME(@Table_Name)
            + ' )src UNPIVOT ([Column Value] for [Column Name] IN ('
            + @Columns + ')) unpvt 
 WHERE [Column Value] = @SearchValue'
 
 --print @SQL -- if we get errors, we may want to print generated SQL
        INSERT  #RESULT
                ( [PK COLUMN] ,
                  [COLUMN VALUE] ,
                  [COLUMN Name] ,
                  [TABLE SCHEMA] ,
                  [TABLE Name]
                
                )
                EXECUTE sp_ExecuteSQL @SQL, N'@SearchValue int', @SearchValue
                PRINT 'Found ' + CAST(@@ROWCOUNT AS VARCHAR(10)) + ' records in ' 
                       + QUOTENAME(@Table_Schema) + '.' + QUOTENAME(@Table_Name)                
    END
CLOSE curAllTables
DEALLOCATE curAllTables
SELECT  *
FROM    #RESULT
ORDER BY [TABLE SCHEMA] ,
        [TABLE Name]

You can see that we can apply the same idea if we want to search for date field or numerical field.

Tags: T-SQL, BRH, #SQL Server, #TSQL, SQL Server,


Naomi
33 · 6% · 1774
2
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

3  Comments  

  • Hi Naomi.

    This is very useful and I have amended it to exclude table ranges I know it doesn't need to search. However as it currently stands, it locks tables as it searches which is causing issues. How and were would I include the 'with(nolock)' parameter please? I've had attempts at placing it in the SQL string but it complains whatever I do so I'm obviously missing something.

    Thanks .... OM

    commented on Oct 14 2011 5:30AM
    OftMeanders
    2271 · 0% · 5
  • We will add it here

     SET @SQL = 'select *, ' + QUOTENAME(@Table_Schema, '''')
                + 'as [Table Schema], ' + QUOTENAME(@Table_Name, '''')
                + ' as [Table Name]' + ' from 
      (select ' + @PkColumn + ' as [PK Column], ' + @Cols + ' from '
                + QUOTENAME(@Table_Schema) + '.' + QUOTENAME(@Table_Name)
                + ' with (nolock) )src UNPIVOT ([Column Value] for [Column Name] IN ('
                + @Columns + ')) unpvt 
     WHERE [Column Value] = @SearchValue'
    
    commented on Oct 15 2011 7:33PM
    Naomi
    33 · 6% · 1774
  • Thanks. That works brilliantly.

    I thought that was one of the options I tried but clearly had a stray ' or + in there somewhere.

    Thanks again. ... OM

    commented on Oct 17 2011 6:19AM
    OftMeanders
    2271 · 0% · 5

Your Comment


Sign Up or Login to post a comment.

"Search integer value in all columns in all tables in the database" rated 5 out of 5 by 2 readers
Search integer value in all columns in all tables in the database , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]