測試本 Instance 的 Linked Server 是否可以正常連結

create proc #sp @cur sysname
as
    declare @sql nvarchar(1000)=N’select count(*) from [@cur].master.sys.objects’
    set @Sql=REPLACE(@sql,’@cur’,@cur)   
    exec(@Sql)
go

declare cur  cursor fast_forward read_only for
select name from master.sys.servers where product=’sql server’
declare @cur sysname
create table #t(ServerName sysname,ConnectStatus varchar(50),ErrMsg nvarchar(max),ErrNo int)        
open cur
fetch next from cur into @cur
while @@FETCH_STATUS=0
begin
    begin try
        exec #sp @cur
        insert #t(ServerName,ConnectStatus) values(@cur,’Succeed’)
    end try
    begin catch
        insert #t(ServerName,ConnectStatus,ErrMsg,ErrNo) values(@cur,’Fail’,ERROR_MESSAGE(),ERROR_NUMBER())
    end catch
    fetch next from cur into @cur
end
close cur
deallocate cur
select * from #t
drop table #t
drop proc #sp

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s

%d 位部落客按了讚: