BEGIN TRANSACTION

DECLARE @PropertyActive uniqueidentifier
SELECT TOP 1 @PropertyActive = ParameterTypeID FROM ParameterTypeList WHERE ParameterTypeName = 'Active'



PRINT 'Deleting from EmailRules...'
DELETE FROM EmailRules							WHERE [ParameterListID] NOT IN (SELECT [ParameterList].[ParameterListID] FROM [ParameterList]  INNER JOIN [ParameterTypeInParameterList] ON [ParameterList].ParameterListID = [ParameterTypeInParameterList].ParameterListID and [ParameterTypeInParameterList].ParameterTypeID =  @PropertyActive)

PRINT 'Deleting from EmailSelectedPackagesRules...'
DELETE FROM EmailSelectedPackagesRules			WHERE [ParameterListID] NOT IN (SELECT [ParameterList].[ParameterListID] FROM [ParameterList]  INNER JOIN [ParameterTypeInParameterList] ON [ParameterList].ParameterListID = [ParameterTypeInParameterList].ParameterListID and [ParameterTypeInParameterList].ParameterTypeID =  @PropertyActive)

PRINT 'Deleting from InvoiceData...'
DELETE FROM InvoiceData							WHERE [ParameterListID] NOT IN (SELECT [ParameterList].[ParameterListID] FROM [ParameterList]  INNER JOIN [ParameterTypeInParameterList] ON [ParameterList].ParameterListID = [ParameterTypeInParameterList].ParameterListID and [ParameterTypeInParameterList].ParameterTypeID =  @PropertyActive)

PRINT 'Deleting from Parameter...'
DELETE FROM Parameter							WHERE [ParameterListID] NOT IN (SELECT [ParameterList].[ParameterListID] FROM [ParameterList]  INNER JOIN [ParameterTypeInParameterList] ON [ParameterList].ParameterListID = [ParameterTypeInParameterList].ParameterListID and [ParameterTypeInParameterList].ParameterTypeID =  @PropertyActive)


PRINT 'Deleting from ParameterListInLookUpSources...'
DELETE FROM ParameterListInLookUpSources		WHERE [ParameterListID] NOT IN (SELECT [ParameterList].[ParameterListID] FROM [ParameterList]  INNER JOIN [ParameterTypeInParameterList] ON [ParameterList].ParameterListID = [ParameterTypeInParameterList].ParameterListID and [ParameterTypeInParameterList].ParameterTypeID =  @PropertyActive)

PRINT 'Deleting from ParameterListInTaskList...'
DELETE FROM ParameterListInTaskList				WHERE [ParameterListID] NOT IN (SELECT [ParameterList].[ParameterListID] FROM [ParameterList]  INNER JOIN [ParameterTypeInParameterList] ON [ParameterList].ParameterListID = [ParameterTypeInParameterList].ParameterListID and [ParameterTypeInParameterList].ParameterTypeID =  @PropertyActive)

PRINT 'Deleting from ParameterListInTaskListConfig...'
DELETE FROM ParameterListInTaskListConfig		WHERE [ParameterListID] NOT IN (SELECT [ParameterList].[ParameterListID] FROM [ParameterList]  INNER JOIN [ParameterTypeInParameterList] ON [ParameterList].ParameterListID = [ParameterTypeInParameterList].ParameterListID and [ParameterTypeInParameterList].ParameterTypeID =  @PropertyActive)

PRINT 'Deleting from ParameterListInTaskListInRoles...'
DELETE FROM ParameterListInTaskListInRoles		WHERE [ParameterListID] NOT IN (SELECT [ParameterList].[ParameterListID] FROM [ParameterList]  INNER JOIN [ParameterTypeInParameterList] ON [ParameterList].ParameterListID = [ParameterTypeInParameterList].ParameterListID and [ParameterTypeInParameterList].ParameterTypeID =  @PropertyActive)

PRINT 'Deleting from ParameterListMandatoryInTaskList...'
DELETE FROM ParameterListMandatoryInTaskList	WHERE [ParameterListID] NOT IN (SELECT [ParameterList].[ParameterListID] FROM [ParameterList]  INNER JOIN [ParameterTypeInParameterList] ON [ParameterList].ParameterListID = [ParameterTypeInParameterList].ParameterListID and [ParameterTypeInParameterList].ParameterTypeID =  @PropertyActive)

PRINT 'Deleting from ParameterListsInGroups...'
DELETE FROM ParameterListsInGroups				WHERE [ParameterListID] NOT IN (SELECT [ParameterList].[ParameterListID] FROM [ParameterList]  INNER JOIN [ParameterTypeInParameterList] ON [ParameterList].ParameterListID = [ParameterTypeInParameterList].ParameterListID and [ParameterTypeInParameterList].ParameterTypeID =  @PropertyActive)

PRINT 'Deleting from ParameterTypeInParameterList...'
DELETE FROM ParameterTypeInParameterList		WHERE [ParameterListID] NOT IN (SELECT [ParameterList].[ParameterListID] FROM [ParameterList]  INNER JOIN [ParameterTypeInParameterList] ON [ParameterList].ParameterListID = [ParameterTypeInParameterList].ParameterListID and [ParameterTypeInParameterList].ParameterTypeID =  @PropertyActive)

PRINT 'Deleting from ParameterValues...'
DELETE FROM ParameterValues						WHERE [ParameterListID] NOT IN (SELECT [ParameterList].[ParameterListID] FROM [ParameterList]  INNER JOIN [ParameterTypeInParameterList] ON [ParameterList].ParameterListID = [ParameterTypeInParameterList].ParameterListID and [ParameterTypeInParameterList].ParameterTypeID =  @PropertyActive)

PRINT 'Deleting from ReportStandardFields...'
DELETE FROM ReportStandardFields				WHERE [ParameterListID] NOT IN (SELECT [ParameterList].[ParameterListID] FROM [ParameterList]  INNER JOIN [ParameterTypeInParameterList] ON [ParameterList].ParameterListID = [ParameterTypeInParameterList].ParameterListID and [ParameterTypeInParameterList].ParameterTypeID =  @PropertyActive)

PRINT 'Deleting from DataHistory...'
DELETE FROM [DataHistory]						WHERE UPPER(SUBSTRING([DataHistoryValue],0,37)) NOT IN (SELECT  CONVERT(NVARCHAR(50),[ParameterList].[ParameterListID]) FROM [ParameterList]  INNER JOIN [ParameterTypeInParameterList] ON [ParameterList].ParameterListID = [ParameterTypeInParameterList].ParameterListID and [ParameterTypeInParameterList].ParameterTypeID =  @PropertyActive)
			
PRINT 'Deleting from ParameterList...'
DELETE FROM ParameterList						WHERE [ParameterListID] NOT IN (SELECT [ParameterList].[ParameterListID] FROM [ParameterList]  INNER JOIN [ParameterTypeInParameterList] ON [ParameterList].ParameterListID = [ParameterTypeInParameterList].ParameterListID and [ParameterTypeInParameterList].ParameterTypeID =  @PropertyActive)



COMMIT TRANSACTION