Other: Global SQL Search

Other: Global SQL Search

The following query will create a stored procedure named SearchAllTables which will let you search all tables in the database for a value. 

It can be used the following ways:
  1. --Search for a column that exactly contains 'Test':
  2. exec SearchAllTables 'Test'
  3. --Search for a column that exactly contains 'Test' anywhere in it:
  4. exec SearchAllTables '%Test%'

Script:


  1. CREATE OR ALTER PROC SearchAllTables
  2. (
  3.     @SearchStr nvarchar(100)
  4. )
  5. AS BEGIN
  6.  
  7.     CREATE TABLE #Results (
  8.                         TableName                  nvarchar(70),
  9.                         ColumnName              nvarchar(370),
  10.                         ColumnValue              nvarchar(3630)
  11.             )
  12.  
  13.     SET NOCOUNT ON
  14.  
  15.     DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
  16.     SET @TableName = ''
  17.     SET @SearchStr2 = QUOTENAME(@SearchStr, '''')
  18.  
  19.     WHILE @TableName IS NOT NULL BEGIN
  20.         SET @ColumnName = ''
  21.         SET @TableName =  (
  22.             SELECT
  23.                                                 MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
  24.             FROM
  25.                                                 INFORMATION_SCHEMA.TABLES
  26.             WHERE 1=1
  27.                                                 AND TABLE_TYPE = 'BASE TABLE'
  28.                 AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
  29.                 AND OBJECTPROPERTY(OBJECT_ID(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)), 'IsMSShipped') = 0
  30.         )
  31.  
  32.         WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL) BEGIN
  33.             SET @ColumnName = (
  34.                 SELECT
  35.                                                             MIN(QUOTENAME(COLUMN_NAME))
  36.                 FROM
  37.                                                             INFORMATION_SCHEMA.COLUMNS
  38.                 WHERE 1=1
  39.                                                             AND TABLE_SCHEMA = PARSENAME(@TableName, 2)
  40.                     AND TABLE_NAME    = PARSENAME(@TableName, 1)
  41.                     AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal', 'uniqueidentifier')
  42.                     AND QUOTENAME(COLUMN_NAME) > @ColumnName
  43.             )
  44.  
  45.             IF @ColumnName IS NOT NULL BEGIN
  46.                 INSERT INTO
  47.                                                             #Results
  48.                 EXEC (''
  49.                     + 'SELECT ''' + @TableName + ''', ''' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630)'
  50.                     + ' FROM ' + @TableName + ' (NOLOCK) '
  51.                     + ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
  52.                 )
  53.             END
  54.         END   
  55.     END
  56.  
  57.     SELECT
  58.                         TableName,
  59.                         ColumnName,
  60.                         ColumnValue,
  61.                         COUNT(*) as Count,
  62.                         Query = CONCAT('SELECT * FROM ', TableName, ' WHERE ', ColumnName, ' LIKE ', @SearchStr2)
  63.             FROM
  64.                         #Results
  65.             GROUP BY
  66.                         TableName, ColumnName,     ColumnValue
  67.             ORDER BY
  68.                         TableName, ColumnName, ColumnValue
  69. END


Once you run the above command, you can search all tables by using the following command:

  1. EXEC SearchAllTables 'SearchTerm'

 




    • Related Articles

    • SQL Spreadsheets: Cherry Picking Contacts

      The following SQL command will generate a spreadsheet that can be used to cherry-pick specific contacts. CREATE OR ALTER FUNCTION SafeSingleLine ( @Input NVARCHAR(MAX) ) RETURNS NVARCHAR(MAX) AS BEGIN DECLARE @Ret NVARCHAR(MAX) = COALESCE(@Input, '') ...
    • Create a "Legacy Matter Number" Custom Field

      Some firms rely heavily on Matter Numbers to search for a specific matter. Sometimes when they migrate to a new system it can't replicate the automatic numbering system from their old system so they want to renumber all their matters to a new scheme ...
    • Delete unused Practice Areas

      The following SQL script will delete unused practice areas. --Delete any practice areas that are not used UPDATE __M_Matters_PracticeAreas SET Final_Status = 'Deleted' WHERE 1=1 AND Id NOT IN( SELECT Final_PracticeArea_Id FROM __M_Matters WHERE ...
    • Other: Total Record Counts

      The following SQL script will count all records in all tables in a database. DECLARE @TableRowCounts TABLE ([TableName] VARCHAR(128), [RowCount] INT) ; INSERT INTO @TableRowCounts ([TableName], [RowCount]) EXEC sp_MSforeachtable 'SELECT ''?'' ...
    • Synthesize Missing Practice Areas

      The following SQL script will create practice areas where they do not exist in the Matters table. --Synthesize missing practice areas from Matters INSERT INTO __M_Matters_PracticeAreas( Id, Final_Subject ) SELECT DISTINCT Final_PracticeArea_Id, ...