简体   繁体   English

SQL Server:在所有表中搜索列中具有特定值的所有行

[英]SQL Server: Search all tables for all rows with a specific value in a column

(I've seen a couple of other posts about this on StackOverflow -- specifically this and this , but they both seem to be Oracle-specific, and I'm using SQL Server.) (我在 StackOverflow 上看到过其他一些关于此的帖子——特别是thisthis ,但它们似乎都是 Oracle 特定的,我正在使用 SQL Server。)

So I've used this post to get all tables that have a specific column.所以我用这篇文章来获取所有具有特定列的表。 That part works great, and it returns me over 100 tables.那部分效果很好,它给我带来了 100 多张桌子。 This is what I'm using, from that post:这就是我正在使用的,来自那个帖子:

SELECT      c.name  AS 'ColumnName'
            ,t.name AS 'TableName'
FROM        sys.columns c
JOIN        sys.tables  t   ON c.object_id = t.object_id
WHERE       c.name LIKE 'myColumn'
ORDER BY    TableName
            ,ColumnName;

What I'm now looking for is a modification to that -- is there a way to get all of the rows in each of those tables where my target column name has a specific value?我现在正在寻找的是对它的修改——有没有办法在我的目标列名具有特定值的每个表中获取所有行? Basically, I want to be able to rip through every table in the system and select every row where a specific column name has a specific value.基本上,我希望能够遍历系统中的每个表并选择特定列名具有特定值的每一行。 (I'll be trying to delete all those rows later, but SELECT first, DELETE later.) (我稍后会尝试删除所有这些行,但先SELECT ,然后再DELETE 。)

Something along the lines of this (which I know doesn't work; it's for illustration purposes):与此类似的东西(我知道这不起作用;这是为了说明目的):

SELECT [row]
    FROM sys.tables tab
    JOIN sys.columns col ON col.object_id = tab.object_id
    WHERE col.name LIKE 'myColumn' AND col.value = 'myVal'

The only other option I can think of is to take the output from the first query and run a SELECT on each one, which would give me unwieldy output in SQL Server Management Studio, to say the least.我能想到的唯一其他选择是从第一个查询中获取输出并在每个查询上运行一个SELECT ,这至少可以在 SQL Server Management Studio 中给我带来笨拙的输出。

Is there a simple way to go about this, without resorting to external tools or whatnot?有没有一种简单的方法来解决这个问题,而无需借助外部工具或诸如此类的东西?

Use INFORMATION_SCHEMA catalogs.使用 INFORMATION_SCHEMA 目录。

SELECT TABLE_CATALOG, TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%SOME_TEXT%'
ORDER BY TABLE_CATALOG, TABLE_NAME, COLUMN_NAME

Check it here: http://rextester.com/JPEZN41582在这里查看: http ://rextester.com/JPEZN41582

below is what i used to find all tables containing specific value for a specific column name下面是我用来查找包含特定列名的特定值的所有表的内容

DECLARE @TableName varchar(250);
DECLARE @SearchValue varchar(250) = 'myVar';
DECLARE @SearchColumn varchar(250)  = 'MyColumn';
DECLARE @SQLQuery AS NVARCHAR(500);

CREATE TABLE #TempTable(
TableName varchar(250)
)

DECLARE CUR_TEST CURSOR FAST_FORWARD FOR
    SELECT     t.name AS 'TableName'
    FROM        sys.columns c
    JOIN        sys.tables  t   ON c.object_id = t.object_id
    WHERE       c.name = @SearchColumn
    ORDER BY    TableName;

OPEN CUR_TEST
FETCH NEXT FROM CUR_TEST INTO @TableName

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @SQLQuery = 'insert into #TempTable(TableName) 
    select DISTINCT ''' + @TableName + ''' from ' + @TableName + ' WHERE ' + @SearchColumn + ' = ''' + @SearchValue + ''''

    EXECUTE sp_executesql @SQLQuery

   FETCH NEXT FROM CUR_TEST INTO @TableName
END
CLOSE CUR_TEST
DEALLOCATE CUR_TEST
GO

select * from #TempTable
Drop Table #TempTable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM