[Solved] Which table contain this data+ SQL


ypercube’s suggestion is exactly correct. Here is an implementation:

DECLARE @searchText VARCHAR(100)
SET @searchText="Assumed Life Claims"

DECLARE @sqlText VARCHAR(8000)
DECLARE @MaxId INT
DECLARE @CurId INT
DECLARE @possibleColumns TABLE (Id INT IDENTITY(1, 1) NOT NULL PRIMARY KEY
                               ,sqlText VARCHAR(8000))
INSERT INTO @possibleColumns(sqlText)
SELECT 'IF EXISTS (SELECT * FROM ' + c.TABLE_NAME + ' WHERE ' + c.COLUMN_NAME + ' = ''' + @searchText + ''') PRINT '' searchText=" + @searchText + " was found in ' + c.TABLE_NAME + '.' + c.COLUMN_NAME + ''''
  FROM INFORMATION_SCHEMA.COLUMNS c
    -- Using hint that this field is a varchar
 WHERE c.DATA_TYPE = 'varchar'

SELECT @CurId = MIN(pc.Id)
       ,@MaxId = MAX(pc.Id)
  FROM @possibleColumns pc

WHILE (@CurId <= @MaxId)
BEGIN
    SELECT @sqlText = pc.sqlText
      FROM @possibleColumns pc
     WHERE pc.Id = @CurId

    -- For testing (uncomment)
    --PRINT @sqlText

    EXEC(@sqlText)

    -- Increment counter
    SET @CurId = @CurId + 1
END

4

solved Which table contain this data+ SQL