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 DISTRIBUTED 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.