Query the specified table exists in those databases

zhaozj2021-02-16  53

- Query the specified table exists in those databases

Declare @tbname sysnameset @ TBNAME = 'Customer Information'

declare @dbname sysname, @ sql nvarchar (4000), @ re bit, @ sql1 varchar (8000) set @ sql1 = '' declare tb cursor for select name from master..sysdatabasesopen tb fetch next from tb into @dbnamewhile @@ fetch_status = 0BEGIN SET @ SQL = 'set @ re = case when @ DBNAME '] .. sysobjects where xtype = 'u' '' and name = '' ' @ tbname ' ') THEN 1 else 0 end 'exec sp_executesql @ SQL, N' @ re bit out ', @ RE OUT IF @ RE = 1 set @ SQL1 = @ SQL1 ' Union All Select '' ' @ DBNAME ' 'Fetch Next from TB INTO @dbnamendclose TBDEAllocate TBSET @ SQL1 = Substring (@ SQL1, 12, 8000) EXEC (@ SQL1)

转载请注明原文地址:https://www.9cbs.com/read-23357.html

New Post(0)