通过链接服务器列出有关SQL Server的信息

时间:2017-05-22 13:35:55

标签: sql-server tsql linked-server

我通过使用SERVERPROPERTY的链接服务器从其他实例中找到信息。不幸的是,SERVERPROPERTY与我正在运行查询的服务器有关。

你能帮助我吗?

查询:

declare @instance_name nvarchar(50)
declare @command nvarchar(max)

declare instance_name_cursor cursor for
     select name 
     from msdb.sys.servers 
     where product = 'SQL Server'

open instance_name_cursor

FETCH NEXT FROM instance_name_cursor INTO @instance_name

while @@FETCH_STATUS = 0
begin
    select @command = 'declare @osversion nvarchar(50)

set @osversion = (select 
case
when windows_release = ''10.0'' then ''Windows Server 2016''
when windows_release = ''6.3'' then ''Windows Server 2012 R2''
when windows_release = ''6.2'' then ''Windows Server 2012''
when windows_release = ''6.1'' then ''Windows Server 2008 R2''
when windows_release = ''6.0'' then ''Windows Server 2008''
end
from sys.dm_os_windows_info )

insert into mydatabase.dbo.sql_server_info

select SERVERPROPERTY (''MachineName''),SERVERPROPERTY (''ProductVersion''),
SERVERPROPERTY (''Edition''),SERVERPROPERTY (''ProductLevel''),
cpu_count,(physical_memory_kb /1024),
(select maximum FROM ['+@instance_name+'].master.sys.configurations 
where configuration_id=1544),@osversion
from ['+@instance_name+'].master.sys.dm_os_sys_info'

     exec (@command)

     FETCH NEXT FROM instance_name_cursor INTO @instance_name
end

CLOSE instance_name_cursor
DEALLOCATE instance_name_cursor

1 个答案:

答案 0 :(得分:0)

您需要在链接服务器上运行SERVERPROPERTY()功能,您可以使用OPENQUERY执行此功能,作为一个简单示例:

SELECT  MachineName
FROM    OPENQUERY([LinkedServer], 'SELECT MachineName = SERVERPROPERTY (''MachineName'')') x;

然后只是稍微调整一下你的查询来处理这个问题,所以删除游标以获得类似的演示术语:

DECLARE @instance_name nvarchar(50) = 'LinkedServer';
DECLARE @command NVARCHAR(MAX);
DECLARE @commandBase NVARCHAR(MAX) = 
    'SELECT MachineName = SERVERPROPERTY (''''MachineName''''),
            ProductVersion = SERVERPROPERTY (''''ProductVersion''''),
            Edition = SERVERPROPERTY (''''Edition''''),
            ProductLevel = SERVERPROPERTY (''''ProductLevel''''),
            Maximum = (SELECT Maximum FROM master.sys.configurations WHERE configuration_id = 1544),
            OSVersion = CASE
                            WHEN wi.windows_release =''''10.0'''' THEN ''''Windows Server 2016''''
                            WHEN wi.windows_release = ''''6.3'''' THEN ''''Windows Server 2012 R2''''
                            WHEN wi.windows_release = ''''6.2'''' THEN ''''Windows Server 2012''''
                            WHEN wi.windows_release = ''''6.1'''' THEN ''''Windows Server 2008 R2''''
                            WHEN wi.windows_release = ''''6.0'''' THEN ''''Windows Server 2008''''
                        END
    FROM    master.sys.dm_os_sys_info AS si
            CROSS JOIN master.sys.dm_os_windows_info AS wi;';


SET @Command = CONCAT('SELECT * FROM OPENQUERY(', QUOTENAME(@instance_name), ', ''', @CommandBase, ''');');

INSERT mydatabase.dbo.sql_server_info (<Columns>)
EXECUTE sp_executesql @Command;

然后只是在光标内插入这个。