I have the following code:
CREATE PROCEDURE [dbo].[USER_ARC_EXPORT_$sp]
AS
DECLARE @errmsg VARCHAR(255),
@errno INT,
@next_batch_id NUMERIC(12,0),
@object_name NVARCHAR(255),
@operation_name NVARCHAR(100)
SELECT @errmsg = NULL,
@next_batch_id = 100 --to simplify my explanation, just setting this to 100. In the actual code it goes through a process generate a batch number
-- Insert process start into tracking table
INSERT INTO LocalDB.[dbo].[ARC_PROCESS_TRACKING] ([BATCH_ID],[TRAN_CREATE_TS], [TRAN_STATUS], [ETL_START_TS], [ETL_END_TS])
SELECT batch_id, TRAN_CREATE_TS, 'START', ETL_START_TS, ETL_END_TS
FROM ARC_PROCESS_TRACKING
WHERE BATCH_ID = @next_batch_id
SELECT @errno = @@error
IF @errno <> 0
BEGIN
SELECT @errmsg = 'An error occurred while inserting LocalDB.[dbo].[ARC_PROCESS_TRACKING] with 2.',
GOTO error
END
---- Move data to remote server
INSERT INTO REMOTE.RemoteDB.[dbo].[ARC_TRANS_HEADER]
SELECT *
FROM LocalDB.[dbo].ARC_TRANSACTION_HEADER
SELECT @errno = @@error
IF @errno <> 0
BEGIN
SELECT @errmsg = 'An error occurred while inserting REMOTE [ARC_TRANS_HEADER].',
GOTO error
END
---- Insert Tracking with status of 2 to show the data has moved
-- **This ROW DOES NOT INSERT **
INSERT INTO LocalDB.[dbo].[ARC_PROCESS_TRACKING] ([BATCH_ID],[TRAN_CREATE_TS], [TRAN_STATUS], [ETL_START_TS], [ETL_END_TS])
SELECT batch_id, TRAN_CREATE_TS, 'END', ETL_START_TS, ETL_END_TS
FROM ARC_PROCESS_TRACKING
WHERE BATCH_ID = @next_batch_id
SELECT @errno = @@error
IF @errno <> 0
BEGIN
SELECT @errmsg = 'An error occurred while inserting LocalDB.[dbo].[ARC_PROCESS_TRACKING] with 2.',
GOTO error
END
RETURN
/* Error Handler */
error:
INSERT INTO error_table
SELECT @errno, @errmsg
RETURN
The issue is that the last insert to the ARC_PROCESS_TRACKING
table does not happen. The first insert to the ARC_PROCESS_TRACKING
table works. The insert to the Remote.RemoteDB.dbo.ARC_TRANS_HEADER
works for all records (20,000).
No errors are logged. If I change the procedure to write to a temp table, it works. Something with the insert into the remote table is causing the last insert not to fire. Is there a requirement with the DBLINK table that I am missing to get the subsequent insert statement to work?