SQL Server triggers, stored procedure operations remote database Insert data, resolve server existing problems

Source: Internet
Author: User

Recently, a small project is not very complex, it is necessary to backup some data of one database to another library. Not local, it could be another database on the network. Think about it, using stored procedures and triggers.

is not very complex, first I need to operate the remote database, and then wrote a stored procedure:

CREATE PROCEDURE sendinfotoremotedb @CardNo varchar, @CardStyle varchar, @userId varchar, @UserName varchar ( @passDate datetime, @inOut intasbegin--exec sp_addlinkedserver<span style= "White-space:pre" ></span> [Email protected]= ' Ims_srv_lnk ', <span style= ' white-space:pre ' ></span>[email protected]= ', <span Style= "White-space:pre" ></span>[email protected]= ' msdasql ', <span style= "White-space:pre" ></ Span>[email protected]= ' Driver={sql SERVER}; server=xxxx.com; uid=xxx; Pwd=xxx;database=xxx ' INSERT into Ims_srv_lnk. IMSDBBAK.dbo.tb_record (Cardno,cardstyle,userid,username,passdate,inout) VALUES (@CardNo, @CardStyle, @userId, @ UserName, @passDate, @inOut) Endgo
The fact is to operate a remote database. Insert the data. And this way, the local database writes a trigger:

Create Trigger Tgr_passrecord_insert on  Passcheckrecord for   Insert--insert trigger   as   --Define variable    declare        @CardNo varchar (), @CardStyle varchar, @userId varchar, @UserName varchar, @passDate datetime, @inOut int   --Query the inserted table for the inserted record information  Select @CardNo = Cardno, @CardStyle = Cardstyle, @userId =userid, @UserName = UserName, @passDate =passdate, @inOut =inoutfrom inserted;set @CardNo = @CardNo; Set @CardStyle = @CardStyle; set @userId = @userId; set @UserName = @UserName; set @passDate = @passDate; Set @inOut = @inOut;  exec [dbo]. [Sendinfotoremotedb]    @CardNo = @CardNo,      @CardStyle = @CardStyle,     @userId = @userId,   @UserName = @UserName,   @passDate = @ Passdate,   @inOut = @inOutprint ' Send message successfully! ‘;

Then the result was first run successfully, but the second time prompted Ims_srv_lnk to exist and not run. Back to think about the code inside the connection server that piece deleted, the result is OK.



Source: http://bluesnowsoft.com

SQL Server triggers, stored procedure operations remote database Insert data, resolve server existing problems

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.