1

I am building a test case that would tell me the number of records loaded to my final fact table.

The steps involved are,

  1. Declare variables.

  2. Load dimension dummy data.

  3. load transaction data.

  4. Execute the procedure that loads the transactions to final fact table while looking up into dimension data to extract few business keys.

  5. check the number of records loaded to fact table.

Step 4 is expected to fail due to a unique index violation on one of the partitions and the procedure moves over to the next transaction till it processes all.

But the key is to this test case is to not commit any changes to the database so I have to include them in between a begin and rollback. Steps 1 through 5 are returning as per the expectations when its not processed as transaction. When I do , I receive The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. error. So I have tried to add XACT_STATE<>0 before ROLLBACK and the error no longer occurs but my step 5 is returning zero records. Any suggestions on how I can make step 5 return results?

BEGIN TRANSACTION;
--#1

DECLARE @current_datetime DATETIME;
SET @current_datetime = GETDATE ();

WAITFOR DELAY '00:00:01';

DECLARE @usp_batch_load_control_info TABLE 
(
    batch_load_control_code BIGINT NOT NULL,
    is_batch_load_processed BIT NOT NULL,
    is_change_data_capture_complete BIT NOT NULL,
    change_data_capture_info NVARCHAR(MAX) NULL
);

DECLARE @batch_load_name NVARCHAR(100) = CAST(NEWID () AS NVARCHAR(100)); --generate a batch load name guaranteed to be unique
INSERT INTO @usp_batch_load_control_info(
batch_load_control_code,
is_batch_load_processed,
is_change_data_capture_complete,
change_data_capture_info)
EXEC load.usp_batch_load_control_info @batch_load_name = @batch_load_name;

DECLARE @batch_load_control_code BIGINT;

SELECT @batch_load_control_code = batch_load_control_code
FROM   @usp_batch_load_control_info;

DECLARE @data_datetime1 DATETIME;
SET @data_datetime1 = GETDATE ();

DECLARE @data_datetime2 DATETIME;
SET @data_datetime2 = DATEADD (HOUR, 1, GETDATE ());

DECLARE @data_datetime3 DATETIME;
SET @data_datetime3 = DATEADD (HOUR, 2, GETDATE ());

---dimension
--#2
DECLARE @source dbo.udt_dim_dimesion_source;

INSERT INTO @source (dim_business_key,effective_date,dim_name)
VALUES ('dim_bk1', GETDATE (), 'dim_name');

EXEC base.usp_load_dim_insert @source = @source,@disable_output = 1;

DELETE @source;

INSERT INTO @source (dim_business_key,effective_date,dim_name)
VALUES ('dim_bk1', '2021-02-01', 'dim_name2');

EXEC base.usp_load_dim_insert @source = @source,@disable_output = 1;

UPDATE base.dim_dimension 
SET end_timestamp = '2021-02-01' 
WHERE  dim_name = 'dim_name' 
AND create_timestamp >= @current_datetime;

SELECT * 
FROM base.dim_dimension 
WHERE create_timestamp >= @current_datetime;

--CDC
--#3
INSERT INTO load.base_fact_transaction_source (batch_load_control_code,data_datetime,transaction_id,is_transaction_deleted,trade_date,dim_business_key) 
VALUES (@batch_load_control_code, @data_datetime1, 'transaction_id1', 0, '2021-01-01', 'dim_bk1'),
(@batch_load_control_code, @data_datetime2, 'transaction_id2', 0, '2021-02-01', 'dim_bk1'),
(@batch_load_control_code, @data_datetime3, 'transaction_id3', 0, '2021-03-01', 'dim_bk1');

--#4 
BEGIN TRY
    EXEC base.usp_load_fact_transaction @batch_load_control_code = @batch_load_control_code ,@disable_output = 1;
END TRY

--#5
BEGIN CATCH
    SELECT   *
    FROM     final.fact_transaction
    WHERE    create_timestamp >= @current_datetime;
END CATCH;

IF XACT_STATE() <> 0
    ROLLBACK TRANSACTION
    ;
Shmiel
  • 1,201
  • 10
  • 25
KahLeon
  • 29
  • 1
  • 5
  • 3
    If you get an exception on `exec base.usp...` inside the try/catch the transaction is rollback automatically, you should use the [@@Transcount](https://learn.microsoft.com/en-us/sql/t-sql/functions/trancount-transact-sql?view=sql-server-ver16) – Max Jun 27 '22 at 16:07
  • Please don't SHOUT at us... – Thom A Jun 27 '22 at 16:12
  • @Larnu - Apologies. Muscle memory induced due to steadfast dev standards. – KahLeon Jun 27 '22 at 16:15
  • Some errors automatically rollback transactions, and you're not doing anything with the error information in the CATCH block. – David Browne - Microsoft Jun 27 '22 at 16:45

0 Answers0