Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- IF DB_ID('oz_test_db') IS NULL BEGIN
- CREATE DATABASE oz_test_db
- END
- GO
- USE oz_test_db
- GO
- -------------------------------------------
- IF (OBJECT_ID(N'dbo.oz_table', 'U') IS NULL) BEGIN
- CREATE TABLE dbo.oz_table (
- ID int IDENTITY PRIMARY KEY NOT NULL,
- sourceID int,
- oz nvarchar(40) NOT NULL
- )
- END
- GO
- -------------------------------------------
- IF (OBJECT_ID(N'dbo.source_table', 'U') IS NULL) BEGIN
- CREATE TABLE dbo.source_table (
- ID int IDENTITY PRIMARY KEY NOT NULL,
- code int,
- some_data nvarchar(50),
- oz1 nvarchar(40),
- oz2 nvarchar(40),
- oz3 nvarchar(40),
- oz4 nvarchar(40)
- )
- END
- GO
- -------------------------------------------
- IF (OBJECT_ID(N'dbo.target_table', 'U') IS NULL) BEGIN
- CREATE TABLE dbo.target_table (
- ID int IDENTITY PRIMARY KEY NOT NULL,
- code int,
- some_data nvarchar(50),
- oz2 nvarchar(40),
- oz3 nvarchar(40),
- oz1 nvarchar(40),
- oz4 nvarchar(40)
- )
- END
- GO
- -------------------------------------------
- IF OBJECT_ID(N'dbo.data_pump', 'P') IS NOT NULL
- DROP PROCEDURE dbo.data_pump
- GO
- CREATE PROCEDURE dbo.data_pump
- AS
- BEGIN
- SET NOCOUNT ON
- DECLARE @result int = -555 -- init (number of affected records)
- DECLARE @info_table TABLE (
- action nvarchar(10),
- code int,
- oz1 nvarchar(40),
- oz2 nvarchar(40),
- oz3 nvarchar(40),
- oz4 nvarchar(40)
- )
- BEGIN TRANSACTION tran_data_pump
- BEGIN TRY
- MERGE target_table AS target
- USING (SELECT code, some_data, oz1, oz2, oz3, oz4
- FROM source_table) AS source
- ON target.code = source.code
- WHEN MATCHED AND (COALESCE(target.some_data, '') != COALESCE(source.some_data, '')
- OR COALESCE(target.oz1, '') != COALESCE(source.oz1, '')
- OR COALESCE(target.oz2, '') != COALESCE(source.oz2, '')
- OR COALESCE(target.oz3, '') != COALESCE(source.oz3, '')
- OR COALESCE(target.oz4, '') != COALESCE(source.oz4, '')
- ) THEN
- UPDATE
- SET target.some_data = source.some_data,
- target.oz1 = source.oz1,
- target.oz2 = source.oz2,
- target.oz3 = source.oz3,
- target.oz4 = source.oz4
- WHEN NOT MATCHED THEN
- INSERT (code, some_data,
- oz1, oz2, oz3, oz4)
- VALUES (source.code, source.some_data,
- source.oz1, source.oz2, source.oz3, source.oz4)
- OUTPUT
- $action AS action, -- INSERT or UPDATE
- inserted.code AS code,
- inserted.oz1 AS oz1,
- inserted.oz2 AS oz2,
- inserted.oz3 AS oz3,
- inserted.oz4 AS oz4
- INTO @info_table;
- SET @result = @@ROWCOUNT
- COMMIT TRANSACTION tran_data_pump
- END TRY
- BEGIN CATCH
- ROLLBACK TRANSACTION tran_data_pump
- SET @result = -1 -- transaction-failed indication
- END CATCH
- RETURN @result -- OK, number of the transfered records
- END
- GO
- -------------------------------------------
- PRINT 'Delete the target_table'
- DELETE target_table
- PRINT 'Delete the oz_table'
- DELETE oz_table
- PRINT 'Delete and initialize the source_table'
- DELETE source_table
- INSERT INTO source_table (code, some_data, oz1, oz2, oz3, oz4) VALUES
- (1, 'data1', 'loginA', 'loginB', 'loginC', 'loginD'),
- (2, 'data2', 'loginX', NULL, NULL, NULL),
- (3, 'data3', NULL, 'loginY', NULL, NULL),
- (4, 'data4', NULL, NULL, 'loginZ', NULL),
- (5, 'data5', NULL, NULL, NULL, 'loginW'),
- (6, 'data6', NULL, NULL, NULL, NULL)
- SELECT * FROM source_table
- PRINT 'Execute the data_pump'
- DECLARE @return_value int
- EXEC @return_value = [dbo].[data_pump]
- SELECT'Affected records' = @return_value
- PRINT 'Execute the data_pump again'
- EXEC @return_value = [dbo].[data_pump]
- SELECT'Affected second time' = @return_value
- SELECT ID, code, some_data, oz1, oz2, oz3, oz4 FROM target_table
- PRINT 'Modify the source table -- data should remain in the target_table'
- DELETE FROM source_table
- WHERE code = 5
- UPDATE source_table
- SET oz1 = 'addedE'
- WHERE code = 4
- SELECT * FROM source_table
- PRINT 'Execute the data_pump'
- EXEC @return_value = [dbo].[data_pump]
- SELECT'Affected records' = @return_value
- SELECT ID, code, some_data, oz1, oz2, oz3, oz4 FROM target_table
- SELECT * FROM oz_table
- GO
- -------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement