0

For example this is my ID: 07E485

I need to find this ID in all tables wherever it is found

All columns, which might carry this value, are sort of string-type...

Something like: select * from **alltables** where **anyColumn**='07E485'

Shnugo
  • 66,100
  • 9
  • 53
  • 114
  • Are you using MySQL or SQL-Server? Don't use tags for software you're not using. – Barmar Aug 12 '16 at 10:39
  • @Barmar Just saw the MySQL tag. Anyway, the actual description has SQL Server, so I answered for that. – Tim Biegeleisen Aug 12 '16 at 10:41
  • Hi vinay, I re-wrote your question the way I took it. Please do a rollback in case of misunderstandings (or do further edits). – Shnugo Aug 13 '16 at 21:26
  • Is this question solved? Do you need further help? Please allow me one hint: It would be very kind of you to tick the acceptance check below the best answer's vote counter. This will 1) mark this issue as solved 2) make it easier for followers to find the best solution 3) pay points to the answerer and 4) pay points to you. Once you crossed the 15 points border yourself you are - additionally - asked to vote on contributions. This is the SO-way to say thank you. Happy Coding! – Shnugo Aug 18 '16 at 10:10
  • I have alreasy answered this question [here](https://stackoverflow.com/a/48739933/9006868). – Ali ahmadkhani Sep 04 '20 at 04:30

3 Answers3

0

The following query will return all tables in the database yourDBName whose name contains 07E485.

SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND
      TABLE_CATALOG = 'yourDBName' AND
      TABLE_NAME LIKE '%07E485%'

If I misread your requirement, and you instead wanted to find all tables precisely named 07E485 in any database, then you can use the following query:

SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND
      TABLE_NAME = '07E485'
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I have id like 07E485 in one table, this id will be there in some other table So i need to find all table list wherever having that unique id i tried ur query i'm not getting anything just i got a column TABLE_NAME without data –  Aug 13 '16 at 06:50
  • What data do you want to get here? – Tim Biegeleisen Aug 13 '16 at 07:08
  • I need list of tables which are that having Id in overall database. –  Aug 13 '16 at 08:43
  • My first query should already be doing this. Have you actually tried it? – Tim Biegeleisen Aug 13 '16 at 10:16
  • Hi @TimBiegeleisen, The question was absolutely unclear and trasformed even more. As far as I got it, the OP was not interested in tables with this string as part of their names, but looked for any occurance of this string in any table and in any column... – Shnugo Aug 13 '16 at 21:20
0

In each database you have a view called INFORMATION_SCHEMA.COLUMNS, you can use this view to query through all of your tables.

This is the way I'ld do it, if anyone know a better way, feel free.. :)

SET NOCOUNT ON
DECLARE @Table varchar(255), @Schema varchar(255), @SQL varchar(MAX)

DECLARE table_cursor CURSOR FOR
    SELECT TABLE_NAME, TABLE_SCHEMA
    FROM INFORMATION_SCHEMA.COLUMNS -- This is a system view where you can see all columns of a database.
    WHERE UPPER(COLUMN_NAME) = 'ID' -- This makes sure you don't loop through any tables that don't have a Column called 'ID'.

    OPEN table_cursor
    FETCH NEXT FROM table_cursor INTO @Table, @Schema
    WHILE @@FETCH_STATUS = 0 BEGIN

        -- This part creates your queries.
        SET @SQL = 'SELECT * FROM '+@Schema+'.'+@Table+'
        WHERE CAST(ID as varchar) = ''07E485''' -- Casting ID to varchar to avoid data type errors.

        -- This executes the query.
        EXEC(@SQL)

        -- If a result is found, i.e. ID is equal to '07E485' somewhere in the table, Table name is printed on the "Messages" tab.
        IF @@ROWCOUNT > 0 PRINT @Table

        FETCH NEXT FROM table_cursor INTO @Table, @Schema
    END
    CLOSE table_cursor
    DEALLOCATE table_cursor

To see which tables contain id = '07E485', go to "Messages" and you will have a list of them.

Erik Blomgren
  • 866
  • 4
  • 8
0

UPDATE My answer completely re-written

Try it like this: This dynamic SQL will check all string-type columns if they are equal to the given search string. You might want to add more data types to the output to get a better look onto the table's row. But one cannot simply put SELECT * as there are data types not allowed in XML without extra effort.

Secondly, by using QUOTENAME, I avoid syntax errors due to column or table names with blanks...

DECLARE @Search VARCHAR(10)='07E485';

DECLARE @cmd VARCHAR(MAX);
WITH TableNames AS
(
    SELECT t.*
          ,t.TABLE_CATALOG + '.' + t.TABLE_SCHEMA + '.' + t.TABLE_NAME AS FullTblName
          ,QUOTENAME(t.TABLE_CATALOG)+ '.' + QUOTENAME(t.TABLE_SCHEMA) + '.' + QUOTENAME(t.TABLE_NAME) AS FullTblNameQuoted
          , 
          STUFF(
          ( 
            SELECT 'OR ' + QUOTENAME(c.COLUMN_NAME) + '=''' + @Search + ''' ' 
            FROM INFORMATION_SCHEMA.COLUMNS AS c
            WHERE c.TABLE_CATALOG=t.TABLE_CATALOG AND c.TABLE_SCHEMA=t.TABLE_SCHEMA AND c.TABLE_NAME=t.TABLE_NAME
              AND DATA_TYPE LIKE '%char%'  --add more types if needed
            FOR XML PATH('')      
          ),1,3,'') AS WhereFilter

    FROM INFORMATION_SCHEMA.TABLES AS t
    WHERE TABLE_TYPE='BASE TABLE'
)
SELECT @cmd = STUFF(
(
    SELECT DISTINCT 'UNION ALL SELECT (SELECT ' + (SELECT STUFF((SELECT ',' + QUOTENAME(COLUMN_NAME) 
                                                                 FROM INFORMATION_SCHEMA.COLUMNS AS c 
                                                                 WHERE c.TABLE_CATALOG=TableNames.TABLE_CATALOG 
                                                                   AND c.TABLE_NAME =TableNames.TABLE_NAME 
                                                                   AND c.DATA_TYPE LIKE '%char%' 
                                                                 FOR XML PATH('')),1,1,'')) + ' FROM ' + FullTblNameQuoted 
                    + ' WHERE ' + WhereFilter
                    + ' FOR XML PATH(''row''),ROOT(''' +  REPLACE(REPLACE(FullTblName,'.','_'),' ','') + '''),TYPE) AS XmlData '
    FROM TableNames
    WHERE WhereFilter IS NOT NULL
    FOR XML PATH('')
),1,10,'')

SET @cmd='SELECT XmlData FROM(' +  @cmd + ') AS tbl WHERE XmlData IS NOT NULL;'
PRINT LEN(@cmd)
EXEC(@cmd)
Community
  • 1
  • 1
Shnugo
  • 66,100
  • 9
  • 53
  • 114
  • Thanx for you suggestion column name different i need tables list by using column data with id like '07E485' –  Aug 13 '16 at 07:04
  • I have id like 07E485 in one table, this id will be there in some other table in column data list SELECT * FROM table where coulmn ='07E485' So i need to find all table list wherever having that unique id in a single database. Hope this will be understood –  Aug 13 '16 at 07:48
  • @vinay You don't know the names of the columns you are searching? Are they -at least - some kind of string type? – Shnugo Aug 13 '16 at 08:17
  • I'm execute your for finding best answer I got following error –  Oct 13 '16 at 12:03
  • Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'group'. Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'desc'. Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'desc'. Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'desc'. Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'USER'. Msg 102, Level 15, State 1, Line 1 Incorrect syntax near ')'. –  Oct 13 '16 at 12:03
  • @vinay Might be, that there are column names or table names with blanks. I'll edit my answer soon... – Shnugo Oct 13 '16 at 12:50
  • thanx @shnugo but i got still exception like this –  Oct 17 '16 at 05:05
  • Msg 6850, Level 16, State 1, Line 1 Column name 'DESCRIPTION ' contains an invalid XML identifier as required by FOR XML; ' '(0x0020) is the first character at fault. –  Oct 17 '16 at 05:05
  • @vinay, Are the tables or schemas with a **blank** in their names? Is it possible to use `PRINT @cmd` and poste the generated command here (with edit option into your question)? I cannot reproduce this. It's working in several databases here... – Shnugo Oct 17 '16 at 09:18