我经常使用此查询来检索与数据库中的字符串匹配的所有列。我试图链接两个表,并且这样做我需要匹配列来连接两个。
我希望能够搜索的是一个包含字符串" CUSTOMER_ID"的列的表。和另一列" WORKORDER_BASE_ID"
有没有一种简单的方法可以做到这一点,从这个sqript工作?我认为某些类型的子查询可能是必要的,但我还没有做任何工作。
SELECT
t.name AS table_name
,SCHEMA_NAME(schema_id) AS schema_name
,c.name AS column_name
,T.modify_date
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE
c.name LIKE '%CUSTOMER_ID%'
ORDER BY modify_date
答案 0 :(得分:1)
这样的事情应该有效:
SELECT t.name AS table_name ,
SCHEMA_NAME(schema_id) AS schema_name ,
c.name AS column_name ,
T.modify_date
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE EXISTS ( SELECT *
FROM sys.columns c1
WHERE c1.object_id = c.object_id
AND c1.name = 'CUSTOMER_ID' )
AND EXISTS ( SELECT *
FROM sys.columns c1
WHERE c1.object_id = c.object_id
AND c1.name = 'WORKORDER_BASE_ID' )
ORDER BY modify_date