Need help finding a column with a specific value.

  • I've got a database with thousands of tables. About a hundred have the column I wish to search. I want those tables that have a specific value in that column. Any script wizards out there?

    When the snows fall and the white winds blow,The lone wolf dies but the pack survives.

    Once you've accepted your flaws, no one can use them against you.

  • Something like this will generate the sql you would need to execute:

    DECLARE @sql NVARCHAR(MAX)

    SET @sql = 'select '

    SELECT

    @sql = @sql + C.[name] + ' From ' + T.[name] + ' Where ' + C.[name] + ' = ''[value]'' Union ALL Select '

    FROM

    sys.columns AS C JOIN

    sys.tables AS T ON

    C.[object_id] = T.[object_id]

    WHERE

    C.[name] = 'name'

    SELECT LEFT(@sql, LEN(@sql) - 16)

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply