I have a database with 100 tables. how can i know which table contain a particular text.
For example which table contains text 'Assumed Life Claims'
I can give a hint that this filed is a varchar
SQL SERVER 2008
I have a database with 100 tables. how can i know which table contain a particular text.
For example which table contains text 'Assumed Life Claims'
I can give a hint that this filed is a varchar
SQL SERVER 2008
 
    
     
    
    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
