/* This query is used to show all deletes, adds and restores of TimeXtender objects as well as who made the change, when did it occur and the version notes. Adds : column NewlyAdded = 1 deletes : column IsDeleted = 1 LastActiveRange : When IsDeleted = 1 this column will show the last active version range before deletion. Restored : All records shown after a deleted record. -> to do: add restore flag. !!! IMPORTANT !!! Updated Script for TimeXtender 20.10.16.64 original script was created on TX 20.5.8.64 Prior versions: You could get errors about non-existing columns in queries that use the EXCEPT statement. You are free to remove these columns or rename should they have been renamed in this version. Newer versions: Check the EXCEPT statements and add new columns if there are any in your version compared to the version I am on. Completion state and cross check of which tables are included and which not (query in the comment below): run the query below to get a view of which TimeXtender objects are covered in this query and which are not Completion might look very low but it actually covers the most used objects in the staging, datawarehouse and semanticlayer steps. Some tables such as "views" are skipped because the views are also listed in the "datatable" table. SELECT T.TABLE_NAME, Completed=ISNULL(Completed,0),Completed_total=CONCAT(SUM(Completed) OVER (), '/',SUM(1) OVER ()),Completed_percentage=CONCAT(CONVERT(DECIMAL(18,2),ROUND(SUM(Completed) OVER () / SUM(1.) OVER () * 100, 2)), '%') FROM INFORMATION_SCHEMA.tables T LEFT OUTER JOIN (SELECT *, 1 Completed FROM (values ('BusinessUnits'), ('ConditionalLookupFields'), ('CustomDataTables'), ('CustomSqlScripts'), ('DataFields'), ('DataMovementRelations'), ('DataRelationFields'), ('DataRelations'), ('DataSources'), ('DataTables'), ('DataWarehouses'), ('DateTables'), ('HistoryKeyFields'), ('HistoryRules'), ('HistoryTypeFields'), ('IncrementalSelectionRuleFields'), ('IncrementalSelectionRules'), ('Joins'), ('JunkDimensionDataFields'), ('JunkTableFieldRelations'), ('LookupFields'), ('PrePostScripts'), ('RelatedRecordMappingFields'), ('RelatedRecordRelations'), ('RelatedRecords'), ('SelectionRuleFields'), ('SelectionRules'), ('SemanticLayerDisplayFolderRelations'), ('SemanticLayerDisplayFolders'), ('SemanticLayerDynamicSecuritySetupItems'), ('SemanticLayerEndpoints'), ('SemanticLayerFieldCustomScripts'), ('SemanticLayerFields'), ('SemanticLayerFormatStringObjects'), ('SemanticLayerHierarchies'), ('SemanticLayerHierarchyFields'), ('SemanticLayerMeasureCustomScripts'), ('SemanticLayerMeasures'), ('SemanticLayerModels'), ('SemanticLayerRelationItems'), ('SemanticLayerRelations'), ('SemanticLayerRoleEndpointRelations'), ('SemanticLayerRoleMembers'), ('SemanticLayerRoles'), ('SemanticLayerSecuritySetupItems'), ('SemanticLayerSecuritySetupRelations'), ('SemanticLayerSecuritySetups'), ('SemanticLayerSelectionRuleFields'), ('SemanticLayerSelectionRules'), ('SemanticLayerTables'), ('SemanticLayerTabularEndpoints'), ('Sortings'), ('SqlServerConnections'), ('StoredProcedures'), ('TableInsertMappings'), ('TableInserts'), ('TableInsertScripts'), ('TableInsertSelectionRules'), ('TableSettings'), ('TargetBasedIncrementalKeys'), ('TargetBasedIncrementalRules'), ('TargetBasedIncrementalValueKeys'), ('TimeTables'), ('TransformationReplaces'), ('Transformations'), ('UserDefinedFunctions'), ('TableInsertSelectionRuleFields') )v(tableVersionChecked) ) A ON T.TABLE_NAME = A.tableVersionChecked ORDER BY Completed desc, 1 Changing the code & "disclaimer" (kinda): You can add or change the subsequent query statements as you see fit. This code is no official TX code, nor is it guaranteed to be free of any bugs. If any bugs are found, you are free to fix them and/or notify other users. */ /* change params if needed. */ DECLARE @project VARCHAR(100) = 'EDPB 2.0' -- TimeXtender project you want to check DECLARE @UseVersionNumbers BIT = 0 -- Choose whether you check versions from a given version number or from a given date. Set to true if you want to start from a version. DECLARE @ValidFrom BIGINT = 1 -- If @userVersionNumber = 1 this param will be used. All versions starting from this version will be taken into account. DECLARE @UpdateFrom datetime = '20210401' -- If @userVersionNumber = 0 this param will be used. All versions created on or after this date will be taken into account. /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! NO PARAMETERS need to be set beyond this point !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ /* some additional variables needed for the query which do not require user input. */ DECLARE @UpdateTo datetime DECLARE @validTo BIGINT DECLARE @projectid Uniqueidentifier SET @validTo = CASE WHEN @validTo IS NULL THEN 99999999 ELSE @validTo END SET @UpdateTo = CASE WHEN @UpdateTo IS NULL THEN '99991231' ELSE @UpdateTo END /* Determine the starting version number based on the data parameter. */ IF @UseVersionNumbers = 0 SELECT @ValidFrom = MIN(Versions.version), @validTo = MAX(Versions.version), @projectid = MAX(Versions.ProjectId) FROM Versions INNER JOIN Projects ON Projects.ProjectId = Versions.ProjectId WHERE UpdateTime BETWEEN @UpdateFrom AND @UpdateTo AND Projects.Name = @project /* Dump often used queries into temp tables for easy use afterwards and speed boost.*/ DROP TABLE IF EXISTS #DataTables SELECT * INTO #DataTables FROM ( SELECT DT.* ,LayerName = COALESCE(DWH.name, BU.name, SBU.name) ,projectid = COALESCE(DWH.projectid, BU.projectid, SBU.projectid) ,Layer = CASE WHEN DT.SqlServerConnectionId IS NOT NULL THEN 'DataWarehouse' WHEN DT.StagingDatabaseId IS NOT NULL THEN 'Business Unit' WHEN DT.DataSourceId IS NOT NULL THEN 'Business Unit' ELSE 'Unknown' END , rn = ROW_NUMBER() over (partition by dt.datatableid order by dt.validto desc) FROM dbo.DataTables DT LEFT OUTER Join (SELECT SqlServerConnectionId, DataWarehouseId FROM ( SELECT SqlServerConnectionId, DataWarehouseId, RN = ROW_NUMBER() over (partition by SqlServerConnectionId order by validTo DESC) FROM SqlServerConnections) A WHERE RN = 1) SSC ON DT.SqlServerConnectionId = SSC.SqlServerConnectionId LEFT OUTER Join (select DataWarehouseId, ProjectId, name FROM ( SELECT DataWarehouseId, ProjectId, name, RN = ROW_NUMBER() over (partition by DataWarehouseId order by validTo DESC) FROM DataWarehouses where ProjectId = @projectid) A where rn = 1 )DWH ON DWH.DataWarehouseId = SSC.DataWarehouseId LEFT OUTER Join (select DataSourceId, BusinessUnitId FROM ( SELECT DataSourceId, BusinessUnitId, RN = ROW_NUMBER() over (partition by DataSourceId order by validTo DESC) FROM DataSources)A where RN=1) DS ON DT.DataSourceId = DS.DataSourceId LEFT OUTER Join (SELECT BusinessUnitId, ProjectId, name FROM (SELECT BusinessUnitId, ProjectId, name, RN = ROW_NUMBER() over (partition by BusinessUnitId order by validTo DESC) FROM BusinessUnits where ProjectId = @projectid) A WHERE RN = 1)BU ON BU.BusinessUnitId = DS.BusinessUnitId LEFT OUTER Join (SELECT StagingDatabaseId, BusinessUnitId FROM ( SELECT StagingDatabaseId, BusinessUnitId, RN = ROW_NUMBER() over (partition by StagingDatabaseId order by validTo DESC) FROM StagingDatabases) A WHERE RN = 1) sdb ON DT.StagingDatabaseId = sdb.StagingDatabaseId LEFT OUTER Join (SELECT BusinessUnitId, ProjectId, name FROM (SELECT BusinessUnitId, ProjectId, name, RN = ROW_NUMBER() over (partition by BusinessUnitId order by validTo DESC) FROM BusinessUnits where ProjectId = @projectid) A WHERE RN = 1)SBU ON SBU.BusinessUnitId = sdb.BusinessUnitId ) A WHERE A.projectid IS NOT NULL CREATE CLUSTERED INDEX IX ON #datatables (DataTableId, rn) DROP TABLE IF EXISTS #datafields SELECT DF.* ,LayerName ,projectid ,layer ,tableName = DT.name , rn = ROW_NUMBER() over (partition by DF.datafieldid order by df.validto desc) INTO #datafields FROM dbo.DataFields DF LEFT OUTER JOIN (SELECT LayerName,projectid,layer,name, DataTableId FROM #DataTables WHERE RN = 1) DT ON DF.DataTableId = DT.DataTableId CREATE CLUSTERED INDEX IX ON #datafields (datafieldid, rn) DROP TABLE IF EXISTS #semanticlayerfields SELECT Layer, LayerName, ProjectId, SemanticLayerModelId, TableName ,T.*,RN = ROW_NUMBER() OVER (PARTITION BY SemanticLayerFieldId ORDER BY ValidTo DESC) INTO #semanticlayerfields FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, T.TableName,T.RN, T.SemanticLayerTableId FROM (SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels T) D INNER JOIN (SELECT TableName = Name, SemanticLayerTableId, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerTableId ORDER BY ValidTo DESC) FROM SemanticLayerTables) T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId AND D.ProjectId = @projectid ) D INNER JOIN SemanticLayerFields T ON D.RN = 1 AND T.SemanticLayerTableId = D.SemanticLayerTableId CREATE CLUSTERED COLUMNSTORE INDEX IX ON #semanticlayerfields DROP TABLE IF EXISTS #semanticlayermeasures SELECT Layer, LayerName, ProjectId, SemanticLayerModelId, TableName ,T.*,RN = ROW_NUMBER() OVER (PARTITION BY SemanticLayerMeasureId ORDER BY ValidTo DESC) INTO #semanticlayermeasures FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, T.TableName,T.RN, T.SemanticLayerTableId FROM (SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels T) D INNER JOIN (SELECT TableName = Name, SemanticLayerTableId, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerTableId ORDER BY ValidTo DESC) FROM SemanticLayerTables) T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId AND D.ProjectId = @projectid ) D INNER JOIN SemanticLayerMeasures T ON D.RN = 1 AND T.SemanticLayerTableId = D.SemanticLayerTableId CREATE CLUSTERED COLUMNSTORE INDEX IX ON #SemanticLayerMeasures DROP TABLE IF EXISTS #semanticlayerhierarchies SELECT Layer, LayerName, ProjectId, SemanticLayerModelId, TableName ,T.*,RN = ROW_NUMBER() OVER (PARTITION BY SemanticLayerHierarchyId ORDER BY ValidTo DESC) INTO #semanticlayerhierarchies FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, T.TableName,T.RN, T.SemanticLayerTableId FROM (SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels T) D INNER JOIN (SELECT TableName = Name, SemanticLayerTableId, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerTableId ORDER BY ValidTo DESC) FROM SemanticLayerTables) T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId AND D.ProjectId = @projectid ) D INNER JOIN SemanticLayerHierarchies T ON D.RN = 1 AND T.SemanticLayerTableId = D.SemanticLayerTableId CREATE CLUSTERED COLUMNSTORE INDEX IX ON #SemanticLayerHierarchies /*Actual start of the query that will list all deletes, adds and restores of the TimeXtender objects.*/ SELECT Object = 'Project' ,TxRepositoryTable = 'Projects' ,ObjectId = PRJ.ProjectId ,ObjectName = Name ,ObjectLayer = '' ,ObjectPath = CONCAT('/', Name) ,ObjectPathAbstract = '/project' ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN PRJ.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM Projects WHERE ProjectId = ''', PRJ.ProjectId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM Projects PRJ INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM Projects A WHERE PRJ.ProjectId = A.ProjectId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND (EXISTS(-- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT ProjectId ,Name /*,IsDeployedVersion*/ ,ProjectType ,LoadLimit ,AllowNulls ,NullCheckApproachIsRecordBased ,ProjectLockPassword ,Locked ,Version ,CreatedVersion ,ImportedFromEnvironment = CONVERT(NVARCHAR(max),ImportedFromEnvironment) ,ImportedFromProjectName = CONVERT(NVARCHAR(max),ImportedFromProjectName) ,ImportedFromProjectVersion ,ImportedByUserName = CONVERT(NVARCHAR(max),ImportedByUserName) ,ImportedDateTime ,DefaultRelationshipType ,PostFixObjects ,UseSSIS ,DefaultShowSystemControlFields ,Protection = CONVERT(NVARCHAR(max),Protection) ,Protected ,DefaultPrimaryKeyBehavior ,UseSSISFolder ,ConcurrentUsers ,EnableSSISLogging ,SSISRootFolder ,IndexBehavior ,SoftwareUpgradeVersion ,CustomSSISProtectionPassword ,DifferentialDeployment ,ManagedDeployment ,QlikViewModelsDefaultPath ,HashingAlgorithm ,SoftwareCompatibilityVersion FROM Projects PRJ_A WHERE PRJ_A.ValidTo = PRJ.ValidFrom - 1 AND PRJ_A.ProjectId = PRJ.ProjectId EXCEPT SELECT ProjectId ,Name /*,IsDeployedVersion*/ ,ProjectType ,LoadLimit ,AllowNulls ,NullCheckApproachIsRecordBased ,ProjectLockPassword ,Locked ,Version ,CreatedVersion ,ImportedFromEnvironment = CONVERT(NVARCHAR(max),ImportedFromEnvironment) ,ImportedFromProjectName = CONVERT(NVARCHAR(max),ImportedFromProjectName) ,ImportedFromProjectVersion ,ImportedByUserName = CONVERT(NVARCHAR(max),ImportedByUserName) ,ImportedDateTime ,DefaultRelationshipType ,PostFixObjects ,UseSSIS ,DefaultShowSystemControlFields ,Protection = CONVERT(NVARCHAR(max),Protection) ,Protected ,DefaultPrimaryKeyBehavior ,UseSSISFolder ,ConcurrentUsers ,EnableSSISLogging ,SSISRootFolder ,IndexBehavior ,SoftwareUpgradeVersion ,CustomSSISProtectionPassword ,DifferentialDeployment ,ManagedDeployment ,QlikViewModelsDefaultPath ,HashingAlgorithm ,SoftwareCompatibilityVersion FROM Projects PRJ_B WHERE PRJ_B.ValidFrom = PRJ.ValidFrom AND PRJ_B.ProjectId = PRJ.ProjectId ) A ) OR PRJ.ValidFrom = OA_MinVals.minValidFrom -- first version ) UNION ALL SELECT Object = 'Business Unit' ,TxRepositoryTable = 'BusinessUnits' ,ObjectId = BU.BusinessUnitId ,ObjectName = BU.Name ,ObjectLayer = 'BusinessUnit:' + BU.Name ,ObjectPath = CONCAT('/', PRJ.Name, '/', BU.Name) ,ObjectPathAbstract = '/project/Business Unit' ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN PRJ.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM BusinessUnits WHERE BusinessUnitId = ''', BU.BusinessUnitId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM Projects PRJ INNER JOIN dbo.BusinessUnits BU ON BU.ProjectId = PRJ.ProjectId AND BU.ValidFrom = PRJ.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM Projects A WHERE PRJ.ProjectId = A.ProjectId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS(-- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT BusinessUnitId,ProjectId,Name,Locked,ExecutionNo,SimpleModeBehavior FROM BusinessUnits A WHERE A.ValidTo = BU.ValidFrom - 1 AND A.BusinessUnitId = BU.BusinessUnitId EXCEPT SELECT BusinessUnitId,ProjectId,Name,Locked,ExecutionNo,SimpleModeBehavior FROM BusinessUnits B WHERE B.ValidFrom = BU.ValidFrom AND B.BusinessUnitId = BU.BusinessUnitId ) A ) OR BU.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( -- is a restored version NOT EXISTS( SELECT * FROM BusinessUnits A WHERE A.BusinessUnitId = BU.BusinessUnitId AND A.ValidTo = BU.ValidFrom - 1 ) AND BU.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Business Unit' ,TxRepositoryTable = 'BusinessUnits' ,ObjectId = BU.BusinessUnitId ,ObjectName = BU.Name ,ObjectLayer = 'BusinessUnit:' + BU.Name ,ObjectPath = CONCAT('/', PRJ.Name, '/', BU.Name) ,ObjectPathAbstract = '/project/Business Unit' ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(BU.ValidFrom, ' - ', BU.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = BU.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM BusinessUnits WHERE BusinessUnitId = ''', BU.BusinessUnitId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM Projects PRJ INNER JOIN dbo.BusinessUnits BU ON BU.ProjectId = PRJ.ProjectId AND BU.ValidFrom = PRJ.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( BU.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM BusinessUnits A WHERE A.ValidTo >= @ValidFrom - 1 AND A.BusinessUnitId = BU.BusinessUnitId ) OR NOT EXISTS(-- indication if ever removed; afterwards restored SELECT * FROM BusinessUnits A WHERE A.BusinessUnitId = BU.BusinessUnitId AND BU.ValidTo + 1 = A.ValidFrom ) ) AND BU.ValidTo <> 99999999 AND BU.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Business Unit' ,TxRepositoryTable = 'DataSources' ,ObjectId = BU.BusinessUnitId ,ObjectName = BU.Name ,ObjectLayer = 'BusinessUnit:' + BU.Name ,ObjectPath = CONCAT('/', PRJ.Name, '/', BU.Name) ,ObjectPathAbstract = '/project/Business Unit' ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN PRJ.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM BusinessUnits WHERE BusinessUnitId = ''', BU.BusinessUnitId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM Projects PRJ INNER JOIN dbo.BusinessUnits BU ON BU.ProjectId = PRJ.ProjectId AND BU.ValidFrom = PRJ.ValidFrom INNER JOIN dbo.DataSources DS ON DS.BusinessUnitId = BU.BusinessUnitId AND DS.ValidFrom = BU.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM DataSources A WHERE DS.DataSourceId = A.DataSourceId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND (EXISTS(-- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT DataSourceId,BusinessUnitId,GlobalDatabaseId,UseGlobalDatabase,Name,Locked,ConvertOutOfRangeDatesToSQLMinMax,ConnectionStringProperties,CommandTimeOut,ConnectionTimeOut,UseSSISApproach,SchemaNameFilter = CONVERT(NVARCHAR(MAX),SchemaNameFilter),TableNameFilter = CONVERT(NVARCHAR(MAX),TableNameFilter),TablePrefix,ManualTablePrefix,DateTimeConversion,MaxExecutionThreads,AllowExecutionFailure,SimpleModeBehavior,MetaDataExtractType,ExtractFullColumnsCache,BitVersion FROM DataSources A WHERE A.ValidTo = DS.ValidFrom - 1 AND A.DataSourceId = DS.DataSourceId EXCEPT SELECT DataSourceId,BusinessUnitId,GlobalDatabaseId,UseGlobalDatabase,Name,Locked,ConvertOutOfRangeDatesToSQLMinMax,ConnectionStringProperties,CommandTimeOut,ConnectionTimeOut,UseSSISApproach,SchemaNameFilter = CONVERT(NVARCHAR(MAX),SchemaNameFilter),TableNameFilter = CONVERT(NVARCHAR(MAX),TableNameFilter),TablePrefix,ManualTablePrefix,DateTimeConversion,MaxExecutionThreads,AllowExecutionFailure,SimpleModeBehavior,MetaDataExtractType,ExtractFullColumnsCache,BitVersion FROM DataSources B WHERE B.ValidFrom = DS.ValidFrom AND B.DataSourceId = DS.DataSourceId ) A ) OR DS.ValidFrom = OA_MinVals.minValidFrom -- first version ) UNION ALL SELECT Object = 'Data Warehouse' ,TxRepositoryTable = 'DataWarehouses' ,ObjectId = DWH.DataWarehouseId ,ObjectName = DWH.Name ,ObjectLayer = 'Data Warehouse:' + DWH.Name ,ObjectPath = CONCAT('/', PRJ.Name, '/', DWH.Name) ,ObjectPathAbstract = '/project/Data Warehouse' ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN PRJ.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,convert(varchar,DWH.ValidFrom) ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM DataWarehouses WHERE DataWarehouseId = ''', DWH.DataWarehouseId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM Projects PRJ INNER JOIN dbo.DataWarehouses DWH ON DWH.ProjectId = PRJ.ProjectId AND DWH.ValidFrom = PRJ.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND DWH.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM DataWarehouses A WHERE DWH.DataWarehouseId = A.DataWarehouseId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS(-- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT DataWarehouseId,ProjectId,Name,Locked,ExecutionNo,SimpleModeBehavior FROM DataWarehouses A WHERE A.ValidTo = DWH.ValidFrom - 1 AND A.DataWarehouseId = DWH.DataWarehouseId EXCEPT SELECT DataWarehouseId,ProjectId,Name,Locked,ExecutionNo,SimpleModeBehavior FROM DataWarehouses B WHERE B.ValidFrom = DWH.ValidFrom AND B.DataWarehouseId = DWH.DataWarehouseId ) A ) OR DWH.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM DataWarehouses A WHERE A.DataWarehouseId = DWH.DataWarehouseId AND A.ValidTo = DWH.ValidFrom - 1 ) AND DWH.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Data Warehouse' ,TxRepositoryTable = 'DataWarehouses' ,ObjectId = DWH.DataWarehouseId ,ObjectName = DWH.Name ,ObjectLayer = 'Data Warehouse:' + DWH.Name ,ObjectPath = CONCAT('/', PRJ.Name, '/', DWH.Name) ,ObjectPathAbstract = '/project/Data Warehouse' ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(DWH.ValidFrom, ' - ', DWH.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = DWH.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM DataWarehouses WHERE DataWarehouseId = ''', DWH.DataWarehouseId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM Projects PRJ INNER JOIN dbo.DataWarehouses DWH ON DWH.ProjectId = PRJ.ProjectId AND DWH.ValidFrom = PRJ.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( DWH.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM DataWarehouses A WHERE A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM DataWarehouses A WHERE A.DataWarehouseId = DWH.DataWarehouseId AND DWH.ValidTo + 1 = A.ValidFrom ) ) AND DWH.ValidTo <> 99999999 AND DWH.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Data Warehouse' ,TxRepositoryTable = 'SqlServerConnections' ,ObjectId = DWH.DataWarehouseId ,ObjectName = DWH.Name ,ObjectLayer = 'Data Warehouse:' + DWH.Name ,ObjectPath = CONCAT('/', PRJ.Name, '/', DWH.Name, '/<>') ,ObjectPathAbstract = '/project/Data Warehouse/<>' ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN PRJ.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM DataWarehouses WHERE DataWarehouseId = ''', DWH.DataWarehouseId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM Projects PRJ INNER JOIN dbo.DataWarehouses DWH ON DWH.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = DWH.ValidFrom INNER JOIN dbo.SqlServerConnections SC ON DWH.DataWarehouseId = SC.DataWarehouseId AND DWH.ValidFrom = SC.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SqlServerConnections A WHERE SC.SqlServerConnectionId = A.SqlServerConnectionId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND (EXISTS(-- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SqlServerConnectionId,DataWarehouseId,GlobalDatabaseId,UseGlobalDatabase,Server,SSISServerName,Authentication,Password,UserName,CatalogName,CommandTimeOut,ConnectionTimeOut,ConnectionStringProperties,UseSSISApproach,MaxRowsToCopy,DeploymentTarget,UserCollation,SecurityRoleDropOption,DirectReadOption,SSLOption,SSISVersion,SSISAuthentication,SSISPassword,SSISUserName,ODXUseAzureDataFactory,ODXAzureDataFactoryTenantId,ODXAzureDataFactoryAppId,ODXAzureDataFactoryAppSecret,ODXAzureDataFactoryResoruceGroup,ODXAzureDataFactorySubscriptionId,ODXAzureDataFactoryName,ODXAzureDataFactoryFolderName,ODXAzureDataFactoryIntegratedRuntimeName,ODXAzureDataFactoryConnectionTimeout FROM SqlServerConnections A WHERE A.ValidTo = SC.ValidFrom - 1 AND A.SqlServerConnectionId = SC.SqlServerConnectionId EXCEPT SELECT SqlServerConnectionId,DataWarehouseId,GlobalDatabaseId,UseGlobalDatabase,Server,SSISServerName,Authentication,Password,UserName,CatalogName,CommandTimeOut,ConnectionTimeOut,ConnectionStringProperties,UseSSISApproach,MaxRowsToCopy,DeploymentTarget,UserCollation,SecurityRoleDropOption,DirectReadOption,SSLOption,SSISVersion,SSISAuthentication,SSISPassword,SSISUserName,ODXUseAzureDataFactory,ODXAzureDataFactoryTenantId,ODXAzureDataFactoryAppId,ODXAzureDataFactoryAppSecret,ODXAzureDataFactoryResoruceGroup,ODXAzureDataFactorySubscriptionId,ODXAzureDataFactoryName,ODXAzureDataFactoryFolderName,ODXAzureDataFactoryIntegratedRuntimeName,ODXAzureDataFactoryConnectionTimeout FROM SqlServerConnections B WHERE B.ValidFrom = SC.ValidFrom AND B.SqlServerConnectionId = SC.SqlServerConnectionId ) A ) OR SC.ValidFrom = OA_MinVals.minValidFrom -- first version ) UNION ALL SELECT Object = 'Data Table' ,TxRepositoryTable = 'DataTables' ,ObjectId = DT.DataTableId ,ObjectName = DT.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', DT.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN DT.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM DataTables WHERE DataTableId = ''', DT.DataTableId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #DataTables DT INNER Join Projects PRJ ON DT.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = DT.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM DataTables A WHERE DT.DataTableId = A.DataTableId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT DataTableId,DataSourceId,SqlServerConnectionId,StagingDatabaseId,ConnectODXId,ODXDataSourceId,ODXTableId,Name,SchemaName,StagingName,OrigName,IsChecked,TableType,NullCheckAllowNulls,NullCheckApproach,Locked,LockedOnStaging,TruncateRawBeforeTransfere,TruncateValidBeforeTransfere,TruncateRawAfterCleansing,KeepErrorsAndWarnings,ShowControlFields,ExternalProperties = CONVERT(NVARCHAR(MAX), ExternalProperties) FROM DataTables A WHERE A.ValidTo = DT.ValidFrom - 1 AND A.DataTableId = DT.DataTableId EXCEPT SELECT DataTableId,DataSourceId,SqlServerConnectionId,StagingDatabaseId,ConnectODXId,ODXDataSourceId,ODXTableId,Name,SchemaName,StagingName,OrigName,IsChecked,TableType,NullCheckAllowNulls,NullCheckApproach,Locked,LockedOnStaging,TruncateRawBeforeTransfere,TruncateValidBeforeTransfere,TruncateRawAfterCleansing,KeepErrorsAndWarnings,ShowControlFields,ExternalProperties = CONVERT(NVARCHAR(MAX), ExternalProperties) FROM DataTables B WHERE B.ValidFrom = DT.ValidFrom AND B.DataTableId = DT.DataTableId ) A ) OR DT.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM DataTables A WHERE A.DataTableId = DT.DataTableId AND A.ValidTo = DT.ValidFrom - 1 ) AND DT.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Data Table' ,TxRepositoryTable = 'DataTables' ,ObjectId = DT.DataTableId ,ObjectName = DT.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', DT.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(DT.ValidFrom, ' - ', DT.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = DT.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM DataTables WHERE DataTableId = ''', DT.DataTableId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #DataTables DT INNER Join Projects PRJ ON DT.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = DT.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( DT.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM #DataTables A WHERE A.DataTableId = DT.DataTableId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM #DataTables A WHERE A.DataTableId = DT.DataTableId AND DT.ValidTo + 1 = A.ValidFrom ) ) AND DT.ValidTo <> 99999999 AND DT.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Data Table' ,TxRepositoryTable = 'TableSettings' ,ObjectId = DT.DataTableId ,ObjectName = DT.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', DT.Name, '/<>') ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/<>') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN DT.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM DataTables WHERE DataTableId = ''', DT.DataTableId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #DataTables DT INNER Join Projects PRJ ON DT.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = DT.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM DataTables A WHERE DT.DataTableId = A.DataTableId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT TableSettingId,DataTableId,PartitionTemplateId,TimeTableId,DataFieldId,Classification,IncrementalTargetBasedEnabled,SourceBasedIncrementalDeleteType,HistoryEnabled,TypeOneUpdateAll,PersistValidAsTable,CreateFunctionsWithSchemaBinding,HistoryHandleDeletes,PrimaryKeyBehavior,UseLeftOuterJoin,IndexBehavior,ValidDataCompressionType,ValidIndexCompression,UseUDFTransformations,RawDataCompressionType,RawIndexCompression,BatchDataCleansingEnabled,BatchSize,SimpleModeBehavior,HistoryEnableReincarnations,HistoryIsValidFromLoad,HistoryEnableTypeIINullsAsTypeI,KeepLookupValuesUpToDate,MergeConditionalLookups,EntireRowHashKey,SQLDataWarehouseDistributionMethod,SQLDataWarehouseHashDistributionColumnId FROM TableSettings A WHERE A.ValidTo = DT.ValidFrom - 1 AND A.DataTableId = DT.DataTableId EXCEPT SELECT TableSettingId,DataTableId,PartitionTemplateId,TimeTableId,DataFieldId,Classification,IncrementalTargetBasedEnabled,SourceBasedIncrementalDeleteType,HistoryEnabled,TypeOneUpdateAll,PersistValidAsTable,CreateFunctionsWithSchemaBinding,HistoryHandleDeletes,PrimaryKeyBehavior,UseLeftOuterJoin,IndexBehavior,ValidDataCompressionType,ValidIndexCompression,UseUDFTransformations,RawDataCompressionType,RawIndexCompression,BatchDataCleansingEnabled,BatchSize,SimpleModeBehavior,HistoryEnableReincarnations,HistoryIsValidFromLoad,HistoryEnableTypeIINullsAsTypeI,KeepLookupValuesUpToDate,MergeConditionalLookups,EntireRowHashKey,SQLDataWarehouseDistributionMethod,SQLDataWarehouseHashDistributionColumnId FROM TableSettings B WHERE B.ValidFrom = DT.ValidFrom AND B.DataTableId = DT.DataTableId ) A ) UNION ALL SELECT Object = 'Data Field' ,TxRepositoryTable = 'DataFields' ,ObjectId = DF.DataFieldId ,ObjectName = DF.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DF.name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Field') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN Df.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM DataFields WHERE DataFieldId = ''', DF.DataFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #datafields DF INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = DF.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM DataFields A WHERE Df.DataFieldId = A.DataFieldId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT DataFieldId,DataTableId,SeqNo,ODXColumnId,Name,OrigName,DataType,StagingName,BaseType,OrigDataType,CharLength,NumericScale,NumericPrecision,IsPrimaryKey,IsChecked,IsCustom,IsRawOnly,FieldAggregationType,ExecutionNo,SubTypeName,Locked,LockedOnStaging,ExternalProperties = CONVERT(NVARCHAR(MAX), ExternalProperties),HashingAlgorithm FROM DataFields A WHERE A.ValidTo = DF.ValidFrom - 1 AND A.DataFieldId = DF.DataFieldId EXCEPT SELECT DataFieldId,DataTableId,SeqNo,ODXColumnId,Name,OrigName,DataType,StagingName,BaseType,OrigDataType,CharLength,NumericScale,NumericPrecision,IsPrimaryKey,IsChecked,IsCustom,IsRawOnly,FieldAggregationType,ExecutionNo,SubTypeName,Locked,LockedOnStaging,ExternalProperties = CONVERT(NVARCHAR(MAX), ExternalProperties),HashingAlgorithm FROM DataFields B WHERE B.ValidFrom = DF.ValidFrom AND B.DataFieldId = DF.DataFieldId ) A ) OR DF.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM DataFields A WHERE A.DataFieldId = DF.DataFieldId AND A.ValidTo = DF.ValidFrom - 1 ) AND DF.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Data Field' ,TxRepositoryTable = 'DataFields' ,ObjectId = DF.DataFieldId ,ObjectName = DF.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DF.name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Field') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(DF.ValidFrom, ' - ', DF.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = DF.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM DataFields WHERE DataFieldId = ''', DF.DataFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #datafields DF INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = DF.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( DF.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM #datafields A WHERE A.DataFieldId = DF.DataFieldId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM #datafields A WHERE A.DataFieldId = DF.DataFieldId AND DF.ValidTo + 1 = A.ValidFrom ) ) AND DF.ValidTo <> 99999999 AND DF.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Transformation' ,TxRepositoryTable = 'Transformations' ,ObjectId = T.TransformationId ,ObjectName = 'Transformation:' + RuleType ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DataFieldName, '/Transformation:', RuleType) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Field/Transformation') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM Transformations WHERE TransformationId = ''', T.TransformationId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM Transformations T INNER JOIN (SELECT DataFieldName = DF.Name, DF.DataFieldId, tableName, Layer, LayerName, projectid FROM #datafields Df WHERE rn = 1 ) DF ON T.DataFieldId = DF.DataFieldId INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM Transformations A WHERE T.TransformationId = A.TransformationId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT TransformationId,DataFieldId,ExecutionNo,RuleType,RuleValue= CONVERT(nvarchar(max), rulevalue),Locked FROM Transformations A WHERE A.ValidTo = T.ValidFrom - 1 AND A.TransformationId = T.TransformationId EXCEPT SELECT TransformationId,DataFieldId,ExecutionNo,RuleType,RuleValue= CONVERT(nvarchar(max), rulevalue),Locked FROM Transformations B WHERE B.ValidFrom = T.ValidFrom AND B.TransformationId = T.TransformationId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM Transformations A WHERE A.TransformationId = T.TransformationId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Transformation' ,TxRepositoryTable = 'Transformations' ,ObjectId = T.TransformationId ,ObjectName = 'Transformation:' + RuleType ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DataFieldName, '/Transformation:', RuleType) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Field/Transformation') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM Transformations WHERE TransformationId = ''', T.TransformationId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM Transformations T INNER JOIN (SELECT DataFieldName = DF.Name, DF.DataFieldId, tableName, Layer, LayerName, projectid FROM #datafields Df WHERE rn = 1 ) DF ON T.DataFieldId = DF.DataFieldId INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM Transformations A WHERE A.TransformationId = T.TransformationId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM Transformations A WHERE A.TransformationId = T.TransformationId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Transformation Replace Values' ,TxRepositoryTable = 'TransformationReplaces' ,ObjectId = TR.TransformationReplaceId ,ObjectName = 'Transformation:' + RuleType ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DataFieldName, '/Transformation:', RuleType) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Field/Transformation') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN TR.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TransformationReplaces WHERE TransformationReplaceId = ''', TR.TransformationReplaceId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM Transformations T INNER JOIN (SELECT DataFieldName = DF.Name, DF.DataFieldId, tableName, Layer, LayerName, projectid FROM #datafields Df WHERE rn = 1 ) DF ON T.DataFieldId = DF.DataFieldId INNER JOIN TransformationReplaces TR ON TR.ValidFrom BETWEEN T.ValidFrom AND T.ValidTo INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = TR.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM TransformationReplaces A WHERE TR.TransformationReplaceId = A.TransformationReplaceId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT TransformationReplaceId,TransformationId,Type,ReplaceValue,ReplaceText,ExecutionNumber FROM TransformationReplaces A WHERE A.ValidTo = TR.ValidFrom - 1 AND A.TransformationReplaceId = TR.TransformationReplaceId EXCEPT SELECT TransformationReplaceId,TransformationId,Type,ReplaceValue,ReplaceText,ExecutionNumber FROM TransformationReplaces B WHERE B.ValidFrom = TR.ValidFrom AND B.TransformationReplaceId = TR.TransformationReplaceId ) A ) OR TR.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM TransformationReplaces A WHERE A.TransformationReplaceId = TR.TransformationReplaceId AND A.ValidTo = TR.ValidFrom - 1 ) AND TR.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Transformation Replace Values' ,TxRepositoryTable = 'TransformationReplaces' ,ObjectId = TR.TransformationReplaceId ,ObjectName = 'Transformation:' + RuleType ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DataFieldName, '/Transformation:', RuleType) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Field/Transformation') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TransformationReplaces WHERE TransformationReplaceId = ''', TR.TransformationReplaceId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM Transformations T INNER JOIN (SELECT DataFieldName = DF.Name, DF.DataFieldId, tableName, Layer, LayerName, projectid FROM #datafields Df WHERE rn = 1 ) DF ON T.DataFieldId = DF.DataFieldId INNER JOIN TransformationReplaces TR ON TR.ValidFrom BETWEEN T.ValidFrom AND T.ValidTo INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = TR.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( TR.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM TransformationReplaces A WHERE A.TransformationReplaceId = TR.TransformationReplaceId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM TransformationReplaces A WHERE A.TransformationReplaceId = TR.TransformationReplaceId AND TR.ValidTo + 1 = A.ValidFrom ) ) AND TR.ValidTo <> 99999999 AND TR.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Lookups' ,TxRepositoryTable = 'LookupFields' ,ObjectId = T.LookupFieldId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DataFieldName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Field/LookupField') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM LookupFields WHERE LookupFieldId = ''', T.LookupFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM LookupFields T INNER JOIN (SELECT DataFieldName = DF.Name, DF.DataFieldId, tableName, Layer, LayerName, projectid FROM #datafields Df WHERE rn = 1 ) DF ON T.ConditionalLookupFieldId = DF.DataFieldId INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM LookupFields A WHERE T.LookupFieldId = A.LookupFieldId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT LookupFieldId,LookupDataFieldId,ConditionalLookupFieldId,Name,FunctionOperator,Locked,SqlMode,ExecutionNumber FROM LookupFields A WHERE A.ValidTo = T.ValidFrom - 1 AND A.LookupFieldId = T.LookupFieldId EXCEPT SELECT LookupFieldId,LookupDataFieldId,ConditionalLookupFieldId,Name,FunctionOperator,Locked,SqlMode,ExecutionNumber FROM LookupFields B WHERE B.ValidFrom = T.ValidFrom AND B.LookupFieldId = T.LookupFieldId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM LookupFields A WHERE A.LookupFieldId = T.LookupFieldId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Lookups' ,TxRepositoryTable = 'LookupFields' ,ObjectId = T.LookupFieldId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DataFieldName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Field/LookupField') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM LookupFields WHERE LookupFieldId = ''', T.LookupFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM LookupFields T INNER JOIN (SELECT DataFieldName = DF.Name, DF.DataFieldId, tableName, Layer, LayerName, projectid FROM #datafields Df WHERE rn = 1 ) DF ON T.ConditionalLookupFieldId = DF.DataFieldId INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM LookupFields A WHERE A.LookupFieldId = T.LookupFieldId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM LookupFields A WHERE A.LookupFieldId = T.LookupFieldId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Lookups' ,TxRepositoryTable = 'ConditionalLookupFields' ,ObjectId = T.ConditionalLookupFieldId ,ObjectName = DF.LookupFieldName ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DataFieldName, '/', DF.LookupFieldName) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Field/LookupField/options') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM ConditionalLookupFields WHERE ConditionalLookupFieldId = ''', T.ConditionalLookupFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.ConditionalLookupFieldId, LookupFieldName = T.name, DataFieldName = DF.Name, DF.DataFieldId, tableName, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by ConditionalLookupFieldId order by T.validto desc) FROM #datafields Df INNER JOIN LookupFields T ON T.ConditionalLookupFieldId = DF.DataFieldId WHERE DF.rn = 1 ) DF INNER JOIN ConditionalLookupFields T ON T.ConditionalLookupFieldId = DF.ConditionalLookupFieldId AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM ConditionalLookupFields A WHERE T.ConditionalLookupFieldId = A.ConditionalLookupFieldId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT ConditionalLookupFieldId,UseRawValues,ForceSingleLookup,OverrideDataType,MultipleLookupFields FROM ConditionalLookupFields A WHERE A.ValidTo = T.ValidFrom - 1 AND A.ConditionalLookupFieldId = T.ConditionalLookupFieldId EXCEPT SELECT ConditionalLookupFieldId,UseRawValues,ForceSingleLookup,OverrideDataType,MultipleLookupFields FROM ConditionalLookupFields B WHERE B.ValidFrom = T.ValidFrom AND B.ConditionalLookupFieldId = T.ConditionalLookupFieldId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM ConditionalLookupFields A WHERE A.ConditionalLookupFieldId = T.ConditionalLookupFieldId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Lookups' ,TxRepositoryTable = 'ConditionalLookupFields' ,ObjectId = T.ConditionalLookupFieldId ,ObjectName = DF.LookupFieldName ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DataFieldName, '/', DF.LookupFieldName) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Field/LookupField/options') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM ConditionalLookupFields WHERE ConditionalLookupFieldId = ''', T.ConditionalLookupFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.ConditionalLookupFieldId, LookupFieldName = T.name, DataFieldName = DF.Name, DF.DataFieldId, tableName, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by ConditionalLookupFieldId order by T.validto desc) FROM #datafields Df INNER JOIN LookupFields T ON T.ConditionalLookupFieldId = DF.DataFieldId WHERE DF.rn = 1 ) DF INNER JOIN ConditionalLookupFields T ON T.ConditionalLookupFieldId = DF.ConditionalLookupFieldId AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM ConditionalLookupFields A WHERE A.ConditionalLookupFieldId = T.ConditionalLookupFieldId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM ConditionalLookupFields A WHERE A.ConditionalLookupFieldId = T.ConditionalLookupFieldId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Lookups' ,TxRepositoryTable = 'Sortings' ,ObjectId = T.SortingId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DataFieldName, '/', DF.LookupFieldName, '/', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Field/LookupField/sorting') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM Sortings WHERE SortingId = ''', T.SortingId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.LookupFieldId, LookupFieldName = T.name, DataFieldName = DF.Name, DF.DataFieldId, tableName, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by LookupFieldId order by T.validto desc) FROM #datafields Df INNER JOIN LookupFields T ON T.ConditionalLookupFieldId = DF.DataFieldId WHERE DF.rn = 1 ) DF INNER JOIN Sortings T ON T.LookUpFieldId = DF.LookUpFieldId AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM Sortings A WHERE T.SortingId = A.SortingId) OA_MinVals LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SortingId,DataFieldId,LookUpFieldId,ViewLookUpFieldId,SeqNo,SortOrder,Locked FROM Sortings A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SortingId = T.SortingId EXCEPT SELECT SortingId,DataFieldId,LookUpFieldId,ViewLookUpFieldId,SeqNo,SortOrder,Locked FROM Sortings B WHERE B.ValidFrom = T.ValidFrom AND B.SortingId = T.SortingId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM Sortings A WHERE A.SortingId = T.SortingId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Lookups' ,TxRepositoryTable = 'Sortings' ,ObjectId = T.SortingId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DataFieldName, '/', DF.LookupFieldName, '/', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Field/LookupField/sorting') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM Sortings WHERE SortingId = ''', T.SortingId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.LookupFieldId, LookupFieldName = T.name, DataFieldName = DF.Name, DF.DataFieldId, tableName, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by LookupFieldId order by T.validto desc) FROM #datafields Df INNER JOIN LookupFields T ON T.ConditionalLookupFieldId = DF.DataFieldId WHERE DF.rn = 1 ) DF INNER JOIN Sortings T ON T.LookUpFieldId = DF.LookUpFieldId AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM Sortings A WHERE A.SortingId = T.SortingId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM Sortings A WHERE A.SortingId = T.SortingId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Lookups' ,TxRepositoryTable = 'Joins' ,ObjectId = T.JoinId ,ObjectName = CONCAT(COALESCE(PrimaryField.name, fixedvalue),' ', JoinOperator, ' ', SecondaryField.name) ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DataFieldName, '/', DF.LookupFieldName, '/',COALESCE(PrimaryField.name, fixedvalue),' ', JoinOperator, ' ', SecondaryField.name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Field/LookupField/joins') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM joins WHERE JoinId = ''', T.JoinId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.LookupFieldId, LookupFieldName = T.name, DataFieldName = DF.Name, DF.DataFieldId, tableName, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by LookupFieldId order by T.validto desc) FROM #datafields Df INNER JOIN LookupFields T ON T.ConditionalLookupFieldId = DF.DataFieldId WHERE DF.rn = 1 ) DF INNER JOIN joins T ON T.LookUpFieldId = DF.LookupFieldId AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM joins A WHERE T.JoinId = A.JoinId) OA_MinVals LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.PrimaryDataFieldId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) SecondaryField ON SecondaryField.DataFieldId = T.JoinDataFieldId WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT JoinId,LookUpFieldId,ViewLookUpFieldId,PrimaryDataFieldId,JoinDataFieldId,FixedValue,SeqNo,JoinOperator,Locked FROM joins A WHERE A.ValidTo = T.ValidFrom - 1 AND A.JoinId = T.JoinId EXCEPT SELECT JoinId,LookUpFieldId,ViewLookUpFieldId,PrimaryDataFieldId,JoinDataFieldId,FixedValue,SeqNo,JoinOperator,Locked FROM joins B WHERE B.ValidFrom = T.ValidFrom AND B.JoinId = T.JoinId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM joins A WHERE A.JoinId = T.JoinId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Lookups' ,TxRepositoryTable = 'Joins' ,ObjectId = T.JoinId ,ObjectName = CONCAT(COALESCE(PrimaryField.name, fixedvalue),' ', JoinOperator, ' ', SecondaryField.name) ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DataFieldName, '/', DF.LookupFieldName, '/',COALESCE(PrimaryField.name, fixedvalue),' ', JoinOperator, ' ', SecondaryField.name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Field/LookupField/joins') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM joins WHERE JoinId = ''', T.JoinId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.LookupFieldId, LookupFieldName = T.name, DataFieldName = DF.Name, DF.DataFieldId, tableName, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by LookupFieldId order by T.validto desc) FROM #datafields Df INNER JOIN LookupFields T ON T.ConditionalLookupFieldId = DF.DataFieldId WHERE DF.rn = 1 ) DF INNER JOIN joins T ON T.LookUpFieldId = DF.LookupFieldId AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.PrimaryDataFieldId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) SecondaryField ON SecondaryField.DataFieldId = T.JoinDataFieldId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM joins A WHERE A.JoinId = T.JoinId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM joins A WHERE A.JoinId = T.JoinId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Relation' ,TxRepositoryTable = 'DataRelations' ,ObjectId = T.DataRelationId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Relation') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM DataRelations WHERE DataRelationId = ''', T.DataRelationId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM DataRelations T INNER JOIN (SELECT TableName = DF.Name, DF.DataTableId, Layer, LayerName, projectid FROM #datatables Df WHERE rn = 1 ) DF ON T.PrimaryDataTableId = DF.DataTableId INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM DataRelations A WHERE T.DataRelationId = A.DataRelationId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT DataRelationId,PrimaryDataTableId,SecondaryDataTableId,Name,Locked,RelationshipType,IsDefault FROM DataRelations A WHERE A.ValidTo = T.ValidFrom - 1 AND A.DataRelationId = T.DataRelationId EXCEPT SELECT DataRelationId,PrimaryDataTableId,SecondaryDataTableId,Name,Locked,RelationshipType,IsDefault FROM DataRelations B WHERE B.ValidFrom = T.ValidFrom AND B.DataRelationId = T.DataRelationId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM DataRelations A WHERE A.DataRelationId = T.DataRelationId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Relation' ,TxRepositoryTable = 'DataRelations' ,ObjectId = T.DataRelationId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Relation') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM DataRelations WHERE DataRelationId = ''', T.DataRelationId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM DataRelations T INNER JOIN (SELECT TableName = DF.Name, DF.DataTableId, Layer, LayerName, projectid FROM #datatables Df WHERE rn = 1 ) DF ON T.PrimaryDataTableId = DF.DataTableId INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM DataRelations A WHERE A.DataRelationId = T.DataRelationId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM DataRelations A WHERE A.DataRelationId = T.DataRelationId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Relation' ,TxRepositoryTable = 'DataRelationFields' ,ObjectId = T.DataRelationFieldId ,ObjectName = CONCAT(PrimaryField.name, ' Equal ', SecondaryField.name) ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DataRelationName, '/',PrimaryField.name, ' Equal ', SecondaryField.name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Relation/RelationField') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM DataRelationFields WHERE DataRelationFieldId = ''', T.DataRelationFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.DataRelationId, DataRelationName = T.name, DataTableName = DF.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.DataRelationId order by T.validto desc) FROM #DataTables Df INNER JOIN DataRelations T ON T.PrimaryDataTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN DataRelationFields T ON T.DataRelationId = DF.DataRelationId AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM DataRelationFields A WHERE T.DataRelationFieldId = A.DataRelationFieldId) OA_MinVals LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.PrimaryDataFieldId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) SecondaryField ON SecondaryField.DataFieldId = T.SecondaryDataFieldId WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT DataRelationFieldId,DataRelationId,PrimaryDataFieldId,SecondaryDataFieldId FROM DataRelationFields A WHERE A.ValidTo = T.ValidFrom - 1 AND A.DataRelationFieldId = T.DataRelationFieldId EXCEPT SELECT DataRelationFieldId,DataRelationId,PrimaryDataFieldId,SecondaryDataFieldId FROM DataRelationFields B WHERE B.ValidFrom = T.ValidFrom AND B.DataRelationFieldId = T.DataRelationFieldId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM DataRelationFields A WHERE A.DataRelationFieldId = T.DataRelationFieldId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Relation' ,TxRepositoryTable = 'DataRelationFields' ,ObjectId = T.DataRelationFieldId ,ObjectName = CONCAT(PrimaryField.name, ' Equal ', SecondaryField.name) ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DataRelationName, '/',PrimaryField.name, ' Equal ', SecondaryField.name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Relation/RelationField') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM DataRelationFields WHERE DataRelationFieldId = ''', T.DataRelationFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.DataRelationId, DataRelationName = T.name, DataTableName = DF.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.DataRelationId order by T.validto desc) FROM #DataTables Df INNER JOIN DataRelations T ON T.PrimaryDataTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN DataRelationFields T ON T.DataRelationId = DF.DataRelationId AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.PrimaryDataFieldId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) SecondaryField ON SecondaryField.DataFieldId = T.SecondaryDataFieldId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM DataRelationFields A WHERE A.DataRelationFieldId = T.DataRelationFieldId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM DataRelationFields A WHERE A.DataRelationFieldId = T.DataRelationFieldId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Date Table' ,TxRepositoryTable = 'DateTables' ,ObjectId = T.DateTableId ,ObjectName = DF.TableName ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', T.StartDate, ' - ', T.EndDate) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/DatePeriod') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM DateTables WHERE DateTableId = ''', T.DateTableId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM DateTables T INNER JOIN (SELECT TableName = DF.Name, DF.DataTableId, Layer, LayerName, projectid FROM #datatables Df WHERE rn = 1 ) DF ON T.DateTableId = DF.DataTableId INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM DateTables A WHERE T.DateTableId = A.DateTableId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT DateTableId,StartDate,EndDate,DaysAhead,DateFormat=CONVERT(NVARCHAR(MAX), DateFormat),DateSeperator=CONVERT(NVARCHAR(MAX), DateSeperator),FiscalType=CONVERT(NVARCHAR(MAX), FiscalType),FiscalStartMonth=CONVERT(NVARCHAR(MAX), FiscalStartMonth),FirstDayOfTheWeek=CONVERT(NVARCHAR(MAX), FirstDayOfTheWeek),FirstWeekOfTheYear=CONVERT(NVARCHAR(MAX), FirstWeekOfTheYear),Monday=CONVERT(NVARCHAR(MAX), Monday), Tuesday=CONVERT(NVARCHAR(MAX), Tuesday), Wednesday=CONVERT(NVARCHAR(MAX), Wednesday), Thursday=CONVERT(NVARCHAR(MAX), Thursday), Friday=CONVERT(NVARCHAR(MAX), Friday), Saturday=CONVERT(NVARCHAR(MAX), Saturday), Sunday=CONVERT(NVARCHAR(MAX), Sunday), January=CONVERT(NVARCHAR(MAX), January), February=CONVERT(NVARCHAR(MAX), February), March=CONVERT(NVARCHAR(MAX), March), April=CONVERT(NVARCHAR(MAX), April), May=CONVERT(NVARCHAR(MAX), May), June=CONVERT(NVARCHAR(MAX), June), July=CONVERT(NVARCHAR(MAX), July), August=CONVERT(NVARCHAR(MAX), August), September=CONVERT(NVARCHAR(MAX), September), October=CONVERT(NVARCHAR(MAX), October), November=CONVERT(NVARCHAR(MAX), November), December=CONVERT(NVARCHAR(MAX), December), FirstQuarter=CONVERT(NVARCHAR(MAX), FirstQuarter), SecondQuarter=CONVERT(NVARCHAR(MAX), SecondQuarter), ThirdQuarter=CONVERT(NVARCHAR(MAX), ThirdQuarter), FourthQuarter=CONVERT(NVARCHAR(MAX), FourthQuarter) FROM DateTables A WHERE A.ValidTo = T.ValidFrom - 1 AND A.DateTableId = T.DateTableId EXCEPT SELECT DateTableId,StartDate,EndDate,DaysAhead,DateFormat=CONVERT(NVARCHAR(MAX), DateFormat),DateSeperator=CONVERT(NVARCHAR(MAX), DateSeperator),FiscalType=CONVERT(NVARCHAR(MAX), FiscalType),FiscalStartMonth=CONVERT(NVARCHAR(MAX), FiscalStartMonth),FirstDayOfTheWeek=CONVERT(NVARCHAR(MAX), FirstDayOfTheWeek),FirstWeekOfTheYear=CONVERT(NVARCHAR(MAX), FirstWeekOfTheYear),Monday=CONVERT(NVARCHAR(MAX), Monday), Tuesday=CONVERT(NVARCHAR(MAX), Tuesday), Wednesday=CONVERT(NVARCHAR(MAX), Wednesday), Thursday=CONVERT(NVARCHAR(MAX), Thursday), Friday=CONVERT(NVARCHAR(MAX), Friday), Saturday=CONVERT(NVARCHAR(MAX), Saturday), Sunday=CONVERT(NVARCHAR(MAX), Sunday), January=CONVERT(NVARCHAR(MAX), January), February=CONVERT(NVARCHAR(MAX), February), March=CONVERT(NVARCHAR(MAX), March), April=CONVERT(NVARCHAR(MAX), April), May=CONVERT(NVARCHAR(MAX), May), June=CONVERT(NVARCHAR(MAX), June), July=CONVERT(NVARCHAR(MAX), July), August=CONVERT(NVARCHAR(MAX), August), September=CONVERT(NVARCHAR(MAX), September), October=CONVERT(NVARCHAR(MAX), October), November=CONVERT(NVARCHAR(MAX), November), December=CONVERT(NVARCHAR(MAX), December), FirstQuarter=CONVERT(NVARCHAR(MAX), FirstQuarter), SecondQuarter=CONVERT(NVARCHAR(MAX), SecondQuarter), ThirdQuarter=CONVERT(NVARCHAR(MAX), ThirdQuarter), FourthQuarter=CONVERT(NVARCHAR(MAX), FourthQuarter) FROM DateTables B WHERE B.ValidFrom = T.ValidFrom AND B.DateTableId = T.DateTableId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM DateTables A WHERE A.DateTableId = T.DateTableId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Date Table' ,TxRepositoryTable = 'DateTables' ,ObjectId = T.DateTableId ,ObjectName = DF.TableName ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', T.StartDate, ' - ', T.EndDate) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/DatePeriod') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM DateTables WHERE DateTableId = ''', T.DateTableId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM DateTables T INNER JOIN (SELECT TableName = DF.Name, DF.DataTableId, Layer, LayerName, projectid FROM #datatables Df WHERE rn = 1 ) DF ON T.DateTableId = DF.DataTableId INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM DateTables A WHERE A.DateTableId = T.DateTableId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM DateTables A WHERE A.DateTableId = T.DateTableId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Time Table' ,TxRepositoryTable = 'TimeTables' ,ObjectId = T.TimeTableId ,ObjectName = DF.tableName ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', T.FromDate, ' - ', T.ToDate) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/TimePeriod') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TimeTables WHERE TimeTableId = ''', T.TimeTableId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM TimeTables T INNER JOIN (SELECT TableName = DF.Name, DF.DataTableId, Layer, LayerName, projectid FROM #datatables Df WHERE rn = 1 ) DF ON T.TimeTableId = DF.DataTableId INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM TimeTables A WHERE T.TimeTableId = A.TimeTableId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT TimeTableId,FromDate,ToDate,Year,YearStartMonth,YearStartDay,HalfYear,QuarterNumber,QuarterName,MonthNumber,MonthName,MonthSortOrder,WeekNumber,FirstDayOfWeek,DayOfWeekName,DayOfYear,DayOfMonth,DayOfWeek,IsWeekDay,IsHoliday,AmPm,Hour,Minute,Second,DaysAhead,DaysBack,CustomQuarters,CustomMonths,CustomWeeks FROM TimeTables A WHERE A.ValidTo = T.ValidFrom - 1 AND A.TimeTableId = T.TimeTableId EXCEPT SELECT TimeTableId,FromDate,ToDate,Year,YearStartMonth,YearStartDay,HalfYear,QuarterNumber,QuarterName,MonthNumber,MonthName,MonthSortOrder,WeekNumber,FirstDayOfWeek,DayOfWeekName,DayOfYear,DayOfMonth,DayOfWeek,IsWeekDay,IsHoliday,AmPm,Hour,Minute,Second,DaysAhead,DaysBack,CustomQuarters,CustomMonths,CustomWeeks FROM TimeTables B WHERE B.ValidFrom = T.ValidFrom AND B.TimeTableId = T.TimeTableId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM TimeTables A WHERE A.TimeTableId = T.TimeTableId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Time Table' ,TxRepositoryTable = 'TimeTables' ,ObjectId = T.TimeTableId ,ObjectName = DF.TableName ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', T.FromDate, ' - ', T.ToDate) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/TimePeriod') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TimeTables WHERE TimeTableId = ''', T.TimeTableId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM TimeTables T INNER JOIN (SELECT TableName = DF.Name, DF.DataTableId, Layer, LayerName, projectid FROM #datatables Df WHERE rn = 1 ) DF ON T.TimeTableId = DF.DataTableId INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM TimeTables A WHERE A.TimeTableId = T.TimeTableId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM TimeTables A WHERE A.TimeTableId = T.TimeTableId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Incremental Selection Rule' ,TxRepositoryTable = 'IncrementalSelectionRules' ,ObjectId = T.IncrementalSelectionRuleId ,ObjectName = 'IncrementalSelectionRule' ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/IncrementalSelectionRule') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM IncrementalSelectionRules WHERE IncrementalSelectionRuleId = ''', T.IncrementalSelectionRuleId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM IncrementalSelectionRules T INNER JOIN (SELECT TableName = DF.Name, DF.DataTableId, Layer, LayerName, projectid FROM #datatables Df WHERE rn = 1 ) DF ON T.DataTableId = DF.DataTableId INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM IncrementalSelectionRules A WHERE T.IncrementalSelectionRuleId = A.IncrementalSelectionRuleId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT IncrementalSelectionRuleId,DataTableId,SeqNo,Locked FROM IncrementalSelectionRules A WHERE A.ValidTo = T.ValidFrom - 1 AND A.IncrementalSelectionRuleId = T.IncrementalSelectionRuleId EXCEPT SELECT IncrementalSelectionRuleId,DataTableId,SeqNo,Locked FROM IncrementalSelectionRules B WHERE B.ValidFrom = T.ValidFrom AND B.IncrementalSelectionRuleId = T.IncrementalSelectionRuleId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM IncrementalSelectionRules A WHERE A.IncrementalSelectionRuleId = T.IncrementalSelectionRuleId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Incremental Selection Rule' ,TxRepositoryTable = 'IncrementalSelectionRules' ,ObjectId = T.IncrementalSelectionRuleId ,ObjectName = 'IncrementalSelectionRule' ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/IncrementalSelectionRule') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM IncrementalSelectionRules WHERE IncrementalSelectionRuleId = ''', T.IncrementalSelectionRuleId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM IncrementalSelectionRules T INNER JOIN (SELECT TableName = DF.Name, DF.DataTableId, Layer, LayerName, projectid FROM #datatables Df WHERE rn = 1 ) DF ON T.DataTableId = DF.DataTableId INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM IncrementalSelectionRules A WHERE A.IncrementalSelectionRuleId = T.IncrementalSelectionRuleId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM IncrementalSelectionRules A WHERE A.IncrementalSelectionRuleId = T.IncrementalSelectionRuleId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Incremental Selection Rule' ,TxRepositoryTable = 'IncrementalSelectionRuleFields' ,ObjectId = T.IncrementalSelectionRuleFieldId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/IncrementalSelectionRuleField: ', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/IncrementalSelectionRule/Fields') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM IncrementalSelectionRuleFields WHERE IncrementalSelectionRuleFieldId = ''', T.IncrementalSelectionRuleFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.IncrementalSelectionRuleId, IncrementalSelectionRuleName = '', DataTableName = DF.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.IncrementalSelectionRuleId order by T.validto desc) FROM #DataTables Df INNER JOIN IncrementalSelectionRules T ON T.DataTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN IncrementalSelectionRuleFields T ON T.IncrementalSelectionRuleId = DF.IncrementalSelectionRuleId AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM IncrementalSelectionRuleFields A WHERE T.IncrementalSelectionRuleFieldId = A.IncrementalSelectionRuleFieldId) OA_MinVals LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT IncrementalSelectionRuleFieldId,IncrementalSelectionRuleId,DataFieldId,SeqNo,AutoSubtract,Locked FROM IncrementalSelectionRuleFields A WHERE A.ValidTo = T.ValidFrom - 1 AND A.IncrementalSelectionRuleFieldId = T.IncrementalSelectionRuleFieldId EXCEPT SELECT IncrementalSelectionRuleFieldId,IncrementalSelectionRuleId,DataFieldId,SeqNo,AutoSubtract,Locked FROM IncrementalSelectionRuleFields B WHERE B.ValidFrom = T.ValidFrom AND B.IncrementalSelectionRuleFieldId = T.IncrementalSelectionRuleFieldId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM IncrementalSelectionRuleFields A WHERE A.IncrementalSelectionRuleFieldId = T.IncrementalSelectionRuleFieldId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Incremental Selection Rule' ,TxRepositoryTable = 'IncrementalSelectionRuleFields' ,ObjectId = T.IncrementalSelectionRuleFieldId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/IncrementalSelectionRuleField: ', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/IncrementalSelectionRule/Fields') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM IncrementalSelectionRuleFields WHERE IncrementalSelectionRuleFieldId = ''', T.IncrementalSelectionRuleFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.IncrementalSelectionRuleId, IncrementalSelectionRuleName = '', DataTableName = DF.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.IncrementalSelectionRuleId order by T.validto desc) FROM #DataTables Df INNER JOIN IncrementalSelectionRules T ON T.DataTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN IncrementalSelectionRuleFields T ON T.IncrementalSelectionRuleId = DF.IncrementalSelectionRuleId AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM IncrementalSelectionRuleFields A WHERE A.IncrementalSelectionRuleId = T.IncrementalSelectionRuleId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM IncrementalSelectionRuleFields A WHERE A.IncrementalSelectionRuleId = T.IncrementalSelectionRuleId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'History' ,TxRepositoryTable = 'HistoryRules' ,ObjectId = T.HistoryRuleId ,ObjectName = 'HistoryRule' ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/HistoryRule') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM HistoryRules WHERE HistoryRuleId = ''', T.HistoryRuleId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM HistoryRules T INNER JOIN (SELECT TableName = DF.Name, DF.DataTableId, Layer, LayerName, projectid FROM #datatables Df WHERE rn = 1 ) DF ON T.DataTableId = DF.DataTableId INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM HistoryRules A WHERE T.HistoryRuleId = A.HistoryRuleId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT HistoryRuleId,DataTableId FROM HistoryRules A WHERE A.ValidTo = T.ValidFrom - 1 AND A.HistoryRuleId = T.HistoryRuleId EXCEPT SELECT HistoryRuleId,DataTableId FROM HistoryRules B WHERE B.ValidFrom = T.ValidFrom AND B.HistoryRuleId = T.HistoryRuleId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM HistoryRules A WHERE A.HistoryRuleId = T.HistoryRuleId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'History' ,TxRepositoryTable = 'HistoryRules' ,ObjectId = T.HistoryRuleId ,ObjectName = 'HistoryRule' ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/HistoryRule') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM HistoryRules WHERE HistoryRuleId = ''', T.HistoryRuleId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM HistoryRules T INNER JOIN (SELECT TableName = DF.Name, DF.DataTableId, Layer, LayerName, projectid FROM #datatables Df WHERE rn = 1 ) DF ON T.DataTableId = DF.DataTableId INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM HistoryRules A WHERE A.HistoryRuleId = T.HistoryRuleId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM HistoryRules A WHERE A.HistoryRuleId = T.HistoryRuleId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'History' ,TxRepositoryTable = 'HistoryKeyFields' ,ObjectId = T.HistoryKeyFieldId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/HistoryKeyField: ', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/HistoryRule/Key') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM HistoryKeyFields WHERE HistoryKeyFieldId = ''', T.HistoryKeyFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.HistoryRuleId, HistoryRuleName = '', DataTableName = DF.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.HistoryRuleId order by T.validto desc) FROM #DataTables Df INNER JOIN HistoryRules T ON T.DataTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN HistoryKeyFields T ON T.HistoryRuleId = DF.HistoryRuleId AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM HistoryKeyFields A WHERE T.HistoryKeyFieldId = A.HistoryKeyFieldId) OA_MinVals LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT HistoryKeyFieldId,HistoryRuleId,DataFieldId FROM HistoryKeyFields A WHERE A.ValidTo = T.ValidFrom - 1 AND A.HistoryKeyFieldId = T.HistoryKeyFieldId EXCEPT SELECT HistoryKeyFieldId,HistoryRuleId,DataFieldId FROM HistoryKeyFields B WHERE B.ValidFrom = T.ValidFrom AND B.HistoryKeyFieldId = T.HistoryKeyFieldId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM HistoryKeyFields A WHERE A.HistoryKeyFieldId = T.HistoryKeyFieldId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'History' ,TxRepositoryTable = 'HistoryKeyFields' ,ObjectId = T.HistoryKeyFieldId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/HistoryKeyField: ', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/HistoryRule/Key') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM HistoryKeyFields WHERE HistoryKeyFieldId = ''', T.HistoryKeyFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.HistoryRuleId, HistoryRuleName = '', DataTableName = DF.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.HistoryRuleId order by T.validto desc) FROM #DataTables Df INNER JOIN HistoryRules T ON T.DataTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN HistoryKeyFields T ON T.HistoryRuleId = DF.HistoryRuleId AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM HistoryKeyFields A WHERE A.HistoryKeyFieldId = T.HistoryKeyFieldId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM HistoryKeyFields A WHERE A.HistoryKeyFieldId = T.HistoryKeyFieldId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'History' ,TxRepositoryTable = 'HistoryTypeFields' ,ObjectId = T.HistoryTypeTwoId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/HistoryTypeField: ', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/HistoryRule/Type') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM HistoryTypeFields WHERE HistoryTypeTwoId = ''', T.HistoryTypeTwoId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.HistoryRuleId, HistoryRuleName = '', DataTableName = DF.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.HistoryRuleId order by T.validto desc) FROM #DataTables Df INNER JOIN HistoryRules T ON T.DataTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN HistoryTypeFields T ON T.HistoryRuleId = DF.HistoryRuleId AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM HistoryTypeFields A WHERE T.HistoryTypeTwoId = A.HistoryTypeTwoId) OA_MinVals LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT HistoryTypeTwoId,HistoryRuleId,DataFieldId,HistoryType FROM HistoryTypeFields A WHERE A.ValidTo = T.ValidFrom - 1 AND A.HistoryTypeTwoId = T.HistoryTypeTwoId EXCEPT SELECT HistoryTypeTwoId,HistoryRuleId,DataFieldId,HistoryType FROM HistoryTypeFields B WHERE B.ValidFrom = T.ValidFrom AND B.HistoryTypeTwoId = T.HistoryTypeTwoId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM HistoryTypeFields A WHERE A.HistoryTypeTwoId = T.HistoryTypeTwoId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'History' ,TxRepositoryTable = 'HistoryTypeFields' ,ObjectId = T.HistoryTypeTwoId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/HistoryTypeField: ', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/HistoryRule/Type') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM HistoryTypeFields WHERE HistoryTypeTwoId = ''', T.HistoryTypeTwoId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.HistoryRuleId, HistoryRuleName = '', DataTableName = DF.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.HistoryRuleId order by T.validto desc) FROM #DataTables Df INNER JOIN HistoryRules T ON T.DataTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN HistoryTypeFields T ON T.HistoryRuleId = DF.HistoryRuleId AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM HistoryTypeFields A WHERE A.HistoryTypeTwoId = T.HistoryTypeTwoId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM HistoryTypeFields A WHERE A.HistoryTypeTwoId = T.HistoryTypeTwoId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Target Based Incremental' ,TxRepositoryTable = 'TargetBasedIncrementalKeys' ,ObjectId = T.TargetBasedIncrementalKeyId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/IncrementalKey: ', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/TargtIncremental/Key') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TargetBasedIncrementalKeys WHERE TargetBasedIncrementalKeyId = ''', T.TargetBasedIncrementalKeyId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM TargetBasedIncrementalKeys T INNER JOIN (SELECT DataFieldName = DF.Name, DF.DataFieldId, tableName, Layer, LayerName, projectid FROM #datafields Df WHERE rn = 1 ) DF ON T.DataFieldId = DF.DataFieldId INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM TargetBasedIncrementalKeys A WHERE T.TargetBasedIncrementalKeyId = A.TargetBasedIncrementalKeyId) OA_MinVals LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT TargetBasedIncrementalKeyId,TargetBasedIncrementalRuleId,DataFieldId FROM TargetBasedIncrementalKeys A WHERE A.ValidTo = T.ValidFrom - 1 AND A.TargetBasedIncrementalKeyId = T.TargetBasedIncrementalKeyId EXCEPT SELECT TargetBasedIncrementalKeyId,TargetBasedIncrementalRuleId,DataFieldId FROM TargetBasedIncrementalKeys B WHERE B.ValidFrom = T.ValidFrom AND B.TargetBasedIncrementalKeyId = T.TargetBasedIncrementalKeyId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM TargetBasedIncrementalKeys A WHERE A.TargetBasedIncrementalKeyId = T.TargetBasedIncrementalKeyId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Target Based Incremental' ,TxRepositoryTable = 'TargetBasedIncrementalKeys' ,ObjectId = T.TargetBasedIncrementalKeyId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/IncrementalKey: ', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/TargtIncremental/Key') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TargetBasedIncrementalKeys WHERE TargetBasedIncrementalKeyId = ''', T.TargetBasedIncrementalKeyId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM TargetBasedIncrementalKeys T INNER JOIN (SELECT DataFieldName = DF.Name, DF.DataFieldId, tableName, Layer, LayerName, projectid FROM #datafields Df WHERE rn = 1 ) DF ON T.DataFieldId = DF.DataFieldId INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM TargetBasedIncrementalKeys A WHERE A.TargetBasedIncrementalKeyId = T.TargetBasedIncrementalKeyId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM TargetBasedIncrementalKeys A WHERE A.TargetBasedIncrementalKeyId = T.TargetBasedIncrementalKeyId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Target Based Incremental' ,TxRepositoryTable = 'TargetBasedIncrementalRules' ,ObjectId = T.TargetBasedIncrementalRuleId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/TargetBasedIncrementalRule: ', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/TargetBasedIncrementalRules') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TargetBasedIncrementalRules WHERE TargetBasedIncrementalRuleId = ''', T.TargetBasedIncrementalRuleId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.TargetBasedIncrementalRuleId, argetBasedIncrementalName = '', DF.DataTableId, tableName = tableName, Df.DataFieldId,Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.TargetBasedIncrementalRuleId order by T.validto desc) FROM #datafields Df INNER JOIN TargetBasedIncrementalKeys T ON T.DataFieldId = DF.DataFieldId WHERE DF.rn = 1 ) DF INNER JOIN TargetBasedIncrementalRules T ON T.TargetBasedIncrementalRuleId = DF.TargetBasedIncrementalRuleId AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM TargetBasedIncrementalRules A WHERE T.TargetBasedIncrementalRuleId = A.TargetBasedIncrementalRuleId) OA_MinVals LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFielId WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT TargetBasedIncrementalRuleId,DataFielId,Inserts,Updates,Deletes FROM TargetBasedIncrementalRules A WHERE A.ValidTo = T.ValidFrom - 1 AND A.TargetBasedIncrementalRuleId = T.TargetBasedIncrementalRuleId EXCEPT SELECT TargetBasedIncrementalRuleId,DataFielId,Inserts,Updates,Deletes FROM TargetBasedIncrementalRules B WHERE B.ValidFrom = T.ValidFrom AND B.TargetBasedIncrementalRuleId = T.TargetBasedIncrementalRuleId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM TargetBasedIncrementalRules A WHERE A.TargetBasedIncrementalRuleId = T.TargetBasedIncrementalRuleId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Target Based Incremental' ,TxRepositoryTable = 'TargetBasedIncrementalRules' ,ObjectId = T.TargetBasedIncrementalRuleId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/TargetBasedIncrementalRule: ', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/TargetBasedIncrementalRules') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TargetBasedIncrementalRules WHERE TargetBasedIncrementalRuleId = ''', T.TargetBasedIncrementalRuleId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.TargetBasedIncrementalRuleId, argetBasedIncrementalName = '', DF.DataTableId, tableName = tableName, Df.DataFieldId,Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.TargetBasedIncrementalRuleId order by T.validto desc) FROM #datafields Df INNER JOIN TargetBasedIncrementalKeys T ON T.DataFieldId = DF.DataFieldId WHERE DF.rn = 1 ) DF INNER JOIN TargetBasedIncrementalRules T ON T.TargetBasedIncrementalRuleId = DF.TargetBasedIncrementalRuleId AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFielId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM TargetBasedIncrementalRules A WHERE A.TargetBasedIncrementalRuleId = T.TargetBasedIncrementalRuleId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM TargetBasedIncrementalRules A WHERE A.TargetBasedIncrementalRuleId = T.TargetBasedIncrementalRuleId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Target Based Incremental' ,TxRepositoryTable = 'TargetBasedIncrementalValueKeys' ,ObjectId = T.TargetBasedIncrementalValueKeyId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/TargetBasedIncrementalValueKey: ', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/TargetBasedIncrementalValueKey') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TargetBasedIncrementalValueKeys WHERE TargetBasedIncrementalValueKeyId = ''', T.TargetBasedIncrementalValueKeyId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.TargetBasedIncrementalRuleId, argetBasedIncrementalName = '', DF.DataTableId, tableName = tableName, Df.DataFieldId,Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.TargetBasedIncrementalRuleId order by T.validto desc) FROM #datafields Df INNER JOIN TargetBasedIncrementalKeys T ON T.DataFieldId = DF.DataFieldId WHERE DF.rn = 1 ) DF INNER JOIN TargetBasedIncrementalValueKeys T ON T.TargetBasedIncrementalRuleId = DF.TargetBasedIncrementalRuleId AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM TargetBasedIncrementalValueKeys A WHERE T.TargetBasedIncrementalValueKeyId = A.TargetBasedIncrementalValueKeyId) OA_MinVals LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT TargetBasedIncrementalValueKeyId,TargetBasedIncrementalRuleId,DataFieldId FROM TargetBasedIncrementalValueKeys A WHERE A.ValidTo = T.ValidFrom - 1 AND A.TargetBasedIncrementalValueKeyId = T.TargetBasedIncrementalValueKeyId EXCEPT SELECT TargetBasedIncrementalValueKeyId,TargetBasedIncrementalRuleId,DataFieldId FROM TargetBasedIncrementalValueKeys B WHERE B.ValidFrom = T.ValidFrom AND B.TargetBasedIncrementalValueKeyId = T.TargetBasedIncrementalValueKeyId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM TargetBasedIncrementalValueKeys A WHERE A.TargetBasedIncrementalValueKeyId = T.TargetBasedIncrementalValueKeyId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Target Based Incremental' ,TxRepositoryTable = 'TargetBasedIncrementalValueKeys' ,ObjectId = T.TargetBasedIncrementalValueKeyId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/TargetBasedIncrementalValueKey: ', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/TargetBasedIncrementalValueKey') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TargetBasedIncrementalValueKeys WHERE TargetBasedIncrementalValueKeyId = ''', T.TargetBasedIncrementalValueKeyId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.TargetBasedIncrementalRuleId, argetBasedIncrementalName = '', DF.DataTableId, tableName = tableName, Df.DataFieldId,Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.TargetBasedIncrementalRuleId order by T.validto desc) FROM #datafields Df INNER JOIN TargetBasedIncrementalKeys T ON T.DataFieldId = DF.DataFieldId WHERE DF.rn = 1 ) DF INNER JOIN TargetBasedIncrementalValueKeys T ON T.TargetBasedIncrementalRuleId = DF.TargetBasedIncrementalRuleId AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM TargetBasedIncrementalValueKeys A WHERE A.TargetBasedIncrementalValueKeyId = T.TargetBasedIncrementalValueKeyId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM TargetBasedIncrementalValueKeys A WHERE A.TargetBasedIncrementalValueKeyId = T.TargetBasedIncrementalValueKeyId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Data Table' ,TxRepositoryTable = 'JunkTableFieldRelations' ,ObjectId = T.JunkTableFieldRelationId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/JunkTableKeyField') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM JunkTableFieldRelations WHERE JunkTableFieldRelationId = ''', T.JunkTableFieldRelationId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM [JunkTableFieldRelations] T INNER JOIN (SELECT TableName = DF.Name, DF.DataTableId, Layer, LayerName, projectid FROM #datatables Df WHERE rn = 1 ) DF ON T.DataTableId = DF.DataTableId INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM [JunkTableFieldRelations] A WHERE T.JunkTableFieldRelationId = A.JunkTableFieldRelationId) OA_MinVals LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT JunkTableFieldRelationId,DataFieldId,DataTableId,ExecutionNumber FROM [JunkTableFieldRelations] A WHERE A.ValidTo = T.ValidFrom - 1 AND A.JunkTableFieldRelationId = T.JunkTableFieldRelationId EXCEPT SELECT JunkTableFieldRelationId,DataFieldId,DataTableId,ExecutionNumber FROM [JunkTableFieldRelations] B WHERE B.ValidFrom = T.ValidFrom AND B.JunkTableFieldRelationId = T.JunkTableFieldRelationId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM [JunkTableFieldRelations] A WHERE A.JunkTableFieldRelationId = T.JunkTableFieldRelationId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Data Table' ,TxRepositoryTable = 'JunkTableFieldRelations' ,ObjectId = T.JunkTableFieldRelationId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/JunkTableKeyField') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM JunkTableFieldRelations WHERE JunkTableFieldRelationId = ''', T.JunkTableFieldRelationId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM [JunkTableFieldRelations] T INNER JOIN (SELECT TableName = DF.Name, DF.DataTableId, Layer, LayerName, projectid FROM #datatables Df WHERE rn = 1 ) DF ON T.DataTableId = DF.DataTableId INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM [JunkTableFieldRelations] A WHERE A.JunkTableFieldRelationId = T.JunkTableFieldRelationId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM [JunkTableFieldRelations] A WHERE A.JunkTableFieldRelationId = T.JunkTableFieldRelationId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Junk Table' ,TxRepositoryTable = 'JunkDimensionDataFields' ,ObjectId = T.JunkDimensionDataFieldId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/ ', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Field') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM JunkDimensionDataFields WHERE JunkDimensionDataFieldId = ''', T.JunkDimensionDataFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM JunkDimensionDataFields T INNER JOIN (SELECT DataFieldName = DF.Name, DF.DataFieldId, tableName, Layer, LayerName, projectid FROM #datafields Df WHERE rn = 1 ) DF ON T.DataFieldId = DF.DataFieldId INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM JunkDimensionDataFields A WHERE T.JunkDimensionDataFieldId = A.JunkDimensionDataFieldId) OA_MinVals LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT JunkDimensionDataFieldId,DataFieldId,RelatedDataFieldId FROM JunkDimensionDataFields A WHERE A.ValidTo = T.ValidFrom - 1 AND A.JunkDimensionDataFieldId = T.JunkDimensionDataFieldId EXCEPT SELECT JunkDimensionDataFieldId,DataFieldId,RelatedDataFieldId FROM JunkDimensionDataFields B WHERE B.ValidFrom = T.ValidFrom AND B.JunkDimensionDataFieldId = T.JunkDimensionDataFieldId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM JunkDimensionDataFields A WHERE A.JunkDimensionDataFieldId = T.JunkDimensionDataFieldId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Junk Table' ,TxRepositoryTable = 'JunkDimensionDataFields' ,ObjectId = T.JunkDimensionDataFieldId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/ ', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Field') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM JunkDimensionDataFields WHERE JunkDimensionDataFieldId = ''', T.JunkDimensionDataFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM JunkDimensionDataFields T INNER JOIN (SELECT DataFieldName = DF.Name, DF.DataFieldId, tableName, Layer, LayerName, projectid FROM #datafields Df WHERE rn = 1 ) DF ON T.DataFieldId = DF.DataFieldId INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM JunkDimensionDataFields A WHERE A.JunkDimensionDataFieldId = T.JunkDimensionDataFieldId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM JunkDimensionDataFields A WHERE A.JunkDimensionDataFieldId = T.JunkDimensionDataFieldId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'PrePostScripts' ,TxRepositoryTable = 'PrePostScripts' ,ObjectId = T.PrePostScriptId ,ObjectName = tableName ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath =CONCAT('/', PRJ.Name, '/', LayerName) + CASE WHEN RT = 1 THEN CONCAT( '/', tableName) ELSE '' END ,ObjectPathAbstract = CONCAT('/project/', layer) + CASE WHEN RT = 1 THEN '/Table' ELSE '' END ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM PrePostScripts WHERE PrePostScriptId = ''', T.PrePostScriptId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM PrePostScripts T INNER JOIN (SELECT RT = 1, TXObjectId = Df.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, RN FROM #DataTables Df WHERE rn = 1 UNION ALL SELECT 2, sqlserverconnectionid,B.Name, Layer = 'Date Warehouse', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY sqlserverconnectionid ORDER BY A.validto desc) FROM SqlServerConnections A INNER JOIN DataWarehouses B ON A.DataWarehouseId = B.DataWarehouseId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 3, DataSourceId,B.Name, Layer = 'Business Unit', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY DataSourceId ORDER BY A.validto desc) FROM DataSources A INNER JOIN BusinessUnits B ON A.BusinessUnitId = B.BusinessUnitId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 4, StagingDatabaseId,B.Name, Layer = 'Business Unit', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY StagingDatabaseId ORDER BY A.validto desc) FROM StagingDatabases A INNER JOIN BusinessUnits B ON A.BusinessUnitId = B.BusinessUnitId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 5, OlapServerId, A.Name, Layer = 'Olap', layername = A.Name, A.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY olapserverid ORDER BY A.validto desc) FROM OlapServers A WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE A.ProjectId = C.ProjectId AND C.Name = @project) ) DF ON T.TXObjectId = DF.TXObjectId AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM PrePostScripts A WHERE T.PrePostScriptId = A.PrePostScriptId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT PrePostScriptId,FtpSourceId,CustomSqlScriptId,BusinessFunctionUserId,TXClassId,TXObjectId,Type FROM PrePostScripts A WHERE A.ValidTo = T.ValidFrom - 1 AND A.PrePostScriptId = T.PrePostScriptId EXCEPT SELECT PrePostScriptId,FtpSourceId,CustomSqlScriptId,BusinessFunctionUserId,TXClassId,TXObjectId,Type FROM PrePostScripts B WHERE B.ValidFrom = T.ValidFrom AND B.PrePostScriptId = T.PrePostScriptId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM PrePostScripts A WHERE A.PrePostScriptId = T.PrePostScriptId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'PrePostScripts' ,TxRepositoryTable = 'PrePostScripts' ,ObjectId = T.PrePostScriptId ,ObjectName = tableName ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath =CONCAT('/', PRJ.Name, '/', LayerName) + CASE WHEN RT = 1 THEN CONCAT( '/', tableName) ELSE '' END ,ObjectPathAbstract = CONCAT('/project/', layer) + CASE WHEN RT = 1 THEN '/Table' ELSE '' END ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM PrePostScripts WHERE PrePostScriptId = ''', T.PrePostScriptId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM PrePostScripts T INNER JOIN (SELECT RT = 1, TXObjectId = Df.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, RN FROM #DataTables Df WHERE rn = 1 UNION ALL SELECT 2, sqlserverconnectionid,B.Name, Layer = 'Date Warehouse', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY sqlserverconnectionid ORDER BY A.validto desc) FROM SqlServerConnections A INNER JOIN DataWarehouses B ON A.DataWarehouseId = B.DataWarehouseId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 3, DataSourceId,B.Name, Layer = 'Business Unit', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY DataSourceId ORDER BY A.validto desc) FROM DataSources A INNER JOIN BusinessUnits B ON A.BusinessUnitId = B.BusinessUnitId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 4, StagingDatabaseId,B.Name, Layer = 'Business Unit', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY StagingDatabaseId ORDER BY A.validto desc) FROM StagingDatabases A INNER JOIN BusinessUnits B ON A.BusinessUnitId = B.BusinessUnitId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 5, OlapServerId, A.Name, Layer = 'Olap', layername = A.Name, A.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY olapserverid ORDER BY A.validto desc) FROM OlapServers A WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE A.ProjectId = C.ProjectId AND C.Name = @project) ) DF ON T.TXObjectId = DF.TXObjectId AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM PrePostScripts A WHERE A.PrePostScriptId = T.PrePostScriptId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM PrePostScripts A WHERE A.PrePostScriptId = T.PrePostScriptId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'PrePostScripts' ,TxRepositoryTable = 'CustomSqlScripts' ,ObjectId = T.CustomSqlScriptId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath =CONCAT('/', PRJ.Name, '/', LayerName, '/', T.name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/ScriptActions') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM CustomSqlScripts WHERE CustomSqlScriptId = ''', T.CustomSqlScriptId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM CustomSqlScripts T INNER JOIN (SELECT RT = 2, TXObjectid =sqlserverconnectionid,B.Name, Layer = 'Date Warehouse', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY sqlserverconnectionid ORDER BY A.validto desc) FROM SqlServerConnections A INNER JOIN DataWarehouses B ON A.DataWarehouseId = B.DataWarehouseId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 3, DataSourceId,B.Name, Layer = 'Business Unit', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY DataSourceId ORDER BY A.validto desc) FROM DataSources A INNER JOIN BusinessUnits B ON A.BusinessUnitId = B.BusinessUnitId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 4, StagingDatabaseId,B.Name, Layer = 'Business Unit', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY StagingDatabaseId ORDER BY A.validto desc) FROM StagingDatabases A INNER JOIN BusinessUnits B ON A.BusinessUnitId = B.BusinessUnitId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 5, OlapServerId, A.Name, Layer = 'Olap', layername = A.Name, A.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY olapserverid ORDER BY A.validto desc) FROM OlapServers A WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE A.ProjectId = C.ProjectId AND C.Name = @project) ) DF ON (T.SqlServerConnectionId = DF.TXObjectId OR T.StagingDatabaseId = DF.TXObjectId OR T.OlapServerId = DF.TXObjectId) AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM CustomSqlScripts A WHERE T.CustomSqlScriptId = A.CustomSqlScriptId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT CustomSqlScriptId,StagingDatabaseId,SqlServerConnectionId,OlapServerId,Name,Script=CONVERT(nvarchar(max),Script),Locked,DataMartId FROM CustomSqlScripts A WHERE A.ValidTo = T.ValidFrom - 1 AND A.CustomSqlScriptId = T.CustomSqlScriptId EXCEPT SELECT CustomSqlScriptId,StagingDatabaseId,SqlServerConnectionId,OlapServerId,Name,Script=CONVERT(nvarchar(max),Script),Locked,DataMartId FROM CustomSqlScripts B WHERE B.ValidFrom = T.ValidFrom AND B.CustomSqlScriptId = T.CustomSqlScriptId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM CustomSqlScripts A WHERE A.CustomSqlScriptId = T.CustomSqlScriptId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'PrePostScripts' ,TxRepositoryTable = 'CustomSqlScripts' ,ObjectId = T.CustomSqlScriptId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath =CONCAT('/', PRJ.Name, '/', LayerName, '/', T.name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/ScriptActions') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM CustomSqlScripts WHERE CustomSqlScriptId = ''', T.CustomSqlScriptId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM CustomSqlScripts T INNER JOIN (SELECT RT = 2, TXObjectid =sqlserverconnectionid,B.Name, Layer = 'Date Warehouse', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY sqlserverconnectionid ORDER BY A.validto desc) FROM SqlServerConnections A INNER JOIN DataWarehouses B ON A.DataWarehouseId = B.DataWarehouseId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 3, DataSourceId,B.Name, Layer = 'Business Unit', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY DataSourceId ORDER BY A.validto desc) FROM DataSources A INNER JOIN BusinessUnits B ON A.BusinessUnitId = B.BusinessUnitId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 4, StagingDatabaseId,B.Name, Layer = 'Business Unit', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY StagingDatabaseId ORDER BY A.validto desc) FROM StagingDatabases A INNER JOIN BusinessUnits B ON A.BusinessUnitId = B.BusinessUnitId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 5, OlapServerId, A.Name, Layer = 'Olap', layername = A.Name, A.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY olapserverid ORDER BY A.validto desc) FROM OlapServers A WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE A.ProjectId = C.ProjectId AND C.Name = @project) ) DF ON (T.SqlServerConnectionId = DF.TXObjectId OR T.StagingDatabaseId = DF.TXObjectId OR T.OlapServerId = DF.TXObjectId) AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM CustomSqlScripts A WHERE A.CustomSqlScriptId = T.CustomSqlScriptId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM CustomSqlScripts A WHERE A.CustomSqlScriptId = T.CustomSqlScriptId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Stored Procedure' ,TxRepositoryTable = 'StoredProcedures' ,ObjectId = T.StoredProcedureId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath =CONCAT('/', PRJ.Name, '/', LayerName, '/', T.name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/StoredProcedure') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM StoredProcedures WHERE StoredProcedureId = ''', T.StoredProcedureId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM StoredProcedures T INNER JOIN (SELECT RT = 2, TXObjectid =sqlserverconnectionid,B.Name, Layer = 'Date Warehouse', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY sqlserverconnectionid ORDER BY A.validto desc) FROM SqlServerConnections A INNER JOIN DataWarehouses B ON A.DataWarehouseId = B.DataWarehouseId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 3, DataSourceId,B.Name, Layer = 'Business Unit', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY DataSourceId ORDER BY A.validto desc) FROM DataSources A INNER JOIN BusinessUnits B ON A.BusinessUnitId = B.BusinessUnitId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 4, StagingDatabaseId,B.Name, Layer = 'Business Unit', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY StagingDatabaseId ORDER BY A.validto desc) FROM StagingDatabases A INNER JOIN BusinessUnits B ON A.BusinessUnitId = B.BusinessUnitId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) ) DF ON (T.SqlServerConnectionId = DF.TXObjectId OR T.StagingDatabaseId = DF.TXObjectId) AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM StoredProcedures A WHERE T.StoredProcedureId = A.StoredProcedureId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT StoredProcedureId,StagingDatabaseId,SqlServerConnectionId,DBTargetSchemaId,Name,RoutineDefinition=CONVERT(nvarchar(max), RoutineDefinition),Encrypted,Locked FROM StoredProcedures A WHERE A.ValidTo = T.ValidFrom - 1 AND A.StoredProcedureId = T.StoredProcedureId EXCEPT SELECT StoredProcedureId,StagingDatabaseId,SqlServerConnectionId,DBTargetSchemaId,Name,RoutineDefinition=CONVERT(nvarchar(max), RoutineDefinition),Encrypted,Locked FROM StoredProcedures B WHERE B.ValidFrom = T.ValidFrom AND B.StoredProcedureId = T.StoredProcedureId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM StoredProcedures A WHERE A.StoredProcedureId = T.StoredProcedureId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Stored Procedure' ,TxRepositoryTable = 'StoredProcedures' ,ObjectId = T.StoredProcedureId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath =CONCAT('/', PRJ.Name, '/', LayerName, '/', T.name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/StoredProcedure') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM StoredProcedures WHERE StoredProcedureId = ''', T.StoredProcedureId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM StoredProcedures T INNER JOIN (SELECT RT = 2, TXObjectid =sqlserverconnectionid,B.Name, Layer = 'Date Warehouse', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY sqlserverconnectionid ORDER BY A.validto desc) FROM SqlServerConnections A INNER JOIN DataWarehouses B ON A.DataWarehouseId = B.DataWarehouseId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 3, DataSourceId,B.Name, Layer = 'Business Unit', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY DataSourceId ORDER BY A.validto desc) FROM DataSources A INNER JOIN BusinessUnits B ON A.BusinessUnitId = B.BusinessUnitId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 4, StagingDatabaseId,B.Name, Layer = 'Business Unit', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY StagingDatabaseId ORDER BY A.validto desc) FROM StagingDatabases A INNER JOIN BusinessUnits B ON A.BusinessUnitId = B.BusinessUnitId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) ) DF ON (T.SqlServerConnectionId = DF.TXObjectId OR T.StagingDatabaseId = DF.TXObjectId) AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM StoredProcedures A WHERE A.StoredProcedureId = T.StoredProcedureId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM StoredProcedures A WHERE A.StoredProcedureId = T.StoredProcedureId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'User Defined Function' ,TxRepositoryTable = 'UserDefinedFunctions' ,ObjectId = T.UserDefinedFunctionId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath =CONCAT('/', PRJ.Name, '/', LayerName, '/', T.name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/UserDefinedFunction') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM UserDefinedFunctions WHERE UserDefinedFunctionId = ''', T.UserDefinedFunctionId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM UserDefinedFunctions T INNER JOIN (SELECT RT = 2, TXObjectid =sqlserverconnectionid,B.Name, Layer = 'Date Warehouse', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY sqlserverconnectionid ORDER BY A.validto desc) FROM SqlServerConnections A INNER JOIN DataWarehouses B ON A.DataWarehouseId = B.DataWarehouseId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 3, DataSourceId,B.Name, Layer = 'Business Unit', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY DataSourceId ORDER BY A.validto desc) FROM DataSources A INNER JOIN BusinessUnits B ON A.BusinessUnitId = B.BusinessUnitId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 4, StagingDatabaseId,B.Name, Layer = 'Business Unit', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY StagingDatabaseId ORDER BY A.validto desc) FROM StagingDatabases A INNER JOIN BusinessUnits B ON A.BusinessUnitId = B.BusinessUnitId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) ) DF ON (T.SqlServerConnectionId = DF.TXObjectId OR T.StagingDatabaseId = DF.TXObjectId) AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM UserDefinedFunctions A WHERE T.UserDefinedFunctionId = A.UserDefinedFunctionId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT UserDefinedFunctionId,StagingDatabaseId,SqlServerConnectionId,DBTargetSchemaId,Name,RoutineDefinition=CONVERT(nvarchar(max), RoutineDefinition),Locked FROM UserDefinedFunctions A WHERE A.ValidTo = T.ValidFrom - 1 AND A.UserDefinedFunctionId = T.UserDefinedFunctionId EXCEPT SELECT UserDefinedFunctionId,StagingDatabaseId,SqlServerConnectionId,DBTargetSchemaId,Name,RoutineDefinition=CONVERT(nvarchar(max), RoutineDefinition),Locked FROM UserDefinedFunctions B WHERE B.ValidFrom = T.ValidFrom AND B.UserDefinedFunctionId = T.UserDefinedFunctionId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM UserDefinedFunctions A WHERE A.UserDefinedFunctionId = T.UserDefinedFunctionId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'User Defined Function' ,TxRepositoryTable = 'UserDefinedFunctions' ,ObjectId = T.UserDefinedFunctionId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath =CONCAT('/', PRJ.Name, '/', LayerName, '/', T.name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/UserDefinedFunction') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM UserDefinedFunctions WHERE UserDefinedFunctionId = ''', T.UserDefinedFunctionId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM UserDefinedFunctions T INNER JOIN (SELECT RT = 2, TXObjectid =sqlserverconnectionid,B.Name, Layer = 'Date Warehouse', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY sqlserverconnectionid ORDER BY A.validto desc) FROM SqlServerConnections A INNER JOIN DataWarehouses B ON A.DataWarehouseId = B.DataWarehouseId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 3, DataSourceId,B.Name, Layer = 'Business Unit', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY DataSourceId ORDER BY A.validto desc) FROM DataSources A INNER JOIN BusinessUnits B ON A.BusinessUnitId = B.BusinessUnitId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) UNION ALL SELECT 4, StagingDatabaseId,B.Name, Layer = 'Business Unit', layername = B.Name, B.ProjectId ,RN = ROW_NUMBER() OVER(PARTITION BY StagingDatabaseId ORDER BY A.validto desc) FROM StagingDatabases A INNER JOIN BusinessUnits B ON A.BusinessUnitId = B.BusinessUnitId AND A.ValidFrom = B.ValidFrom WHERE EXISTS (SELECT TOP 1 1 FROM Projects C WHERE B.ProjectId = C.ProjectId AND C.Name = @project) ) DF ON (T.SqlServerConnectionId = DF.TXObjectId OR T.StagingDatabaseId = DF.TXObjectId) AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM UserDefinedFunctions A WHERE A.UserDefinedFunctionId = T.UserDefinedFunctionId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM UserDefinedFunctions A WHERE A.UserDefinedFunctionId = T.UserDefinedFunctionId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Selection Rule' ,TxRepositoryTable = 'SelectionRules' ,ObjectId = T.SelectionRuleId ,ObjectName = TableName ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/SelectionRule') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SelectionRules WHERE SelectionRuleId = ''', T.SelectionRuleId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM SelectionRules T INNER JOIN (SELECT TableName = DF.Name, DF.DataTableId, Layer, LayerName, projectid FROM #datatables Df WHERE rn = 1 ) DF ON T.DataTableId = DF.DataTableId INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SelectionRules A WHERE T.SelectionRuleId = A.SelectionRuleId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SelectionRuleId,DataTableId,DataWarehouseMappingTableId,SeqNo,Locked FROM SelectionRules A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SelectionRuleId = T.SelectionRuleId EXCEPT SELECT SelectionRuleId,DataTableId,DataWarehouseMappingTableId,SeqNo,Locked FROM SelectionRules B WHERE B.ValidFrom = T.ValidFrom AND B.SelectionRuleId = T.SelectionRuleId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SelectionRules A WHERE A.SelectionRuleId = T.SelectionRuleId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Selection Rule' ,TxRepositoryTable = 'SelectionRules' ,ObjectId = T.SelectionRuleId ,ObjectName = TableName ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/SelectionRule') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SelectionRules WHERE SelectionRuleId = ''', T.SelectionRuleId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM SelectionRules T INNER JOIN (SELECT TableName = DF.Name, DF.DataTableId, Layer, LayerName, projectid FROM #datatables Df WHERE rn = 1 ) DF ON T.DataTableId = DF.DataTableId INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SelectionRules A WHERE A.SelectionRuleId = T.SelectionRuleId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SelectionRules A WHERE A.SelectionRuleId = T.SelectionRuleId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Selection Rule' ,TxRepositoryTable = 'SelectionRuleFields' ,ObjectId = T.SelectionRuleFieldId ,ObjectName = ISNULL(PrimaryField.Name, RuleType) ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/SelectionRuleField') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SelectionRuleFields WHERE SelectionRuleFieldId = ''', T.SelectionRuleFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.SelectionRuleId, selectionRuleName = '', DF.DataTableId, tableName = name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.SelectionRuleId order by T.validto desc) FROM #DataTables Df INNER JOIN SelectionRules T ON T.DataTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN SelectionRuleFields T ON T.SelectionRuleId = DF.SelectionRuleId AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SelectionRuleFields A WHERE T.SelectionRuleFieldId = A.SelectionRuleFieldId) OA_MinVals LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SelectionRuleFieldId,SelectionRuleId,DataFieldId,SeqNo,RuleType,RuleValue,Locked FROM SelectionRuleFields A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SelectionRuleFieldId = T.SelectionRuleFieldId EXCEPT SELECT SelectionRuleFieldId,SelectionRuleId,DataFieldId,SeqNo,RuleType,RuleValue,Locked FROM SelectionRuleFields B WHERE B.ValidFrom = T.ValidFrom AND B.SelectionRuleFieldId = T.SelectionRuleFieldId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SelectionRuleFields A WHERE A.SelectionRuleFieldId = T.SelectionRuleFieldId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Selection Rule' ,TxRepositoryTable = 'SelectionRuleFields' ,ObjectId = T.SelectionRuleFieldId ,ObjectName = ISNULL(PrimaryField.Name, RuleType) ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/SelectionRuleField') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SelectionRuleFields WHERE SelectionRuleFieldId = ''', T.SelectionRuleFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.SelectionRuleId, selectionRuleName = '', DF.DataTableId, tableName = name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.SelectionRuleId order by T.validto desc) FROM #DataTables Df INNER JOIN SelectionRules T ON T.DataTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN SelectionRuleFields T ON T.SelectionRuleId = DF.SelectionRuleId AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SelectionRuleFields A WHERE A.SelectionRuleFieldId = T.SelectionRuleFieldId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SelectionRuleFields A WHERE A.SelectionRuleFieldId = T.SelectionRuleFieldId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Custom Data' ,TxRepositoryTable = 'CustomDataTables' ,ObjectId = T.CustomDataTableId ,ObjectName = TableName ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/CustomData') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM CustomDataTables WHERE CustomDataTableId = ''', T.CustomDataTableId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM CustomDataTables T INNER JOIN (SELECT TableName = DF.Name, DF.DataTableId, Layer, LayerName, projectid FROM #datatables Df WHERE rn = 1 ) DF ON T.DataTableId = DF.DataTableId INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM CustomDataTables A WHERE T.CustomDataTableId = A.CustomDataTableId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT CustomDataTableId,DataTableId,CustomData=CONVERT(nvarchar(max),CustomData) FROM CustomDataTables A WHERE A.ValidTo = T.ValidFrom - 1 AND A.CustomDataTableId = T.CustomDataTableId EXCEPT SELECT CustomDataTableId,DataTableId,CustomData=CONVERT(nvarchar(max),CustomData) FROM CustomDataTables B WHERE B.ValidFrom = T.ValidFrom AND B.CustomDataTableId = T.CustomDataTableId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM CustomDataTables A WHERE A.CustomDataTableId = T.CustomDataTableId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Custom Data' ,TxRepositoryTable = 'CustomDataTables' ,ObjectId = T.CustomDataTableId ,ObjectName = TableName ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/CustomData') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM CustomDataTables WHERE CustomDataTableId = ''', T.CustomDataTableId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM CustomDataTables T INNER JOIN (SELECT TableName = DF.Name, DF.DataTableId, Layer, LayerName, projectid FROM #datatables Df WHERE rn = 1 ) DF ON T.DataTableId = DF.DataTableId INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM CustomDataTables A WHERE A.CustomDataTableId = T.CustomDataTableId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM CustomDataTables A WHERE A.CustomDataTableId = T.CustomDataTableId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Table Inserts' ,TxRepositoryTable = 'TableInserts' ,ObjectId = T.TableInsertId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', T.name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/TableInsert') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TableInserts WHERE TableInsertId = ''', T.TableInsertId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM TableInserts T INNER JOIN (SELECT TableName = DF.Name, DF.DataTableId, Layer, LayerName, projectid FROM #datatables Df WHERE rn = 1 ) DF ON T.DestinationTableId = DF.DataTableId INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM TableInserts A WHERE T.TableInsertId = A.TableInsertId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT TableInsertId,DestinationTableId,SourceTableId,Name,DestinationTableType,ExecutionNumber FROM TableInserts A WHERE A.ValidTo = T.ValidFrom - 1 AND A.TableInsertId = T.TableInsertId EXCEPT SELECT TableInsertId,DestinationTableId,SourceTableId,Name,DestinationTableType,ExecutionNumber FROM TableInserts B WHERE B.ValidFrom = T.ValidFrom AND B.TableInsertId = T.TableInsertId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM TableInserts A WHERE A.TableInsertId = T.TableInsertId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Table Inserts' ,TxRepositoryTable = 'TableInserts' ,ObjectId = T.TableInsertId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', T.name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/TableInsert') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TableInserts WHERE TableInsertId = ''', T.TableInsertId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM TableInserts T INNER JOIN (SELECT TableName = DF.Name, DF.DataTableId, Layer, LayerName, projectid FROM #datatables Df WHERE rn = 1 ) DF ON T.DestinationTableId = DF.DataTableId INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM TableInserts A WHERE A.TableInsertId = T.TableInsertId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM TableInserts A WHERE A.TableInsertId = T.TableInsertId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Table Inserts' ,TxRepositoryTable = 'TableInsertMappings' ,ObjectId = T.TableInsertMappingId ,ObjectName = PrimaryField.name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DF.name, '/', PrimaryField.name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/TableInsert/Field') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TableInsertMappings WHERE TableInsertMappingId = ''', T.TableInsertMappingId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.TableInsertId, name = T.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.tableInsertId order by T.validto desc) FROM #DataTables Df INNER JOIN TableInserts T ON T.DestinationTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN TableInsertMappings T ON T.TableInsertId = DF.TableInsertId AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM TableInsertMappings A WHERE T.TableInsertMappingId = A.TableInsertMappingId) OA_MinVals LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DestinationFieldId WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT TableInsertMappingId,TableInsertId,DestinationFieldId,SourceFieldId,FixedValue,DefaultValue FROM TableInsertMappings A WHERE A.ValidTo = T.ValidFrom - 1 AND A.TableInsertMappingId = T.TableInsertMappingId EXCEPT SELECT TableInsertMappingId,TableInsertId,DestinationFieldId,SourceFieldId,FixedValue,DefaultValue FROM TableInsertMappings B WHERE B.ValidFrom = T.ValidFrom AND B.TableInsertMappingId = T.TableInsertMappingId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM TableInsertMappings A WHERE A.TableInsertMappingId = T.TableInsertMappingId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Table Inserts' ,TxRepositoryTable = 'TableInsertMappings' ,ObjectId = T.TableInsertMappingId ,ObjectName = PrimaryField.name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DF.name, '/', PrimaryField.name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/TableInsert/Field') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TableInsertMappings WHERE TableInsertMappingId = ''', T.TableInsertMappingId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.TableInsertId, name = T.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.tableInsertId order by T.validto desc) FROM #DataTables Df INNER JOIN TableInserts T ON T.DestinationTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN TableInsertMappings T ON T.TableInsertId = DF.TableInsertId AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DestinationFieldId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM TableInsertMappings A WHERE A.TableInsertMappingId = T.TableInsertMappingId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM TableInsertMappings A WHERE A.TableInsertMappingId = T.TableInsertMappingId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Table Inserts' ,TxRepositoryTable = 'TableInsertScripts' ,ObjectId = T.TableInsertScriptId ,ObjectName = 'script' ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DF.name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/TableInsert/Scrpt') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TableInsertScripts WHERE TableInsertScriptId = ''', T.TableInsertScriptId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.TableInsertId, name = T.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.tableInsertId order by T.validto desc) FROM #DataTables Df INNER JOIN TableInserts T ON T.DestinationTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN TableInsertScripts T ON T.TableInsertId = DF.TableInsertId AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM TableInsertScripts A WHERE T.TableInsertScriptId = A.TableInsertScriptId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT TableInsertScriptId,TableInsertId,Script=CONVERT(NVARCHAR(MAX), Script) FROM TableInsertScripts A WHERE A.ValidTo = T.ValidFrom - 1 AND A.TableInsertScriptId = T.TableInsertScriptId EXCEPT SELECT TableInsertScriptId,TableInsertId,Script=CONVERT(NVARCHAR(MAX), Script) FROM TableInsertScripts B WHERE B.ValidFrom = T.ValidFrom AND B.TableInsertScriptId = T.TableInsertScriptId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM TableInsertScripts A WHERE A.TableInsertScriptId = T.TableInsertScriptId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Table Inserts' ,TxRepositoryTable = 'TableInsertScripts' ,ObjectId = T.TableInsertScriptId ,ObjectName = 'script' ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DF.name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/TableInsert/Scrpt') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TableInsertScripts WHERE TableInsertScriptId = ''', T.TableInsertScriptId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.TableInsertId, name = T.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.tableInsertId order by T.validto desc) FROM #DataTables Df INNER JOIN TableInserts T ON T.DestinationTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN TableInsertScripts T ON T.TableInsertId = DF.TableInsertId AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM TableInsertScripts A WHERE A.TableInsertScriptId = T.TableInsertScriptId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM TableInsertScripts A WHERE A.TableInsertScriptId = T.TableInsertScriptId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Table Inserts' ,TxRepositoryTable = 'TableInsertSelectionRules' ,ObjectId = T.TableInsertSelectionRuleId ,ObjectName = 'selection rule' ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DF.name, '/selectionrule') ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/TableInsert/selectionrule') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TableInsertSelectionRules WHERE TableInsertSelectionRuleId = ''', T.TableInsertSelectionRuleId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.TableInsertId, name = T.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.tableInsertId order by T.validto desc) FROM #DataTables Df INNER JOIN TableInserts T ON T.DestinationTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN TableInsertSelectionRules T ON T.TableInsertId = DF.TableInsertId AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM TableInsertSelectionRules A WHERE T.TableInsertSelectionRuleId = A.TableInsertSelectionRuleId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT TableInsertSelectionRuleId,TableInsertId,ExecutionNumber FROM TableInsertSelectionRules A WHERE A.ValidTo = T.ValidFrom - 1 AND A.TableInsertSelectionRuleId = T.TableInsertSelectionRuleId EXCEPT SELECT TableInsertSelectionRuleId,TableInsertId,ExecutionNumber FROM TableInsertSelectionRules B WHERE B.ValidFrom = T.ValidFrom AND B.TableInsertSelectionRuleId = T.TableInsertSelectionRuleId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM TableInsertSelectionRules A WHERE A.TableInsertSelectionRuleId = T.TableInsertSelectionRuleId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Table Inserts' ,TxRepositoryTable = 'TableInsertSelectionRules' ,ObjectId = T.TableInsertSelectionRuleId ,ObjectName = 'selection rule' ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', DF.name, '/selectionrule') ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/TableInsert/selectionrule') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TableInsertSelectionRules WHERE TableInsertSelectionRuleId = ''', T.TableInsertSelectionRuleId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM (SELECT T.TableInsertId, name = T.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.tableInsertId order by T.validto desc) FROM #DataTables Df INNER JOIN TableInserts T ON T.DestinationTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN TableInsertSelectionRules T ON T.TableInsertId = DF.TableInsertId AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM TableInsertSelectionRules A WHERE A.TableInsertSelectionRuleId = T.TableInsertSelectionRuleId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM TableInsertSelectionRules A WHERE A.TableInsertSelectionRuleId = T.TableInsertSelectionRuleId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Table Inserts' ,TxRepositoryTable = 'TableInsertSelectionRuleFields' ,ObjectId = T.TableInsertSelectionRuleFieldId ,ObjectName = ISNUll(PrimaryField.name, CONCAT(Ruletype, ' ', value)) ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', TableInsertName, '/selectionrule/', ISNUll(PrimaryField.name, CONCAT(Ruletype, ' ', value))) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/TableInsert/selectionrule/Field') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TableInsertSelectionRuleFields WHERE TableInsertSelectionRuleFieldId = ''', T.TableInsertSelectionRuleFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT T.TableInsertSelectionRuleId, TableInsertName = DF.name, DataTableId, tablename, layer, layername, projectid, rn = ROW_NUMBER() over (partition by T.TableInsertSelectionRuleId order by T.validto desc) FROM (SELECT T.TableInsertId, name = T.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.tableInsertId order by T.validto desc) FROM #DataTables Df INNER JOIN TableInserts T ON T.DestinationTableId = DF.DataTableId WHERE DF.rn = 1) DF INNER JOIN TableInsertSelectionRules T ON T.TableInsertId = DF.TableInsertId WHERE DF.rn = 1 ) DF INNER JOIN TableInsertSelectionRuleFields T ON T.TableInsertSelectionRuleId = DF.TableInsertSelectionRuleId AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM TableInsertSelectionRuleFields A WHERE T.TableInsertSelectionRuleFieldId = A.TableInsertSelectionRuleFieldId) OA_MinVals LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT TableInsertSelectionRuleFieldId,TableInsertSelectionRuleId,DataFieldId,RuleType,Value FROM TableInsertSelectionRuleFields A WHERE A.ValidTo = T.ValidFrom - 1 AND A.TableInsertSelectionRuleFieldId = T.TableInsertSelectionRuleFieldId EXCEPT SELECT TableInsertSelectionRuleFieldId,TableInsertSelectionRuleId,DataFieldId,RuleType,Value FROM TableInsertSelectionRuleFields B WHERE B.ValidFrom = T.ValidFrom AND B.TableInsertSelectionRuleFieldId = T.TableInsertSelectionRuleFieldId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM TableInsertSelectionRuleFields A WHERE A.TableInsertSelectionRuleFieldId = T.TableInsertSelectionRuleFieldId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Table Inserts' ,TxRepositoryTable = 'TableInsertSelectionRuleFields' ,ObjectId = T.TableInsertSelectionRuleFieldId ,ObjectName = ISNUll(PrimaryField.name, CONCAT(Ruletype, ' ', value)) ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', TableInsertName, '/selectionrule/', ISNUll(PrimaryField.name, CONCAT(Ruletype, ': ', value))) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/TableInsert/selectionrule/Field') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM TableInsertSelectionRuleFields WHERE TableInsertSelectionRuleFieldId = ''', T.TableInsertSelectionRuleFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT T.TableInsertSelectionRuleId, TableInsertName = DF.name, DataTableId, tablename, layer, layername, projectid, rn = ROW_NUMBER() over (partition by T.TableInsertSelectionRuleId order by T.validto desc) FROM (SELECT T.TableInsertId, name = T.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.tableInsertId order by T.validto desc) FROM #DataTables Df INNER JOIN TableInserts T ON T.DestinationTableId = DF.DataTableId WHERE DF.rn = 1) DF INNER JOIN TableInsertSelectionRules T ON T.TableInsertId = DF.TableInsertId WHERE DF.rn = 1 ) DF INNER JOIN TableInsertSelectionRuleFields T ON T.TableInsertSelectionRuleId = DF.TableInsertSelectionRuleId AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM TableInsertSelectionRuleFields A WHERE A.TableInsertSelectionRuleFieldId = T.TableInsertSelectionRuleFieldId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM TableInsertSelectionRuleFields A WHERE A.TableInsertSelectionRuleFieldId = T.TableInsertSelectionRuleFieldId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Related Table Insert' ,TxRepositoryTable = 'RelatedRecords' ,ObjectId = T.RelatedRecordId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/RelatedTable') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM RelatedRecords WHERE RelatedRecordId = ''', T.RelatedRecordId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid FROM #DataTables Df where rn = 1 ) DF INNER JOIN RelatedRecords T ON T.DataManTableId = DF.DataTableId INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM RelatedRecords A WHERE T.RelatedRecordId = A.RelatedRecordId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT RelatedRecordId,DataManTableId,LookupTableId,Name,DataDestinationTable,RecordCondition,ExecutionNo FROM RelatedRecords A WHERE A.ValidTo = T.ValidFrom - 1 AND A.RelatedRecordId = T.RelatedRecordId EXCEPT SELECT RelatedRecordId,DataManTableId,LookupTableId,Name,DataDestinationTable,RecordCondition,ExecutionNo FROM RelatedRecords B WHERE B.ValidFrom = T.ValidFrom AND B.RelatedRecordId = T.RelatedRecordId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM RelatedRecords A WHERE A.RelatedRecordId = T.RelatedRecordId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Related Table Insert' ,TxRepositoryTable = 'RelatedRecords' ,ObjectId = T.RelatedRecordId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/RelatedTable') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM RelatedRecords WHERE RelatedRecordId = ''', T.RelatedRecordId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid FROM #DataTables Df where rn = 1 ) DF INNER JOIN RelatedRecords T ON T.DataManTableId = DF.DataTableId INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM RelatedRecords A WHERE A.RelatedRecordId = T.RelatedRecordId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM RelatedRecords A WHERE A.RelatedRecordId = T.RelatedRecordId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Related Table Insert' ,TxRepositoryTable = 'RelatedRecordMappingFields' ,ObjectId = T.RelatedRecordMappingFieldId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', RelatedRecordName, '/', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/RelatedTable/Field') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM RelatedRecordMappingFields WHERE RelatedRecordMappingFieldId = ''', T.RelatedRecordMappingFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT T.RelatedRecordId, RelatedRecordName = T.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.RelatedRecordId order by T.validto desc) FROM #DataTables Df INNER JOIN RelatedRecords T ON T.DataManTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN RelatedRecordMappingFields T ON T.RelatedRecordId = DF.RelatedRecordId AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM RelatedRecordMappingFields A WHERE T.RelatedRecordMappingFieldId = A.RelatedRecordMappingFieldId) OA_MinVals LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT RelatedRecordMappingFieldId,RelatedRecordId,DataFieldId,MappedDataFieldId,DefaultValue,FixedValue FROM RelatedRecordMappingFields A WHERE A.ValidTo = T.ValidFrom - 1 AND A.RelatedRecordMappingFieldId = T.RelatedRecordMappingFieldId EXCEPT SELECT RelatedRecordMappingFieldId,RelatedRecordId,DataFieldId,MappedDataFieldId,DefaultValue,FixedValue FROM RelatedRecordMappingFields B WHERE B.ValidFrom = T.ValidFrom AND B.RelatedRecordMappingFieldId = T.RelatedRecordMappingFieldId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM RelatedRecordMappingFields A WHERE A.RelatedRecordMappingFieldId = T.RelatedRecordMappingFieldId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Related Table Insert' ,TxRepositoryTable = 'RelatedRecordMappingFields' ,ObjectId = T.RelatedRecordMappingFieldId ,ObjectName = PrimaryField.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', RelatedRecordName, '/', PrimaryField.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/RelatedTable/Field') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM RelatedRecordMappingFields WHERE RelatedRecordMappingFieldId = ''', T.RelatedRecordMappingFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT T.RelatedRecordId, RelatedRecordName = T.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.RelatedRecordId order by T.validto desc) FROM #DataTables Df INNER JOIN RelatedRecords T ON T.DataManTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN RelatedRecordMappingFields T ON T.RelatedRecordId = DF.RelatedRecordId AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM RelatedRecordMappingFields A WHERE A.RelatedRecordMappingFieldId = T.RelatedRecordMappingFieldId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM RelatedRecordMappingFields A WHERE A.RelatedRecordMappingFieldId = T.RelatedRecordMappingFieldId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Related Table Insert' ,TxRepositoryTable = 'RelatedRecordRelations' ,ObjectId = T.RelatedRecordRelationId ,ObjectName = 'Relation' ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', RelatedRecordName, '/Relation') ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/RelatedTable/Relation') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM RelatedRecordRelations WHERE RelatedRecordRelationId = ''', T.RelatedRecordRelationId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT T.RelatedRecordId, RelatedRecordName = T.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.RelatedRecordId order by T.validto desc) FROM #DataTables Df INNER JOIN RelatedRecords T ON T.DataManTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN RelatedRecordRelations T ON T.RelatedRecordId = DF.RelatedRecordId AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM RelatedRecordRelations A WHERE T.RelatedRecordRelationId = A.RelatedRecordRelationId) OA_MinVals LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT RelatedRecordRelationId,RelatedRecordId,DataFieldId,RelatedDataFieldId,ConditionType,FixedValue,ExecutionNo FROM RelatedRecordRelations A WHERE A.ValidTo = T.ValidFrom - 1 AND A.RelatedRecordRelationId = T.RelatedRecordRelationId EXCEPT SELECT RelatedRecordRelationId,RelatedRecordId,DataFieldId,RelatedDataFieldId,ConditionType,FixedValue,ExecutionNo FROM RelatedRecordRelations B WHERE B.ValidFrom = T.ValidFrom AND B.RelatedRecordRelationId = T.RelatedRecordRelationId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM RelatedRecordRelations A WHERE A.RelatedRecordRelationId = T.RelatedRecordRelationId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Related Table Insert' ,TxRepositoryTable = 'RelatedRecordRelations' ,ObjectId = T.RelatedRecordRelationId ,ObjectName = 'Relation' ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/', RelatedRecordName, '/Relation') ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/RelatedTable/Relation') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM RelatedRecordRelations WHERE RelatedRecordRelationId = ''', T.RelatedRecordRelationId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT T.RelatedRecordId, RelatedRecordName = T.Name, DF.DataTableId, tableName = Df.Name, Layer, LayerName, projectid, rn = ROW_NUMBER() over (partition by T.RelatedRecordId order by T.validto desc) FROM #DataTables Df INNER JOIN RelatedRecords T ON T.DataManTableId = DF.DataTableId WHERE DF.rn = 1 ) DF INNER JOIN RelatedRecordRelations T ON T.RelatedRecordId = DF.RelatedRecordId AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId LEFT OUTER JOIN (SELECT DISTINCT D.DataFieldId, D.Name from #datafields D) PrimaryField ON PrimaryField.DataFieldId = T.DataFieldId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM RelatedRecordRelations A WHERE A.RelatedRecordRelationId = T.RelatedRecordRelationId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM RelatedRecordRelations A WHERE A.RelatedRecordRelationId = T.RelatedRecordRelationId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Data Movement' ,TxRepositoryTable = 'DataMovementRelations' ,ObjectId = T.DataMovementRelationId ,ObjectName = DF.DataFieldName ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/',DataFieldName) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Field') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM DataMovementRelations WHERE DataMovementRelationId = ''', T.DataMovementRelationId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT DataFieldId, DataFieldName = Df.Name, DF.DataTableId, tableName, Layer, LayerName, projectid, rn FROM #DataFields Df WHERE DF.rn = 1 ) DF INNER JOIN DataMovementRelations T ON T.DestinationDataFieldId = DF.DataFieldId AND DF.rn = 1 INNER JOIN Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM DataMovementRelations A WHERE T.DataMovementRelationId = A.DataMovementRelationId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT DataMovementRelationId,ProjectId,SourceDataFieldId,DestinationDataFieldId,MovementType,Locked FROM DataMovementRelations A WHERE A.ValidTo = T.ValidFrom - 1 AND A.DataMovementRelationId = T.DataMovementRelationId EXCEPT SELECT DataMovementRelationId,ProjectId,SourceDataFieldId,DestinationDataFieldId,MovementType,Locked FROM DataMovementRelations B WHERE B.ValidFrom = T.ValidFrom AND B.DataMovementRelationId = T.DataMovementRelationId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM DataMovementRelations A WHERE A.DataMovementRelationId = T.DataMovementRelationId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Data Movement' ,TxRepositoryTable = 'DataMovementRelations' ,ObjectId = T.DataMovementRelationId ,ObjectName = DF.DataFieldName ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', tableName, '/',DataFieldName) ,ObjectPathAbstract = CONCAT('/project/', layer, '/Table/Field') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM DataMovementRelations WHERE DataMovementRelationId = ''', T.DataMovementRelationId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT DataFieldId, DataFieldName = Df.Name, DF.DataTableId, tableName, Layer, LayerName, projectid, rn FROM #DataFields Df WHERE DF.rn = 1 ) DF INNER JOIN DataMovementRelations T ON T.DestinationDataFieldId = DF.DataFieldId AND DF.rn = 1 INNER Join Projects PRJ ON DF.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM DataMovementRelations A WHERE A.DataMovementRelationId = T.DataMovementRelationId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM DataMovementRelations A WHERE A.DataMovementRelationId = T.DataMovementRelationId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer' ,TxRepositoryTable = 'SemanticLayerModels' ,ObjectId = T.SemanticLayerModelId ,ObjectName = T.Name ,ObjectLayer = 'Semantic' + ':' + T.Name ,ObjectPath = CONCAT('/', PRJ.Name, '/', T.name) ,ObjectPathAbstract = CONCAT('/project/semantic','') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerModels WHERE SemanticLayerModelId = ''', T.SemanticLayerModelId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM SemanticLayerModels T INNER JOIN Projects PRJ ON T.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerModels A WHERE T.SemanticLayerModelId = A.SemanticLayerModelId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerModelId,ProjectId,Name,EndpointTypesEnabled,ExecutionNumber FROM SemanticLayerModels A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerModelId = T.SemanticLayerModelId EXCEPT SELECT SemanticLayerModelId,ProjectId,Name,EndpointTypesEnabled,ExecutionNumber FROM SemanticLayerModels B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerModelId = T.SemanticLayerModelId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerModels A WHERE A.SemanticLayerModelId = T.SemanticLayerModelId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer' ,TxRepositoryTable = 'SemanticLayerModels' ,ObjectId = T.SemanticLayerModelId ,ObjectName = T.Name ,ObjectLayer = 'Semantic' + ':' + T.Name ,ObjectPath = CONCAT('/', PRJ.Name, '/', T.name) ,ObjectPathAbstract = CONCAT('/project/semantic','') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerModels WHERE SemanticLayerModelId = ''', T.SemanticLayerModelId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM SemanticLayerModels T INNER Join Projects PRJ ON T.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerModels A WHERE A.SemanticLayerModelId = T.SemanticLayerModelId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerModels A WHERE A.SemanticLayerModelId = T.SemanticLayerModelId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer End Point' ,TxRepositoryTable = 'SemanticLayerEndpoints' ,ObjectId = T.SemanticLayerEndpointId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/EndPoint') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerEndpoints WHERE SemanticLayerEndpointId = ''', T.SemanticLayerEndpointId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY T.SemanticLayerModelId ORDER BY T.ValidTo DESC) FROM SemanticLayerModels T ) D INNER JOIN SemanticLayerEndpoints T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerEndpoints A WHERE T.SemanticLayerEndpointId = A.SemanticLayerEndpointId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerEndpointId,SemanticLayerModelId,Name,GlobalEndpointId,ExecutionNumber FROM SemanticLayerEndpoints A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerEndpointId = T.SemanticLayerEndpointId EXCEPT SELECT SemanticLayerEndpointId,SemanticLayerModelId,Name,GlobalEndpointId,ExecutionNumber FROM SemanticLayerEndpoints B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerEndpointId = T.SemanticLayerEndpointId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerEndpoints A WHERE A.SemanticLayerEndpointId = T.SemanticLayerEndpointId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer End Point' ,TxRepositoryTable = 'SemanticLayerEndpoints' ,ObjectId = T.SemanticLayerEndpointId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/EndPoint') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerEndpoints WHERE SemanticLayerEndpointId = ''', T.SemanticLayerEndpointId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY T.SemanticLayerModelId ORDER BY T.ValidTo DESC) FROM SemanticLayerModels T ) D INNER JOIN SemanticLayerEndpoints T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerEndpoints A WHERE A.SemanticLayerEndpointId = T.SemanticLayerEndpointId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerEndpoints A WHERE A.SemanticLayerEndpointId = T.SemanticLayerEndpointId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer End Point' ,TxRepositoryTable = 'SemanticLayerTabularEndpoints' ,ObjectId = T.SemanticLayerTabularEndpointId ,ObjectName = 'settings' ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', EndPointName, '/<>') ,ObjectPathAbstract = CONCAT('/project/', layer, '/EndPoint/<>') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerTabularEndpoints WHERE SemanticLayerTabularEndpointId = ''', T.SemanticLayerTabularEndpointId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, T.EndPointName,T.RN, T.SemanticLayerEndpointId FROM (SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels T) D INNER JOIN (SELECT EndPointName = Name, SemanticLayerEndpointId, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerEndpointId ORDER BY ValidTo DESC) FROM SemanticLayerEndpoints) T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId ) D INNER JOIN SemanticLayerTabularEndpoints T ON D.RN = 1 AND T.SemanticLayerTabularEndpointId = D.SemanticLayerEndpointId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerTabularEndpoints A WHERE T.SemanticLayerTabularEndpointId = A.SemanticLayerTabularEndpointId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerTabularEndpointId,Server,"Database",DeploymentTarget,ProcessModel,ProcessImpersonationMode,UserName,Password,CompatibilityLevel,ServerUsername,ServerPassword,UseServerLogin FROM SemanticLayerTabularEndpoints A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerTabularEndpointId = T.SemanticLayerTabularEndpointId EXCEPT SELECT SemanticLayerTabularEndpointId,Server,"Database",DeploymentTarget,ProcessModel,ProcessImpersonationMode,UserName,Password,CompatibilityLevel,ServerUsername,ServerPassword,UseServerLogin FROM SemanticLayerTabularEndpoints B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerTabularEndpointId = T.SemanticLayerTabularEndpointId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerTabularEndpoints A WHERE A.SemanticLayerTabularEndpointId = T.SemanticLayerTabularEndpointId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer End Point' ,TxRepositoryTable = 'SemanticLayerTabularEndpoints' ,ObjectId = T.SemanticLayerTabularEndpointId ,ObjectName = 'settings' ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', EndPointName, '/<>') ,ObjectPathAbstract = CONCAT('/project/', layer, '/EndPoint/<>') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerTabularEndpoints WHERE SemanticLayerTabularEndpointId = ''', T.SemanticLayerTabularEndpointId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, T.EndPointName,T.RN, T.SemanticLayerEndpointId FROM (SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels T) D INNER JOIN (SELECT EndPointName = Name, SemanticLayerEndpointId, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerEndpointId ORDER BY ValidTo DESC) FROM SemanticLayerEndpoints) T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId ) D INNER JOIN SemanticLayerTabularEndpoints T ON D.RN = 1 AND T.SemanticLayerTabularEndpointId = D.SemanticLayerEndpointId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerTabularEndpoints A WHERE A.SemanticLayerTabularEndpointId = T.SemanticLayerTabularEndpointId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerTabularEndpoints A WHERE A.SemanticLayerTabularEndpointId = T.SemanticLayerTabularEndpointId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Tables' ,TxRepositoryTable = 'SemanticLayerTables' ,ObjectId = T.SemanticLayerTableId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerTables WHERE SemanticLayerTableId = ''', T.SemanticLayerTableId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY T.SemanticLayerModelId ORDER BY T.ValidTo DESC) FROM SemanticLayerModels T ) D INNER JOIN SemanticLayerTables T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerTables A WHERE T.SemanticLayerTableId = A.SemanticLayerTableId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerTableId,SemanticLayerModelId,TableId,Name,TableCategory,Description,ExecutionNumber FROM SemanticLayerTables A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerTableId = T.SemanticLayerTableId EXCEPT SELECT SemanticLayerTableId,SemanticLayerModelId,TableId,Name,TableCategory,Description,ExecutionNumber FROM SemanticLayerTables B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerTableId = T.SemanticLayerTableId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerTables A WHERE A.SemanticLayerTableId = T.SemanticLayerTableId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Tables' ,TxRepositoryTable = 'SemanticLayerTables' ,ObjectId = T.SemanticLayerTableId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerTables WHERE SemanticLayerTableId = ''', T.SemanticLayerTableId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY T.SemanticLayerModelId ORDER BY T.ValidTo DESC) FROM SemanticLayerModels T ) D INNER JOIN SemanticLayerTables T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerTables A WHERE A.SemanticLayerTableId = T.SemanticLayerTableId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerTables A WHERE A.SemanticLayerTableId = T.SemanticLayerTableId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Field' ,TxRepositoryTable = 'SemanticLayerFields' ,ObjectId = T.SemanticLayerFieldId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', TableName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/fields') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerFields WHERE SemanticLayerFieldId = ''', T.SemanticLayerFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #SemanticLayerFields T INNER JOIN Projects PRJ ON T.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerFields A WHERE T.SemanticLayerFieldId = A.SemanticLayerFieldId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerFieldId,SemanticLayerTableId,FieldId,Name,DataType,FieldType,Description,ExecutionNumber,Hidden,SortByFieldId FROM SemanticLayerFields A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerFieldId = T.SemanticLayerFieldId EXCEPT SELECT SemanticLayerFieldId,SemanticLayerTableId,FieldId,Name,DataType,FieldType,Description,ExecutionNumber,Hidden,SortByFieldId FROM SemanticLayerFields B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerFieldId = T.SemanticLayerFieldId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerFields A WHERE A.SemanticLayerFieldId = T.SemanticLayerFieldId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Field' ,TxRepositoryTable = 'SemanticLayerFields' ,ObjectId = T.SemanticLayerFieldId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', TableName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/fields') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerFields WHERE SemanticLayerFieldId = ''', T.SemanticLayerFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #SemanticLayerFields T INNER JOIN Projects PRJ ON T.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerFields A WHERE A.SemanticLayerFieldId = T.SemanticLayerFieldId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerFields A WHERE A.SemanticLayerFieldId = T.SemanticLayerFieldId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Field' ,TxRepositoryTable = 'SemanticLayerFieldCustomScripts' ,ObjectId = T.SemanticLayerFieldCustomScriptId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', TableName, '/', D.Name, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/fields/customScript') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerFieldCustomScripts WHERE SemanticLayerFieldCustomScriptId = ''', T.SemanticLayerFieldCustomScriptId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #SemanticLayerFields D INNER JOIN SemanticLayerFieldCustomScripts T ON D.RN = 1 AND T.SemanticLayerFieldId = D.SemanticLayerFieldId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerFieldCustomScripts A WHERE T.SemanticLayerFieldCustomScriptId = A.SemanticLayerFieldCustomScriptId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerFieldCustomScriptId,SemanticLayerFieldId,Name,Script=CONVERT(NVARCHAR(MAX),Script),EndpointType FROM SemanticLayerFieldCustomScripts A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerFieldCustomScriptId = T.SemanticLayerFieldCustomScriptId EXCEPT SELECT SemanticLayerFieldCustomScriptId,SemanticLayerFieldId,Name,Script=CONVERT(NVARCHAR(MAX),Script),EndpointType FROM SemanticLayerFieldCustomScripts B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerFieldCustomScriptId = T.SemanticLayerFieldCustomScriptId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerFieldCustomScripts A WHERE A.SemanticLayerFieldCustomScriptId = T.SemanticLayerFieldCustomScriptId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Field' ,TxRepositoryTable = 'SemanticLayerFieldCustomScripts' ,ObjectId = T.SemanticLayerFieldCustomScriptId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', TableName, '/', D.Name, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/fields/customScript') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerFieldCustomScripts WHERE SemanticLayerFieldCustomScriptId = ''', T.SemanticLayerFieldCustomScriptId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #SemanticLayerFields D INNER JOIN SemanticLayerFieldCustomScripts T ON D.RN = 1 AND T.SemanticLayerFieldId = D.SemanticLayerFieldId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerFieldCustomScripts A WHERE A.SemanticLayerFieldCustomScriptId = T.SemanticLayerFieldCustomScriptId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerFieldCustomScripts A WHERE A.SemanticLayerFieldCustomScriptId = T.SemanticLayerFieldCustomScriptId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Measure' ,TxRepositoryTable = 'SemanticLayerMeasures' ,ObjectId = T.SemanticLayerMeasureId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', TableName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/measure') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerMeasures WHERE SemanticLayerMeasureId = ''', T.SemanticLayerMeasureId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #SemanticLayerMeasures T INNER JOIN Projects PRJ ON T.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerMeasures A WHERE T.SemanticLayerMeasureId = A.SemanticLayerMeasureId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerMeasureId,SemanticLayerTableId,SemanticLayerFieldId,Name,DataType,MeasureType,Description,ExecutionNumber,Hidden FROM SemanticLayerMeasures A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerMeasureId = T.SemanticLayerMeasureId EXCEPT SELECT SemanticLayerMeasureId,SemanticLayerTableId,SemanticLayerFieldId,Name,DataType,MeasureType,Description,ExecutionNumber,Hidden FROM SemanticLayerMeasures B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerMeasureId = T.SemanticLayerMeasureId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerMeasures A WHERE A.SemanticLayerMeasureId = T.SemanticLayerMeasureId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Measure' ,TxRepositoryTable = 'SemanticLayerMeasures' ,ObjectId = T.SemanticLayerMeasureId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', TableName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/measure') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerMeasures WHERE SemanticLayerMeasureId = ''', T.SemanticLayerMeasureId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #SemanticLayerMeasures T INNER JOIN Projects PRJ ON T.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerMeasures A WHERE A.SemanticLayerMeasureId = T.SemanticLayerMeasureId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerMeasures A WHERE A.SemanticLayerMeasureId = T.SemanticLayerMeasureId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Measure' ,TxRepositoryTable = 'SemanticLayerMeasureCustomScripts' ,ObjectId = T.SemanticLayerMeasureCustomScriptId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', TableName, '/', D.Name, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/Measures/customScript') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerMeasureCustomScripts WHERE SemanticLayerMeasureCustomScriptId = ''', T.SemanticLayerMeasureCustomScriptId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #SemanticLayerMeasures D INNER JOIN SemanticLayerMeasureCustomScripts T ON D.RN = 1 AND T.SemanticLayerMeasureId = D.SemanticLayerMeasureId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerMeasureCustomScripts A WHERE T.SemanticLayerMeasureCustomScriptId = A.SemanticLayerMeasureCustomScriptId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerMeasureCustomScriptId,SemanticLayerMeasureId,Name,Script=CONVERT(NVARCHAR(MAX),Script),EndpointType FROM SemanticLayerMeasureCustomScripts A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerMeasureCustomScriptId = T.SemanticLayerMeasureCustomScriptId EXCEPT SELECT SemanticLayerMeasureCustomScriptId,SemanticLayerMeasureId,Name,Script=CONVERT(NVARCHAR(MAX),Script),EndpointType FROM SemanticLayerMeasureCustomScripts B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerMeasureCustomScriptId = T.SemanticLayerMeasureCustomScriptId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerMeasureCustomScripts A WHERE A.SemanticLayerMeasureCustomScriptId = T.SemanticLayerMeasureCustomScriptId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Measure' ,TxRepositoryTable = 'SemanticLayerMeasureCustomScripts' ,ObjectId = T.SemanticLayerMeasureCustomScriptId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', TableName, '/', D.Name, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/Measures/customScript') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerMeasureCustomScripts WHERE SemanticLayerMeasureCustomScriptId = ''', T.SemanticLayerMeasureCustomScriptId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #SemanticLayerMeasures D INNER JOIN SemanticLayerMeasureCustomScripts T ON D.RN = 1 AND T.SemanticLayerMeasureId = D.SemanticLayerMeasureId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerMeasureCustomScripts A WHERE A.SemanticLayerMeasureCustomScriptId = T.SemanticLayerMeasureCustomScriptId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerMeasureCustomScripts A WHERE A.SemanticLayerMeasureCustomScriptId = T.SemanticLayerMeasureCustomScriptId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Hierarchy' ,TxRepositoryTable = 'SemanticLayerHierarchies' ,ObjectId = T.SemanticLayerHierarchyId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', TableName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/hierarchy') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerHierarchies WHERE SemanticLayerHierarchyId = ''', T.SemanticLayerHierarchyId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #SemanticLayerHierarchies T INNER JOIN Projects PRJ ON T.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerHierarchies A WHERE T.SemanticLayerHierarchyId = A.SemanticLayerHierarchyId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerHierarchyId,SemanticLayerTableId,Name,HideMembers,Description,ExecutionNumber FROM SemanticLayerHierarchies A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerHierarchyId = T.SemanticLayerHierarchyId EXCEPT SELECT SemanticLayerHierarchyId,SemanticLayerTableId,Name,HideMembers,Description,ExecutionNumber FROM SemanticLayerHierarchies B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerHierarchyId = T.SemanticLayerHierarchyId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerHierarchies A WHERE A.SemanticLayerHierarchyId = T.SemanticLayerHierarchyId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Hierarchy' ,TxRepositoryTable = 'SemanticLayerHierarchies' ,ObjectId = T.SemanticLayerHierarchyId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', TableName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/hierarchy') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerHierarchies WHERE SemanticLayerHierarchyId = ''', T.SemanticLayerHierarchyId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #SemanticLayerHierarchies T INNER JOIN Projects PRJ ON T.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerHierarchies A WHERE A.SemanticLayerHierarchyId = T.SemanticLayerHierarchyId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerHierarchies A WHERE A.SemanticLayerHierarchyId = T.SemanticLayerHierarchyId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Hierarchy' ,TxRepositoryTable = 'SemanticLayerHierarchyFields' ,ObjectId = T.SemanticLayerHierarchyFieldId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', D.Name, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/hierarchy/field') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerHierarchyFields WHERE SemanticLayerHierarchyFieldId = ''', T.SemanticLayerHierarchyFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #SemanticLayerHierarchies D INNER JOIN SemanticLayerHierarchyFields T ON D.RN = 1 AND T.SemanticLayerHierarchyId = D.SemanticLayerHierarchyId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerHierarchyFields A WHERE T.SemanticLayerHierarchyFieldId = A.SemanticLayerHierarchyFieldId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerHierarchyFieldId,SemanticLayerHierarchyId,Name,ExecutionNumber,SemanticLayerFieldId FROM SemanticLayerHierarchyFields A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerHierarchyFieldId = T.SemanticLayerHierarchyFieldId EXCEPT SELECT SemanticLayerHierarchyFieldId,SemanticLayerHierarchyId,Name,ExecutionNumber,SemanticLayerFieldId FROM SemanticLayerHierarchyFields B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerHierarchyFieldId = T.SemanticLayerHierarchyFieldId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerHierarchyFields A WHERE A.SemanticLayerHierarchyFieldId = T.SemanticLayerHierarchyFieldId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Hierarchy' ,TxRepositoryTable = 'SemanticLayerHierarchyFields' ,ObjectId = T.SemanticLayerHierarchyFieldId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', D.Name, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/hierarchy/field') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerHierarchyFields WHERE SemanticLayerHierarchyFieldId = ''', T.SemanticLayerHierarchyFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #SemanticLayerHierarchies D INNER JOIN SemanticLayerHierarchyFields T ON D.RN = 1 AND T.SemanticLayerHierarchyId = D.SemanticLayerHierarchyId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerHierarchyFields A WHERE A.SemanticLayerHierarchyFieldId = T.SemanticLayerHierarchyFieldId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerHierarchyFields A WHERE A.SemanticLayerHierarchyFieldId = T.SemanticLayerHierarchyFieldId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Display Folder' ,TxRepositoryTable = 'SemanticLayerDisplayFolders' ,ObjectId = T.SemanticLayerDisplayFolderId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/displayfolder') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerDisplayFolders WHERE SemanticLayerDisplayFolderId = ''', T.SemanticLayerDisplayFolderId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY T.SemanticLayerModelId ORDER BY T.ValidTo DESC) FROM SemanticLayerModels T ) D INNER JOIN SemanticLayerDisplayFolders T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerDisplayFolders A WHERE T.SemanticLayerDisplayFolderId = A.SemanticLayerDisplayFolderId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerDisplayFolderId,SemanticLayerModelId,Name,ExecutionNumber FROM SemanticLayerDisplayFolders A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerDisplayFolderId = T.SemanticLayerDisplayFolderId EXCEPT SELECT SemanticLayerDisplayFolderId,SemanticLayerModelId,Name,ExecutionNumber FROM SemanticLayerDisplayFolders B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerDisplayFolderId = T.SemanticLayerDisplayFolderId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerDisplayFolders A WHERE A.SemanticLayerDisplayFolderId = T.SemanticLayerDisplayFolderId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Display Folder' ,TxRepositoryTable = 'SemanticLayerDisplayFolders' ,ObjectId = T.SemanticLayerDisplayFolderId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/displayfolder') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerDisplayFolders WHERE SemanticLayerDisplayFolderId = ''', T.SemanticLayerDisplayFolderId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY T.SemanticLayerModelId ORDER BY T.ValidTo DESC) FROM SemanticLayerModels T ) D INNER JOIN SemanticLayerDisplayFolders T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerDisplayFolders A WHERE A.SemanticLayerDisplayFolderId = T.SemanticLayerDisplayFolderId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerDisplayFolders A WHERE A.SemanticLayerDisplayFolderId = T.SemanticLayerDisplayFolderId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Display Folder' ,TxRepositoryTable = 'SemanticLayerDisplayFolderRelations' ,ObjectId = T.SemanticLayerDisplayFolderRelationId ,ObjectName = COALESCE(OA_Field.name, OA_Measure.name, OA_hierarchy.name) ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', DisplayFolder, '/', COALESCE(OA_Field.name, OA_Measure.name, OA_hierarchy.name)) ,ObjectPathAbstract = CONCAT('/project/', layer, '/displayfolder/relation') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerDisplayFolderRelations WHERE SemanticLayerDisplayFolderRelationId = ''', T.SemanticLayerDisplayFolderRelationId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, T.DisplayFolder, T.SemanticLayerDisplayFolderId, T.RN FROM (SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels T) D INNER JOIN (SELECT DisplayFolder = Name, SemanticLayerDisplayFolderId, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerDisplayFolderId ORDER BY ValidTo DESC) FROM SemanticLayerDisplayFolders) T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId ) D INNER JOIN SemanticLayerDisplayFolderRelations T ON D.RN = 1 AND T.SemanticLayerDisplayFolderId = D.SemanticLayerDisplayFolderId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerDisplayFolderRelations A WHERE T.SemanticLayerDisplayFolderRelationId = A.SemanticLayerDisplayFolderRelationId) OA_MinVals OUTER APPLY (SELECT Name = 'Field: ' + A.Name FROM #SemanticLayerFields A WHERE T.SemanticLayerFieldId = A.SemanticLayerFieldId AND RN = 1) OA_Field OUTER APPLY (SELECT Name = 'Measure: ' + A.Name FROM #SemanticLayerMeasures A WHERE T.SemanticLayerMeasureId = A.SemanticLayerMeasureId AND RN = 1) OA_Measure OUTER APPLY (SELECT Name = 'Hierarchy: ' + A.Name FROM #SemanticLayerHierarchies A WHERE T.SemanticLayerHierarchyId = A.SemanticLayerHierarchyId AND RN = 1) OA_hierarchy WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerDisplayFolderRelationId,SemanticLayerDisplayFolderId,SemanticLayerFieldId,SemanticLayerMeasureId,SemanticLayerHierarchyId FROM SemanticLayerDisplayFolderRelations A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerDisplayFolderRelationId = T.SemanticLayerDisplayFolderRelationId EXCEPT SELECT SemanticLayerDisplayFolderRelationId,SemanticLayerDisplayFolderId,SemanticLayerFieldId,SemanticLayerMeasureId,SemanticLayerHierarchyId FROM SemanticLayerDisplayFolderRelations B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerDisplayFolderRelationId = T.SemanticLayerDisplayFolderRelationId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerDisplayFolderRelations A WHERE A.SemanticLayerDisplayFolderRelationId = T.SemanticLayerDisplayFolderRelationId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Display Folder' ,TxRepositoryTable = 'SemanticLayerDisplayFolderRelations' ,ObjectId = T.SemanticLayerDisplayFolderRelationId ,ObjectName = COALESCE(OA_Field.name, OA_Measure.name, OA_hierarchy.name) ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', DisplayFolder, '/', COALESCE(OA_Field.name, OA_Measure.name, OA_hierarchy.name)) ,ObjectPathAbstract = CONCAT('/project/', layer, '/displayfolder/relation') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerDisplayFolderRelations WHERE SemanticLayerDisplayFolderRelationId = ''', T.SemanticLayerDisplayFolderRelationId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, T.DisplayFolder, T.SemanticLayerDisplayFolderId, T.RN FROM (SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels T) D INNER JOIN (SELECT DisplayFolder = Name, SemanticLayerDisplayFolderId, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerDisplayFolderId ORDER BY ValidTo DESC) FROM SemanticLayerDisplayFolders) T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId ) D INNER JOIN SemanticLayerDisplayFolderRelations T ON D.RN = 1 AND T.SemanticLayerDisplayFolderId = D.SemanticLayerDisplayFolderId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT Name = 'Field: ' + A.Name FROM #SemanticLayerFields A WHERE T.SemanticLayerFieldId = A.SemanticLayerFieldId AND RN = 1) OA_Field OUTER APPLY (SELECT Name = 'Measure: ' + A.Name FROM #SemanticLayerMeasures A WHERE T.SemanticLayerMeasureId = A.SemanticLayerMeasureId AND RN = 1) OA_Measure OUTER APPLY (SELECT Name = 'Hierarchy: ' + A.Name FROM #SemanticLayerHierarchies A WHERE T.SemanticLayerHierarchyId = A.SemanticLayerHierarchyId AND RN = 1) OA_hierarchy WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerDisplayFolderRelations A WHERE A.SemanticLayerDisplayFolderRelationId = T.SemanticLayerDisplayFolderRelationId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerDisplayFolderRelations A WHERE A.SemanticLayerDisplayFolderRelationId = T.SemanticLayerDisplayFolderRelationId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Relation' ,TxRepositoryTable = 'SemanticLayerRelations' ,ObjectId = T.SemanticLayerRelationId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/relation') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerRelations WHERE SemanticLayerRelationId = ''', T.SemanticLayerRelationId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY T.SemanticLayerModelId ORDER BY T.ValidTo DESC) FROM SemanticLayerModels T ) D INNER JOIN SemanticLayerRelations T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerRelations A WHERE T.SemanticLayerRelationId = A.SemanticLayerRelationId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerRelationId,SemanticLayerModelId,Name,IsDefaultRelation,Cardinality,FilterDirection FROM SemanticLayerRelations A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerRelationId = T.SemanticLayerRelationId EXCEPT SELECT SemanticLayerRelationId,SemanticLayerModelId,Name,IsDefaultRelation,Cardinality,FilterDirection FROM SemanticLayerRelations B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerRelationId = T.SemanticLayerRelationId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerRelations A WHERE A.SemanticLayerRelationId = T.SemanticLayerRelationId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Relation' ,TxRepositoryTable = 'SemanticLayerRelations' ,ObjectId = T.SemanticLayerRelationId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/relation') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerRelations WHERE SemanticLayerRelationId = ''', T.SemanticLayerRelationId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY T.SemanticLayerModelId ORDER BY T.ValidTo DESC) FROM SemanticLayerModels T ) D INNER JOIN SemanticLayerRelations T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerRelations A WHERE A.SemanticLayerRelationId = T.SemanticLayerRelationId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerRelations A WHERE A.SemanticLayerRelationId = T.SemanticLayerRelationId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Relation' ,TxRepositoryTable = 'SemanticLayerRelationItems' ,ObjectId = T.SemanticLayerRelationItemId ,ObjectName = CONCAT(OA_Field.name, '<->', OA_RefField.name) ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', relation, '/',CONCAT(OA_Field.name, '<->', OA_RefField.name)) ,ObjectPathAbstract = CONCAT('/project/', layer, '/relation/item') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerRelationItems WHERE SemanticLayerRelationItemId = ''', T.SemanticLayerRelationItemId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, T.Relation, T.SemanticLayerRelationId, T.RN FROM (SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels T) D INNER JOIN (SELECT Relation = Name, SemanticLayerRelationId, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerRelationId ORDER BY ValidTo DESC) FROM SemanticLayerRelations) T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId ) D INNER JOIN SemanticLayerRelationItems T ON D.RN = 1 AND T.SemanticLayerRelationId = D.SemanticLayerRelationId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerRelationItems A WHERE T.SemanticLayerRelationItemId = A.SemanticLayerRelationItemId) OA_MinVals OUTER APPLY (SELECT Name FROM #SemanticLayerFields A WHERE T.FieldId = A.SemanticLayerFieldId AND RN = 1) OA_Field OUTER APPLY (SELECT Name FROM #SemanticLayerFields A WHERE T.ReferencedFieldId = A.SemanticLayerFieldId AND RN = 1) OA_RefField WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerRelationItemId,SemanticLayerRelationId,FieldId,ReferencedFieldId,ExecutionNumber FROM SemanticLayerRelationItems A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerRelationItemId = T.SemanticLayerRelationItemId EXCEPT SELECT SemanticLayerRelationItemId,SemanticLayerRelationId,FieldId,ReferencedFieldId,ExecutionNumber FROM SemanticLayerRelationItems B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerRelationItemId = T.SemanticLayerRelationItemId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerRelationItems A WHERE A.SemanticLayerRelationItemId = T.SemanticLayerRelationItemId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Relation' ,TxRepositoryTable = 'SemanticLayerRelationItems' ,ObjectId = T.SemanticLayerRelationItemId ,ObjectName = CONCAT(OA_Field.name, '<->', OA_RefField.name) ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', relation, '/',CONCAT(OA_Field.name, '<->', OA_RefField.name)) ,ObjectPathAbstract = CONCAT('/project/', layer, '/relation/item') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerRelationItems WHERE SemanticLayerRelationItemId = ''', T.SemanticLayerRelationItemId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, T.Relation, T.SemanticLayerRelationId, T.RN FROM (SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels T) D INNER JOIN (SELECT Relation = Name, SemanticLayerRelationId, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerRelationId ORDER BY ValidTo DESC) FROM SemanticLayerRelations) T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId ) D INNER JOIN SemanticLayerRelationItems T ON D.RN = 1 AND T.SemanticLayerRelationId = D.SemanticLayerRelationId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT Name FROM #SemanticLayerFields A WHERE T.FieldId = A.SemanticLayerFieldId AND RN = 1) OA_Field OUTER APPLY (SELECT Name FROM #SemanticLayerFields A WHERE T.ReferencedFieldId = A.SemanticLayerFieldId AND RN = 1) OA_RefField WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerRelationItems A WHERE A.SemanticLayerRelationItemId = T.SemanticLayerRelationItemId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerRelationItems A WHERE A.SemanticLayerRelationItemId = T.SemanticLayerRelationItemId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Selection Rule' ,TxRepositoryTable = 'SemanticLayerSelectionRules' ,ObjectId = T.SemanticLayerSelectionRuleId ,ObjectName = TableName +': selection rule' ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', TableName, '/selection rule') ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/selectionrule') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerSelectionRules WHERE SemanticLayerSelectionRuleId = ''', T.SemanticLayerSelectionRuleId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, T.TableName,T.RN, T.SemanticLayerTableId FROM (SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels T) D INNER JOIN (SELECT TableName = Name, SemanticLayerTableId, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerTableId ORDER BY ValidTo DESC) FROM SemanticLayerTables) T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId AND D.ProjectId = @projectid ) D INNER JOIN SemanticLayerSelectionRules T ON D.RN = 1 AND T.SemanticLayerTableId = D.SemanticLayerTableId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerSelectionRules A WHERE T.SemanticLayerSelectionRuleId = A.SemanticLayerSelectionRuleId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerSelectionRuleId,SemanticLayerTableId,ExecutionNumber FROM SemanticLayerSelectionRules A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerSelectionRuleId = T.SemanticLayerSelectionRuleId EXCEPT SELECT SemanticLayerSelectionRuleId,SemanticLayerTableId,ExecutionNumber FROM SemanticLayerSelectionRules B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerSelectionRuleId = T.SemanticLayerSelectionRuleId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerSelectionRules A WHERE A.SemanticLayerSelectionRuleId = T.SemanticLayerSelectionRuleId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Selection Rule' ,TxRepositoryTable = 'SemanticLayerSelectionRules' ,ObjectId = T.SemanticLayerSelectionRuleId ,ObjectName = TableName +': selection rule' ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', TableName, '/selection rule') ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/selectionrule') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerSelectionRules WHERE SemanticLayerSelectionRuleId = ''', T.SemanticLayerSelectionRuleId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, T.TableName,T.RN, T.SemanticLayerTableId FROM (SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels T) D INNER JOIN (SELECT TableName = Name, SemanticLayerTableId, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerTableId ORDER BY ValidTo DESC) FROM SemanticLayerTables) T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId AND D.ProjectId = @projectid ) D INNER JOIN SemanticLayerSelectionRules T ON D.RN = 1 AND T.SemanticLayerTableId = D.SemanticLayerTableId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerSelectionRules A WHERE A.SemanticLayerSelectionRuleId = T.SemanticLayerSelectionRuleId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerSelectionRules A WHERE A.SemanticLayerSelectionRuleId = T.SemanticLayerSelectionRuleId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Selection Rule' ,TxRepositoryTable = 'SemanticLayerSelectionRuleFields' ,ObjectId = T.SemanticLayerSelectionRuleFieldId ,ObjectName = CONCAT(OA_Field.name,' ', T.RuleType, ' ', T.RuleValue) ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', TableName, '/selection rule/',CONCAT(OA_Field.name,' ', T.RuleType, ' ', T.RuleValue)) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/selectionrule/values') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerSelectionRuleFields WHERE SemanticLayerSelectionRuleFieldId = ''', T.SemanticLayerSelectionRuleFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, T.TableName, T.SemanticLayerTableId,R.SemanticLayerSelectionRuleId,R.RN FROM (SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels T) D INNER JOIN (SELECT TableName = Name, SemanticLayerTableId, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerTableId ORDER BY ValidTo DESC) FROM SemanticLayerTables) T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId AND D.ProjectId = @projectid INNER JOIN (SELECT SemanticLayerSelectionRuleId, SemanticLayerTableId , RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerSelectionRuleId ORDER BY ValidTo DESC) FROM SemanticLayerSelectionRules) R ON T.RN = 1 AND R.SemanticLayerTableId = T.SemanticLayerTableId ) D INNER JOIN SemanticLayerSelectionRuleFields T ON D.RN = 1 AND T.SemanticLayerSelectionRuleId = D.SemanticLayerSelectionRuleId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerSelectionRuleFields A WHERE T.SemanticLayerSelectionRuleFieldId = A.SemanticLayerSelectionRuleFieldId) OA_MinVals OUTER APPLY (SELECT Name FROM #SemanticLayerFields A WHERE T.SemanticLayerFieldId = A.SemanticLayerFieldId AND RN = 1) OA_Field WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerSelectionRuleFieldId,SemanticLayerSelectionRuleId,SemanticLayerFieldId,ExecutionNumber,RuleType,RuleValue FROM SemanticLayerSelectionRuleFields A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerSelectionRuleFieldId = T.SemanticLayerSelectionRuleFieldId EXCEPT SELECT SemanticLayerSelectionRuleFieldId,SemanticLayerSelectionRuleId,SemanticLayerFieldId,ExecutionNumber,RuleType,RuleValue FROM SemanticLayerSelectionRuleFields B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerSelectionRuleFieldId = T.SemanticLayerSelectionRuleFieldId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerSelectionRuleFields A WHERE A.SemanticLayerSelectionRuleFieldId = T.SemanticLayerSelectionRuleFieldId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Selection Rule' ,TxRepositoryTable = 'SemanticLayerSelectionRuleFields' ,ObjectId = T.SemanticLayerSelectionRuleFieldId ,ObjectName = CONCAT(OA_Field.name,' ', T.RuleType, ' ', T.RuleValue) ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', TableName, '/selection rule/',CONCAT(OA_Field.name,' ', T.RuleType, ' ', T.RuleValue)) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/selectionrule/values') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerSelectionRuleFields WHERE SemanticLayerSelectionRuleFieldId = ''', T.SemanticLayerSelectionRuleFieldId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, T.TableName, T.SemanticLayerTableId,R.SemanticLayerSelectionRuleId,R.RN FROM (SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels T) D INNER JOIN (SELECT TableName = Name, SemanticLayerTableId, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerTableId ORDER BY ValidTo DESC) FROM SemanticLayerTables) T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId AND D.ProjectId = @projectid INNER JOIN (SELECT SemanticLayerSelectionRuleId, SemanticLayerTableId , RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerSelectionRuleId ORDER BY ValidTo DESC) FROM SemanticLayerSelectionRules) R ON T.RN = 1 AND R.SemanticLayerTableId = T.SemanticLayerTableId ) D INNER JOIN SemanticLayerSelectionRuleFields T ON D.RN = 1 AND T.SemanticLayerSelectionRuleId = D.SemanticLayerSelectionRuleId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT Name FROM #SemanticLayerFields A WHERE T.SemanticLayerFieldId = A.SemanticLayerFieldId AND RN = 1) OA_Field WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerSelectionRuleFields A WHERE A.SemanticLayerSelectionRuleFieldId = T.SemanticLayerSelectionRuleFieldId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerSelectionRuleFields A WHERE A.SemanticLayerSelectionRuleFieldId = T.SemanticLayerSelectionRuleFieldId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer format string objects' ,TxRepositoryTable = 'SemanticLayerFormatStringObjects' ,ObjectId = T.SemanticLayerFormatStringObjectId ,ObjectName = D.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', TableName, '/', D.Name, '/<>') ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/field/<>') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerFormatStringObjects WHERE SemanticLayerFormatStringObjectId = ''', T.SemanticLayerFormatStringObjectId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #semanticlayerfields D INNER JOIN SemanticLayerFormatStringObjects T ON D.RN = 1 AND T.SemanticLayerFieldId = D.SemanticLayerFieldId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerFormatStringObjects A WHERE T.SemanticLayerFormatStringObjectId = A.SemanticLayerFormatStringObjectId) OA_MinVals OUTER APPLY (SELECT Name FROM #SemanticLayerFields A WHERE T.SemanticLayerFieldId = A.SemanticLayerFieldId AND RN = 1) OA_Field WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerFormatStringObjectId,SemanticLayerFieldId,SemanticLayerMeasureId,CurrencyIndex,DecimalPlaces,UseThousandsSeparator,CultureName,DateTimeFormatString,DataTypeFormat,SummarizeBy,DataCatagory FROM SemanticLayerFormatStringObjects A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerFormatStringObjectId = T.SemanticLayerFormatStringObjectId EXCEPT SELECT SemanticLayerFormatStringObjectId,SemanticLayerFieldId,SemanticLayerMeasureId,CurrencyIndex,DecimalPlaces,UseThousandsSeparator,CultureName,DateTimeFormatString,DataTypeFormat,SummarizeBy,DataCatagory FROM SemanticLayerFormatStringObjects B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerFormatStringObjectId = T.SemanticLayerFormatStringObjectId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerFormatStringObjects A WHERE A.SemanticLayerFormatStringObjectId = T.SemanticLayerFormatStringObjectId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer format string objects' ,TxRepositoryTable = 'SemanticLayerFormatStringObjects' ,ObjectId = T.SemanticLayerFormatStringObjectId ,ObjectName = D.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', TableName, '/', D.Name, '/<>') ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/field/<>') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerFormatStringObjects WHERE SemanticLayerFormatStringObjectId = ''', T.SemanticLayerFormatStringObjectId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #semanticlayerfields D INNER JOIN SemanticLayerFormatStringObjects T ON D.RN = 1 AND T.SemanticLayerFieldId = D.SemanticLayerFieldId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT Name FROM #SemanticLayerFields A WHERE T.SemanticLayerFieldId = A.SemanticLayerFieldId AND RN = 1) OA_Field WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerFormatStringObjects A WHERE A.SemanticLayerFormatStringObjectId = T.SemanticLayerFormatStringObjectId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerFormatStringObjects A WHERE A.SemanticLayerFormatStringObjectId = T.SemanticLayerFormatStringObjectId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer format string objects' ,TxRepositoryTable = 'SemanticLayerFormatStringObjects' ,ObjectId = T.SemanticLayerFormatStringObjectId ,ObjectName = D.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', TableName, '/', D.Name, '/<>') ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/measure/<>') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerFormatStringObjects WHERE SemanticLayerFormatStringObjectId = ''', T.SemanticLayerFormatStringObjectId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #semanticlayermeasures D INNER JOIN SemanticLayerFormatStringObjects T ON D.RN = 1 AND T.SemanticLayerMeasureId = D.SemanticLayerMeasureId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerFormatStringObjects A WHERE T.SemanticLayerFormatStringObjectId = A.SemanticLayerFormatStringObjectId) OA_MinVals OUTER APPLY (SELECT Name FROM #SemanticLayerFields A WHERE T.SemanticLayerFieldId = A.SemanticLayerFieldId AND RN = 1) OA_Field WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerFormatStringObjectId,SemanticLayerFieldId,SemanticLayerMeasureId,CurrencyIndex,DecimalPlaces,UseThousandsSeparator,CultureName,DateTimeFormatString,DataTypeFormat,SummarizeBy,DataCatagory FROM SemanticLayerFormatStringObjects A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerFormatStringObjectId = T.SemanticLayerFormatStringObjectId EXCEPT SELECT SemanticLayerFormatStringObjectId,SemanticLayerFieldId,SemanticLayerMeasureId,CurrencyIndex,DecimalPlaces,UseThousandsSeparator,CultureName,DateTimeFormatString,DataTypeFormat,SummarizeBy,DataCatagory FROM SemanticLayerFormatStringObjects B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerFormatStringObjectId = T.SemanticLayerFormatStringObjectId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerFormatStringObjects A WHERE A.SemanticLayerFormatStringObjectId = T.SemanticLayerFormatStringObjectId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer format string objects' ,TxRepositoryTable = 'SemanticLayerFormatStringObjects' ,ObjectId = T.SemanticLayerFormatStringObjectId ,ObjectName = D.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', TableName, '/', D.Name, '/<>') ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/measure/<>') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerFormatStringObjects WHERE SemanticLayerFormatStringObjectId = ''', T.SemanticLayerFormatStringObjectId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #semanticlayermeasures D INNER JOIN SemanticLayerFormatStringObjects T ON D.RN = 1 AND T.SemanticLayerMeasureId = D.SemanticLayerMeasureId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT Name FROM #SemanticLayerFields A WHERE T.SemanticLayerFieldId = A.SemanticLayerFieldId AND RN = 1) OA_Field WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerFormatStringObjects A WHERE A.SemanticLayerFormatStringObjectId = T.SemanticLayerFormatStringObjectId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerFormatStringObjects A WHERE A.SemanticLayerFormatStringObjectId = T.SemanticLayerFormatStringObjectId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Roles' ,TxRepositoryTable = 'SemanticLayerRoles' ,ObjectId = T.SemanticLayerRoleId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/roles') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerRoles WHERE SemanticLayerRoleId = ''', T.SemanticLayerRoleId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels ) D INNER JOIN SemanticLayerRoles T ON D.RN = 1 AND T.ModelId = D.SemanticLayerModelId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerRoles A WHERE T.SemanticLayerRoleId = A.SemanticLayerRoleId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerRoleId,ModelId,Name,EnvironmentEnabled,ExecutionNumber FROM SemanticLayerRoles A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerRoleId = T.SemanticLayerRoleId EXCEPT SELECT SemanticLayerRoleId,ModelId,Name,EnvironmentEnabled,ExecutionNumber FROM SemanticLayerRoles B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerRoleId = T.SemanticLayerRoleId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerRoles A WHERE A.SemanticLayerRoleId = T.SemanticLayerRoleId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Roles' ,TxRepositoryTable = 'SemanticLayerRoles' ,ObjectId = T.SemanticLayerRoleId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/roles') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerRoles WHERE SemanticLayerRoleId = ''', T.SemanticLayerRoleId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels ) D INNER JOIN SemanticLayerRoles T ON D.RN = 1 AND T.ModelId = D.SemanticLayerModelId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerRoles A WHERE A.SemanticLayerRoleId = T.SemanticLayerRoleId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerRoles A WHERE A.SemanticLayerRoleId = T.SemanticLayerRoleId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Roles' ,TxRepositoryTable = 'SemanticLayerRoleEndpointRelations' ,ObjectId = T.SemanticLayerRoleEndpointRelationId ,ObjectName = 'Related: ' + OA_EndPoint.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', RoleName, '/', OA_EndPoint.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/roles/endpoint') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerRoleEndpointRelations WHERE SemanticLayerRoleEndpointRelationId = ''', T.SemanticLayerRoleEndpointRelationId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, T.RoleName,T.RN, T.SemanticLayerRoleId FROM (SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels T) D INNER JOIN (SELECT RoleName = Name, SemanticLayerRoleId, SemanticLayerModelId = ModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerRoleId ORDER BY ValidTo DESC) FROM SemanticLayerRoles) T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId AND D.ProjectId = @projectid ) D INNER JOIN SemanticLayerRoleEndpointRelations T ON D.RN = 1 AND T.SemanticLayerRoleId = D.SemanticLayerRoleId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerRoleEndpointRelations A WHERE T.SemanticLayerRoleEndpointRelationId = A.SemanticLayerRoleEndpointRelationId) OA_MinVals OUTER APPLY (SELECT TOP 1 Name FROM SemanticLayerEndpoints A WHERE T.SemanticLayerEndpointId = A.SemanticLayerEndpointId ORDER BY ValidTo DESC) OA_EndPoint WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerRoleEndpointRelationId,SemanticLayerRoleId,SemanticLayerEndpointId FROM SemanticLayerRoleEndpointRelations A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerRoleEndpointRelationId = T.SemanticLayerRoleEndpointRelationId EXCEPT SELECT SemanticLayerRoleEndpointRelationId,SemanticLayerRoleId,SemanticLayerEndpointId FROM SemanticLayerRoleEndpointRelations B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerRoleEndpointRelationId = T.SemanticLayerRoleEndpointRelationId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerRoleEndpointRelations A WHERE A.SemanticLayerRoleEndpointRelationId = T.SemanticLayerRoleEndpointRelationId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Roles' ,TxRepositoryTable = 'SemanticLayerRoleEndpointRelations' ,ObjectId = T.SemanticLayerRoleEndpointRelationId ,ObjectName = 'Related: ' + OA_EndPoint.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', RoleName, '/', OA_EndPoint.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/roles/endpoint') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerRoleEndpointRelations WHERE SemanticLayerRoleEndpointRelationId = ''', T.SemanticLayerRoleEndpointRelationId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, T.RoleName,T.RN, T.SemanticLayerRoleId FROM (SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels T) D INNER JOIN (SELECT RoleName = Name, SemanticLayerRoleId, SemanticLayerModelId = ModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerRoleId ORDER BY ValidTo DESC) FROM SemanticLayerRoles) T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId AND D.ProjectId = @projectid ) D INNER JOIN SemanticLayerRoleEndpointRelations T ON D.RN = 1 AND T.SemanticLayerRoleId = D.SemanticLayerRoleId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT TOP 1 Name FROM SemanticLayerEndpoints A WHERE T.SemanticLayerEndpointId = A.SemanticLayerEndpointId ORDER BY ValidTo DESC) OA_EndPoint WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerRoleEndpointRelations A WHERE A.SemanticLayerRoleEndpointRelationId = T.SemanticLayerRoleEndpointRelationId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerRoleEndpointRelations A WHERE A.SemanticLayerRoleEndpointRelationId = T.SemanticLayerRoleEndpointRelationId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Roles' ,TxRepositoryTable = 'SemanticLayerRoleMembers' ,ObjectId = T.SemanticLayerRoleMemberId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', RoleName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/roles/member') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerRoleMembers WHERE SemanticLayerRoleMemberId = ''', T.SemanticLayerRoleMemberId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, T.RoleName,T.RN, T.SemanticLayerRoleId FROM (SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels T) D INNER JOIN (SELECT RoleName = Name, SemanticLayerRoleId, SemanticLayerModelId = ModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerRoleId ORDER BY ValidTo DESC) FROM SemanticLayerRoles) T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId AND D.ProjectId = @projectid ) D INNER JOIN SemanticLayerRoleMembers T ON D.RN = 1 AND T.SemanticLayerRoleId = D.SemanticLayerRoleId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerRoleMembers A WHERE T.SemanticLayerRoleMemberId = A.SemanticLayerRoleMemberId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerRoleMemberId,SemanticLayerRoleId,EnvironmentId,Name=CONVERT(NVARCHAR(MAX),Name),SID,IdentityProvider FROM SemanticLayerRoleMembers A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerRoleMemberId = T.SemanticLayerRoleMemberId EXCEPT SELECT SemanticLayerRoleMemberId,SemanticLayerRoleId,EnvironmentId,Name=CONVERT(NVARCHAR(MAX),Name),SID,IdentityProvider FROM SemanticLayerRoleMembers B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerRoleMemberId = T.SemanticLayerRoleMemberId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerRoleMembers A WHERE A.SemanticLayerRoleMemberId = T.SemanticLayerRoleMemberId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Roles' ,TxRepositoryTable = 'SemanticLayerRoleMembers' ,ObjectId = T.SemanticLayerRoleMemberId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', RoleName, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/roles/member') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerRoleMembers WHERE SemanticLayerRoleMemberId = ''', T.SemanticLayerRoleMemberId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, T.RoleName,T.RN, T.SemanticLayerRoleId FROM (SELECT Layer = 'Semantic', LayerName = Name, projectid, SemanticLayerModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerModelId ORDER BY ValidTo DESC) FROM SemanticLayerModels T) D INNER JOIN (SELECT RoleName = Name, SemanticLayerRoleId, SemanticLayerModelId = ModelId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerRoleId ORDER BY ValidTo DESC) FROM SemanticLayerRoles) T ON D.RN = 1 AND T.SemanticLayerModelId = D.SemanticLayerModelId AND D.ProjectId = @projectid ) D INNER JOIN SemanticLayerRoleMembers T ON D.RN = 1 AND T.SemanticLayerRoleId = D.SemanticLayerRoleId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerRoleMembers A WHERE A.SemanticLayerRoleMemberId = T.SemanticLayerRoleMemberId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerRoleMembers A WHERE A.SemanticLayerRoleMemberId = T.SemanticLayerRoleMemberId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Security Setup' ,TxRepositoryTable = 'SemanticLayerSecuritySetups' ,ObjectId = T.SemanticLayerSecuritySetupId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', D.TableName,'/', D.Name, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/field/security setup') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerSecuritySetups WHERE SemanticLayerSecuritySetupId = ''', T.SemanticLayerSecuritySetupId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #semanticlayerfields D INNER JOIN SemanticLayerSecuritySetups T ON D.RN = 1 AND T.SemanticLayerFieldId = D.SemanticLayerFieldId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerSecuritySetups A WHERE T.SemanticLayerSecuritySetupId = A.SemanticLayerSecuritySetupId) OA_MinVals OUTER APPLY (SELECT Name FROM #SemanticLayerFields A WHERE T.SemanticLayerFieldId = A.SemanticLayerFieldId AND RN = 1) OA_Field WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerSecuritySetupId,SemanticLayerFieldId,Name,ExecutionNumber,SecuritySetupType FROM SemanticLayerSecuritySetups A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerSecuritySetupId = T.SemanticLayerSecuritySetupId EXCEPT SELECT SemanticLayerSecuritySetupId,SemanticLayerFieldId,Name,ExecutionNumber,SecuritySetupType FROM SemanticLayerSecuritySetups B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerSecuritySetupId = T.SemanticLayerSecuritySetupId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerSecuritySetups A WHERE A.SemanticLayerSecuritySetupId = T.SemanticLayerSecuritySetupId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Security Setup' ,TxRepositoryTable = 'SemanticLayerSecuritySetups' ,ObjectId = T.SemanticLayerSecuritySetupId ,ObjectName = T.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', D.TableName,'/', D.Name, '/', T.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/field/security setup') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerSecuritySetups WHERE SemanticLayerSecuritySetupId = ''', T.SemanticLayerSecuritySetupId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM #semanticlayerfields D INNER JOIN SemanticLayerSecuritySetups T ON D.RN = 1 AND T.SemanticLayerFieldId = D.SemanticLayerFieldId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT Name FROM #SemanticLayerFields A WHERE T.SemanticLayerFieldId = A.SemanticLayerFieldId AND RN = 1) OA_Field WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerSecuritySetups A WHERE A.SemanticLayerSecuritySetupId = T.SemanticLayerSecuritySetupId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerSecuritySetups A WHERE A.SemanticLayerSecuritySetupId = T.SemanticLayerSecuritySetupId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Security Setup' ,TxRepositoryTable = 'SemanticLayerSecuritySetupRelations' ,ObjectId = T.SemanticLayerSecuritySetupRelationId ,ObjectName = 'Related: ' + OA_Role.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', D.TableName,'/', D.FieldName, '/', D.SecuritySetupName, '/', 'Related: ' + OA_Role.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/field/security setup/related role') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerSecuritySetupRelations WHERE SemanticLayerSecuritySetupRelationId = ''', T.SemanticLayerSecuritySetupRelationId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, D.TableName, D.SemanticLayerTableId, D.SemanticLayerFieldId, D.FieldName, T.SecuritySetupName, T.SemanticLayerSecuritySetupId,T.RN FROM (SELECT Layer, LayerName, projectid, SemanticLayerModelId, TableName, SemanticLayerTableId, SemanticLayerFieldId, FieldName=T.Name, rn FROM #semanticlayerfields T) D INNER JOIN (SELECT SecuritySetupName = Name, SemanticLayerSecuritySetupId, SemanticLayerFieldId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerSecuritySetupId ORDER BY ValidTo DESC) FROM SemanticLayerSecuritySetups) T ON D.RN = 1 AND T.SemanticLayerFieldId = D.SemanticLayerFieldId AND D.ProjectId = @projectid ) D INNER JOIN SemanticLayerSecuritySetupRelations T ON D.RN = 1 AND T.SemanticLayerSecuritySetupId = D.SemanticLayerSecuritySetupId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerSecuritySetupRelations A WHERE T.SemanticLayerSecuritySetupRelationId = A.SemanticLayerSecuritySetupRelationId) OA_MinVals OUTER APPLY (SELECT TOP 1 Name FROM SemanticLayerRoles A WHERE T.SemanticLayerRoleId = A.SemanticLayerRoleId ORDER BY ValidTo DESC) OA_Role WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerSecuritySetupRelationId,SemanticLayerSecuritySetupId,SemanticLayerRoleId FROM SemanticLayerSecuritySetupRelations A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerSecuritySetupRelationId = T.SemanticLayerSecuritySetupRelationId EXCEPT SELECT SemanticLayerSecuritySetupRelationId,SemanticLayerSecuritySetupId,SemanticLayerRoleId FROM SemanticLayerSecuritySetupRelations B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerSecuritySetupRelationId = T.SemanticLayerSecuritySetupRelationId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerSecuritySetupRelations A WHERE A.SemanticLayerSecuritySetupRelationId = T.SemanticLayerSecuritySetupRelationId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Security Setup' ,TxRepositoryTable = 'SemanticLayerSecuritySetupRelations' ,ObjectId = T.SemanticLayerSecuritySetupRelationId ,ObjectName = 'Related: ' + OA_Role.Name ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', D.TableName,'/', D.FieldName, '/', D.SecuritySetupName, '/', 'Related: ' + OA_Role.Name) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/field/security setup/related role') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerSecuritySetupRelations WHERE SemanticLayerSecuritySetupRelationId = ''', T.SemanticLayerSecuritySetupRelationId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, D.TableName, D.SemanticLayerTableId, D.SemanticLayerFieldId, D.FieldName, T.SecuritySetupName, T.SemanticLayerSecuritySetupId,T.RN FROM (SELECT Layer, LayerName, projectid, SemanticLayerModelId, TableName, SemanticLayerTableId, SemanticLayerFieldId, FieldName=T.Name, rn FROM #semanticlayerfields T) D INNER JOIN (SELECT SecuritySetupName = Name, SemanticLayerSecuritySetupId, SemanticLayerFieldId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerSecuritySetupId ORDER BY ValidTo DESC) FROM SemanticLayerSecuritySetups) T ON D.RN = 1 AND T.SemanticLayerFieldId = D.SemanticLayerFieldId AND D.ProjectId = @projectid ) D INNER JOIN SemanticLayerSecuritySetupRelations T ON D.RN = 1 AND T.SemanticLayerSecuritySetupId = D.SemanticLayerSecuritySetupId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT TOP 1 Name FROM SemanticLayerRoles A WHERE T.SemanticLayerRoleId = A.SemanticLayerRoleId ORDER BY ValidTo DESC) OA_Role WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerSecuritySetupRelations A WHERE A.SemanticLayerSecuritySetupRelationId = T.SemanticLayerSecuritySetupRelationId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerSecuritySetupRelations A WHERE A.SemanticLayerSecuritySetupRelationId = T.SemanticLayerSecuritySetupRelationId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Security Setup' ,TxRepositoryTable = 'SemanticLayerSecuritySetupItems' ,ObjectId = T.SemanticLayerSecuritySetupItemId ,ObjectName = CONCAT(T.value, ' ', T.Type, ' ', T.memberValue) ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', D.TableName,'/', D.FieldName, '/', D.SecuritySetupName, '/', CONCAT(T.value, ' ', T.Type, ' ', T.memberValue)) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/field/security setup/item') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerSecuritySetupItems WHERE SemanticLayerSecuritySetupItemId = ''', T.SemanticLayerSecuritySetupItemId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, D.TableName, D.SemanticLayerTableId, D.SemanticLayerFieldId, D.FieldName, T.SecuritySetupName, T.SemanticLayerSecuritySetupId,T.RN FROM (SELECT Layer, LayerName, projectid, SemanticLayerModelId, TableName, SemanticLayerTableId, SemanticLayerFieldId, FieldName=T.Name, rn FROM #semanticlayerfields T) D INNER JOIN (SELECT SecuritySetupName = Name, SemanticLayerSecuritySetupId, SemanticLayerFieldId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerSecuritySetupId ORDER BY ValidTo DESC) FROM SemanticLayerSecuritySetups) T ON D.RN = 1 AND T.SemanticLayerFieldId = D.SemanticLayerFieldId AND D.ProjectId = @projectid ) D INNER JOIN SemanticLayerSecuritySetupItems T ON D.RN = 1 AND T.SemanticLayerSecuritySetupId = D.SemanticLayerSecuritySetupId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerSecuritySetupItems A WHERE T.SemanticLayerSecuritySetupItemId = A.SemanticLayerSecuritySetupItemId) OA_MinVals WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerSecuritySetupItemId,SemanticLayerSecuritySetupId,Type,MemberValue,IsAll,Value FROM SemanticLayerSecuritySetupItems A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerSecuritySetupItemId = T.SemanticLayerSecuritySetupItemId EXCEPT SELECT SemanticLayerSecuritySetupItemId,SemanticLayerSecuritySetupId,Type,MemberValue,IsAll,Value FROM SemanticLayerSecuritySetupItems B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerSecuritySetupItemId = T.SemanticLayerSecuritySetupItemId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerSecuritySetupItems A WHERE A.SemanticLayerSecuritySetupItemId = T.SemanticLayerSecuritySetupItemId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Security Setup' ,TxRepositoryTable = 'SemanticLayerSecuritySetupItems' ,ObjectId = T.SemanticLayerSecuritySetupItemId ,ObjectName = CONCAT(T.value, ' ', T.Type, ' ', T.memberValue) ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', D.TableName,'/', D.FieldName, '/', D.SecuritySetupName, '/', CONCAT(T.value, ' ', T.Type, ' ', T.memberValue)) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/field/security setup/item') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerSecuritySetupItems WHERE SemanticLayerSecuritySetupItemId = ''', T.SemanticLayerSecuritySetupItemId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, D.TableName, D.SemanticLayerTableId, D.SemanticLayerFieldId, D.FieldName, T.SecuritySetupName, T.SemanticLayerSecuritySetupId,T.RN FROM (SELECT Layer, LayerName, projectid, SemanticLayerModelId, TableName, SemanticLayerTableId, SemanticLayerFieldId, FieldName=T.Name, rn FROM #semanticlayerfields T) D INNER JOIN (SELECT SecuritySetupName = Name, SemanticLayerSecuritySetupId, SemanticLayerFieldId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerSecuritySetupId ORDER BY ValidTo DESC) FROM SemanticLayerSecuritySetups) T ON D.RN = 1 AND T.SemanticLayerFieldId = D.SemanticLayerFieldId AND D.ProjectId = @projectid ) D INNER JOIN SemanticLayerSecuritySetupItems T ON D.RN = 1 AND T.SemanticLayerSecuritySetupId = D.SemanticLayerSecuritySetupId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerSecuritySetupItems A WHERE A.SemanticLayerSecuritySetupItemId = T.SemanticLayerSecuritySetupItemId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerSecuritySetupItems A WHERE A.SemanticLayerSecuritySetupItemId = T.SemanticLayerSecuritySetupItemId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project UNION ALL SELECT Object = 'Semantic Layer Security Setup' ,TxRepositoryTable = 'SemanticLayerDynamicSecuritySetupItems' ,ObjectId = T.SemanticLayerDynamicSecuritySetupId ,ObjectName = CONCAT('Dynamic setup base table: ',OA_T.TableName) ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', D.TableName,'/', D.FieldName, '/', D.SecuritySetupName, '/', CONCAT('Dynamic setup base table: ',OA_T.TableName)) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/field/security setup/dynamic') ,IsDeleted = 0 ,DeletedLastActiveRange = '' ,IsNewlyAdded = CASE WHEN T.ValidFrom = OA_MinVals.minValidFrom THEN 1 ELSE 0 END ,U.UserName ,U.MachineName ,V.Version ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerDynamicSecuritySetupItems WHERE SemanticLayerDynamicSecuritySetupId = ''', T.SemanticLayerDynamicSecuritySetupId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, D.TableName, D.SemanticLayerTableId, D.SemanticLayerFieldId, D.FieldName, T.SecuritySetupName, T.SemanticLayerSecuritySetupId,T.RN FROM (SELECT Layer, LayerName, projectid, SemanticLayerModelId, TableName, SemanticLayerTableId, SemanticLayerFieldId, FieldName=T.Name, rn FROM #semanticlayerfields T) D INNER JOIN (SELECT SecuritySetupName = Name, SemanticLayerSecuritySetupId, SemanticLayerFieldId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerSecuritySetupId ORDER BY ValidTo DESC) FROM SemanticLayerSecuritySetups) T ON D.RN = 1 AND T.SemanticLayerFieldId = D.SemanticLayerFieldId AND D.ProjectId = @projectid ) D INNER JOIN SemanticLayerDynamicSecuritySetupItems T ON D.RN = 1 AND T.SemanticLayerSecuritySetupId = D.SemanticLayerSecuritySetupId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT minValidFrom = MIN(Validfrom) FROM SemanticLayerDynamicSecuritySetupItems A WHERE T.SemanticLayerDynamicSecuritySetupId = A.SemanticLayerDynamicSecuritySetupId) OA_MinVals OUTER APPLY (SELECT TOP 1 TableName FROM #DataTables A WHERE T.SecurityDataTableId = A.DataTableId AND RN = 1) OA_T WHERE PRJ.ValidFrom BETWEEN @ValidFrom AND @ValidTo AND PRJ.name = @project AND ( EXISTS( -- display only if changes relative to previous version SELECT TOP 1 1 FROM ( SELECT SemanticLayerDynamicSecuritySetupId,SemanticLayerSecuritySetupId,SecurityDataTableId,ValuesDataFieldId,RoleMembersDataFieldId,AllValuesDefinition,AllRoleMembersDefinition FROM SemanticLayerDynamicSecuritySetupItems A WHERE A.ValidTo = T.ValidFrom - 1 AND A.SemanticLayerDynamicSecuritySetupId = T.SemanticLayerDynamicSecuritySetupId EXCEPT SELECT SemanticLayerDynamicSecuritySetupId,SemanticLayerSecuritySetupId,SecurityDataTableId,ValuesDataFieldId,RoleMembersDataFieldId,AllValuesDefinition,AllRoleMembersDefinition FROM SemanticLayerDynamicSecuritySetupItems B WHERE B.ValidFrom = T.ValidFrom AND B.SemanticLayerDynamicSecuritySetupId = T.SemanticLayerDynamicSecuritySetupId ) A ) OR T.ValidFrom = OA_MinVals.minValidFrom -- first version OR ( NOT EXISTS( -- is a restored version SELECT * FROM SemanticLayerDynamicSecuritySetupItems A WHERE A.SemanticLayerDynamicSecuritySetupId = T.SemanticLayerDynamicSecuritySetupId AND A.ValidTo = T.ValidFrom - 1 ) AND T.ValidTo = 99999999 ) ) UNION ALL SELECT Object = 'Semantic Layer Security Setup' ,TxRepositoryTable = 'SemanticLayerDynamicSecuritySetupItems' ,ObjectId = T.SemanticLayerDynamicSecuritySetupId ,ObjectName = CONCAT('Dynamic setup base table: ',OA_T.TableName) ,ObjectLayer = Layer + ':' + LayerName ,ObjectPath = CONCAT('/', PRJ.Name, '/', LayerName, '/', D.TableName,'/', D.FieldName, '/', D.SecuritySetupName, '/', CONCAT('Dynamic setup base table: ',OA_T.TableName)) ,ObjectPathAbstract = CONCAT('/project/', layer, '/table/field/security setup/dynamic') ,IsDeleted = 1 ,DeletedLastActiveRange = CONCAT(T.ValidFrom, ' - ', T.validTo) ,IsNewlyAdded = 0 ,U.UserName ,U.MachineName ,Version = T.ValidTo + 1 ,V.UpdateTime ,V.VersionNote ,ViewAllVersions = CONCAT('SELECT * FROM SemanticLayerDynamicSecuritySetupItems WHERE SemanticLayerDynamicSecuritySetupId = ''', T.SemanticLayerDynamicSecuritySetupId, ''' AND validfrom <=', V.Version,' ORDER BY ValidFrom') FROM ( SELECT D.Layer, D.LayerName, D.ProjectId, D.SemanticLayerModelId, D.TableName, D.SemanticLayerTableId, D.SemanticLayerFieldId, D.FieldName, T.SecuritySetupName, T.SemanticLayerSecuritySetupId,T.RN FROM (SELECT Layer, LayerName, projectid, SemanticLayerModelId, TableName, SemanticLayerTableId, SemanticLayerFieldId, FieldName=T.Name, rn FROM #semanticlayerfields T) D INNER JOIN (SELECT SecuritySetupName = Name, SemanticLayerSecuritySetupId, SemanticLayerFieldId, RN = ROW_NUMBER() OVER(PARTITION BY SemanticLayerSecuritySetupId ORDER BY ValidTo DESC) FROM SemanticLayerSecuritySetups) T ON D.RN = 1 AND T.SemanticLayerFieldId = D.SemanticLayerFieldId AND D.ProjectId = @projectid ) D INNER JOIN SemanticLayerDynamicSecuritySetupItems T ON D.RN = 1 AND T.SemanticLayerSecuritySetupId = D.SemanticLayerSecuritySetupId INNER JOIN Projects PRJ ON D.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = T.ValidFrom INNER JOIN Versions V ON V.ProjectId = PRJ.ProjectId AND PRJ.ValidFrom = V.Version INNER JOIN Users U ON V.UserId = U.UserId OUTER APPLY (SELECT TOP 1 TableName FROM #DataTables A WHERE T.SecurityDataTableId = A.DataTableId AND RN = 1) OA_T WHERE ( T.ValidTo >= ( -- if the object was removed just after the last check also display SELECT MAX(A.validto) FROM SemanticLayerDynamicSecuritySetupItems A WHERE A.SemanticLayerDynamicSecuritySetupId = T.SemanticLayerDynamicSecuritySetupId AND A.ValidTo >= @ValidFrom - 1 ) OR NOT EXISTS( -- indication if ever removed; afterwards restored SELECT * FROM SemanticLayerDynamicSecuritySetupItems A WHERE A.SemanticLayerDynamicSecuritySetupId = T.SemanticLayerDynamicSecuritySetupId AND T.ValidTo + 1 = A.ValidFrom ) ) AND T.ValidTo <> 99999999 AND T.ValidTo + 1 >= @ValidFrom AND PRJ.name = @project order by 5, 1,3, 14