

DECLARE @categoryID INT
DECLARE @isActive INT 
DECLARE @currentDatabase NVARCHAR(MAX)
DECLARE @run INT 
SET @run = 0
SET @currentDatabase = DB_NAME()

PRINT 'Executing script against the ' + @currentDatabase + ' database...'

IF NOT EXISTS (SELECT * FROM [pal_TaskCategory] WHERE taskCategory = 'Service Desk')
BEGIN
    PRINT 'Service Desk Category does not exist in [pal_TaskCategory] table. Creating...'
    INSERT INTO [dbo].[pal_TaskCategory]
           ([taskCategory]
           ,[isActive])
     VALUES
           ('Service Desk'
           ,1)

    SET @categoryID = (SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY])
    PRINT 'Entry creation on [pal_TaskCategory] table. Done...'
    SET @run = 1
END
ELSE
BEGIN
    SET @isActive = (SELECT isActive FROM [dbo].[pal_TaskCategory] WHERE taskCategory = 'Service Desk')
    SET @categoryID = (SELECT recId FROM [dbo].[pal_TaskCategory] WHERE taskCategory = 'Service Desk')
    IF (@isActive = 0)
    BEGIN
	   PRINT 'Service Desk Category is not active in [pal_TaskCategory] table. Setting to active...'
	   UPDATE [dbo].[pal_TaskCategory]
	   SET [isActive] = 1
	   WHERE recId = @categoryID
	   PRINT 'Activating Service Desk Category in [pal_TaskCategory] table. Done...'
	   SET @run = 1
    END 
END



IF NOT EXISTS (SELECT * FROM [pal_TaskType] WHERE taskType = 'Support')
BEGIN
    PRINT 'Support Task Type does not exist in [pal_TaskType] table. Creating...'
    INSERT INTO [dbo].[pal_TaskType]
           ([taskCategoryId]
           ,[taskType]
           ,[isActive])
     VALUES
           (2
           ,'Support'
           ,1)
    PRINT 'Entry creation on [pal_TaskType] table. Done...'
    SET @run = 1
END
ELSE
BEGIN
    DECLARE @recId INT
    SET @isActive = (SELECT isActive FROM [dbo].[pal_TaskType] WHERE taskType = 'Support')
    SET @recId = (SELECT recId FROM [dbo].[pal_TaskType] WHERE taskType = 'Support')
    IF (@isActive = 0)
    BEGIN
	   PRINT 'Support Task Type is not active in [pal_TaskType] table. Setting to active...'
	   UPDATE [dbo].[pal_TaskType]
	   SET [isActive] = 1
	   WHERE recId = @recId
	   PRINT 'Activating Support Task Type in [pal_TaskType] table. Done...'
	   SET @run = 1
    END
END 

IF (@run = 1)
BEGIN
    PRINT 'Script logic executed against the ' + @currentDatabase + ' database complete.'
END

IF (@run = 0)
BEGIN
    PRINT 'Service Desk Logic on ' + @currentDatabase + ' database is up to date. Database remains unchanged.'
END

GO

