Advertisement
Guest User

Question on implementing a data pump

a guest
Sep 27th, 2016
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.66 KB | None | 0 0
  1. IF DB_ID('oz_test_db') IS NULL BEGIN
  2.     CREATE DATABASE oz_test_db
  3.     END
  4. GO
  5.  
  6. USE oz_test_db
  7. GO
  8.  
  9. -------------------------------------------
  10.  
  11. IF (OBJECT_ID(N'dbo.oz_table', 'U') IS NULL) BEGIN
  12.     CREATE TABLE dbo.oz_table (
  13.         ID int IDENTITY PRIMARY KEY NOT NULL,
  14.         sourceID int,
  15.         oz nvarchar(40) NOT NULL
  16.     )
  17. END
  18.  
  19. GO
  20.  
  21. -------------------------------------------
  22.  
  23. IF (OBJECT_ID(N'dbo.source_table', 'U') IS NULL) BEGIN
  24.     CREATE TABLE dbo.source_table (
  25.         ID int IDENTITY PRIMARY KEY NOT NULL,
  26.         code int,
  27.         some_data nvarchar(50),
  28.         oz1 nvarchar(40),
  29.         oz2 nvarchar(40),
  30.         oz3 nvarchar(40),
  31.         oz4 nvarchar(40)
  32.     )
  33. END
  34.  
  35. GO
  36.  
  37. -------------------------------------------
  38.  
  39. IF (OBJECT_ID(N'dbo.target_table', 'U') IS NULL) BEGIN
  40.     CREATE TABLE dbo.target_table (
  41.         ID int IDENTITY PRIMARY KEY NOT NULL,
  42.         code int,
  43.         some_data nvarchar(50),
  44.         oz2 nvarchar(40),
  45.         oz3 nvarchar(40),
  46.         oz1 nvarchar(40),
  47.         oz4 nvarchar(40)
  48.     )
  49. END
  50.  
  51. GO
  52.  
  53. -------------------------------------------
  54.  
  55.  
  56. IF OBJECT_ID(N'dbo.data_pump', 'P') IS NOT NULL
  57.     DROP PROCEDURE dbo.data_pump
  58. GO
  59.  
  60. CREATE PROCEDURE dbo.data_pump
  61. AS
  62. BEGIN
  63.     SET NOCOUNT ON
  64.     DECLARE @result int = -555   -- init (number of affected records)
  65.  
  66.     DECLARE @info_table TABLE (
  67.         action nvarchar(10),
  68.         code int,
  69.         oz1 nvarchar(40),
  70.         oz2 nvarchar(40),
  71.         oz3 nvarchar(40),
  72.         oz4 nvarchar(40)
  73.     )
  74.  
  75.     BEGIN TRANSACTION tran_data_pump
  76.     BEGIN TRY
  77.         MERGE target_table AS target
  78.         USING (SELECT code, some_data, oz1, oz2, oz3, oz4
  79.                FROM source_table) AS source
  80.         ON target.code = source.code
  81.         WHEN MATCHED AND (COALESCE(target.some_data, '') != COALESCE(source.some_data, '')
  82.                           OR COALESCE(target.oz1, '') != COALESCE(source.oz1, '')
  83.                           OR COALESCE(target.oz2, '') != COALESCE(source.oz2, '')
  84.                           OR COALESCE(target.oz3, '') != COALESCE(source.oz3, '')
  85.                           OR COALESCE(target.oz4, '') != COALESCE(source.oz4, '')
  86.                           ) THEN
  87.             UPDATE
  88.             SET target.some_data = source.some_data,
  89.                 target.oz1 = source.oz1,
  90.                 target.oz2 = source.oz2,
  91.                 target.oz3 = source.oz3,
  92.                 target.oz4 = source.oz4
  93.         WHEN NOT MATCHED THEN
  94.             INSERT (code, some_data,
  95.                     oz1, oz2, oz3, oz4)
  96.             VALUES (source.code, source.some_data,
  97.                     source.oz1, source.oz2, source.oz3, source.oz4)
  98.         OUTPUT
  99.             $action AS action,  -- INSERT or UPDATE
  100.             inserted.code AS code,
  101.             inserted.oz1 AS oz1,
  102.             inserted.oz2 AS oz2,
  103.             inserted.oz3 AS oz3,
  104.             inserted.oz4 AS oz4
  105.         INTO @info_table;
  106.  
  107.         SET @result = @@ROWCOUNT
  108.  
  109.         COMMIT TRANSACTION tran_data_pump
  110.     END TRY
  111.     BEGIN CATCH
  112.         ROLLBACK TRANSACTION tran_data_pump
  113.         SET @result = -1    -- transaction-failed indication
  114.     END CATCH
  115.     RETURN @result  -- OK, number of the transfered records
  116. END
  117.  
  118. GO
  119.  
  120. -------------------------------------------
  121.  
  122. PRINT 'Delete the target_table'
  123. DELETE target_table
  124.  
  125. PRINT 'Delete the oz_table'
  126. DELETE oz_table
  127.  
  128. PRINT 'Delete and initialize the source_table'
  129. DELETE source_table
  130.  
  131. INSERT INTO source_table (code, some_data, oz1, oz2, oz3, oz4) VALUES
  132.     (1, 'data1', 'loginA', 'loginB', 'loginC', 'loginD'),
  133.     (2, 'data2', 'loginX', NULL,     NULL,     NULL),
  134.     (3, 'data3', NULL,     'loginY', NULL,     NULL),
  135.     (4, 'data4', NULL,     NULL,     'loginZ', NULL),
  136.     (5, 'data5', NULL,     NULL,     NULL,     'loginW'),
  137.     (6, 'data6', NULL,     NULL,     NULL,     NULL)
  138.  
  139. SELECT * FROM source_table
  140.  
  141. PRINT 'Execute the data_pump'
  142. DECLARE @return_value int
  143. EXEC @return_value = [dbo].[data_pump]
  144. SELECT'Affected records' = @return_value
  145.  
  146. PRINT 'Execute the data_pump again'
  147. EXEC @return_value = [dbo].[data_pump]
  148. SELECT'Affected second time' = @return_value
  149.  
  150. SELECT ID, code, some_data, oz1, oz2, oz3, oz4 FROM target_table
  151.  
  152. PRINT 'Modify the source table -- data should remain in the target_table'
  153. DELETE FROM source_table
  154. WHERE code = 5
  155.  
  156. UPDATE source_table
  157. SET oz1 = 'addedE'
  158. WHERE code = 4
  159.  
  160. SELECT * FROM source_table
  161.  
  162. PRINT 'Execute the data_pump'
  163. EXEC @return_value = [dbo].[data_pump]
  164. SELECT'Affected records' = @return_value
  165.  
  166. SELECT ID, code, some_data, oz1, oz2, oz3, oz4 FROM target_table
  167.  
  168. SELECT * FROM oz_table
  169. GO
  170.  
  171. -------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement