为cdc中的每个列更改获取单独的行

时间:2014-12-15 18:03:48

标签: sql sql-server pivot

我创建了一个包含3个字段的简单Employee

FirstName , LastName , CurrentPayScale

这是我到目前为止所做的

DECLARE @begin_lsn BINARY(10), @end_lsn BINARY(10) , @A NVARCHAR(100)
SET @begin_lsn = sys.fn_cdc_get_min_lsn('dbo_Employee')
SET @end_lsn = sys.fn_cdc_get_max_lsn()

SELECT  __$operation AS OperationTypeId,
   CASE __$operation WHEN 1 THEN 'has deleted' WHEN 2 THEN 'has added' ELSE 'has updated' END AS [Action],
   ( SELECT CC.column_name + ',' 
     FROM  cdc.captured_columns CC
     INNER JOIN cdc.change_tables CT ON CC.[object_id] = CT.[object_id]
     WHERE capture_instance = 'dbo_Employee' 
       AND sys.fn_cdc_is_bit_set(CC.column_ordinal,    EmployeeCDC.__$update_mask) = 1
    FOR XML PATH('')) AS ChangedColumns,
   (SELECT FIRSTNAME + ','
    FROM [cdc].[dbo_Employee_CT]
    WHERE __$start_lsn = EmployeeCDC.__$start_lsn 
      AND __$operation = 3 FOR XML PATH('')) as 'Old Value',
    EmployeeCDC.FirstName  AS 'New Value'
 FROM [cdc].[dbo_Employee_CT] AS EmployeeCDC 
 WHERE EmployeeCDC.__$operation <> 3

此查询返回此结果

enter image description here

现在在第三行,您可以看到有2列已更改FirstName and CurrentPayScale

我只想查看每列的结果,我认为只要我搜索就可以使用pivot,但我也不知道如何使用pivot。

0 个答案:

没有答案