TransformDataSourceDimResource (Resource)

Element properties:

TypeResource
File NameTransformDataSourceDim.sql
AccessibilityPublic

Source Code:

<Resource ID="TransformDataSourceDimResource" Accessibility="Public" FileName="TransformDataSourceDim.sql"/>

File Content: TransformDataSourceDim.sql

if not exists (select * from sysobjects where type = 'P' AND name = N'TransformDataSourceDimProc')

begin
execute ('create procedure dbo.[TransformDataSourceDimProc] @WaterMark xml as return 1')
end
go

DECLARE @Statement NVARCHAR(MAX)

SET @Statement=
N'alter procedure [dbo].[TransformDataSourceDimProc] (@WaterMark xml)
as
begin
set nocount on

declare @utc DATETIME = GETUTCDATE()
-- Get Metadata
declare @BatchId int, @Updated int, @Inserted int, @ModuleName nvarchar(128),
@source1WM datetime, @source1MaxWM datetime, @retval int, @err int, @nullguid uniqueidentifier

set @nullguid = ''00000000-0000-0000-0000-000000000000''
set @Updated = 0

EXEC dbo.InitializeTransform
@transformName = ''TransformDataSourceDimProc'',
@transformTemplateType = ''Manual'',
@waterMark = @WaterMark,
@warehouseEntityName = ''MTV_Microsoft$SystemCenter$DataWarehouse$CMDBSource'',
@waterMarkType = ''DateTime'',
@sourceTableName = ''inbound.MTV_Microsoft$SystemCenter$DataWarehouse$DataSource'',
@utc = @utc,
@source1WM = @source1WM OUTPUT,
@source1MaxWM = @source1MaxWM OUTPUT,
@batchId = @batchId OUTPUT

merge dbo.DataSourceDim AS target
using (
select distinct src.BaseManagedEntityId,
src.[System.Entity!DisplayName],
src.[Microsoft.SystemCenter.DataWarehouse.DataSource!DataSourceName],
src.[Microsoft.SystemCenter.DataWarehouse.DataSource!DatasourceType],
src.[Microsoft.SystemCenter.DataWarehouse.DataSource!DateRegistered],
src.[Microsoft.SystemCenter.DataWarehouse.DataSource!DateUnRegistered],
src.[Microsoft.SystemCenter.DataWarehouse.DataSource!ProductVersion],
src.[Microsoft.SystemCenter.DataWarehouse.DataSource!RegistrationStatus],
src.[Microsoft.SystemCenter.DataWarehouse.DataSource!SecureReferenceId],
src.[Microsoft.SystemCenter.DataWarehouse.DataSource!DataSourceId],
src.IsDeleted,
src.DataSourceId
from inbound.MTV_Microsoft$SystemCenter$DataWarehouse$DataSource src,
(
SELECT MAX(DWTimestamp) as MaxTimestamp, BaseManagedEntityId as EntityId
from inbound.MTV_Microsoft$SystemCenter$DataWarehouse$DataSource
where DWTimestamp >= @source1WM
and DWTimestamp < @source1MaxWM
GROUP BY BaseManagedEntityId
) maxResults
where src.DWTimestamp = MaxTimestamp and src.BaseManagedEntityId = EntityId

) as source
on (target.SourceId = source.[Microsoft.SystemCenter.DataWarehouse.DataSource!DataSourceId])
when matched then
update set
BaseManagedEntityId = source.BaseManagedEntityId,
DisplayName = isnull(source.[System.Entity!DisplayName],''NA''),
DataSourceName = isnull(source.[Microsoft.SystemCenter.DataWarehouse.DataSource!DataSourceName],''NA''),
DatasourceType = source.[Microsoft.SystemCenter.DataWarehouse.DataSource!DatasourceType],
DateRegistered = source.[Microsoft.SystemCenter.DataWarehouse.DataSource!DateRegistered],
DateUnRegistered = isnull(source.[Microsoft.SystemCenter.DataWarehouse.DataSource!DateUnRegistered],cast(''1/1/1900'' as DateTime)),
ProductVersion = source.[Microsoft.SystemCenter.DataWarehouse.DataSource!ProductVersion],
RegistrationStatus = source.[Microsoft.SystemCenter.DataWarehouse.DataSource!RegistrationStatus],
SecureReferenceId = isnull(source.[Microsoft.SystemCenter.DataWarehouse.DataSource!SecureReferenceId],@nullguid),
DataSourceId = isnull(source.[Microsoft.SystemCenter.DataWarehouse.DataSource!DataSourceId],@nullguid),
IsDeleted = source.IsDeleted,
UpdatedBatchId = @BatchId,
@Updated = @Updated + 1

when not matched
by target then
insert (BaseManagedEntityId, SourceId, DisplayName, DataSourceName, DataSourceType, DateRegistered, DateUnRegistered, ProductVersion,
RegistrationStatus, SecureReferenceId, ManagementGroupId, IsDeleted, UpdatedBatchId, InsertedBatchId, DataSourceId)
values (BaseManagedEntityId,
isnull(source.[Microsoft.SystemCenter.DataWarehouse.DataSource!DataSourceId], @nullguid),
isnull(source.[System.Entity!DisplayName],''NA''),
source.[Microsoft.SystemCenter.DataWarehouse.DataSource!DataSourceName],
source.[Microsoft.SystemCenter.DataWarehouse.DataSource!DataSourceType],
source.[Microsoft.SystemCenter.DataWarehouse.DataSource!DateRegistered],
isnull(source.[Microsoft.SystemCenter.DataWarehouse.DataSource!DateUnRegistered],cast(''1/1/1900'' as DateTime)),
source.[Microsoft.SystemCenter.DataWarehouse.DataSource!ProductVersion],
source.[Microsoft.SystemCenter.DataWarehouse.DataSource!RegistrationStatus],
source.[Microsoft.SystemCenter.DataWarehouse.DataSource!SecureReferenceId],
@nullguid,
source.IsDeleted,
0,
@BatchId,
source.[Microsoft.SystemCenter.DataWarehouse.DataSource!DataSourceId]);


select @Inserted=@@RowCount,@err=@@error
set @Inserted = @Inserted - @Updated

-- PS 225486 There are cases during upgrade
-- when we are in between upgrading the datawarehouse
-- datasource type and the new properties, we can get
-- null values in the Dimension. Deleting those values.
delete dbo.DataSourceDim
where SourceId = @nullguid and BaseManagedEntityId != @nullguid

if(@err<>0)
begin
raiserror(''Failed to insert into DataSource Dimension'', 16,1)
return
end

EXEC dbo.UninitializeTransform
@transformName = ''TransformDataSourceDimProc'',
@transformTemplateType = ''Manual'',
@waterMark = @WaterMark,
@warehouseEntityName = ''MTV_Microsoft$SystemCenter$DataWarehouse$CMDBSource'',
@waterMarkType = ''DateTime'',
@source1MaxWM = @source1MaxWM,
@batchId = @BatchId,
@inserted = @Inserted,
@updated = @Updated

set nocount off
end'

IF EXISTS(SELECT * FROM sys.objects WHERE type = 'U' AND name = 'TablePartition' )
BEGIN
EXEC sp_executesql @Statement
END
GO


/*
Adding this to the same file in the interest of time.
We need to move this out to a seperate file
A bug will be opened to track this.
*/

IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = N'InferDataSourceDimProc')
BEGIN
EXECUTE ('CREATE PROCEDURE dbo.[InferDataSourceDimProc] @WaterMark xml AS RETURN 1')
END
GO

DECLARE @Statement1 NVARCHAR(MAX)

SET @Statement1=
N'ALTER PROCEDURE [dbo].[InferDataSourceDimProc](
@sourceTableName NVARCHAR(256),
@columnName SYSNAME,
@filterColumnName SYSNAME,
@minTimeStamp DATETIME,
@maxTimeStamp DATETIME,
@batchId INT
)
AS BEGIN
SET NOCOUNT ON
SET XACT_ABORT ON

DECLARE @errorNumber INT,
@errorSeverity INT,
@errorState INT,
@errorLine INT,
@errorProcedure NVARCHAR(256),
@errorMessage NVARCHAR(MAX),
@task NVARCHAR(512)

DECLARE @retval INT = -1,
@err INT = 0,
@startTranCount INT = @@TRANCOUNT,
@queryTemplate NVARCHAR(MAX)

BEGIN TRY
IF OBJECT_ID(@sourceTableName) IS NULL
RETURN;

SELECT @task = ''Shredding WaterMark''
SELECT @queryTemplate = ''
INSERT INTO dbo.DataSourceDim(
BaseManagedEntityId,
SourceId,
DataSourceName,
SecureReferenceId,
DisplayName,
DateRegistered,
DateUnRegistered,
DatasourceType,
ManagementGroupId,
ProductVersion,
RegistrationStatus,
IsDeleted,
InsertedBatchId,
UpdatedBatchId,
DataSourceId
)
SELECT DISTINCT
src.%COLUMNNAME% AS BaseManagedEntityId,
src.%COLUMNNAME% AS SourceId,
NULL AS DataSourceName,
NULL AS SecureReferenceId,
NULL AS DisplayName,
NULL AS DateRegistered,
NULL AS DateUnRegistered,
NULL AS DatasourceType,
NULL AS ManagementGroupId,
NULL AS ProductVersion,
NULL AS RegistrationStatus,
0 AS IsDeleted,
%BATCHID% AS InsertedBatchId,
0 AS UpdatedBatchId,
NULL AS DataSourceId
FROM %TABLENAME% src
LEFT JOIN dbo.DatasourceDim dimDS ON
src.%COLUMNNAME% = dimDS.SourceId
WHERE src.%FILTERTIMESTAMPCOLUMN% > ''''%MINTIMESTAMP%''''
AND src.%FILTERTIMESTAMPCOLUMN% <= ''''%MAXTIMESTAMP%''''
AND dimDS.SourceId IS NULL
AND src.%COLUMNNAME% IS NOT NULL
''

SELECT @queryTemplate = REPLACE(@queryTemplate, ''%COLUMNNAME%'', @columnName)
SELECT @queryTemplate = REPLACE(@queryTemplate, ''%TABLENAME%'', @sourceTableName)
SELECT @queryTemplate = REPLACE(@queryTemplate, ''%BATCHID%'', @batchId)
SELECT @queryTemplate = REPLACE(@queryTemplate, ''%FILTERTIMESTAMPCOLUMN%'', @filterColumnName)
SELECT @queryTemplate = REPLACE(@queryTemplate, ''%MINTIMESTAMP%'', CONVERT(VARCHAR(32), @minTimeStamp, 127))
SELECT @queryTemplate = REPLACE(@queryTemplate, ''%MAXTIMESTAMP%'', CONVERT(VARCHAR(32), @maxTimeStamp, 127))

EXEC(@queryTemplate)
END TRY
BEGIN CATCH
DECLARE @errorFmt VARCHAR(256)

SELECT @errorFmt = N''ErrorNumber="%d" Message="%s" Severity="%d" State="%d" ProcedureName="%s" LineNumber="%d" Task="%s"'',
@errorNumber = ERROR_NUMBER(),
@errorMessage = ERROR_MESSAGE(),
@errorSeverity = ERROR_SEVERITY(),
@errorState = ERROR_STATE(),
@errorLine = ERROR_LINE(),
@errorProcedure = ERROR_PROCEDURE()

IF(@@TRANCOUNT > @startTranCount) ROLLBACK TRANSACTION

IF OBJECT_ID(''tempdb..#tempTable'') IS NOT NULL
BEGIN
DROP TABLE #tempTable
END

IF OBJECT_ID(''tempdb..#transformTemp1'') IS NOT NULL
BEGIN
DROP TABLE #transformTemp1
END

RAISERROR (
@errorFmt,
@errorSeverity,
@errorState,
@errorNumber,
@errorMessage,
@errorSeverity,
@errorState,
@errorProcedure,
@errorLine,
@task
)

RETURN -1
END CATCH

SET XACT_ABORT OFF
SET NOCOUNT OFF
END'

IF EXISTS(SELECT * FROM sys.objects WHERE type = 'U' AND name = 'TablePartition' )
BEGIN
EXEC sp_executesql @Statement1
END
GO