1

I found some SQL code on this site that will search all the tables for a string and return the results without requiring elevated permissions due to restrictions in our environment. This runs without having to create a stored procedure or to create tables and seems to work fine. However, the table and column names are not returned as the same text I see in Microsoft SQL Server Management Studio and I can't determine which tables and columns this is actually referring to.

One line of the results are shown below (note: the query returns thousands of lines depending on the search string). Any help is appreciated.

Results:

ColumnName                      ColumnValue
--------------------------------------------
[dbo].[T1005].[C303497400]      NES Echelon

These results correlate to

ColumnName                                      ColumnValue
-------------------------------------------------------------    
[dbo].[AST_Application].[Application_Name]      NES Echelon

The code

    DECLARE @SearchStr nvarchar(100) = 'SEARCH_TEXT'
    DECLARE @Results TABLE (ColumnName nvarchar(370), ColumnValue nvarchar(3630))

    SET NOCOUNT ON

    DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
    SET  @TableName = ''
    SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')

    WHILE @TableName IS NOT NULL
    BEGIN
        SET @ColumnName = ''
        SET @TableName = 
        (
            SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
            FROM INFORMATION_SCHEMA.TABLES
            WHERE TABLE_TYPE = 'BASE TABLE'
              AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
              AND OBJECTPROPERTY(
                OBJECT_ID(
                    QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
                     ), 'IsMSShipped'
                       ) = 0
)

WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
BEGIN
    SET @ColumnName =
    (
        SELECT MIN(QUOTENAME(COLUMN_NAME))
        FROM     INFORMATION_SCHEMA.COLUMNS
        WHERE         TABLE_SCHEMA    = PARSENAME(@TableName, 2)
            AND    TABLE_NAME    = PARSENAME(@TableName, 1)
            AND    DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal')
            AND    QUOTENAME(COLUMN_NAME) > @ColumnName
    )

    IF @ColumnName IS NOT NULL

    BEGIN
        INSERT INTO @Results
        EXEC
        (
            'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) 
            FROM ' + @TableName + ' (NOLOCK) ' +
            ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
        )
    END
    END    
    END

    SELECT ColumnName, ColumnValue FROM @Results
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
J Fish
  • 11
  • 4
  • Possible duplicate of [Find a string by searching all tables in SQL Server Management Studio 2008](https://stackoverflow.com/questions/15757263/find-a-string-by-searching-all-tables-in-sql-server-management-studio-2008) – Thiago Loureiro Sep 12 '17 at 22:02
  • sql server version? I tested in 2008 R2 and 2012 and works as expected. – Horaciux Sep 13 '17 at 01:21
  • are you using synonyms? – Horaciux Sep 13 '17 at 01:21
  • You're saying you get the right answer but the wrong table and column names? Are these incorrect tables in the same or a different database? I don't see anything in that code that would produce a result like that. Are you sure you're running it in the correct database? – Nick.Mc Sep 13 '17 at 04:55
  • Does table T1005, column C303497400 exist? Are you sure it doesn't contain the value that it says it does? – Nick.Mc Sep 13 '17 at 04:57
  • To troubleshoot I suggest you print out the sql that goes into the EXEC statement – Nick.Mc Sep 13 '17 at 04:58

0 Answers0