[Sqlserver] stored procedures for linking databases

Source: Internet
Author: User
Create a database link: /**/ /*--------------------

-- Running example: pro_linkserver 'dbserver', '192. 168.1.1 ', 'sa', 'sapassword'
-- Operation database after running: Select * from DB. Pubs. DBO. Titles
-- Parameter:
-- @ Linknm: link alias
-- @ Rmserv: link address
-- @ Rmuser: User Name
-- @ Rmpawd: User Password

---------------------*/

Create   Procedure Hd_addlinkserver
@ Linknm Sysname,
@ Rmserv Sysname,
@ Rmuser Sysname,
@ Rmpawd Sysname,
@ Flag   Tinyint = 0   -- 0: Add 1: Drop 2: List
As
Begin
If   @ Flag ! = 2   And   Isnull ( @ Linknm , '' ) = ''
Begin
Raiserror ( ' The linked server name cannot be blank. ' , 16 , - 1 )
Return
End

If   @ Flag = 0   And   Isnull ( @ Rmuser , '' ) = ''
Begin
Raiserror ( ' The remote login user name cannot be blank. ' , 16 , - 1 )
Return
End

Declare   @ Err   Int

If   @ Flag = 0
Begin
If   Isnull ( @ Rmserv , '' ) = ''   Select   @ Rmserv = ' 127.0.0.1 '
Exec   @ Err = Master. DBO. sp_addmediaserver @ Linknm

If   @ Err = 0
Begin
Exec   @ Err = Master. DBO. sp_serveroption @ Linknm , ' Use Remote collation ' , ' False '
If   Isnull ( @ Rmserv , '' ) Not   In ( '' , @ Linknm )
Exec   @ Err = Master. DBO. sp_setnetname @ Linknm , @ Rmserv
If   @ Err = 0
Exec   @ Err = Master. DBO. sp_add1_srvlogin @ Linknm , ' False ' , Null , @ Rmuser , @ Rmpawd
End
End   Else
If   @ Flag = 1
Begin
Exec   @ Err = Master. DBO. sp_dropserver @ Linknm , ' Droplogins '
End   Else
If   @ Flag = 2
Begin
Exec   @ Err = Master. DBO. sp_helpserver
Select   *   From Master. DBO. sysservers
End

If   @ Err = 0   Print   ' SQL Server link success '   Else   Print   ' SQL Server link fail '

End
Go

Delete link: /**/ /*******************************
-- Function:
-- Create sqlserver database link ing
-- Parameter description:
-- @ Sqlserver: Server IP Address/Server alias
********************************/
Create   Proc Hd_droplinkserver
@ Sqlserver   Nvarchar ( 50 )
As
Exec Sp_dropserver @ Sqlserver , ' Droplogins '

Go

application example after link creation: select * from [ dbserver ] . [ pubs ] . [ DBO ] . [ titles ]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.