查询数据库所有表,和已关联发布订阅的表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010690818/article/details/89674828

数据库类型:SqlServer 2012

--检查是否存在发布订阅的系统表
if (select count(*) from sys.tables where name=N'sysarticles' or name= N'syspublications') =2
            --存在, 查询数据库 下所有表的 订阅情况
            select a.name as 'TableName' , (case when b.artid is null then 0 else 1 end ) as 'IsCheck',c.name as'PublicationName'
            from sys.tables a
            left join [PhoneAndPic].[dbo].[sysarticles] b on a.object_id=b.objid and b.dest_owner='dbo'
            left join [PhoneAndPic].[dbo].[syspublications] c on b.pubid=c.pubid
            where a.is_ms_shipped =0 
            order by c.name desc ;
else
            --不存在,只查询普通表
            select a.name as  'TableName' , 0 as'IsCheck', ''as'PublicationName'  
            from sys.tables a 
            where a.is_ms_shipped =0 ;

猜你喜欢

转载自blog.csdn.net/u010690818/article/details/89674828