Find a specific column entry in an unknown table in a database?

后端 未结 6 960
野趣味
野趣味 2020-12-11 20:53

I\'m aware of this topic ( Find a specific column in an unknown table in a database? ) and my problem is quite similar. The query I need is quite similar to this one (I thin

相关标签:
6条回答
  • 2020-12-11 21:21

    Ok, I think that for your problem, you are gonna need dynamic sql, so first take a look at this link. If that weren't enough, the only solution that came to mind involves cursors, so I advise you to keep looking for others implementation of your problem. That said, you can try the following code (but you should test it on small tables first).

    DECLARE @Query NVARCHAR(MAX), @Column NVARCHAR(100), @Table NVARCHAR(100)
    DECLARE @Search NVARCHAR(100)
    SET @Search = 'Your string'
    
    CREATE TABLE #Results(Table_Name VARCHAR(100), Column_Name VARCHAR(100))
    
    DECLARE Col CURSOR FOR
    SELECT Table_Name, Column_Name
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE COLLATION_NAME IS NOT NULL
    ORDER BY TABLE_NAME, ORDINAL_POSITION
    
    OPEN Col
    FETCH NEXT FROM Col INTO @Table, @Column
    WHILE @@FETCH_STATUS = 0
    BEGIN
        SET @Query = 'IF EXISTS (SELECT * FROM '+QUOTENAME(@Table)+' WHERE '+QUOTENAME(@Column)+'='''+@Search+''')
                        SELECT '''+@Table+''','''+@Column+''''
    
        INSERT INTO #Results
        EXEC sp_executesql @Query
        FETCH NEXT FROM Col INTO @Table, @Column
    END
    CLOSE Col
    DEALLOCATE Col
    
    SELECT * FROM #Results
    
    0 讨论(0)
  • 2020-12-11 21:27

    Using SQL Workbench/J you can run the following statement:

    WbGrepData -searchValue=watcher
    

    it will search through all columns in all (accessible) tables and return all rows where the search term is found in at least one column.

    0 讨论(0)
  • 2020-12-11 21:29
    
    USE DBName
    
    GO
    
    SELECT t.name AS table_name,
    
    SCHEMA_NAME(schema_id) AS schema_name,
    c.name AS column_name
    
    FROM sys.tables AS t
    INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
    
    WHERE c.name LIKE '%ColumName start from%'
    
    ORDER BY schema_name, table_name;
    
    
    0 讨论(0)
  • 2020-12-11 21:33

    Try using ApexSQL Search – it searches for both objects and data and it’s a free tool similar to SQL Search from Red Gate.

    Another option if you don’t want to deal with third party tools is query that will use cursors to iterate through all columns in all tables but I’m afraid that it would turn out too complex and performance intensive.

    0 讨论(0)
  • 2020-12-11 21:40

    Have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).

    enter image description here

    enter image description here

    It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??

    0 讨论(0)
  • 2020-12-11 21:43

    It could help too

    DECLARE @columnName as varchar(100)
    SET @columnName = 'ColumnName'
    
    SELECT t.name AS Table, c.name AS Column,
    ty.name AS Tipo, c.max_length AS Length
    FROM sys.tables AS t
    INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
    INNER JOIN sys.types ty ON c.system_type_id = ty.system_type_id
    WHERE c.name LIKE @columnName
    ORDER BY t.name, c.name
    
    0 讨论(0)
提交回复
热议问题