---This fails, as there are two records in TABLEA and when insert into TABLE B
---Only one record should exist at the end of it into TABLEB.
---There should be a record in the TRACK table.
Results are nothing, i don't get anything in TABLEB or the TRACK table
CREATE TABLE [dbo].[TableA](
[Value1] varchar NOT NULL)
GO
CREATE TABLE [dbo].[Tableb](
[Value1] varchar NOT NULL)
CREATE TABLE [dbo].[Track](
[Value1] varchar NOT NULL
) ON [PRIMARY]
GO
--Two rows exists in TableA
Insert into TableA (Value1)
select '1'
Insert into TableA (Value1)
select '1'
---There should be 2 records created in Track and one record in TableB
---There is a duplicate row so it doesn't write anything to the Track Table
TRUNCATE TABLE Track
TRUNCATE TABLE TableB
-----Write all to TRACKER
DROP TABLE IF EXISTS #TEMP_TableB
BEGIN
BEGIN TRANSACTION
BEGIN TRY
SELECT * INTO #TEMP_TableB from TableA
INSERT INTO TableB (Value1)
select * FROM #TEMP_TableB
INSERT INTO Track(value1)
SELECT *
FROM #TEMP_TableB
COMMIT TRANSACTION
END TRY
BEGIN CATCH
IF XACT_STATE() <> 0
INSERT INTO Track(Value1)
SELECT Value1
FROM #TEMP_TableB
ROLLBACK TRANSACTION
END CATCH
END
SELECT * FROM Track
SELECT * FROM TableB