Event data set

Microsoft.SystemCenter.DataWarehouse.DataSet.Event (DataWarehouseDataSet)

Defines storage and aggregation for event information

Element properties:

AccessibilityPublic

Source Code:

<DataWarehouseDataSet ID="Microsoft.SystemCenter.DataWarehouse.DataSet.Event" Accessibility="Public">
<Dependencies>
<DataWarehouseScript>Microsoft.SystemCenter.DataWarehouse.Script.DatasetDebuggingSupport</DataWarehouseScript>
<DataWarehouseScript>Microsoft.SystemCenter.DataWarehouse.Script.StandardDatasetSupport</DataWarehouseScript>
</Dependencies>
<Configuration>
<IncludeSchemaTypes>
<SchemaType>Microsoft.SystemCenter.DataWarehouse.StandardDatasetSchema</SchemaType>
</IncludeSchemaTypes>
<xsd:element name="Storage" type="AggregationSettingsType"/>
<xsd:element name="RawInsertTableCount" type="xsd:positiveInteger"/>
<xsd:element name="BlockingMaintenanceDailyStartTime" type="TimeOfDayType"/>
<xsd:element name="BlockingMaintenanceDurationMinutes" type="xsd:positiveInteger"/>
</Configuration>
<Install>
SET NOCOUNT ON

EXEC StandardDatasetDelete @DatasetId = '$Config/DatasetId$'
GO

INSERT StandardDataset (
DatasetId
,SchemaName
,DefaultAggregationIntervalCount
,RawInsertTableCount
,StagingProcessorStoredProcedureName
,BlockingMaintenanceDailyStartTime
,BlockingMaintenanceDurationMinutes
)
VALUES
(
'$Config/DatasetId$'
,'Event'
,0
,$Config/RawInsertTableCount$
,'EventProcessStaging'
,'$Config/BlockingMaintenanceDailyStartTime$'
,$Config/BlockingMaintenanceDurationMinutes$
)
GO

SET NOCOUNT ON

INSERT StandardDatasetAggregation
(
DatasetId
,AggregationTypeId
,AggregationIntervalDurationMinutes
,AggregationStartDelayMinutes
,BuildAggregationStoredProcedureName
,DeleteAggregationStoredProcedureName
,GroomStoredProcedureName
,IndexOptimizationIntervalMinutes
,MaxDataAgeDays
,GroomingIntervalMinutes
,MaxRowsToGroom
,LastGroomingDateTime
,DataFileGroupName
,IndexFileGroupName
)
VALUES (
'$Config/DatasetId$'
,0
,NULL
,NULL
,NULL
,NULL
,'EventGroom'
,$Config/Storage/IndexOptimizationIntervalMinutes$
,$Config/Storage/MaxDataAgeDays$
,$Config/Storage/GroomingIntervalMinutes$
,$Config/Storage/MaxRowsToGroom$
,GETUTCDATE()
,ISNULL(CAST(NULLIF('$Config/Storage/DataFileGroupName$', '') AS sysname), 'default')
,ISNULL(CAST(NULLIF('$Config/Storage/IndexFileGroupName$', '') AS sysname), 'default')
)
GO

SET NOCOUNT ON

DECLARE
@MaxTableRowCount int
,@MaxTableSizeKb int

SELECT
@MaxTableSizeKb = NULLIF('$Config/Storage/MaxTableSizeKb$', '')
,@MaxTableRowCount = NULLIF('$Config/Storage/MaxTableRowCount$', '')

INSERT StandardDatasetAggregationStorage
(
DatasetId
,AggregationTypeId
,BaseTableName
,DependentTableInd
,TableTemplate
,CoverViewSelectClause
,MaxTableRowCount
,MaxTableSizeKb
)
VALUES
(
'$Config/DatasetId$'
,0
,'Event'
,0
,'
CREATE TABLE [Event].[Event_$Guid$]
(
EventRowId int NOT NULL IDENTITY(1, 1)
,EventOriginId uniqueidentifier NOT NULL
,[DateTime] datetime NOT NULL
,EventPublisherRowId int NOT NULL
,EventChannelRowId smallint NOT NULL
,EventCategoryRowId int NOT NULL
,EventLevelId tinyint NOT NULL
,LoggingComputerRowId int NOT NULL
,EventNumber bigint NOT NULL
,EventDisplayNumber int NOT NULL
,UserNameRowId int NOT NULL
,RawDescriptionHash uniqueidentifier NULL
,ParameterHash uniqueidentifier NULL
,EventDataHash uniqueidentifier NULL

,CONSTRAINT [PK_Event_$Guid$] PRIMARY KEY CLUSTERED (EventRowId) ON $DataFileGroupName$
) ON $DataFileGroupName$
'
,'SELECT
EventOriginId
,[DateTime]
,EventPublisherRowId
,EventChannelRowId
,EventCategoryRowId
,EventLevelId
,LoggingComputerRowId
,EventNumber
,EventDisplayNumber
,UserNameRowId
,RawDescriptionHash
,ParameterHash
,EventDataHash
'
,@MaxTableRowCount
,@MaxTableSizeKb
)

INSERT StandardDatasetAggregationStorage
(
DatasetId
,AggregationTypeId
,BaseTableName
,TableTag
,DependentTableInd
,TableTemplate
,CoverViewSelectClause
,MaxTableRowCount
,MaxTableSizeKb
)
VALUES
(
'$Config/DatasetId$'
,0
,'EventRule'
,'rule'
,1
,'
CREATE TABLE [Event].[EventRule_$Guid$]
(
EventRuleRowId int NOT NULL IDENTITY(1, 1)
,EventOriginId uniqueidentifier NOT NULL
,RuleRowId int NOT NULL
,ManagedEntityRowId int NOT NULL

,CONSTRAINT [PK_EventRule_$Guid$] PRIMARY KEY CLUSTERED (EventRuleRowId) ON $DataFileGroupName$

) ON $DataFileGroupName$
'
,'SELECT
EventOriginId
,RuleRowId
,ManagedEntityRowId
'
,@MaxTableRowCount
,@MaxTableSizeKb
)

INSERT StandardDatasetAggregationStorage
(
DatasetId
,AggregationTypeId
,BaseTableName
,TableTag
,DependentTableInd
,TableTemplate
,CoverViewSelectClause
,MaxTableRowCount
,MaxTableSizeKb
)
VALUES
(
'$Config/DatasetId$'
,0
,'EventParameter'
,'parameter'
,1
,'
CREATE TABLE [Event].[EventParameter_$Guid$]
(
EventParameterRowId int NOT NULL IDENTITY(1, 1)
,EventOriginId uniqueidentifier NOT NULL
,ParameterIndex tinyint NOT NULL
,ParameterValue nvarchar(max) NULL

,CONSTRAINT [PK_EventParameter_$Guid$] PRIMARY KEY CLUSTERED (EventParameterRowId) ON $DataFileGroupName$

) ON $DataFileGroupName$
'
,'SELECT
EventOriginId
,ParameterIndex
,ParameterValue
'
,@MaxTableRowCount
,@MaxTableSizeKb
)

INSERT StandardDatasetAggregationStorage
(
DatasetId
,AggregationTypeId
,BaseTableName
,TableTag
,DependentTableInd
,TableTemplate
,CoverViewSelectClause
,MaxTableRowCount
,MaxTableSizeKb
)
VALUES
(
'$Config/DatasetId$'
,0
,'EventDetail'
,'detail'
,1
,'
CREATE TABLE [Event].[EventDetail_$Guid$]
(
EventDetailRowId int NOT NULL IDENTITY(1, 1)
,EventOriginId uniqueidentifier NOT NULL
,RawDescription nvarchar(max) NULL
,RenderedDescription nvarchar(max) NULL
,EventData xml NULL

,CONSTRAINT [PK_EventDetail_$Guid$] PRIMARY KEY CLUSTERED (EventDetailRowId) ON $DataFileGroupName$
) ON $DataFileGroupName$
'
,'SELECT
EventOriginId
,RawDescription
,RenderedDescription
,EventData
'
,@MaxTableRowCount
,@MaxTableSizeKb
)
GO

-- main event table indexes

EXEC StandardDatasetIndexDelete
@DatasetId = '$Config/DatasetId$'
,@IndexGuid = 'FADEE782-6397-4D34-B0C9-10DD260C7D76'
,@DeleteIndexOnExistingTablesInd = 0

EXEC StandardDatasetIndexInsert
@DatasetId = '$Config/DatasetId$'
,@AggregationTypeId = 0
,@DependentTableInd = 0
,@TableTag = NULL
,@UniqueInd = 0
,@IndexGuid = 'FADEE782-6397-4D34-B0C9-10DD260C7D76'
,@IndexDefinition = '([DateTime])'
,@CreateIndexOnExistingTablesInd = 0

EXEC StandardDatasetIndexDelete
@DatasetId = '$Config/DatasetId$'
,@IndexGuid = '4CFFAA7E-9B6C-432C-B90C-0A28994FAE6F'
,@DeleteIndexOnExistingTablesInd = 0

EXEC StandardDatasetIndexInsert
@DatasetId = '$Config/DatasetId$'
,@AggregationTypeId = 0
,@DependentTableInd = 0
,@TableTag = NULL
,@UniqueInd = 1
,@IndexGuid = '4CFFAA7E-9B6C-432C-B90C-0A28994FAE6F'
,@IndexDefinition = '(EventOriginId)'
,@CreateIndexOnExistingTablesInd = 0

-- parameter table indexes

EXEC StandardDatasetIndexDelete
@DatasetId = '$Config/DatasetId$'
,@IndexGuid = '2E308EFB-5FFB-48ac-8A55-A43751F569F5'
,@DeleteIndexOnExistingTablesInd = 0

EXEC StandardDatasetIndexInsert
@DatasetId = '$Config/DatasetId$'
,@AggregationTypeId = 0
,@DependentTableInd = 1
,@TableTag = 'parameter'
,@UniqueInd = 1
,@IndexGuid = '2E308EFB-5FFB-48ac-8A55-A43751F569F5'
,@IndexDefinition = '(EventOriginId, ParameterIndex)'
,@CreateIndexOnExistingTablesInd = 0

-- detail table indexes

EXEC StandardDatasetIndexDelete
@DatasetId = '$Config/DatasetId$'
,@IndexGuid = '04937270-50DE-49b8-AB6F-0E214749914A'
,@DeleteIndexOnExistingTablesInd = 0

EXEC StandardDatasetIndexInsert
@DatasetId = '$Config/DatasetId$'
,@AggregationTypeId = 0
,@DependentTableInd = 1
,@TableTag = 'detail'
,@UniqueInd = 1
,@IndexGuid = '04937270-50DE-49b8-AB6F-0E214749914A'
,@IndexDefinition = '(EventOriginId)'
,@CreateIndexOnExistingTablesInd = 0

IF NOT EXISTS (SELECT * FROM sys.schemas WHERE [name] = 'Event')
BEGIN
EXECUTE('CREATE SCHEMA Event')
END
GO

-- change schema ownership to allow index rebuild and reorganize
ALTER AUTHORIZATION ON SCHEMA::Event TO OpsMgrWriter
GO

-- change schema alter to allow domain table index rebuild
GRANT ALTER ON SCHEMA::dbo TO OpsMgrWriter
GO

-- grant create table permissions
GRANT CREATE TABLE TO OpsMgrWriter
GO

EXEC StandardDatasetAllocateStorage @DatasetId = '$Config/DatasetId$', @AggregationTypeId = 0
GO


IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EventCategory' AND TABLE_SCHEMA = 'dbo')
BEGIN
DROP TABLE dbo.EventCategory
END
GO

CREATE TABLE dbo.EventCategory
(
EventCategoryRowId int NOT NULL IDENTITY(1,1)
,EventPublisherRowId int NOT NULL
,EventCategoryId int NOT NULL
,EventCategoryTitle nvarchar(255) NOT NULL
,LastReceivedDateTime smalldatetime NOT NULL DEFAULT (GETUTCDATE())

,CONSTRAINT PK_EventCategory PRIMARY KEY CLUSTERED (EventCategoryRowId)
,CONSTRAINT UN_EventCategory_PublisherIdEventCategoryId UNIQUE (EventPublisherRowId, EventCategoryId)
)
GO

CREATE INDEX IX_EventCategory_LastReceivedDateTime ON EventCategory (LastReceivedDateTime)
GO

EXEC DomainTableRegisterIndexOptimization
@TableName = 'EventCategory'
,@DatasetId = '$Config/DatasetId$'
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EventChannel' AND TABLE_SCHEMA = 'dbo')
BEGIN
DROP TABLE dbo.EventChannel
END
GO

CREATE TABLE dbo.EventChannel
(
EventChannelRowId smallint NOT NULL IDENTITY(1,1)
,EventChannelTitle nvarchar(255) NOT NULL
,LastReceivedDateTime smalldatetime NOT NULL DEFAULT (GETUTCDATE())

,CONSTRAINT PK_EventChannel PRIMARY KEY CLUSTERED (EventChannelRowId)
,CONSTRAINT UN_EventChannel_EventChannelTitle UNIQUE (EventChannelTitle)
)
GO

CREATE INDEX IX_EventChannel_LastReceivedDateTime ON EventChannel (LastReceivedDateTime)
GO

EXEC DomainTableRegisterIndexOptimization
@TableName = 'EventChannel'
,@DatasetId = '$Config/DatasetId$'
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EventLevel' AND TABLE_SCHEMA = 'dbo')
BEGIN
DROP TABLE dbo.EventLevel
END
GO

CREATE TABLE dbo.EventLevel
(
EventLevelId tinyint NOT NULL
,EventLevelTitle nvarchar(255) NOT NULL

,CONSTRAINT PK_EventLevel PRIMARY KEY CLUSTERED (EventLevelId)
)
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EventLoggingComputer' AND TABLE_SCHEMA = 'dbo')
BEGIN
DROP TABLE dbo.EventLoggingComputer
END
GO

CREATE TABLE dbo.EventLoggingComputer
(
EventLoggingComputerRowId int NOT NULL IDENTITY(1,1)
,ComputerName nvarchar(255) NOT NULL
,LastReceivedDateTime smalldatetime NOT NULL DEFAULT (GETUTCDATE())

,CONSTRAINT PK_EventLoggingComputer PRIMARY KEY CLUSTERED (EventLoggingComputerRowId)
,CONSTRAINT UN_EventLoggingComputer_ComputerName UNIQUE (ComputerName)
)
GO

CREATE INDEX IX_EventLoggingComputer_LastReceivedDateTime ON EventLoggingComputer (LastReceivedDateTime)
GO

EXEC DomainTableRegisterIndexOptimization
@TableName = 'EventLoggingComputer'
,@DatasetId = '$Config/DatasetId$'
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EventPublisher' AND TABLE_SCHEMA = 'dbo')
BEGIN
DROP TABLE dbo.EventPublisher
END
GO

CREATE TABLE dbo.EventPublisher
(
EventPublisherRowId int NOT NULL IDENTITY(1,1)
,EventPublisherGuid uniqueidentifier NOT NULL
,EventPublisherName nvarchar(255) NOT NULL
,LastReceivedDateTime smalldatetime NOT NULL DEFAULT (GETUTCDATE())

,CONSTRAINT PK_EventPublisher PRIMARY KEY CLUSTERED (EventPublisherRowId)
,CONSTRAINT UN_EventPublisher_PublisherGuid UNIQUE (EventPublisherGuid)
)
GO

CREATE INDEX IX_EventPublisher_LastReceivedDateTime ON EventPublisher (LastReceivedDateTime)
GO

EXEC DomainTableRegisterIndexOptimization
@TableName = 'EventPublisher'
,@DatasetId = '$Config/DatasetId$'
GO



-- insert staging definition into StandardDatasetStagingArea
BEGIN TRAN

IF EXISTS (SELECT * FROM StandardDatasetStagingArea WHERE DatasetId = '$Config/DatasetId$')
BEGIN
DELETE StandardDatasetStagingArea
WHERE DatasetId = '$Config/DatasetId$'
END

INSERT StandardDatasetStagingArea
(
DatasetId
,StagingTableColumnDefinition
,MaxRowsToProcessPerTransactionCount
,BatchedProcessingSupportedInd
)
VALUES
(
'$Config/DatasetId$'
,'
DatasetId uniqueidentifier NOT NULL
,ManagementGroupGuid uniqueidentifier NOT NULL
,EventOriginId uniqueidentifier NOT NULL
,[DateTime] datetime NOT NULL
,RuleRowId int NOT NULL
,ManagedEntityRowId int NOT NULL
,EventPublisherRowId int NOT NULL
,EventChannelRowId smallint NOT NULL
,EventCategoryRowId int NOT NULL
,EventLevelId tinyint NOT NULL
,LoggingComputerRowId int NOT NULL
,EventDisplayNumber int NOT NULL
,EventNumber bigint NOT NULL
,UserNameRowId int NOT NULL
,RawDescription nvarchar(max) NULL
,RenderedDescription nvarchar(max) NULL
,EventParameters xml NULL
,EventData xml NULL
,RawDescriptionHash uniqueidentifier NULL
,ParameterHash uniqueidentifier NULL
,EventDataHash uniqueidentifier NULL

,EventStageRowId bigint NOT NULL IDENTITY(1, 1) PRIMARY KEY
'
,20000
,1
)

COMMIT

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EventStage' AND TABLE_SCHEMA = 'Event')
BEGIN
DECLARE @Statement nvarchar(max)

SELECT @Statement = 'CREATE TABLE Event.EventStage (' + StagingTableColumnDefinition + ')'
FROM StandardDatasetStagingArea
WHERE DatasetId = '$Config/DatasetId$'

EXECUTE (@Statement)
END
GO

GRANT ALTER, SELECT, INSERT, UPDATE, DELETE ON Event.EventStage TO OpsMgrWriter
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EventUserName' AND TABLE_SCHEMA = 'dbo')
BEGIN
DROP TABLE dbo.EventUserName
END
GO

CREATE TABLE dbo.EventUserName
(
EventUserNameRowId int NOT NULL IDENTITY(1,1)
,UserName nvarchar(255) NOT NULL
,LastReceivedDateTime smalldatetime NOT NULL DEFAULT (GETUTCDATE())

,CONSTRAINT PK_EventUserName PRIMARY KEY CLUSTERED (EventUserNameRowId)
,CONSTRAINT UN_EventUserName_UserName UNIQUE (UserName)
)
GO

CREATE INDEX IX_EventUserName_LastReceivedDateTime ON EventUserName (LastReceivedDateTime)
GO

EXEC DomainTableRegisterIndexOptimization
@TableName = 'EventUserName'
,@DatasetId = '$Config/DatasetId$'
GO


SET NOCOUNT ON

DELETE EventLevel

INSERT EventLevel(EventLevelId, EventLevelTitle) VALUES ( 0, N'Undefined')
INSERT EventLevel(EventLevelId, EventLevelTitle) VALUES ( 1, N'Error')
INSERT EventLevel(EventLevelId, EventLevelTitle) VALUES ( 2, N'Warning')
INSERT EventLevel(EventLevelId, EventLevelTitle) VALUES ( 4, N'Information')
INSERT EventLevel(EventLevelId, EventLevelTitle) VALUES ( 8, N'Success Audit')
INSERT EventLevel(EventLevelId, EventLevelTitle) VALUES (16, N'Failure Audit')
GO


IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'V' AND name = 'vEventCategory')
BEGIN
EXECUTE ('CREATE VIEW dbo.vEventCategory AS SELECT A = 1')
END
GO

ALTER VIEW dbo.vEventCategory
AS
SELECT *
FROM dbo.EventCategory (NOLOCK)
GO

GRANT SELECT ON dbo.vEventCategory TO OpsMgrReader, OpsMgrWriter
GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'V' AND name = 'vEventChannel')
BEGIN
EXECUTE ('CREATE VIEW dbo.vEventChannel AS SELECT A = 1')
END
GO

ALTER VIEW dbo.vEventChannel
AS
SELECT *
FROM dbo.EventChannel (NOLOCK)
GO

GRANT SELECT ON dbo.vEventChannel TO OpsMgrReader, OpsMgrWriter
GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'V' AND name = 'vEventLevel')
BEGIN
EXECUTE ('CREATE VIEW dbo.vEventLevel AS SELECT A = 1')
END
GO

ALTER VIEW dbo.vEventLevel
AS
SELECT *
FROM dbo.EventLevel (NOLOCK)
GO

GRANT SELECT ON dbo.vEventLevel TO OpsMgrReader, OpsMgrWriter
GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'V' AND name = 'vEventLoggingComputer')
BEGIN
EXECUTE ('CREATE VIEW dbo.vEventLoggingComputer AS SELECT A = 1')
END
GO

ALTER VIEW dbo.vEventLoggingComputer
AS
SELECT *
FROM dbo.EventLoggingComputer (NOLOCK)
GO

GRANT SELECT ON dbo.vEventLoggingComputer TO OpsMgrReader, OpsMgrWriter
GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'V' AND name = 'vEventPublisher')
BEGIN
EXECUTE ('CREATE VIEW dbo.vEventPublisher AS SELECT A = 1')
END
GO

ALTER VIEW dbo.vEventPublisher
AS
SELECT *
FROM dbo.EventPublisher (NOLOCK)
GO

GRANT SELECT ON dbo.vEventPublisher TO OpsMgrReader, OpsMgrWriter
GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'V' AND name = 'vEventUserName')
BEGIN
EXECUTE ('CREATE VIEW dbo.vEventUserName AS SELECT A = 1')
END
GO

ALTER VIEW dbo.vEventUserName
AS
SELECT *
FROM dbo.EventUserName (NOLOCK)
GO

GRANT SELECT ON dbo.vEventUserName TO OpsMgrReader, OpsMgrWriter
GO


IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventCategoryRowIdResolve')
BEGIN
EXECUTE ('CREATE PROCEDURE dbo.EventCategoryRowIdResolve AS RETURN 1')
END
GO

ALTER PROCEDURE dbo.EventCategoryRowIdResolve
@ManagementGroupGuid uniqueidentifier
,@ObjectKeyXml ntext
AS
BEGIN
SET NOCOUNT ON

DECLARE @ManagementGroupRowId int

-- check management group is connected to this DW
DECLARE @CheckExecResult int

EXEC @CheckExecResult = ManagementGroupAccessCheck @ManagementGroupGuid, @ManagementGroupRowId OUTPUT
IF (@CheckExecResult &lt;&gt; 0) RETURN @CheckExecResult

DECLARE
@ErrorInd bit
,@ErrorMessage nvarchar(max)
,@ErrorNumber int
,@ErrorSeverity int
,@ErrorState int
,@ErrorLine int
,@ErrorProcedure nvarchar(256)
,@ErrorMessageText nvarchar(max)

SET @ErrorInd = 0

BEGIN TRY

DECLARE
@ExecResult int
,@XmlDocHandle int

EXEC @ExecResult = sp_xml_preparedocument @XmlDocHandle OUTPUT, @ObjectKeyXml
IF @ExecResult &lt;&gt; 0 RAISERROR(777971000, 16, 1, 'EventCategoryKeys', @ExecResult)

IF (OBJECT_ID('tempdb..#EventCategory') IS NOT NULL)
DROP TABLE #EventCategory

CREATE TABLE #EventCategory (
NodeOrdinal int NOT NULL
,PublisherGuid uniqueidentifier NOT NULL
,PublisherName nvarchar(256) NOT NULL
,CategoryId int NOT NULL
,CategoryTitle nvarchar(256) NOT NULL
)

INSERT #EventCategory (
NodeOrdinal
,PublisherGuid
,PublisherName
,CategoryId
,CategoryTitle
)
SELECT
NodeOrdinal
,PublisherGuid
,PublisherName
,CategoryId
,CategoryTitle
FROM OPENXML(@XmlDocHandle, 'Keys/Key', 2) WITH (
NodeOrdinal int '@mp:id'
,PublisherGuid uniqueidentifier 'PublisherGuid'
,PublisherName nvarchar(256) 'PublisherName'
,CategoryId int 'CategoryId'
,CategoryTitle nvarchar(256) 'CategoryTitle') xml

EXEC sp_xml_removedocument @XmlDocHandle
SET @XmlDocHandle = NULL

-- work with publisher domain first

UPDATE ep
SET LastReceivedDateTime = GETUTCDATE()
FROM EventPublisher ep
JOIN #EventCategory e ON (ep.EventPublisherGuid = e.PublisherGuid)
WHERE (ep.LastReceivedDateTime &lt; DATEADD(day, -1, GETUTCDATE()))

INSERT EventPublisher (EventPublisherGuid, EventPublisherName)
SELECT DISTINCT e.PublisherGuid, e.PublisherName
FROM #EventCategory e
WHERE NOT EXISTS (SELECT * FROM EventPublisher WHERE (EventPublisherGuid = e.PublisherGuid))

-- update category domain

UPDATE ec
SET LastReceivedDateTime = GETUTCDATE()
FROM EventCategory ec
JOIN EventPublisher ep ON (ec.EventPublisherRowId = ep.EventPublisherRowId)
JOIN #EventCategory e ON (ec.EventCategoryId = e.CategoryId) AND (ep.EventPublisherGuid = e.PublisherGuid)
WHERE (ec.LastReceivedDateTime &lt; DATEADD(day, -1, GETUTCDATE()))

INSERT EventCategory (
EventPublisherRowId
,EventCategoryId
,EventCategoryTitle
)
SELECT DISTINCT
ep.EventPublisherRowId
,e.CategoryId
,e.CategoryTitle
FROM #EventCategory e
JOIN EventPublisher ep ON (e.PublisherGuid = ep.EventPublisherGuid)
WHERE NOT EXISTS (SELECT *
FROM EventCategory
WHERE (EventCategoryId = e.CategoryId)
AND (EventPublisherRowId = ep.EventPublisherRowId)
)

SELECT
ec.EventCategoryRowId
FROM #EventCategory e
JOIN EventPublisher ep ON (e.PublisherGuid = ep.EventPublisherGuid)
JOIN EventCategory ec ON (e.CategoryId = ec.EventCategoryId) AND (ec.EventPublisherRowId = ep.EventPublisherRowId)
ORDER BY e.NodeOrdinal
END TRY
BEGIN CATCH
IF (@@TRANCOUNT &gt; 0)
ROLLBACK TRAN

SELECT
@ErrorNumber = ERROR_NUMBER()
,@ErrorSeverity = ERROR_SEVERITY()
,@ErrorState = ERROR_STATE()
,@ErrorLine = ERROR_LINE()
,@ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-')
,@ErrorMessageText = ERROR_MESSAGE()

SET @ErrorInd = 1
END CATCH

IF (@XmlDocHandle IS NOT NULL)
EXEC sp_xml_removedocument @XmlDocHandle

IF (OBJECT_ID('tempdb..#EventCategory') IS NOT NULL)
DROP TABLE #EventCategory

-- report error if any
IF (@ErrorInd = 1)
BEGIN
DECLARE @AdjustedErrorSeverity int

SET @AdjustedErrorSeverity = CASE
WHEN @ErrorSeverity &gt; 18 THEN 18
ELSE @ErrorSeverity
END

RAISERROR (777971002, @AdjustedErrorSeverity, 1
,@ErrorNumber
,@ErrorSeverity
,@ErrorState
,@ErrorProcedure
,@ErrorLine
,@ErrorMessageText
)
END
END
GO

GRANT EXECUTE ON EventCategoryRowIdResolve TO OpsMgrWriter
GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventChannelRowIdResolve')
BEGIN
EXECUTE ('CREATE PROCEDURE dbo.EventChannelRowIdResolve AS RETURN 1')
END
GO

ALTER PROCEDURE dbo.EventChannelRowIdResolve
@ManagementGroupGuid uniqueidentifier
,@ObjectKeyXml ntext
AS
BEGIN
SET NOCOUNT ON

DECLARE @ManagementGroupRowId int

-- check management group is connected to this DW
DECLARE @CheckExecResult int

EXEC @CheckExecResult = ManagementGroupAccessCheck @ManagementGroupGuid, @ManagementGroupRowId OUTPUT
IF (@CheckExecResult &lt;&gt; 0) RETURN @CheckExecResult

DECLARE
@ErrorInd bit
,@ErrorMessage nvarchar(max)
,@ErrorNumber int
,@ErrorSeverity int
,@ErrorState int
,@ErrorLine int
,@ErrorProcedure nvarchar(256)
,@ErrorMessageText nvarchar(max)

SET @ErrorInd = 0

BEGIN TRY

DECLARE
@ExecResult int
,@XmlDocHandle int

EXEC @ExecResult = sp_xml_preparedocument @XmlDocHandle OUTPUT, @ObjectKeyXml
IF @ExecResult &lt;&gt; 0 RAISERROR(777971000, 16, 1, 'EventChannelKeys', @ExecResult)

IF (OBJECT_ID('tempdb..#EventChannel') IS NOT NULL)
DROP TABLE #EventChannel

CREATE TABLE #EventChannel (
NodeOrdinal int NOT NULL
,ChannelName nvarchar(256) NOT NULL
)

INSERT #EventChannel (
NodeOrdinal
,ChannelName
)
SELECT
NodeOrdinal
,ChannelName
FROM OPENXML(@XmlDocHandle, 'Keys/Key', 2) WITH (
NodeOrdinal int '@mp:id'
,ChannelName nvarchar(256) '.') xml

EXEC sp_xml_removedocument @XmlDocHandle
SET @XmlDocHandle = NULL

UPDATE ec
SET LastReceivedDateTime = GETUTCDATE()
FROM EventChannel ec
JOIN #EventChannel e ON (ec.EventChannelTitle = e.ChannelName COLLATE database_default)
WHERE (ec.LastReceivedDateTime &lt; DATEADD(day, -1, GETUTCDATE()))

INSERT EventChannel (EventChannelTitle)
SELECT DISTINCT e.ChannelName
FROM #EventChannel e
WHERE NOT EXISTS (SELECT * FROM EventChannel WHERE (EventChannelTitle = e.ChannelName COLLATE database_default))

SELECT
CAST(ec.EventChannelRowId AS int)
FROM #EventChannel e
JOIN EventChannel ec ON (e.ChannelName = ec.EventChannelTitle COLLATE database_default)
ORDER BY e.NodeOrdinal
END TRY
BEGIN CATCH
IF (@@TRANCOUNT &gt; 0)
ROLLBACK TRAN

SELECT
@ErrorNumber = ERROR_NUMBER()
,@ErrorSeverity = ERROR_SEVERITY()
,@ErrorState = ERROR_STATE()
,@ErrorLine = ERROR_LINE()
,@ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-')
,@ErrorMessageText = ERROR_MESSAGE()

SET @ErrorInd = 1
END CATCH

IF (@XmlDocHandle IS NOT NULL)
EXEC sp_xml_removedocument @XmlDocHandle

IF (OBJECT_ID('tempdb..#EventChannel') IS NOT NULL)
DROP TABLE #EventChannel

-- report error if any
IF (@ErrorInd = 1)
BEGIN
DECLARE @AdjustedErrorSeverity int

SET @AdjustedErrorSeverity = CASE
WHEN @ErrorSeverity &gt; 18 THEN 18
ELSE @ErrorSeverity
END

RAISERROR (777971002, @AdjustedErrorSeverity, 1
,@ErrorNumber
,@ErrorSeverity
,@ErrorState
,@ErrorProcedure
,@ErrorLine
,@ErrorMessageText
)
END
END
GO

GRANT EXECUTE ON EventChannelRowIdResolve TO OpsMgrWriter
GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventGroom')
BEGIN
EXECUTE ('CREATE PROCEDURE dbo.EventGroom AS RETURN 1')
END
GO

ALTER PROCEDURE dbo.EventGroom
@DatasetId uniqueidentifier
,@AggregationTypeId tinyint
,@TableGuid uniqueidentifier
,@CutoffDateTime datetime
,@MaxRowsToGroom int
,@RowsDeleted int OUTPUT
AS
BEGIN
SET NOCOUNT ON

DECLARE
@ErrorInd bit
,@ErrorMessage nvarchar(4000)
,@ErrorNumber int
,@ErrorSeverity int
,@ErrorState int
,@ErrorLine int
,@ErrorProcedure sysname
,@ErrorMessageText nvarchar(4000)

SET @ErrorInd = 0

DECLARE
@MainTableName sysname
,@TableName sysname
,@Statement nvarchar(max)
,@DebugLevel tinyint
,@StandardDatasetAggregationStorageRowId int
,@SchemaName sysname

BEGIN TRY
SELECT
@DebugLevel = DebugLevel
,@SchemaName = SchemaName
FROM StandardDataset
WHERE DatasetId = @DatasetId

BEGIN TRAN

CREATE TABLE #EventGroom (
EventOriginId uniqueidentifier NOT NULL
)

SELECT @MainTableName = BaseTableName + '_' + REPLACE(CAST(@TableGuid AS varchar(50)), '-', '')
FROM StandardDatasetAggregationStorage
WHERE (DatasetId = @DatasetId)
AND (AggregationTypeId = @AggregationTypeId)
AND (DependentTableInd = 0)

SET @Statement = 'INSERT #EventGroom (EventOriginId)'
+ ' SELECT TOP ' + CAST(@MaxRowsToGroom AS varchar(15)) + ' EventOriginId'
+ ' FROM ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@MainTableName)
+ ' WHERE ([DateTime] &lt; CONVERT(datetime, ''' + CONVERT(varchar(50), @CutoffDateTime, 120) + ''', 120))'

EXECUTE (@Statement)

SET @StandardDatasetAggregationStorageRowId = 0

WHILE EXISTS (SELECT *
FROM StandardDatasetAggregationStorage
WHERE (DatasetId = @DatasetId)
AND (AggregationTypeId = @AggregationTypeId)
AND (DependentTableInd = 1)
AND (StandardDatasetAggregationStorageRowId &gt; @StandardDatasetAggregationStorageRowId)
)
BEGIN
SELECT TOP 1
@StandardDatasetAggregationStorageRowId = StandardDatasetAggregationStorageRowId
,@TableName = BaseTableName + '_' + REPLACE(CAST(@TableGuid AS varchar(50)), '-', '')
FROM StandardDatasetAggregationStorage
WHERE (DatasetId = @DatasetId)
AND (AggregationTypeId = @AggregationTypeId)
AND (DependentTableInd = 1)
AND (StandardDatasetAggregationStorageRowId &gt; @StandardDatasetAggregationStorageRowId)
Order By StandardDatasetAggregationStorageRowId Asc

SET @Statement = 'DELETE d'
+ ' FROM ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + ' d'
+ ' JOIN #EventGroom e ON (d.EventOriginId = e.EventOriginId)'
EXECUTE (@Statement)
END

SET @Statement = 'DELETE d'
+ ' FROM ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@MainTableName) +' d'
+ ' JOIN #EventGroom e ON (d.EventOriginId = e.EventOriginId)'
EXECUTE (@Statement)
SET @RowsDeleted = @@ROWCOUNT

DROP TABLE #EventGroom

COMMIT

-- groom dynamic dimentions
DECLARE @MaxDataAgeDays int

SELECT @MaxDataAgeDays = MAX(MaxDataAgeDays)
FROM StandardDatasetAggregation
WHERE (DatasetId = @DatasetId)

DELETE e
FROM EventCategory e
WHERE LastReceivedDateTime &lt; DATEADD(day, -@MaxDataAgeDays, GETUTCDATE())
OPTION (RECOMPILE)

DELETE e
FROM EventChannel e
WHERE LastReceivedDateTime &lt; DATEADD(day, -@MaxDataAgeDays, GETUTCDATE())
OPTION (RECOMPILE)

DELETE e
FROM EventLoggingComputer e
WHERE LastReceivedDateTime &lt; DATEADD(day, -@MaxDataAgeDays, GETUTCDATE())
OPTION (RECOMPILE)

DELETE e
FROM EventPublisher e
WHERE LastReceivedDateTime &lt; DATEADD(day, -@MaxDataAgeDays, GETUTCDATE())
OPTION (RECOMPILE)

DELETE e
FROM EventUserName e
WHERE LastReceivedDateTime &lt; DATEADD(day, -@MaxDataAgeDays, GETUTCDATE())
OPTION (RECOMPILE)
END TRY
BEGIN CATCH
IF (@@TRANCOUNT &gt; 0)
ROLLBACK TRAN

SELECT
@ErrorNumber = ERROR_NUMBER()
,@ErrorSeverity = ERROR_SEVERITY()
,@ErrorState = ERROR_STATE()
,@ErrorLine = ERROR_LINE()
,@ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-')
,@ErrorMessageText = ERROR_MESSAGE()

SET @ErrorInd = 1
END CATCH

-- report error if any
IF (@ErrorInd = 1)
BEGIN
IF (@DebugLevel &gt; 0)
BEGIN
DECLARE @DebugMessageText nvarchar(max)

SET @DebugMessageText = N'Failed to groom data for event data set. Error ' + CAST(@ErrorNumber AS varchar(15))
+ ', Procedure ' + @ErrorProcedure
+ ', Line ' + CAST(@ErrorLine AS varchar(15))
+ ', Message: '+ @ErrorMessageText

EXEC DebugMessageInsert
@DatasetId = @DatasetId
,@MessageLevel = 1
,@MessageText = @DebugMessageText
END

DECLARE @AdjustedErrorSeverity int

SET @AdjustedErrorSeverity = CASE
WHEN @ErrorSeverity &gt; 18 THEN 18
ELSE @ErrorSeverity
END

RAISERROR (777971002, @AdjustedErrorSeverity, 1
,@ErrorNumber
,@ErrorSeverity
,@ErrorState
,@ErrorProcedure
,@ErrorLine
,@ErrorMessageText
)
END

RETURN @RowsDeleted
END
GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventLoggingComputerRowIdResolve')
BEGIN
EXECUTE ('CREATE PROCEDURE dbo.EventLoggingComputerRowIdResolve AS RETURN 1')
END
GO

ALTER PROCEDURE dbo.EventLoggingComputerRowIdResolve
@ManagementGroupGuid uniqueidentifier
,@ObjectKeyXml ntext
AS
BEGIN
SET NOCOUNT ON

DECLARE @ManagementGroupRowId int

-- check management group is connected to this DW
DECLARE @CheckExecResult int

EXEC @CheckExecResult = ManagementGroupAccessCheck @ManagementGroupGuid, @ManagementGroupRowId OUTPUT
IF (@CheckExecResult &lt;&gt; 0) RETURN @CheckExecResult

DECLARE
@ErrorInd bit
,@ErrorMessage nvarchar(max)
,@ErrorNumber int
,@ErrorSeverity int
,@ErrorState int
,@ErrorLine int
,@ErrorProcedure nvarchar(256)
,@ErrorMessageText nvarchar(max)

SET @ErrorInd = 0

BEGIN TRY

DECLARE
@ExecResult int
,@XmlDocHandle int

EXEC @ExecResult = sp_xml_preparedocument @XmlDocHandle OUTPUT, @ObjectKeyXml
IF @ExecResult &lt;&gt; 0 RAISERROR(777971000, 16, 1, 'EventLoggingComputerKeys', @ExecResult)

IF (OBJECT_ID('tempdb..#EventLoggingComputer') IS NOT NULL)
DROP TABLE #EventLoggingComputer

CREATE TABLE #EventLoggingComputer (
NodeOrdinal int NOT NULL
,ComputerName nvarchar(256) NOT NULL
)

INSERT #EventLoggingComputer (
NodeOrdinal
,ComputerName
)
SELECT
NodeOrdinal
,LoggingComputerName
FROM OPENXML(@XmlDocHandle, 'Keys/Key', 2) WITH (
NodeOrdinal int '@mp:id'
,LoggingComputerName nvarchar(256) '.') xml

EXEC sp_xml_removedocument @XmlDocHandle
SET @XmlDocHandle = NULL

UPDATE ec
SET LastReceivedDateTime = GETUTCDATE()
FROM EventLoggingComputer ec
JOIN #EventLoggingComputer e ON (ec.ComputerName = e.ComputerName COLLATE database_default)
WHERE (ec.LastReceivedDateTime &lt; DATEADD(day, -1, GETUTCDATE()))

INSERT EventLoggingComputer (ComputerName)
SELECT DISTINCT e.ComputerName
FROM #EventLoggingComputer e
WHERE NOT EXISTS (SELECT * FROM EventLoggingComputer WHERE (ComputerName = e.ComputerName COLLATE database_default))

SELECT
ec.EventLoggingComputerRowId
FROM #EventLoggingComputer e
JOIN EventLoggingComputer ec ON (e.ComputerName = ec.ComputerName COLLATE database_default)
ORDER BY e.NodeOrdinal
END TRY
BEGIN CATCH
IF (@@TRANCOUNT &gt; 0)
ROLLBACK TRAN

SELECT
@ErrorNumber = ERROR_NUMBER()
,@ErrorSeverity = ERROR_SEVERITY()
,@ErrorState = ERROR_STATE()
,@ErrorLine = ERROR_LINE()
,@ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-')
,@ErrorMessageText = ERROR_MESSAGE()

SET @ErrorInd = 1
END CATCH

IF (@XmlDocHandle IS NOT NULL)
EXEC sp_xml_removedocument @XmlDocHandle

IF (OBJECT_ID('tempdb..#EventLoggingComputer') IS NOT NULL)
DROP TABLE #EventLoggingComputer

-- report error if any
IF (@ErrorInd = 1)
BEGIN
DECLARE @AdjustedErrorSeverity int

SET @AdjustedErrorSeverity = CASE
WHEN @ErrorSeverity &gt; 18 THEN 18
ELSE @ErrorSeverity
END

RAISERROR (777971002, @AdjustedErrorSeverity, 1
,@ErrorNumber
,@ErrorSeverity
,@ErrorState
,@ErrorProcedure
,@ErrorLine
,@ErrorMessageText
)
END
END
GO

GRANT EXECUTE ON EventLoggingComputerRowIdResolve TO OpsMgrWriter
GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventProcessStaging')
BEGIN
EXECUTE ('CREATE PROCEDURE dbo.EventProcessStaging AS RETURN 1')
END
GO

ALTER PROCEDURE dbo.EventProcessStaging
@DatasetId uniqueidentifier
AS
BEGIN
SET NOCOUNT ON

DECLARE
@ErrorInd bit
,@ErrorMessage nvarchar(max)
,@ErrorNumber int
,@ErrorSeverity int
,@ErrorState int
,@ErrorLine int
,@ErrorProcedure nvarchar(256)
,@ErrorMessageText nvarchar(max)

SET @ErrorInd = 0

DECLARE
@DebugLevel int
,@SchemaName sysname
,@ExecResult int
,@LockResourceName sysname
,@InsertTableName sysname
,@InsertTableGuid uniqueidentifier
,@InsertStartedDateTime datetime
,@Statement nvarchar(max)
,@MessageText varchar(max)
,@OperationDurationMs bigint
,@MaxStagingRowsToProcess int

BEGIN TRY

SELECT
@DebugLevel = DebugLevel
,@SchemaName = SchemaName
FROM StandardDataset
WHERE (DatasetId = @DatasetId)

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EventStage2Process' AND TABLE_SCHEMA = 'Event')
BEGIN
BEGIN TRAN

DECLARE @ProcessAllRowsInd bit
SET @ProcessAllRowsInd = 1

-- figure out what we need to do with staging area
SELECT
@Statement = StagingTableColumnDefinition
,@MaxStagingRowsToProcess = MaxRowsToProcessPerTransactionCount
FROM StandardDatasetStagingArea
WHERE DatasetId = @DatasetId

IF (@MaxStagingRowsToProcess IS NOT NULL)
BEGIN
DECLARE @EventStageRowCount int
DECLARE @SpaceUsedInfo TABLE (
[name] sysname
,[rows] int
,reserved nvarchar(100)
,data nvarchar(100)
,index_size nvarchar(100)
,unused nvarchar(100)
)

INSERT @SpaceUsedInfo
EXEC sp_spaceused 'Event.EventStage'

SELECT @EventStageRowCount = [rows]
FROM @SpaceUsedInfo

IF (@EventStageRowCount &gt;= @MaxStagingRowsToProcess)
BEGIN
SET @ProcessAllRowsInd = 0
END
END

IF (@ProcessAllRowsInd = 1)
BEGIN
-- no limit set for the number of rows to process
-- rename and re-create staging table

-- lock, rename EventStage table and re-create it
EXEC @ExecResult = sp_rename 'Event.EventStage', 'EventStage2Process'

IF (@ExecResult &lt; 0)
BEGIN
RAISERROR(777971003, 16, 1, @ExecResult)
END

SELECT @Statement = 'CREATE TABLE Event.EventStage (' + @Statement + ')'
EXECUTE (@Statement)
END
ELSE
BEGIN
-- need to process subset of rows
-- create extra column to store original row id
SELECT @Statement =
'CREATE TABLE Event.EventStage2Process ('
+ @Statement
+ ',OriginalEventStageRowId int NOT NULL'
+ ')'
EXECUTE (@Statement)

SELECT @Statement =
' INSERT Event.EventStage2Process'
+ ' ('
+ ' DatasetId'
+ ' ,ManagementGroupGuid'
+ ' ,EventOriginId'
+ ' ,[DateTime]'
+ ' ,RuleRowId'
+ ' ,ManagedEntityRowId'
+ ' ,EventPublisherRowId'
+ ' ,EventChannelRowId'
+ ' ,EventCategoryRowId'
+ ' ,EventLevelId'
+ ' ,LoggingComputerRowId'
+ ' ,EventDisplayNumber'
+ ' ,EventNumber'
+ ' ,UserNameRowId'
+ ' ,RawDescription'
+ ' ,RenderedDescription'
+ ' ,EventParameters'
+ ' ,EventData'
+ ' ,RawDescriptionHash'
+ ' ,ParameterHash'
+ ' ,EventDataHash'
+ ' ,OriginalEventStageRowId'
+ ' )'
+ ' SELECT TOP (' + CAST(@MaxStagingRowsToProcess AS varchar(15)) + ')'
+ ' DatasetId'
+ ' ,ManagementGroupGuid'
+ ' ,EventOriginId'
+ ' ,[DateTime]'
+ ' ,RuleRowId'
+ ' ,ManagedEntityRowId'
+ ' ,EventPublisherRowId'
+ ' ,EventChannelRowId'
+ ' ,EventCategoryRowId'
+ ' ,EventLevelId'
+ ' ,LoggingComputerRowId'
+ ' ,EventDisplayNumber'
+ ' ,EventNumber'
+ ' ,UserNameRowId'
+ ' ,RawDescription'
+ ' ,RenderedDescription'
+ ' ,EventParameters'
+ ' ,EventData'
+ ' ,RawDescriptionHash'
+ ' ,ParameterHash'
+ ' ,EventDataHash'
+ ' ,EventStageRowId'
+ ' FROM Event.EventStage'
+ ' ORDER BY EventStageRowId'
EXECUTE(@Statement)

SELECT @Statement =
' CREATE INDEX IX_EventStage2Process_EventOriginId ON Event.EventStage2Process (EventOriginId)'
EXECUTE(@Statement)

SELECT @Statement =
'DELETE es'
+ ' FROM Event.EventStage es'
+ ' JOIN Event.EventStage2Process esp ON (es.EventStageRowId = esp.OriginalEventStageRowId)'
EXECUTE(@Statement)
END

GRANT ALTER, SELECT, INSERT, UPDATE, DELETE ON Event.EventStage TO OpsMgrWriter

GRANT CONTROL ON Event.EventStage2Process TO OpsMgrWriter

COMMIT
END

BEGIN TRAN

-- lock table map to ensure we insert into table opened for insertion
SET @LockResourceName = CAST(@DatasetId AS varchar(50)) + '_TableMap'

EXEC @ExecResult = sp_getapplock
@Resource = @LockResourceName
,@LockMode = 'Shared'
,@LockOwner = 'Transaction'

IF (@ExecResult &lt; 0)
BEGIN
RAISERROR(777971001, 16, 1, 'Shared:StandardDatasetTableMap', @ExecResult)
END

EXEC dbo.StandardDatasetGetInsertTableGuid @DatasetId, 0, @InsertTableGuid OUTPUT

IF (@DebugLevel &gt; 3)
BEGIN
SELECT @MessageText = 'Starting to insert ' + CAST(COUNT(*) AS varchar(30)) + ' events into ' + @InsertTableName
FROM Event.EventStage2Process

SET @InsertStartedDateTime = GETUTCDATE()

EXEC DebugMessageInsert
@DatasetId = @DatasetId
,@MessageLevel = 4
,@MessageText = @MessageText
END

-- insert event &lt;-&gt; rule/me relationships
-- NOTE: We do not check for event-rule-ME uniqueness
-- to save time (can produce dupes if event was delivered twice
-- in two different staging processing windows)
-- We also saw Sql having troubles coming up with a good plan

SELECT @InsertTableName = BaseTableName + '_' + REPLACE(CAST(@InsertTableGuid AS varchar(50)), '-', '')
FROM StandardDatasetAggregationStorage
WHERE (DatasetId = @DatasetId)
AND (AggregationTypeId = 0)
AND (DependentTableInd = 1)
AND (TableTag = 'rule')

SELECT @Statement =
' INSERT ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@InsertTableName) + ' ('
+ ' EventOriginId'
+ ' ,RuleRowId'
+ ' ,ManagedEntityRowId'
+ ' )'
+ ' SELECT'
+ ' s.EventOriginId'
+ ' ,s.RuleRowId'
+ ' ,s.ManagedEntityRowId'
+ ' FROM Event.EventStage2Process s'

EXECUTE(@Statement)

-- delete events collected more then once - leave latest collected only
DELETE s
FROM Event.EventStage2Process s
WHERE EXISTS (SELECT *
FROM Event.EventStage2Process s_later
WHERE (s.EventOriginId = s_later.EventOriginId)
AND (s_later.EventStageRowId &gt; s.EventStageRowId)
)

-- insert events
SELECT @InsertTableName = BaseTableName + '_' + REPLACE(CAST(@InsertTableGuid AS varchar(50)), '-', '')
FROM StandardDatasetAggregationStorage
WHERE (DatasetId = @DatasetId)
AND (AggregationTypeId = 0)
AND (DependentTableInd = 0)

-- first delete events that were already delivered

SELECT @Statement = 'DELETE s'
+ ' FROM Event.EventStage2Process s, ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@InsertTableName) + ' e'
+ ' WHERE (s.EventOriginId = e.EventOriginId)'
EXECUTE (@Statement)

SELECT @Statement =
' INSERT ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@InsertTableName) + ' ('
+ ' EventOriginId'
+ ' ,[DateTime]'
+ ' ,EventPublisherRowId'
+ ' ,EventChannelRowId'
+ ' ,EventCategoryRowId'
+ ' ,EventLevelId'
+ ' ,LoggingComputerRowId'
+ ' ,EventNumber'
+ ' ,EventDisplayNumber'
+ ' ,UserNameRowId'
+ ' ,RawDescriptionHash'
+ ' ,ParameterHash'
+ ' ,EventDataHash'
+ ' )'
+ ' SELECT '
+ ' s.EventOriginId'
+ ' ,s.[DateTime]'
+ ' ,s.EventPublisherRowId'
+ ' ,s.EventChannelRowId'
+ ' ,s.EventCategoryRowId'
+ ' ,s.EventLevelId'
+ ' ,s.LoggingComputerRowId'
+ ' ,s.EventNumber'
+ ' ,s.EventDisplayNumber'
+ ' ,s.UserNameRowId'
+ ' ,s.RawDescriptionHash'
+ ' ,s.ParameterHash'
+ ' ,s.EventDataHash'
+ ' FROM Event.EventStage2Process s'

EXECUTE(@Statement)

-- insert parameters

SELECT @InsertTableName = BaseTableName + '_' + REPLACE(CAST(@InsertTableGuid AS varchar(50)), '-', '')
FROM StandardDatasetAggregationStorage
WHERE (DatasetId = @DatasetId)
AND (AggregationTypeId = 0)
AND (DependentTableInd = 1)
AND (TableTag = 'parameter')

SELECT @Statement =
' INSERT ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@InsertTableName) + ' ('
+ ' EventOriginId'
+ ' ,ParameterIndex'
+ ' ,ParameterValue'
+ ' )'
+ ' SELECT '
+ ' s.EventOriginId'
+ ' ,ROW_NUMBER() OVER(PARTITION BY s.EventOriginId ORDER BY s.EventOriginId)'
+ ' ,p.ParameterValue.value(''.'', ''nvarchar(max)'')'
+ ' FROM Event.EventStage2Process s'
+ ' CROSS APPLY s.EventParameters.nodes(''Params/Param'') as p(ParameterValue)'

EXECUTE(@Statement)

-- insert event details

SELECT @InsertTableName = BaseTableName + '_' + REPLACE(CAST(@InsertTableGuid AS varchar(50)), '-', '')
FROM StandardDatasetAggregationStorage
WHERE (DatasetId = @DatasetId)
AND (AggregationTypeId = 0)
AND (DependentTableInd = 1)
AND (TableTag = 'detail')

SELECT @Statement =
' INSERT ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@InsertTableName) + ' ('
+ ' EventOriginId'
+ ' ,RawDescription'
+ ' ,RenderedDescription'
+ ' ,EventData'
+ ' )'
+ ' SELECT '
+ ' s.EventOriginId'
+ ' ,s.RawDescription'
+ ' ,s.RenderedDescription'
+ ' ,s.EventData'
+ ' FROM Event.EventStage2Process s'

EXECUTE(@Statement)

-- delete all inserted data from staging
DROP TABLE Event.EventStage2Process

IF (@DebugLevel &gt; 3)
BEGIN
SET @MessageText = 'Finished inserting events into ' + @InsertTableName
SET @OperationDurationMs = ABS(DATEDIFF(ms, GETUTCDATE(), @InsertStartedDateTime))

EXEC DebugMessageInsert
@DatasetId = @DatasetId
,@MessageLevel = 4
,@MessageText = @MessageText
,@OperationDurationMs = @OperationDurationMs
END

COMMIT
END TRY
BEGIN CATCH
IF (@@TRANCOUNT &gt; 0)
ROLLBACK TRAN

SELECT
@ErrorNumber = ERROR_NUMBER()
,@ErrorSeverity = ERROR_SEVERITY()
,@ErrorState = ERROR_STATE()
,@ErrorLine = ERROR_LINE()
,@ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-')
,@ErrorMessageText = ERROR_MESSAGE()

SET @ErrorInd = 1
END CATCH

-- report error if any
IF (@ErrorInd = 1)
BEGIN
IF (@DebugLevel &gt; 0)
BEGIN
DECLARE @DebugMessageText nvarchar(max)

SET @DebugMessageText = N'Failed to process data in the event data set staging area. Error ' + CAST(@ErrorNumber AS varchar(15))
+ ', Procedure ' + @ErrorProcedure
+ ', Line ' + CAST(@ErrorLine AS varchar(15))
+ ', Message: '+ @ErrorMessageText

EXEC DebugMessageInsert
@DatasetId = @DatasetId
,@MessageLevel = 1
,@MessageText = @DebugMessageText
END

DECLARE @AdjustedErrorSeverity int

SET @AdjustedErrorSeverity = CASE
WHEN @ErrorSeverity &gt; 18 THEN 18
ELSE @ErrorSeverity
END

RAISERROR (777971002, @AdjustedErrorSeverity, 1
,@ErrorNumber
,@ErrorSeverity
,@ErrorState
,@ErrorProcedure
,@ErrorLine
,@ErrorMessageText
)
END
END
GO

GRANT EXECUTE ON dbo.EventProcessStaging TO OpsMgrWriter
GO


IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventPublisherRowIdResolve')
BEGIN
EXECUTE ('CREATE PROCEDURE dbo.EventPublisherRowIdResolve AS RETURN 1')
END
GO

ALTER PROCEDURE dbo.EventPublisherRowIdResolve
@ManagementGroupGuid uniqueidentifier
,@ObjectKeyXml ntext
AS
BEGIN
SET NOCOUNT ON

DECLARE @ManagementGroupRowId int

-- check management group is connected to this DW
DECLARE @CheckExecResult int

EXEC @CheckExecResult = ManagementGroupAccessCheck @ManagementGroupGuid, @ManagementGroupRowId OUTPUT
IF (@CheckExecResult &lt;&gt; 0) RETURN @CheckExecResult

DECLARE
@ErrorInd bit
,@ErrorMessage nvarchar(max)
,@ErrorNumber int
,@ErrorSeverity int
,@ErrorState int
,@ErrorLine int
,@ErrorProcedure nvarchar(256)
,@ErrorMessageText nvarchar(max)

SET @ErrorInd = 0

BEGIN TRY

DECLARE
@ExecResult int
,@XmlDocHandle int

EXEC @ExecResult = sp_xml_preparedocument @XmlDocHandle OUTPUT, @ObjectKeyXml
IF @ExecResult &lt;&gt; 0 RAISERROR(777971000, 16, 1, 'EventPublisherKeys', @ExecResult)

IF (OBJECT_ID('tempdb..#EventPublisher') IS NOT NULL)
DROP TABLE #EventPublisher

CREATE TABLE #EventPublisher (
NodeOrdinal int NOT NULL
,PublisherGuid uniqueidentifier NOT NULL
,PublisherName nvarchar(256) NOT NULL
)

INSERT #EventPublisher (
NodeOrdinal
,PublisherGuid
,PublisherName
)
SELECT
NodeOrdinal
,PublisherGuid
,PublisherName
FROM OPENXML(@XmlDocHandle, 'Keys/Key', 2) WITH (
NodeOrdinal int '@mp:id'
,PublisherGuid uniqueidentifier 'PublisherGuid'
,PublisherName nvarchar(256) 'PublisherName') xml

EXEC sp_xml_removedocument @XmlDocHandle
SET @XmlDocHandle = NULL

UPDATE ep
SET LastReceivedDateTime = GETUTCDATE()
FROM EventPublisher ep
JOIN #EventPublisher e ON (ep.EventPublisherGuid = e.PublisherGuid)
WHERE (ep.LastReceivedDateTime &lt; DATEADD(day, -1, GETUTCDATE()))

INSERT EventPublisher (EventPublisherGuid, EventPublisherName)
SELECT DISTINCT e.PublisherGuid, e.PublisherName
FROM #EventPublisher e
WHERE NOT EXISTS (SELECT * FROM EventPublisher WHERE (EventPublisherGuid = e.PublisherGuid))

SELECT
ep.EventPublisherRowId
FROM #EventPublisher e
JOIN EventPublisher ep ON (e.PublisherGuid = ep.EventPublisherGuid)
ORDER BY e.NodeOrdinal
END TRY
BEGIN CATCH
IF (@@TRANCOUNT &gt; 0)
ROLLBACK TRAN

SELECT
@ErrorNumber = ERROR_NUMBER()
,@ErrorSeverity = ERROR_SEVERITY()
,@ErrorState = ERROR_STATE()
,@ErrorLine = ERROR_LINE()
,@ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-')
,@ErrorMessageText = ERROR_MESSAGE()

SET @ErrorInd = 1
END CATCH

IF (@XmlDocHandle IS NOT NULL)
EXEC sp_xml_removedocument @XmlDocHandle

IF (OBJECT_ID('tempdb..#EventPublisher') IS NOT NULL)
DROP TABLE #EventPublisher

-- report error if any
IF (@ErrorInd = 1)
BEGIN
DECLARE @AdjustedErrorSeverity int

SET @AdjustedErrorSeverity = CASE
WHEN @ErrorSeverity &gt; 18 THEN 18
ELSE @ErrorSeverity
END

RAISERROR (777971002, @AdjustedErrorSeverity, 1
,@ErrorNumber
,@ErrorSeverity
,@ErrorState
,@ErrorProcedure
,@ErrorLine
,@ErrorMessageText
)
END
END
GO

GRANT EXECUTE ON EventPublisherRowIdResolve TO OpsMgrWriter
GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventUserNameRowIdResolve')
BEGIN
EXECUTE ('CREATE PROCEDURE dbo.EventUserNameRowIdResolve AS RETURN 1')
END
GO

ALTER PROCEDURE dbo.EventUserNameRowIdResolve
@ManagementGroupGuid uniqueidentifier
,@ObjectKeyXml ntext
AS
BEGIN
SET NOCOUNT ON

DECLARE @ManagementGroupRowId int

-- check management group is connected to this DW
DECLARE @CheckExecResult int

EXEC @CheckExecResult = ManagementGroupAccessCheck @ManagementGroupGuid, @ManagementGroupRowId OUTPUT
IF (@CheckExecResult &lt;&gt; 0) RETURN @CheckExecResult

DECLARE
@ErrorInd bit
,@ErrorMessage nvarchar(max)
,@ErrorNumber int
,@ErrorSeverity int
,@ErrorState int
,@ErrorLine int
,@ErrorProcedure nvarchar(256)
,@ErrorMessageText nvarchar(max)

SET @ErrorInd = 0

BEGIN TRY

DECLARE
@ExecResult int
,@XmlDocHandle int

EXEC @ExecResult = sp_xml_preparedocument @XmlDocHandle OUTPUT, @ObjectKeyXml
IF @ExecResult &lt;&gt; 0 RAISERROR(777971000, 16, 1, 'EventUserNameKeys', @ExecResult)

IF (OBJECT_ID('tempdb..#EventUserName') IS NOT NULL)
DROP TABLE #EventUserName

CREATE TABLE #EventUserName (
NodeOrdinal int NOT NULL
,UserName nvarchar(256) NOT NULL
)

INSERT #EventUserName (
NodeOrdinal
,UserName
)
SELECT
NodeOrdinal
,UserName
FROM OPENXML(@XmlDocHandle, 'Keys/Key', 2) WITH (
NodeOrdinal int '@mp:id'
,UserName nvarchar(256) '.') xml

EXEC sp_xml_removedocument @XmlDocHandle
SET @XmlDocHandle = NULL

UPDATE ec
SET LastReceivedDateTime = GETUTCDATE()
FROM EventUserName ec
JOIN #EventUserName e ON (ec.UserName = e.UserName COLLATE database_default)
WHERE (ec.LastReceivedDateTime &lt; DATEADD(day, -1, GETUTCDATE()))

INSERT EventUserName (UserName)
SELECT DISTINCT e.UserName
FROM #EventUserName e
WHERE NOT EXISTS (SELECT * FROM EventUserName WHERE (UserName = e.UserName COLLATE database_default))

SELECT
ec.EventUserNameRowId
FROM #EventUserName e
JOIN EventUserName ec ON (e.UserName = ec.UserName COLLATE database_default)
ORDER BY e.NodeOrdinal
END TRY
BEGIN CATCH
IF (@@TRANCOUNT &gt; 0)
ROLLBACK TRAN

SELECT
@ErrorNumber = ERROR_NUMBER()
,@ErrorSeverity = ERROR_SEVERITY()
,@ErrorState = ERROR_STATE()
,@ErrorLine = ERROR_LINE()
,@ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-')
,@ErrorMessageText = ERROR_MESSAGE()

SET @ErrorInd = 1
END CATCH

IF (@XmlDocHandle IS NOT NULL)
EXEC sp_xml_removedocument @XmlDocHandle

IF (OBJECT_ID('tempdb..#EventUserName') IS NOT NULL)
DROP TABLE #EventUserName

-- report error if any
IF (@ErrorInd = 1)
BEGIN
DECLARE @AdjustedErrorSeverity int

SET @AdjustedErrorSeverity = CASE
WHEN @ErrorSeverity &gt; 18 THEN 18
ELSE @ErrorSeverity
END

RAISERROR (777971002, @AdjustedErrorSeverity, 1
,@ErrorNumber
,@ErrorSeverity
,@ErrorState
,@ErrorProcedure
,@ErrorLine
,@ErrorMessageText
)
END
END
GO

GRANT EXECUTE ON EventUserNameRowIdResolve TO OpsMgrWriter
GO

</Install>
<Uninstall>
-- drop SPs
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventGroom')
BEGIN
EXECUTE ('DROP PROCEDURE dbo.EventGroom')
END

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventProcessStaging')
BEGIN
EXECUTE ('DROP PROCEDURE dbo.EventProcessStaging')
END

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventCategoryRowIdResolve')
BEGIN
EXECUTE ('DROP PROCEDURE dbo.EventCategoryRowIdResolve')
END

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventChannelRowIdResolve')
BEGIN
EXECUTE ('DROP PROCEDURE dbo.EventChannelRowIdResolve')
END

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventLoggingComputerRowIdResolve')
BEGIN
EXECUTE ('DROP PROCEDURE dbo.EventLoggingComputerRowIdResolve')
END

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventPublisherRowIdResolve')
BEGIN
EXECUTE ('DROP PROCEDURE dbo.EventPublisherRowIdResolve')
END

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventUserNameRowIdResolve')
BEGIN
EXECUTE ('DROP PROCEDURE dbo.EventUserNameRowIdResolve')
END

-- drop views
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'V' AND name = 'vEventCategory')
BEGIN
EXECUTE ('DROP VIEW vEventCategory')
END

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'V' AND name = 'vEventChannel')
BEGIN
EXECUTE ('DROP VIEW vEventChannel')
END

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'V' AND name = 'vEventLevel')
BEGIN
EXECUTE ('DROP VIEW vEventLevel')
END

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'V' AND name = 'vEventLoggingComputer')
BEGIN
EXECUTE ('DROP VIEW vEventLoggingComputer')
END

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'V' AND name = 'vEventPublisher')
BEGIN
EXECUTE ('DROP VIEW vEventPublisher')
END

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'V' AND name = 'vEventUserName')
BEGIN
EXECUTE ('DROP VIEW vEventUserName')
END

-- drop tables
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EventCategory' AND TABLE_SCHEMA = 'dbo')
BEGIN
EXECUTE('DROP TABLE dbo.EventCategory')
END

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EventChannel' AND TABLE_SCHEMA = 'dbo')
BEGIN
EXECUTE('DROP TABLE dbo.EventChannel')
END

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EventLevel' AND TABLE_SCHEMA = 'dbo')
BEGIN
EXECUTE('DROP TABLE dbo.EventLevel')
END

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EventLoggingComputer' AND TABLE_SCHEMA = 'dbo')
BEGIN
EXECUTE('DROP TABLE dbo.EventLoggingComputer')
END

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EventPublisher' AND TABLE_SCHEMA = 'dbo')
BEGIN
EXECUTE('DROP TABLE dbo.EventPublisher')
END

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EventUserName' AND TABLE_SCHEMA = 'dbo')
BEGIN
EXECUTE('DROP TABLE dbo.EventUserName')
END

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EventStage' AND TABLE_SCHEMA = 'Event')
BEGIN
EXECUTE('DROP TABLE Event.EventStage')
END

-- delete std dataset related data
EXEC StandardDatasetDelete '$Config/DatasetId$'
GO

</Uninstall>
<Upgrade>
SET NOCOUNT ON

-- update table definition templates
UPDATE sdas
SET TableTemplate = '
CREATE TABLE [Event].[EventParameter_$Guid$]
(
EventParameterRowId int NOT NULL IDENTITY(1, 1)
,EventOriginId uniqueidentifier NOT NULL
,ParameterIndex tinyint NOT NULL
,ParameterValue nvarchar(max) NULL

,CONSTRAINT [PK_EventParameter_$Guid$] PRIMARY KEY CLUSTERED (EventParameterRowId) ON $DataFileGroupName$

) ON $DataFileGroupName$
'
FROM StandardDatasetAggregationStorage sdas
JOIN StandardDataset sd ON (sdas.DatasetId = sd.DatasetId)
WHERE (sd.DatasetId = '$Config/DatasetId$')
AND (sdas.TableTag = 'parameter')
AND (sdas.DependentTableInd = 1)
GO

-- update all tables that were already created
DECLARE
@StandardDatasetTableMapRowId int
,@Statement nvarchar(max)
,@SchemaName sysname
,@TableNameSuffix sysname
,@BaseTableName sysname

SET @StandardDatasetTableMapRowId = 0

WHILE EXISTS (SELECT *
FROM StandardDatasetTableMap tm
WHERE (tm.StandardDatasetTableMapRowId &gt; @StandardDatasetTableMapRowId)
AND (tm.DatasetId = '$Config/DatasetId$')
AND (tm.InsertInd = 1)
)
BEGIN
SELECT TOP 1
@StandardDatasetTableMapRowId = tm.StandardDatasetTableMapRowId
,@SchemaName = sd.SchemaName
,@TableNameSuffix = tm.TableNameSuffix
,@BaseTableName = sdas.BaseTableName
FROM StandardDatasetTableMap tm
JOIN StandardDataset sd ON (tm.DatasetId = sd.DatasetId)
JOIN StandardDatasetAggregationStorage sdas ON (sdas.DatasetId = tm.DatasetId) AND (sdas.AggregationTypeId = tm.AggregationTypeId)
WHERE (tm.StandardDatasetTableMapRowId &gt; @StandardDatasetTableMapRowId)
AND (tm.DatasetId = '$Config/DatasetId$')
AND (tm.InsertInd = 1)
AND (sdas.TableTag = 'parameter')
AND (sdas.DependentTableInd = 1)
ORDER BY tm.StandardDatasetTableMapRowId

SET @Statement = 'ALTER TABLE ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@BaseTableName + '_' + @TableNameSuffix)
+ ' ALTER COLUMN ParameterValue nvarchar(max) NULL'

EXECUTE (@Statement)
END
GO

-- alter cover views
EXEC StandardDatasetBuildCoverView '$Config/DatasetId$', 0
GO



IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventCategoryRowIdResolve')
BEGIN
EXECUTE ('CREATE PROCEDURE dbo.EventCategoryRowIdResolve AS RETURN 1')
END
GO

ALTER PROCEDURE dbo.EventCategoryRowIdResolve
@ManagementGroupGuid uniqueidentifier
,@ObjectKeyXml ntext
AS
BEGIN
SET NOCOUNT ON

DECLARE @ManagementGroupRowId int

-- check management group is connected to this DW
DECLARE @CheckExecResult int

EXEC @CheckExecResult = ManagementGroupAccessCheck @ManagementGroupGuid, @ManagementGroupRowId OUTPUT
IF (@CheckExecResult &lt;&gt; 0) RETURN @CheckExecResult

DECLARE
@ErrorInd bit
,@ErrorMessage nvarchar(max)
,@ErrorNumber int
,@ErrorSeverity int
,@ErrorState int
,@ErrorLine int
,@ErrorProcedure nvarchar(256)
,@ErrorMessageText nvarchar(max)

SET @ErrorInd = 0

BEGIN TRY

DECLARE
@ExecResult int
,@XmlDocHandle int

EXEC @ExecResult = sp_xml_preparedocument @XmlDocHandle OUTPUT, @ObjectKeyXml
IF @ExecResult &lt;&gt; 0 RAISERROR(777971000, 16, 1, 'EventCategoryKeys', @ExecResult)

IF (OBJECT_ID('tempdb..#EventCategory') IS NOT NULL)
DROP TABLE #EventCategory

CREATE TABLE #EventCategory (
NodeOrdinal int NOT NULL
,PublisherGuid uniqueidentifier NOT NULL
,PublisherName nvarchar(256) NOT NULL
,CategoryId int NOT NULL
,CategoryTitle nvarchar(256) NOT NULL
)

INSERT #EventCategory (
NodeOrdinal
,PublisherGuid
,PublisherName
,CategoryId
,CategoryTitle
)
SELECT
NodeOrdinal
,PublisherGuid
,PublisherName
,CategoryId
,CategoryTitle
FROM OPENXML(@XmlDocHandle, 'Keys/Key', 2) WITH (
NodeOrdinal int '@mp:id'
,PublisherGuid uniqueidentifier 'PublisherGuid'
,PublisherName nvarchar(256) 'PublisherName'
,CategoryId int 'CategoryId'
,CategoryTitle nvarchar(256) 'CategoryTitle') xml

EXEC sp_xml_removedocument @XmlDocHandle
SET @XmlDocHandle = NULL

-- work with publisher domain first

UPDATE ep
SET LastReceivedDateTime = GETUTCDATE()
FROM EventPublisher ep
JOIN #EventCategory e ON (ep.EventPublisherGuid = e.PublisherGuid)
WHERE (ep.LastReceivedDateTime &lt; DATEADD(day, -1, GETUTCDATE()))

INSERT EventPublisher (EventPublisherGuid, EventPublisherName)
SELECT DISTINCT e.PublisherGuid, e.PublisherName
FROM #EventCategory e
WHERE NOT EXISTS (SELECT * FROM EventPublisher WHERE (EventPublisherGuid = e.PublisherGuid))

-- update category domain

UPDATE ec
SET LastReceivedDateTime = GETUTCDATE()
FROM EventCategory ec
JOIN EventPublisher ep ON (ec.EventPublisherRowId = ep.EventPublisherRowId)
JOIN #EventCategory e ON (ec.EventCategoryId = e.CategoryId) AND (ep.EventPublisherGuid = e.PublisherGuid)
WHERE (ec.LastReceivedDateTime &lt; DATEADD(day, -1, GETUTCDATE()))

INSERT EventCategory (
EventPublisherRowId
,EventCategoryId
,EventCategoryTitle
)
SELECT DISTINCT
ep.EventPublisherRowId
,e.CategoryId
,e.CategoryTitle
FROM #EventCategory e
JOIN EventPublisher ep ON (e.PublisherGuid = ep.EventPublisherGuid)
WHERE NOT EXISTS (SELECT *
FROM EventCategory
WHERE (EventCategoryId = e.CategoryId)
AND (EventPublisherRowId = ep.EventPublisherRowId)
)

SELECT
ec.EventCategoryRowId
FROM #EventCategory e
JOIN EventPublisher ep ON (e.PublisherGuid = ep.EventPublisherGuid)
JOIN EventCategory ec ON (e.CategoryId = ec.EventCategoryId) AND (ec.EventPublisherRowId = ep.EventPublisherRowId)
ORDER BY e.NodeOrdinal
END TRY
BEGIN CATCH
IF (@@TRANCOUNT &gt; 0)
ROLLBACK TRAN

SELECT
@ErrorNumber = ERROR_NUMBER()
,@ErrorSeverity = ERROR_SEVERITY()
,@ErrorState = ERROR_STATE()
,@ErrorLine = ERROR_LINE()
,@ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-')
,@ErrorMessageText = ERROR_MESSAGE()

SET @ErrorInd = 1
END CATCH

IF (@XmlDocHandle IS NOT NULL)
EXEC sp_xml_removedocument @XmlDocHandle

IF (OBJECT_ID('tempdb..#EventCategory') IS NOT NULL)
DROP TABLE #EventCategory

-- report error if any
IF (@ErrorInd = 1)
BEGIN
DECLARE @AdjustedErrorSeverity int

SET @AdjustedErrorSeverity = CASE
WHEN @ErrorSeverity &gt; 18 THEN 18
ELSE @ErrorSeverity
END

RAISERROR (777971002, @AdjustedErrorSeverity, 1
,@ErrorNumber
,@ErrorSeverity
,@ErrorState
,@ErrorProcedure
,@ErrorLine
,@ErrorMessageText
)
END
END
GO

GRANT EXECUTE ON EventCategoryRowIdResolve TO OpsMgrWriter
GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventChannelRowIdResolve')
BEGIN
EXECUTE ('CREATE PROCEDURE dbo.EventChannelRowIdResolve AS RETURN 1')
END
GO

ALTER PROCEDURE dbo.EventChannelRowIdResolve
@ManagementGroupGuid uniqueidentifier
,@ObjectKeyXml ntext
AS
BEGIN
SET NOCOUNT ON

DECLARE @ManagementGroupRowId int

-- check management group is connected to this DW
DECLARE @CheckExecResult int

EXEC @CheckExecResult = ManagementGroupAccessCheck @ManagementGroupGuid, @ManagementGroupRowId OUTPUT
IF (@CheckExecResult &lt;&gt; 0) RETURN @CheckExecResult

DECLARE
@ErrorInd bit
,@ErrorMessage nvarchar(max)
,@ErrorNumber int
,@ErrorSeverity int
,@ErrorState int
,@ErrorLine int
,@ErrorProcedure nvarchar(256)
,@ErrorMessageText nvarchar(max)

SET @ErrorInd = 0

BEGIN TRY

DECLARE
@ExecResult int
,@XmlDocHandle int

EXEC @ExecResult = sp_xml_preparedocument @XmlDocHandle OUTPUT, @ObjectKeyXml
IF @ExecResult &lt;&gt; 0 RAISERROR(777971000, 16, 1, 'EventChannelKeys', @ExecResult)

IF (OBJECT_ID('tempdb..#EventChannel') IS NOT NULL)
DROP TABLE #EventChannel

CREATE TABLE #EventChannel (
NodeOrdinal int NOT NULL
,ChannelName nvarchar(256) NOT NULL
)

INSERT #EventChannel (
NodeOrdinal
,ChannelName
)
SELECT
NodeOrdinal
,ChannelName
FROM OPENXML(@XmlDocHandle, 'Keys/Key', 2) WITH (
NodeOrdinal int '@mp:id'
,ChannelName nvarchar(256) '.') xml

EXEC sp_xml_removedocument @XmlDocHandle
SET @XmlDocHandle = NULL

UPDATE ec
SET LastReceivedDateTime = GETUTCDATE()
FROM EventChannel ec
JOIN #EventChannel e ON (ec.EventChannelTitle = e.ChannelName COLLATE database_default)
WHERE (ec.LastReceivedDateTime &lt; DATEADD(day, -1, GETUTCDATE()))

INSERT EventChannel (EventChannelTitle)
SELECT DISTINCT e.ChannelName
FROM #EventChannel e
WHERE NOT EXISTS (SELECT * FROM EventChannel WHERE (EventChannelTitle = e.ChannelName COLLATE database_default))

SELECT
CAST(ec.EventChannelRowId AS int)
FROM #EventChannel e
JOIN EventChannel ec ON (e.ChannelName = ec.EventChannelTitle COLLATE database_default)
ORDER BY e.NodeOrdinal
END TRY
BEGIN CATCH
IF (@@TRANCOUNT &gt; 0)
ROLLBACK TRAN

SELECT
@ErrorNumber = ERROR_NUMBER()
,@ErrorSeverity = ERROR_SEVERITY()
,@ErrorState = ERROR_STATE()
,@ErrorLine = ERROR_LINE()
,@ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-')
,@ErrorMessageText = ERROR_MESSAGE()

SET @ErrorInd = 1
END CATCH

IF (@XmlDocHandle IS NOT NULL)
EXEC sp_xml_removedocument @XmlDocHandle

IF (OBJECT_ID('tempdb..#EventChannel') IS NOT NULL)
DROP TABLE #EventChannel

-- report error if any
IF (@ErrorInd = 1)
BEGIN
DECLARE @AdjustedErrorSeverity int

SET @AdjustedErrorSeverity = CASE
WHEN @ErrorSeverity &gt; 18 THEN 18
ELSE @ErrorSeverity
END

RAISERROR (777971002, @AdjustedErrorSeverity, 1
,@ErrorNumber
,@ErrorSeverity
,@ErrorState
,@ErrorProcedure
,@ErrorLine
,@ErrorMessageText
)
END
END
GO

GRANT EXECUTE ON EventChannelRowIdResolve TO OpsMgrWriter
GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventGroom')
BEGIN
EXECUTE ('CREATE PROCEDURE dbo.EventGroom AS RETURN 1')
END
GO

ALTER PROCEDURE dbo.EventGroom
@DatasetId uniqueidentifier
,@AggregationTypeId tinyint
,@TableGuid uniqueidentifier
,@CutoffDateTime datetime
,@MaxRowsToGroom int
,@RowsDeleted int OUTPUT
AS
BEGIN
SET NOCOUNT ON

DECLARE
@ErrorInd bit
,@ErrorMessage nvarchar(4000)
,@ErrorNumber int
,@ErrorSeverity int
,@ErrorState int
,@ErrorLine int
,@ErrorProcedure sysname
,@ErrorMessageText nvarchar(4000)

SET @ErrorInd = 0

DECLARE
@MainTableName sysname
,@TableName sysname
,@Statement nvarchar(max)
,@DebugLevel tinyint
,@StandardDatasetAggregationStorageRowId int
,@SchemaName sysname

BEGIN TRY
SELECT
@DebugLevel = DebugLevel
,@SchemaName = SchemaName
FROM StandardDataset
WHERE DatasetId = @DatasetId

BEGIN TRAN

CREATE TABLE #EventGroom (
EventOriginId uniqueidentifier NOT NULL
)

SELECT @MainTableName = BaseTableName + '_' + REPLACE(CAST(@TableGuid AS varchar(50)), '-', '')
FROM StandardDatasetAggregationStorage
WHERE (DatasetId = @DatasetId)
AND (AggregationTypeId = @AggregationTypeId)
AND (DependentTableInd = 0)

SET @Statement = 'INSERT #EventGroom (EventOriginId)'
+ ' SELECT TOP ' + CAST(@MaxRowsToGroom AS varchar(15)) + ' EventOriginId'
+ ' FROM ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@MainTableName)
+ ' WHERE ([DateTime] &lt; CONVERT(datetime, ''' + CONVERT(varchar(50), @CutoffDateTime, 120) + ''', 120))'

EXECUTE (@Statement)

SET @StandardDatasetAggregationStorageRowId = 0

WHILE EXISTS (SELECT *
FROM StandardDatasetAggregationStorage
WHERE (DatasetId = @DatasetId)
AND (AggregationTypeId = @AggregationTypeId)
AND (DependentTableInd = 1)
AND (StandardDatasetAggregationStorageRowId &gt; @StandardDatasetAggregationStorageRowId)
)
BEGIN
SELECT TOP 1
@StandardDatasetAggregationStorageRowId = StandardDatasetAggregationStorageRowId
,@TableName = BaseTableName + '_' + REPLACE(CAST(@TableGuid AS varchar(50)), '-', '')
FROM StandardDatasetAggregationStorage
WHERE (DatasetId = @DatasetId)
AND (AggregationTypeId = @AggregationTypeId)
AND (DependentTableInd = 1)
AND (StandardDatasetAggregationStorageRowId &gt; @StandardDatasetAggregationStorageRowId)
Order By StandardDatasetAggregationStorageRowId Asc

SET @Statement = 'DELETE d'
+ ' FROM ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + ' d'
+ ' JOIN #EventGroom e ON (d.EventOriginId = e.EventOriginId)'
EXECUTE (@Statement)
END

SET @Statement = 'DELETE d'
+ ' FROM ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@MainTableName) +' d'
+ ' JOIN #EventGroom e ON (d.EventOriginId = e.EventOriginId)'
EXECUTE (@Statement)
SET @RowsDeleted = @@ROWCOUNT

DROP TABLE #EventGroom

COMMIT

-- groom dynamic dimentions
DECLARE @MaxDataAgeDays int

SELECT @MaxDataAgeDays = MAX(MaxDataAgeDays)
FROM StandardDatasetAggregation
WHERE (DatasetId = @DatasetId)

DELETE e
FROM EventCategory e
WHERE LastReceivedDateTime &lt; DATEADD(day, -@MaxDataAgeDays, GETUTCDATE())
OPTION (RECOMPILE)

DELETE e
FROM EventChannel e
WHERE LastReceivedDateTime &lt; DATEADD(day, -@MaxDataAgeDays, GETUTCDATE())
OPTION (RECOMPILE)

DELETE e
FROM EventLoggingComputer e
WHERE LastReceivedDateTime &lt; DATEADD(day, -@MaxDataAgeDays, GETUTCDATE())
OPTION (RECOMPILE)

DELETE e
FROM EventPublisher e
WHERE LastReceivedDateTime &lt; DATEADD(day, -@MaxDataAgeDays, GETUTCDATE())
OPTION (RECOMPILE)

DELETE e
FROM EventUserName e
WHERE LastReceivedDateTime &lt; DATEADD(day, -@MaxDataAgeDays, GETUTCDATE())
OPTION (RECOMPILE)
END TRY
BEGIN CATCH
IF (@@TRANCOUNT &gt; 0)
ROLLBACK TRAN

SELECT
@ErrorNumber = ERROR_NUMBER()
,@ErrorSeverity = ERROR_SEVERITY()
,@ErrorState = ERROR_STATE()
,@ErrorLine = ERROR_LINE()
,@ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-')
,@ErrorMessageText = ERROR_MESSAGE()

SET @ErrorInd = 1
END CATCH

-- report error if any
IF (@ErrorInd = 1)
BEGIN
IF (@DebugLevel &gt; 0)
BEGIN
DECLARE @DebugMessageText nvarchar(max)

SET @DebugMessageText = N'Failed to groom data for event data set. Error ' + CAST(@ErrorNumber AS varchar(15))
+ ', Procedure ' + @ErrorProcedure
+ ', Line ' + CAST(@ErrorLine AS varchar(15))
+ ', Message: '+ @ErrorMessageText

EXEC DebugMessageInsert
@DatasetId = @DatasetId
,@MessageLevel = 1
,@MessageText = @DebugMessageText
END

DECLARE @AdjustedErrorSeverity int

SET @AdjustedErrorSeverity = CASE
WHEN @ErrorSeverity &gt; 18 THEN 18
ELSE @ErrorSeverity
END

RAISERROR (777971002, @AdjustedErrorSeverity, 1
,@ErrorNumber
,@ErrorSeverity
,@ErrorState
,@ErrorProcedure
,@ErrorLine
,@ErrorMessageText
)
END

RETURN @RowsDeleted
END
GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventLoggingComputerRowIdResolve')
BEGIN
EXECUTE ('CREATE PROCEDURE dbo.EventLoggingComputerRowIdResolve AS RETURN 1')
END
GO

ALTER PROCEDURE dbo.EventLoggingComputerRowIdResolve
@ManagementGroupGuid uniqueidentifier
,@ObjectKeyXml ntext
AS
BEGIN
SET NOCOUNT ON

DECLARE @ManagementGroupRowId int

-- check management group is connected to this DW
DECLARE @CheckExecResult int

EXEC @CheckExecResult = ManagementGroupAccessCheck @ManagementGroupGuid, @ManagementGroupRowId OUTPUT
IF (@CheckExecResult &lt;&gt; 0) RETURN @CheckExecResult

DECLARE
@ErrorInd bit
,@ErrorMessage nvarchar(max)
,@ErrorNumber int
,@ErrorSeverity int
,@ErrorState int
,@ErrorLine int
,@ErrorProcedure nvarchar(256)
,@ErrorMessageText nvarchar(max)

SET @ErrorInd = 0

BEGIN TRY

DECLARE
@ExecResult int
,@XmlDocHandle int

EXEC @ExecResult = sp_xml_preparedocument @XmlDocHandle OUTPUT, @ObjectKeyXml
IF @ExecResult &lt;&gt; 0 RAISERROR(777971000, 16, 1, 'EventLoggingComputerKeys', @ExecResult)

IF (OBJECT_ID('tempdb..#EventLoggingComputer') IS NOT NULL)
DROP TABLE #EventLoggingComputer

CREATE TABLE #EventLoggingComputer (
NodeOrdinal int NOT NULL
,ComputerName nvarchar(256) NOT NULL
)

INSERT #EventLoggingComputer (
NodeOrdinal
,ComputerName
)
SELECT
NodeOrdinal
,LoggingComputerName
FROM OPENXML(@XmlDocHandle, 'Keys/Key', 2) WITH (
NodeOrdinal int '@mp:id'
,LoggingComputerName nvarchar(256) '.') xml

EXEC sp_xml_removedocument @XmlDocHandle
SET @XmlDocHandle = NULL

UPDATE ec
SET LastReceivedDateTime = GETUTCDATE()
FROM EventLoggingComputer ec
JOIN #EventLoggingComputer e ON (ec.ComputerName = e.ComputerName COLLATE database_default)
WHERE (ec.LastReceivedDateTime &lt; DATEADD(day, -1, GETUTCDATE()))

INSERT EventLoggingComputer (ComputerName)
SELECT DISTINCT e.ComputerName
FROM #EventLoggingComputer e
WHERE NOT EXISTS (SELECT * FROM EventLoggingComputer WHERE (ComputerName = e.ComputerName COLLATE database_default))

SELECT
ec.EventLoggingComputerRowId
FROM #EventLoggingComputer e
JOIN EventLoggingComputer ec ON (e.ComputerName = ec.ComputerName COLLATE database_default)
ORDER BY e.NodeOrdinal
END TRY
BEGIN CATCH
IF (@@TRANCOUNT &gt; 0)
ROLLBACK TRAN

SELECT
@ErrorNumber = ERROR_NUMBER()
,@ErrorSeverity = ERROR_SEVERITY()
,@ErrorState = ERROR_STATE()
,@ErrorLine = ERROR_LINE()
,@ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-')
,@ErrorMessageText = ERROR_MESSAGE()

SET @ErrorInd = 1
END CATCH

IF (@XmlDocHandle IS NOT NULL)
EXEC sp_xml_removedocument @XmlDocHandle

IF (OBJECT_ID('tempdb..#EventLoggingComputer') IS NOT NULL)
DROP TABLE #EventLoggingComputer

-- report error if any
IF (@ErrorInd = 1)
BEGIN
DECLARE @AdjustedErrorSeverity int

SET @AdjustedErrorSeverity = CASE
WHEN @ErrorSeverity &gt; 18 THEN 18
ELSE @ErrorSeverity
END

RAISERROR (777971002, @AdjustedErrorSeverity, 1
,@ErrorNumber
,@ErrorSeverity
,@ErrorState
,@ErrorProcedure
,@ErrorLine
,@ErrorMessageText
)
END
END
GO

GRANT EXECUTE ON EventLoggingComputerRowIdResolve TO OpsMgrWriter
GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventProcessStaging')
BEGIN
EXECUTE ('CREATE PROCEDURE dbo.EventProcessStaging AS RETURN 1')
END
GO

ALTER PROCEDURE dbo.EventProcessStaging
@DatasetId uniqueidentifier
AS
BEGIN
SET NOCOUNT ON

DECLARE
@ErrorInd bit
,@ErrorMessage nvarchar(max)
,@ErrorNumber int
,@ErrorSeverity int
,@ErrorState int
,@ErrorLine int
,@ErrorProcedure nvarchar(256)
,@ErrorMessageText nvarchar(max)

SET @ErrorInd = 0

DECLARE
@DebugLevel int
,@SchemaName sysname
,@ExecResult int
,@LockResourceName sysname
,@InsertTableName sysname
,@InsertTableGuid uniqueidentifier
,@InsertStartedDateTime datetime
,@Statement nvarchar(max)
,@MessageText varchar(max)
,@OperationDurationMs bigint
,@MaxStagingRowsToProcess int

BEGIN TRY

SELECT
@DebugLevel = DebugLevel
,@SchemaName = SchemaName
FROM StandardDataset
WHERE (DatasetId = @DatasetId)

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EventStage2Process' AND TABLE_SCHEMA = 'Event')
BEGIN
BEGIN TRAN

DECLARE @ProcessAllRowsInd bit
SET @ProcessAllRowsInd = 1

-- figure out what we need to do with staging area
SELECT
@Statement = StagingTableColumnDefinition
,@MaxStagingRowsToProcess = MaxRowsToProcessPerTransactionCount
FROM StandardDatasetStagingArea
WHERE DatasetId = @DatasetId

IF (@MaxStagingRowsToProcess IS NOT NULL)
BEGIN
DECLARE @EventStageRowCount int
DECLARE @SpaceUsedInfo TABLE (
[name] sysname
,[rows] int
,reserved nvarchar(100)
,data nvarchar(100)
,index_size nvarchar(100)
,unused nvarchar(100)
)

INSERT @SpaceUsedInfo
EXEC sp_spaceused 'Event.EventStage'

SELECT @EventStageRowCount = [rows]
FROM @SpaceUsedInfo

IF (@EventStageRowCount &gt;= @MaxStagingRowsToProcess)
BEGIN
SET @ProcessAllRowsInd = 0
END
END

IF (@ProcessAllRowsInd = 1)
BEGIN
-- no limit set for the number of rows to process
-- rename and re-create staging table

-- lock, rename EventStage table and re-create it
EXEC @ExecResult = sp_rename 'Event.EventStage', 'EventStage2Process'

IF (@ExecResult &lt; 0)
BEGIN
RAISERROR(777971003, 16, 1, @ExecResult)
END

SELECT @Statement = 'CREATE TABLE Event.EventStage (' + @Statement + ')'
EXECUTE (@Statement)
END
ELSE
BEGIN
-- need to process subset of rows
-- create extra column to store original row id
SELECT @Statement =
'CREATE TABLE Event.EventStage2Process ('
+ @Statement
+ ',OriginalEventStageRowId int NOT NULL'
+ ')'
EXECUTE (@Statement)

SELECT @Statement =
' INSERT Event.EventStage2Process'
+ ' ('
+ ' DatasetId'
+ ' ,ManagementGroupGuid'
+ ' ,EventOriginId'
+ ' ,[DateTime]'
+ ' ,RuleRowId'
+ ' ,ManagedEntityRowId'
+ ' ,EventPublisherRowId'
+ ' ,EventChannelRowId'
+ ' ,EventCategoryRowId'
+ ' ,EventLevelId'
+ ' ,LoggingComputerRowId'
+ ' ,EventDisplayNumber'
+ ' ,EventNumber'
+ ' ,UserNameRowId'
+ ' ,RawDescription'
+ ' ,RenderedDescription'
+ ' ,EventParameters'
+ ' ,EventData'
+ ' ,RawDescriptionHash'
+ ' ,ParameterHash'
+ ' ,EventDataHash'
+ ' ,OriginalEventStageRowId'
+ ' )'
+ ' SELECT TOP (' + CAST(@MaxStagingRowsToProcess AS varchar(15)) + ')'
+ ' DatasetId'
+ ' ,ManagementGroupGuid'
+ ' ,EventOriginId'
+ ' ,[DateTime]'
+ ' ,RuleRowId'
+ ' ,ManagedEntityRowId'
+ ' ,EventPublisherRowId'
+ ' ,EventChannelRowId'
+ ' ,EventCategoryRowId'
+ ' ,EventLevelId'
+ ' ,LoggingComputerRowId'
+ ' ,EventDisplayNumber'
+ ' ,EventNumber'
+ ' ,UserNameRowId'
+ ' ,RawDescription'
+ ' ,RenderedDescription'
+ ' ,EventParameters'
+ ' ,EventData'
+ ' ,RawDescriptionHash'
+ ' ,ParameterHash'
+ ' ,EventDataHash'
+ ' ,EventStageRowId'
+ ' FROM Event.EventStage'
+ ' ORDER BY EventStageRowId'
EXECUTE(@Statement)

SELECT @Statement =
' CREATE INDEX IX_EventStage2Process_EventOriginId ON Event.EventStage2Process (EventOriginId)'
EXECUTE(@Statement)

SELECT @Statement =
'DELETE es'
+ ' FROM Event.EventStage es'
+ ' JOIN Event.EventStage2Process esp ON (es.EventStageRowId = esp.OriginalEventStageRowId)'
EXECUTE(@Statement)
END

GRANT ALTER, SELECT, INSERT, UPDATE, DELETE ON Event.EventStage TO OpsMgrWriter

GRANT CONTROL ON Event.EventStage2Process TO OpsMgrWriter

COMMIT
END

BEGIN TRAN

-- lock table map to ensure we insert into table opened for insertion
SET @LockResourceName = CAST(@DatasetId AS varchar(50)) + '_TableMap'

EXEC @ExecResult = sp_getapplock
@Resource = @LockResourceName
,@LockMode = 'Shared'
,@LockOwner = 'Transaction'

IF (@ExecResult &lt; 0)
BEGIN
RAISERROR(777971001, 16, 1, 'Shared:StandardDatasetTableMap', @ExecResult)
END

EXEC dbo.StandardDatasetGetInsertTableGuid @DatasetId, 0, @InsertTableGuid OUTPUT

IF (@DebugLevel &gt; 3)
BEGIN
SELECT @MessageText = 'Starting to insert ' + CAST(COUNT(*) AS varchar(30)) + ' events into ' + @InsertTableName
FROM Event.EventStage2Process

SET @InsertStartedDateTime = GETUTCDATE()

EXEC DebugMessageInsert
@DatasetId = @DatasetId
,@MessageLevel = 4
,@MessageText = @MessageText
END

-- insert event &lt;-&gt; rule/me relationships
-- NOTE: We do not check for event-rule-ME uniqueness
-- to save time (can produce dupes if event was delivered twice
-- in two different staging processing windows)
-- We also saw Sql having troubles coming up with a good plan

SELECT @InsertTableName = BaseTableName + '_' + REPLACE(CAST(@InsertTableGuid AS varchar(50)), '-', '')
FROM StandardDatasetAggregationStorage
WHERE (DatasetId = @DatasetId)
AND (AggregationTypeId = 0)
AND (DependentTableInd = 1)
AND (TableTag = 'rule')

SELECT @Statement =
' INSERT ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@InsertTableName) + ' ('
+ ' EventOriginId'
+ ' ,RuleRowId'
+ ' ,ManagedEntityRowId'
+ ' )'
+ ' SELECT'
+ ' s.EventOriginId'
+ ' ,s.RuleRowId'
+ ' ,s.ManagedEntityRowId'
+ ' FROM Event.EventStage2Process s'

EXECUTE(@Statement)

-- delete events collected more then once - leave latest collected only
DELETE s
FROM Event.EventStage2Process s
WHERE EXISTS (SELECT *
FROM Event.EventStage2Process s_later
WHERE (s.EventOriginId = s_later.EventOriginId)
AND (s_later.EventStageRowId &gt; s.EventStageRowId)
)

-- insert events
SELECT @InsertTableName = BaseTableName + '_' + REPLACE(CAST(@InsertTableGuid AS varchar(50)), '-', '')
FROM StandardDatasetAggregationStorage
WHERE (DatasetId = @DatasetId)
AND (AggregationTypeId = 0)
AND (DependentTableInd = 0)

-- first delete events that were already delivered

SELECT @Statement = 'DELETE s'
+ ' FROM Event.EventStage2Process s, ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@InsertTableName) + ' e'
+ ' WHERE (s.EventOriginId = e.EventOriginId)'
EXECUTE (@Statement)

SELECT @Statement =
' INSERT ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@InsertTableName) + ' ('
+ ' EventOriginId'
+ ' ,[DateTime]'
+ ' ,EventPublisherRowId'
+ ' ,EventChannelRowId'
+ ' ,EventCategoryRowId'
+ ' ,EventLevelId'
+ ' ,LoggingComputerRowId'
+ ' ,EventNumber'
+ ' ,EventDisplayNumber'
+ ' ,UserNameRowId'
+ ' ,RawDescriptionHash'
+ ' ,ParameterHash'
+ ' ,EventDataHash'
+ ' )'
+ ' SELECT '
+ ' s.EventOriginId'
+ ' ,s.[DateTime]'
+ ' ,s.EventPublisherRowId'
+ ' ,s.EventChannelRowId'
+ ' ,s.EventCategoryRowId'
+ ' ,s.EventLevelId'
+ ' ,s.LoggingComputerRowId'
+ ' ,s.EventNumber'
+ ' ,s.EventDisplayNumber'
+ ' ,s.UserNameRowId'
+ ' ,s.RawDescriptionHash'
+ ' ,s.ParameterHash'
+ ' ,s.EventDataHash'
+ ' FROM Event.EventStage2Process s'

EXECUTE(@Statement)

-- insert parameters

SELECT @InsertTableName = BaseTableName + '_' + REPLACE(CAST(@InsertTableGuid AS varchar(50)), '-', '')
FROM StandardDatasetAggregationStorage
WHERE (DatasetId = @DatasetId)
AND (AggregationTypeId = 0)
AND (DependentTableInd = 1)
AND (TableTag = 'parameter')

SELECT @Statement =
' INSERT ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@InsertTableName) + ' ('
+ ' EventOriginId'
+ ' ,ParameterIndex'
+ ' ,ParameterValue'
+ ' )'
+ ' SELECT '
+ ' s.EventOriginId'
+ ' ,ROW_NUMBER() OVER(PARTITION BY s.EventOriginId ORDER BY s.EventOriginId)'
+ ' ,p.ParameterValue.value(''.'', ''nvarchar(max)'')'
+ ' FROM Event.EventStage2Process s'
+ ' CROSS APPLY s.EventParameters.nodes(''Params/Param'') as p(ParameterValue)'

EXECUTE(@Statement)

-- insert event details

SELECT @InsertTableName = BaseTableName + '_' + REPLACE(CAST(@InsertTableGuid AS varchar(50)), '-', '')
FROM StandardDatasetAggregationStorage
WHERE (DatasetId = @DatasetId)
AND (AggregationTypeId = 0)
AND (DependentTableInd = 1)
AND (TableTag = 'detail')

SELECT @Statement =
' INSERT ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@InsertTableName) + ' ('
+ ' EventOriginId'
+ ' ,RawDescription'
+ ' ,RenderedDescription'
+ ' ,EventData'
+ ' )'
+ ' SELECT '
+ ' s.EventOriginId'
+ ' ,s.RawDescription'
+ ' ,s.RenderedDescription'
+ ' ,s.EventData'
+ ' FROM Event.EventStage2Process s'

EXECUTE(@Statement)

-- delete all inserted data from staging
DROP TABLE Event.EventStage2Process

IF (@DebugLevel &gt; 3)
BEGIN
SET @MessageText = 'Finished inserting events into ' + @InsertTableName
SET @OperationDurationMs = ABS(DATEDIFF(ms, GETUTCDATE(), @InsertStartedDateTime))

EXEC DebugMessageInsert
@DatasetId = @DatasetId
,@MessageLevel = 4
,@MessageText = @MessageText
,@OperationDurationMs = @OperationDurationMs
END

COMMIT
END TRY
BEGIN CATCH
IF (@@TRANCOUNT &gt; 0)
ROLLBACK TRAN

SELECT
@ErrorNumber = ERROR_NUMBER()
,@ErrorSeverity = ERROR_SEVERITY()
,@ErrorState = ERROR_STATE()
,@ErrorLine = ERROR_LINE()
,@ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-')
,@ErrorMessageText = ERROR_MESSAGE()

SET @ErrorInd = 1
END CATCH

-- report error if any
IF (@ErrorInd = 1)
BEGIN
IF (@DebugLevel &gt; 0)
BEGIN
DECLARE @DebugMessageText nvarchar(max)

SET @DebugMessageText = N'Failed to process data in the event data set staging area. Error ' + CAST(@ErrorNumber AS varchar(15))
+ ', Procedure ' + @ErrorProcedure
+ ', Line ' + CAST(@ErrorLine AS varchar(15))
+ ', Message: '+ @ErrorMessageText

EXEC DebugMessageInsert
@DatasetId = @DatasetId
,@MessageLevel = 1
,@MessageText = @DebugMessageText
END

DECLARE @AdjustedErrorSeverity int

SET @AdjustedErrorSeverity = CASE
WHEN @ErrorSeverity &gt; 18 THEN 18
ELSE @ErrorSeverity
END

RAISERROR (777971002, @AdjustedErrorSeverity, 1
,@ErrorNumber
,@ErrorSeverity
,@ErrorState
,@ErrorProcedure
,@ErrorLine
,@ErrorMessageText
)
END
END
GO

GRANT EXECUTE ON dbo.EventProcessStaging TO OpsMgrWriter
GO


IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventPublisherRowIdResolve')
BEGIN
EXECUTE ('CREATE PROCEDURE dbo.EventPublisherRowIdResolve AS RETURN 1')
END
GO

ALTER PROCEDURE dbo.EventPublisherRowIdResolve
@ManagementGroupGuid uniqueidentifier
,@ObjectKeyXml ntext
AS
BEGIN
SET NOCOUNT ON

DECLARE @ManagementGroupRowId int

-- check management group is connected to this DW
DECLARE @CheckExecResult int

EXEC @CheckExecResult = ManagementGroupAccessCheck @ManagementGroupGuid, @ManagementGroupRowId OUTPUT
IF (@CheckExecResult &lt;&gt; 0) RETURN @CheckExecResult

DECLARE
@ErrorInd bit
,@ErrorMessage nvarchar(max)
,@ErrorNumber int
,@ErrorSeverity int
,@ErrorState int
,@ErrorLine int
,@ErrorProcedure nvarchar(256)
,@ErrorMessageText nvarchar(max)

SET @ErrorInd = 0

BEGIN TRY

DECLARE
@ExecResult int
,@XmlDocHandle int

EXEC @ExecResult = sp_xml_preparedocument @XmlDocHandle OUTPUT, @ObjectKeyXml
IF @ExecResult &lt;&gt; 0 RAISERROR(777971000, 16, 1, 'EventPublisherKeys', @ExecResult)

IF (OBJECT_ID('tempdb..#EventPublisher') IS NOT NULL)
DROP TABLE #EventPublisher

CREATE TABLE #EventPublisher (
NodeOrdinal int NOT NULL
,PublisherGuid uniqueidentifier NOT NULL
,PublisherName nvarchar(256) NOT NULL
)

INSERT #EventPublisher (
NodeOrdinal
,PublisherGuid
,PublisherName
)
SELECT
NodeOrdinal
,PublisherGuid
,PublisherName
FROM OPENXML(@XmlDocHandle, 'Keys/Key', 2) WITH (
NodeOrdinal int '@mp:id'
,PublisherGuid uniqueidentifier 'PublisherGuid'
,PublisherName nvarchar(256) 'PublisherName') xml

EXEC sp_xml_removedocument @XmlDocHandle
SET @XmlDocHandle = NULL

UPDATE ep
SET LastReceivedDateTime = GETUTCDATE()
FROM EventPublisher ep
JOIN #EventPublisher e ON (ep.EventPublisherGuid = e.PublisherGuid)
WHERE (ep.LastReceivedDateTime &lt; DATEADD(day, -1, GETUTCDATE()))

INSERT EventPublisher (EventPublisherGuid, EventPublisherName)
SELECT DISTINCT e.PublisherGuid, e.PublisherName
FROM #EventPublisher e
WHERE NOT EXISTS (SELECT * FROM EventPublisher WHERE (EventPublisherGuid = e.PublisherGuid))

SELECT
ep.EventPublisherRowId
FROM #EventPublisher e
JOIN EventPublisher ep ON (e.PublisherGuid = ep.EventPublisherGuid)
ORDER BY e.NodeOrdinal
END TRY
BEGIN CATCH
IF (@@TRANCOUNT &gt; 0)
ROLLBACK TRAN

SELECT
@ErrorNumber = ERROR_NUMBER()
,@ErrorSeverity = ERROR_SEVERITY()
,@ErrorState = ERROR_STATE()
,@ErrorLine = ERROR_LINE()
,@ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-')
,@ErrorMessageText = ERROR_MESSAGE()

SET @ErrorInd = 1
END CATCH

IF (@XmlDocHandle IS NOT NULL)
EXEC sp_xml_removedocument @XmlDocHandle

IF (OBJECT_ID('tempdb..#EventPublisher') IS NOT NULL)
DROP TABLE #EventPublisher

-- report error if any
IF (@ErrorInd = 1)
BEGIN
DECLARE @AdjustedErrorSeverity int

SET @AdjustedErrorSeverity = CASE
WHEN @ErrorSeverity &gt; 18 THEN 18
ELSE @ErrorSeverity
END

RAISERROR (777971002, @AdjustedErrorSeverity, 1
,@ErrorNumber
,@ErrorSeverity
,@ErrorState
,@ErrorProcedure
,@ErrorLine
,@ErrorMessageText
)
END
END
GO

GRANT EXECUTE ON EventPublisherRowIdResolve TO OpsMgrWriter
GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EventUserNameRowIdResolve')
BEGIN
EXECUTE ('CREATE PROCEDURE dbo.EventUserNameRowIdResolve AS RETURN 1')
END
GO

ALTER PROCEDURE dbo.EventUserNameRowIdResolve
@ManagementGroupGuid uniqueidentifier
,@ObjectKeyXml ntext
AS
BEGIN
SET NOCOUNT ON

DECLARE @ManagementGroupRowId int

-- check management group is connected to this DW
DECLARE @CheckExecResult int

EXEC @CheckExecResult = ManagementGroupAccessCheck @ManagementGroupGuid, @ManagementGroupRowId OUTPUT
IF (@CheckExecResult &lt;&gt; 0) RETURN @CheckExecResult

DECLARE
@ErrorInd bit
,@ErrorMessage nvarchar(max)
,@ErrorNumber int
,@ErrorSeverity int
,@ErrorState int
,@ErrorLine int
,@ErrorProcedure nvarchar(256)
,@ErrorMessageText nvarchar(max)

SET @ErrorInd = 0

BEGIN TRY

DECLARE
@ExecResult int
,@XmlDocHandle int

EXEC @ExecResult = sp_xml_preparedocument @XmlDocHandle OUTPUT, @ObjectKeyXml
IF @ExecResult &lt;&gt; 0 RAISERROR(777971000, 16, 1, 'EventUserNameKeys', @ExecResult)

IF (OBJECT_ID('tempdb..#EventUserName') IS NOT NULL)
DROP TABLE #EventUserName

CREATE TABLE #EventUserName (
NodeOrdinal int NOT NULL
,UserName nvarchar(256) NOT NULL
)

INSERT #EventUserName (
NodeOrdinal
,UserName
)
SELECT
NodeOrdinal
,UserName
FROM OPENXML(@XmlDocHandle, 'Keys/Key', 2) WITH (
NodeOrdinal int '@mp:id'
,UserName nvarchar(256) '.') xml

EXEC sp_xml_removedocument @XmlDocHandle
SET @XmlDocHandle = NULL

UPDATE ec
SET LastReceivedDateTime = GETUTCDATE()
FROM EventUserName ec
JOIN #EventUserName e ON (ec.UserName = e.UserName COLLATE database_default)
WHERE (ec.LastReceivedDateTime &lt; DATEADD(day, -1, GETUTCDATE()))

INSERT EventUserName (UserName)
SELECT DISTINCT e.UserName
FROM #EventUserName e
WHERE NOT EXISTS (SELECT * FROM EventUserName WHERE (UserName = e.UserName COLLATE database_default))

SELECT
ec.EventUserNameRowId
FROM #EventUserName e
JOIN EventUserName ec ON (e.UserName = ec.UserName COLLATE database_default)
ORDER BY e.NodeOrdinal
END TRY
BEGIN CATCH
IF (@@TRANCOUNT &gt; 0)
ROLLBACK TRAN

SELECT
@ErrorNumber = ERROR_NUMBER()
,@ErrorSeverity = ERROR_SEVERITY()
,@ErrorState = ERROR_STATE()
,@ErrorLine = ERROR_LINE()
,@ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-')
,@ErrorMessageText = ERROR_MESSAGE()

SET @ErrorInd = 1
END CATCH

IF (@XmlDocHandle IS NOT NULL)
EXEC sp_xml_removedocument @XmlDocHandle

IF (OBJECT_ID('tempdb..#EventUserName') IS NOT NULL)
DROP TABLE #EventUserName

-- report error if any
IF (@ErrorInd = 1)
BEGIN
DECLARE @AdjustedErrorSeverity int

SET @AdjustedErrorSeverity = CASE
WHEN @ErrorSeverity &gt; 18 THEN 18
ELSE @ErrorSeverity
END

RAISERROR (777971002, @AdjustedErrorSeverity, 1
,@ErrorNumber
,@ErrorSeverity
,@ErrorState
,@ErrorProcedure
,@ErrorLine
,@ErrorMessageText
)
END
END
GO

GRANT EXECUTE ON EventUserNameRowIdResolve TO OpsMgrWriter
GO


SET NOCOUNT ON

UPDATE StandardDatasetStagingArea
SET StagingTableColumnDefinition =
'DatasetId uniqueidentifier NOT NULL
,ManagementGroupGuid uniqueidentifier NOT NULL
,EventOriginId uniqueidentifier NOT NULL
,[DateTime] datetime NOT NULL
,RuleRowId int NOT NULL
,ManagedEntityRowId int NOT NULL
,EventPublisherRowId int NOT NULL
,EventChannelRowId smallint NOT NULL
,EventCategoryRowId int NOT NULL
,EventLevelId tinyint NOT NULL
,LoggingComputerRowId int NOT NULL
,EventDisplayNumber int NOT NULL
,EventNumber bigint NOT NULL
,UserNameRowId int NOT NULL
,RawDescription nvarchar(max) NULL
,RenderedDescription nvarchar(max) NULL
,EventParameters xml NULL
,EventData xml NULL
,RawDescriptionHash uniqueidentifier NULL
,ParameterHash uniqueidentifier NULL
,EventDataHash uniqueidentifier NULL

,EventStageRowId bigint NOT NULL IDENTITY(1, 1) PRIMARY KEY
'
,MaxRowsToProcessPerTransactionCount = CASE WHEN MaxRowsToProcessPerTransactionCount &lt; 20000 THEN MaxRowsToProcessPerTransactionCount ELSE 20000 END
WHERE DatasetId = '$Config/DatasetId$'

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE (TABLE_NAME = 'EventStage')
AND (TABLE_SCHEMA = 'Event')
AND (CONSTRAINT_TYPE = 'PRIMARY KEY')
)
BEGIN
ALTER TABLE Event.EventStage
ADD CONSTRAINT PK_EventStage PRIMARY KEY NONCLUSTERED (EventStageRowId)
END
GO

</Upgrade>
</DataWarehouseDataSet>