Hi ,
I'm trying to impliment SCD type 2 using Merge using below blog as reference but sime how it is erroring out with error
http://www.made2mentor.com/2013/08/how-to-load-slowly-changing-dimensions-using-t-sql-merge/
Msg 207, Level 16, State 1, Line 40
Invalid column name 'Current'.
Msg 207, Level 16, State 1, Line 38
Invalid column name 'Current'.
Msg 207, Level 16, State 1, Line 47
Invalid column name 'Current'.
Here is the code below...
--Create Temporaty table to hold dimension records IF OBJECT_ID('tempdb..#DimVirtualQueue') IS NOT NULL DROP TABLE #DimVirtualQueue; CREATE TABLE #DimVirtualQueue ( [VQ_name] [varchar](50) NULL, [contact_type] [varchar](50) NULL, [center_node_id] [int] NULL, [sed_id] [datetime] NULL, [eed_id] [datetime] NULL, [insert_date] [datetime] NULL, [Current] [char](1) NOT NULL ) INSERT INTO #DimVirtualQueue(VQ_name, contact_type, center_node_id, sed_id, eed_id, insert_date,[Current] ) SELECT VQ_name, contact_type, center_node_id, sed_id , eed_id,GETDATE(),'Y' FROM ( --Declare Source and Target tables. MERGE dbo.tblSwDM_dim_VQ_test AS TARGET --Source USING (SELECT RTRIM(LTRIM(Stage.RESOURCE_NAME)) AS VQ_name, 'Unknown' AS contact_type, 0 AS center_node_id, CONVERT(INT,CONVERT(VARCHAR(8),GMT_START_TIME,112)) AS sed_id, CONVERT(INT,CONVERT(VARCHAR(8),ISNULL(GMT_END_TIME,'2070-01-01'),112)) AS eed_id, GETDATE() AS insert_date FROM dbo.tblGenesys_stg_RESOURCE_ Stage WHERE resource_type = 'queue' AND resource_subtype = 'VirtualQueue' AND NOT EXISTS (SELECT 1 FROM dbo.tblSwDM_dim_VQ AS dim WHERE RTRIM(LTRIM(stage.RESOURCE_NAME)) = RTRIM(LTRIM(dim.vq_name))) ) SOURCE ON TARGET.VQ_name = SOURCE.VQ_name WHEN NOT MATCHED BY TARGET THEN INSERT ( VQ_name, contact_type, center_node_id, sed_id, eed_id, insert_date,[Current] ) VALUES (SOURCE.VQ_name,SOURCE.contact_type,SOURCE.center_node_id,SOURCE.sed_id,SOURCE.eed_id,SOURCE.insert_date,'Y') WHEN MATCHED AND TARGET.[Current] = 'Y' AND EXISTS ( SELECT SOURCE.VQ_name EXCEPT SELECT TARGET.VQ_name ) --Expire the records in target if exist in source. THEN UPDATE SET TARGET.[Current] = 'N', TARGET.[eed_id] = SOURCE.eed_id OUTPUT $Action ActionOut, SOURCE.VQ_name,SOURCE.contact_type,SOURCE.center_node_id,SOURCE.sed_id,SOURCE.eed_id) AS MergeOut WHERE MergeOut.ActionOut = 'UPDATE'; --Insert data into dimension INSERT tblSwDM_dim_VQ_test SELECT VQ_name,contact_type,center_node_id,sed_id,eed_id,insert_date,[Current] FROM #DimVirtualQueue
Any help to resolve issue is appreciated...
Thanks,
Vishal..