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

自闭症网瘾萝莉.ら 提交于 2019-11-27 07:59:52

问题


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 think):

SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name LIKE '%watcher%'

But what I need is a query where the column name is unknown, but I know what the content will be and I want to find out what the name of table/column is. (I know this sounds strange :-/ ). I this possible?


回答1:


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.




回答2:


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



回答3:


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).

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??




回答4:


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.




回答5:



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;




回答6:


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


来源:https://stackoverflow.com/questions/5818598/find-a-specific-column-entry-in-an-unknown-table-in-a-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!