开发者

Is using transaction for communicating with a linked server good?

开发者 https://www.devze.com 2023-03-05 20:55 出处:网络
I have the same local and remote databases structure and I connect to remote database using linked server.

I have the same local and remote databases structure and I connect to remote database using linked server. my local database is SQL2000 and remote one is SQL2008 My local table:

tbl_name
Id       Name         flag
1      Raymond         1
2      Sara            1
3      Souzan          2

My Remote table

tbl_name
Id       Name

Now I want to insert every record with falg field value=1 into remote table and then delete the inserted records in local database.

    create proc TransferInsertedRecords
    as
    begin
        create temp @table
        (
          id int,
          name nvarchar(10)
        )
        insert into @temp 
            select id,name from tbl_name
                   where flag=1
        Insert into RemoteServer.dbo.Worker.tbl_Names.name
        values select name from @temp

        delete from t1
                from tbl_name t1
                     inner join @temp t2
                           on t1.id=t2.id and flag=1
end

I have a job that runs this procedure every 5 minutes. How can I be sure every inserted record in remote database will delete in local database? My mean is about connection lose. suppose some data is inserted in remote database but before inserting the rest of data and deleting them in local database the connection lose.

Should I use D开发者_运维知识库ISTRIBUTED TRANSACTION if so where should I put COMMIT TRANSACTION or ROLLBACk statements.


Yes, use DISTRIBUTED TRANSACTION, that's the only way you can ensure your changes will work.

DISTRIBUTED TRANSACTION should be able to handle connection failure, in which case you will detect the error and rollback.

0

精彩评论

暂无评论...
验证码 换一张
取 消