USE [Hik]
GO
/****** Object: StoredProcedure [dbo].[sp_test] Script Date: 08/21/2015 09:55:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Batch submitted through debugger: SQLQuery17.sql|7|0|C:\Users\a\AppData\Local\Temp\~vs6295.sql
-- Batch submitted through debugger: SQLQuery14.sql|7|0|C:\Users\a\AppData\Local\Temp\~vsCDAD.sql
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[sp_test]
@ServerName VARCHAR(100)
AS
BEGIN SET NOCOUNT ON; --如果经常访问或数据量大,建议用链接服务器 --创建链接服务器
exec sp_addlinkedserver @ServerName, ' ', 'SQLOLEDB ', '192.168.1.9 ' ;
exec sp_addlinkedsrvlogin @ServerName, 'false ',null, 'sa ', '' ; --查询示例
--declare @tname varchar(20),@num int
--set @tname=@ServerName+'.Hik'
--declare @sql Nvarchar(1000)=N'select @num=COUNT(*) from '+@tname+'.dbo.Device';
--Exec sp_executesql @sql,N'@num int output',@num output
--select @num declare @sql Nvarchar(max)=N'select count(*) from '+@ServerName+'.Hik.dbo.Device';
Exec sp_executesql @sql; --处理完成后删除链接服务器
exec sp_dropserver @ServerName, 'droplogins '; END
我也是在网上找到的资源,然后修改了一下,实现了需求的原形,供大家参考。