SqlServer高级教程之链接数据库
DECLARE @DatabaseName NVARCHAR(128) DECLARE @Rmtsrvname NVARCHAR(128) DECLARE @Rmtuser NVARCHAR(64) DECLARE @Rmtpassword NVARCHAR(64) DECLARE @Server NVARCHAR(128) DECLARE @Id UNIQUEIDENTIFIER --先清理链接数据库 EXECUTE sys.sp_droplinkedsrvlogin @Server, null EXECUTE sys.sp_dropserver @Server, 'droplogins' --添加链接数据库 EXEC master.dbo.sp_addlinkedserver @Server, '', N'SQLOLEDB', @Rmtsrvname; EXEC master.dbo.sp_addlinkedsrvlogin @Server, N'False', NULL, @Rmtuser, @Rmtpassword; -- 访问链接数据库 EXEC('SELECT * FROM [' + @Rmtsrvname + '].[' + @DatabaseName + '].[dbo].[DrugBookMaster] WHERE Id = ''' + @Id + ''' AND IsDeleted = 0')