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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

-- delete std dataset related data

DECLARE curDataSetIds INSENSITIVE CURSOR 
  FOR SELECT DataSetId FROM StandardDataSet WHERE SchemaName = 'KMSEvent'

DECLARE @dataSetId UNIQUEIDENTIFIER

OPEN curDataSetIds

FETCH NEXT FROM curDataSetIds
INTO @dataSetId

WHILE @@FETCH_STATUS = 0
BEGIN
  PRINT 'Deleting dataset: ' + CAST(@dataSetId AS NVARCHAR(50))

  EXEC StandardDatasetDelete @dataSetId
  FETCH NEXT FROM curDataSetIds
  INTO @dataSetId
END

CLOSE curDataSetIds 
DEALLOCATE curDataSetIds


GO
  