SQL Server 2012。
以下查询获取容器DB中存在的所有架构和相应的表:
select table_schema,TABLE_NAME from information_schema.tables where
TABLE_SCHEMA not in ('cdc') and table_type in ('base table','view')
cdc架构如下:
事实:
我正在尝试编写一个查询,该查询根据在其他模式中找到的所有表格提供cdc模式下所有CT表的列表,但我不知道如何包含正则表达式(如果可能,如select子句中的 SchemaName_TableName_NEW_CT )(以下查询只选择一个CT表):
select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME,TABLE_TYPE from information_schema.tables where
TABLE_SCHEMA in ('cdc')
and table_name in (select table_schema+'_'+TABLE_NAME+'_'+'CT' from information_schema.tables where
TABLE_SCHEMA not in ('cdc') and table_type in ('base table','view'))
order by TABLE_SCHEMA, TABLE_NAME;
答案 0 :(得分:1)
试试这个:
select t1.TABLE_CATALOG, t1.TABLE_SCHEMA, t1.TABLE_NAME, t1.TABLE_TYPE
from information_schema.tables as t1
inner join
(
select table_schema+'_'+TABLE_NAME as TableName
from information_schema.tables
where TABLE_SCHEMA <> 'cdc'
and table_type in ('base table','view')
) as t2
on t1.TableName LIKE t2.TableName +'%CT'
where t1.TABLE_SCHEMA = 'cdc'
order by TABLE_SCHEMA, TABLE_NAME;
答案 1 :(得分:1)
使用Zohar Peled代码,使用reverse
字符串函数使其更快地运行:
on REVERSE (t1.TableName) LIKE 'TC%'
您保存字符串并且LIKE 'TC%'
的工作速度比LIKE '%CT'
答案 2 :(得分:0)
以下内容仅适用于:
您可以使用LIKE
和NOT LIKE
并添加%符号来表示我正在搜索以ct结尾的所有内容。
table_name NOT LIKE '%ct'
会返回所有名称末尾没有ct的表格
table_name LIKE '%ct'
将返回名称末尾有ct的所有表
你也可以做table_name LIKE '%ct%'
,它会返回包含字母ct的所有表格(这当然是最不理想的)
select
TABLE_CATALOG
,TABLE_SCHEMA
,TABLE_NAME
,TABLE_TYPE
from information_schema.tables
where
TABLE_SCHEMA in ('cdc') and
(table_name not like '%ct' and TABLE_SCHEMA not in ('cdc') and table_type in ('base table','view'))
order by
TABLE_SCHEMA, TABLE_NAME;
答案 3 :(得分:0)
试试这个, 首先,它创建一个临时表包含所有更改表(CT)名称,然后尝试检查cdc架构中是否存在匹配
create table #temp(tempName varchar(1000))
GO
Insert into #temp
select a.name + '_' + b.name + '_CT' from
sys.schemas a
left join
sys.tables b on a.schema_id = b.schema_id
where a.name != 'cdc'
select * from #temp
GO
select * from
sys.schemas a
left join
sys.tables b on a.schema_id = b.schema_id
left join
#temp c on c.tempName = b.name
where c.tempName != null and a.name = 'cdc'
Drop table #temp