Tsql sp_MSForeachdb插入声明的虚拟表问题

Tsql sp_MSForeachdb插入声明的虚拟表问题,tsql,sql-server-2016,sp-msforeachdb,Tsql,Sql Server 2016,Sp Msforeachdb,我在获取要插入到@TBL2表中的信息时遇到问题 我做错了什么 DECLARE @command varchar(1000) DECLARE @SQLStatment varchar(1000) DECLARE @TBL2 table ( Database_Name nvarchar(max), SI_SITE nvarchar(max), SI_DB_USER nvarc

我在获取要插入到@TBL2表中的信息时遇到问题

我做错了什么

DECLARE @command varchar(1000) 
DECLARE @SQLStatment varchar(1000) 

DECLARE @TBL2 table (
                    Database_Name nvarchar(max),
                    SI_SITE nvarchar(max),
                    SI_DB_USER nvarchar(max)
                    )  

SELECT @command = 'IF ''?'' NOT IN(''master'', ''model'', ''msdb'', ''tempdb'') BEGIN USE ? insert into @tbl2  EXEC('+ @SQLStatment +') END'

set @SQLStatment =  'select top 1 Db_Name() as Database_Name, SI_SITE, SI_DB_USER from t_site'

EXEC master.. sp_MSForeachdb @command

select * from @TBL2
试试这个: 您所采取的方法存在几个问题:

我认为USE语句不可能是动态的,也可能是错误的

SQL是在您尝试使用它之后声明的

sp_msforeachdb没有文档记录,不应该依赖它,即使它可以在许多情况下工作

我的方法使用sys.databases和字符串连接来生成适当的SQL字符串,以从除系统数据库之外的所有数据库中的每个表中获取所需的数据,然后将结果执行到临时表中。该方法还假设dbo模式。必要时进行调整

declare @SQL nvarchar(max)
set @SQL = ''

Create Table #TBL2 (
                    Database_Name nvarchar(max),
                    SI_SITE nvarchar(max),
                    SI_DB_USER nvarchar(max)
                    )

Select @SQL = @SQL + 'INSERT INTO #TBL2 (Database_Name, SI_SITE, SI_DB_USER) select top 1 ''' + name + ''' as Database_Name, SI_SITE, SI_DB_USER from ' + name + '..t_site;' + char(13)  
From sys.databases
Where name not in ('master', 'model', 'msdb', 'tempdb') 

print @SQL                      
exec sp_executesql @SQL

Select * From #TBL2

drop table #TBL2

我仍然有一些问题要解决,但这就是它可能看起来的样子

declare @SQL nvarchar(max)
set @SQL = ''

Create Table #TBL3 (
                Server_Name nvarchar(max),
                Database_Name nvarchar(max),
                DB_Owner nvarchar(max),
                SI_SITE nvarchar(max),
                SI_DB_USER nvarchar(max),
                DB_Creation_date nvarchar(max),
                DB_state nvarchar(max),
                DB_SQL_version nvarchar(max)
                )

Select @SQL = @SQL + 'INSERT INTO #TBL3 
(
     Server_Name
    ,[Database_Name]
    ,[DB_Owner]
    ,[DB_Creation_Date]
    ,[DB_State]
    ,[DB_SQL_Version]       
    ,[SI_SITE]
    ,[SI_DB_USER]
)

SELECT 
     Server_Name
    ,quotename(''' + name + ''')
    ,[DB_Owner]
    ,[DB_Creation_date]
    ,[DB_state]
    ,[DB_SQL_version]       
    ,[SI_SITE]
    ,[SI_DB_USER]

From( SELECT TOP 1
         [SI_SITE]
        ,[SI_DB_USER]
      From  [' + name + ']..[t_site]) Q1,

    ( SELECT
        @@SERVERNAME as [Server_Name]
        ,suser_sname(owner_sid) as [DB_Owner]
        ,[Create_Date] as [DB_Creation_date]
        ,[state_desc] as [DB_state]
        ,case  [compatibility_level] 
        when 80 then ''SQL Server 2000''  when 90 then ''SQL Server 2005''  when 100 then ''SQL Server 2008''   when 110 then ''SQL Server 2012''   when 120 then ''SQL Server 2014''   when 130 then ''SQL Server 2016''   when 140 then ''SQL Server 2017''   else cast(compatibility_level as varchar(100)) end as DB_SQL_version
    from [sys].[databases]
    where [name] = ''' + name + '''
    ) Q2;' + char(13)

    From sys.databases
Where name not in ('master', 'model', 'msdb', 'tempdb') 
and name not in ('DBX') -- where [T_site] table does not exist  
and name not in ('DBY')  -- offline, need offline clause added


    print @SQL   

    exec sp_executesql @SQL

    Select * From #TBL3

    DROP TABLE #TBL3

尝试使用临时表。@table变量超出了动态sql上下文的范围。您真正想做什么?您可以通过从sys.databases Hi中选择来获取所有数据库名称,这个想法是从我的每个子数据库的表中提取所需的信息,并比较结果是否不一致。>不从“DECLARE@command varchar1000 DECLARE@sqlstation varchar1000 drop table temp create table temp database_Name nvarcharmax,SI_SITE nvarcharmax,SI_DB_用户nvarcharmax SELECT@command='如果?不是INmaster,model,msdb,tempdb开始使用吗?插入主控形状。。Temp EXEC'+@sqlstation+'END'set@sqlstation='选择前1个数据库名称作为数据库名称,SI_站点,来自t_站点的SI_数据库用户'EXEC master。。sp_MSForeachdb@command select*from Temp'每个数据库中是否存在相同的表?非常感谢,我从未想过这样做。我将上传我的编辑版本,因为我在中添加了一些额外的东西。还有几个问题我需要解决。。。