﻿GO
PRINT N'Creating [dbo].[T_KIRKONTENER_OSSZES]...';


GO
CREATE VIEW [dbo].T_KIRKONTENER_OSSZES
        AS
        SELECT *
        FROM dbo.T_KIRKONTENER
GO
PRINT N'Creating [dbo].[T_KIRKONTENERELEM_OSSZES]...';


GO
CREATE VIEW [dbo].T_KIRKONTENERELEM_OSSZES
        AS
        SELECT *
        FROM dbo.T_KIRKONTENERELEM
GO
PRINT N'Creating [dbo].[T_KIRKONTENERSTATUSZ_OSSZES]...';


GO
CREATE VIEW [dbo].T_KIRKONTENERSTATUSZ_OSSZES
        AS
        SELECT *
        FROM dbo.T_KIRKONTENERSTATUSZ


GO


--------------------

IF OBJECT_ID('[dbo].[sp_AddNewSchemaViews]') IS NOT NULL BEGIN
  DROP PROCEDURE [dbo].[sp_AddNewSchemaViews]
END
GO

CREATE PROCEDURE [dbo].[sp_AddNewSchemaViews]
		@IntezmenyId int,
		@IntezmenyAzonosito nvarchar(30),
		@TanevId int
AS
BEGIN

DECLARE @IntezmenyTemplate nvarchar(50) = 'KR_'+@IntezmenyAzonosito
DECLARE @Schema nvarchar(50) = @IntezmenyTemplate+'_Schema'
DECLARE @SchemaUser nvarchar(50) = @IntezmenyTemplate + '_user'
DECLARE @AktivTanev nvarchar(500)
DECLARE @Sql nvarchar(max)

/*============================================================================*/
/* View : T_KIRKONTENER                                                           */
/*============================================================================*/
/* Package: Kreta */
IF OBJECT_ID('['+@Schema+'].T_KIRKONTENER', 'V') IS NOT NULL BEGIN
    SET @Sql = N'EXEC sp_refreshview ''['+@Schema+'].T_KIRKONTENER'''
    EXEC sp_executesql @Sql

    IF OBJECT_ID('['+@Schema+'].T_KIRKONTENER_OSSZES', 'V') IS NOT NULL BEGIN
        SET @Sql = N'EXEC sp_refreshview ''['+@Schema+'].T_KIRKONTENER_OSSZES'''
        EXEC sp_executesql @Sql
    END
END
ELSE BEGIN
    /* Ha nem létezik a view, akkor létrehozzuk */
    IF (SELECT COUNT(1)
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = N'T_KIRKONTENER' AND TABLE_SCHEMA = 'dbo' AND COLUMN_NAME = 'C_TANEVID' 
            ) > 0
    BEGIN
        SET @AktivTanev = N' C_TANEVID = '+ CAST(@TanevId as nvarchar) +' AND '
    END
    ELSE 
    BEGIN
        SET @AktivTanev = N''
    END

    IF (SELECT COUNT(1)
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = N'T_KIRKONTENER' AND TABLE_SCHEMA = 'dbo' AND COLUMN_NAME = 'C_INTEZMENYID' 
            ) > 0
    BEGIN
        /* T_KIRKONTENER */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_KIRKONTENER
        AS
        SELECT *
        FROM dbo.T_KIRKONTENER
        WHERE '+@AktivTanev+' (TOROLT = ''F'') AND C_INTEZMENYID = '+CAST(@IntezmenyId as nvarchar)
        EXECUTE sp_executesql @Sql

        /* T_KIRKONTENER_OSSZES */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_KIRKONTENER_OSSZES
        AS
        SELECT *
        FROM dbo.T_KIRKONTENER
        WHERE C_INTEZMENYID = '+CAST(@IntezmenyId as nvarchar)
        EXECUTE sp_executesql @Sql
    END
    ELSE 
    BEGIN
        /* T_KIRKONTENER */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_KIRKONTENER
        AS
        SELECT *
        FROM dbo.T_KIRKONTENER
        WHERE '+@AktivTanev+' (TOROLT = ''F'')'
        EXECUTE sp_executesql @Sql

        /* T_KIRKONTENER_OSSZES */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_KIRKONTENER_OSSZES
        AS
        SELECT *
        FROM dbo.T_KIRKONTENER'
        EXECUTE sp_executesql @Sql
    END
END


/*============================================================================*/
/* View : T_KIRKONTENERSTATUSZ                                                    */
/*============================================================================*/
/* Package: Kreta */
IF OBJECT_ID('['+@Schema+'].T_KIRKONTENERSTATUSZ', 'V') IS NOT NULL BEGIN
    SET @Sql = N'EXEC sp_refreshview ''['+@Schema+'].T_KIRKONTENERSTATUSZ'''
    EXEC sp_executesql @Sql

    IF OBJECT_ID('['+@Schema+'].T_KIRKONTENERSTATUSZ_OSSZES', 'V') IS NOT NULL BEGIN
        SET @Sql = N'EXEC sp_refreshview ''['+@Schema+'].T_KIRKONTENERSTATUSZ_OSSZES'''
        EXEC sp_executesql @Sql
    END
END
ELSE BEGIN
    /* Ha nem létezik a view, akkor létrehozzuk */
    IF (SELECT COUNT(1)
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = N'T_KIRKONTENERSTATUSZ' AND TABLE_SCHEMA = 'dbo' AND COLUMN_NAME = 'C_TANEVID' 
            ) > 0
    BEGIN
        SET @AktivTanev = N' C_TANEVID = '+ CAST(@TanevId as nvarchar) +' AND '
    END
    ELSE 
    BEGIN
        SET @AktivTanev = N''
    END

    IF (SELECT COUNT(1)
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = N'T_KIRKONTENERSTATUSZ' AND TABLE_SCHEMA = 'dbo' AND COLUMN_NAME = 'C_INTEZMENYID' 
            ) > 0
    BEGIN
        /* T_KIRKONTENERSTATUSZ */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_KIRKONTENERSTATUSZ
        AS
        SELECT *
        FROM dbo.T_KIRKONTENERSTATUSZ
        WHERE '+@AktivTanev+' (TOROLT = ''F'') AND C_INTEZMENYID = '+CAST(@IntezmenyId as nvarchar)
        EXECUTE sp_executesql @Sql

        /* T_KIRKONTENERSTATUSZ_OSSZES */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_KIRKONTENERSTATUSZ_OSSZES
        AS
        SELECT *
        FROM dbo.T_KIRKONTENERSTATUSZ
        WHERE C_INTEZMENYID = '+CAST(@IntezmenyId as nvarchar)
        EXECUTE sp_executesql @Sql
    END
    ELSE 
    BEGIN
        /* T_KIRKONTENERSTATUSZ */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_KIRKONTENERSTATUSZ
        AS
        SELECT *
        FROM dbo.T_KIRKONTENERSTATUSZ
        WHERE '+@AktivTanev+' (TOROLT = ''F'')'
        EXECUTE sp_executesql @Sql

        /* T_KIRKONTENERSTATUSZ_OSSZES */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_KIRKONTENERSTATUSZ_OSSZES
        AS
        SELECT *
        FROM dbo.T_KIRKONTENERSTATUSZ'
        EXECUTE sp_executesql @Sql
    END
END


/*============================================================================*/
/* View : T_KIRKONTENERELEM                                                       */
/*============================================================================*/
/* Package: Kreta */
IF OBJECT_ID('['+@Schema+'].T_KIRKONTENERELEM', 'V') IS NOT NULL BEGIN
    SET @Sql = N'EXEC sp_refreshview ''['+@Schema+'].T_KIRKONTENERELEM'''
    EXEC sp_executesql @Sql

    IF OBJECT_ID('['+@Schema+'].T_KIRKONTENERELEM_OSSZES', 'V') IS NOT NULL BEGIN
        SET @Sql = N'EXEC sp_refreshview ''['+@Schema+'].T_KIRKONTENERELEM_OSSZES'''
        EXEC sp_executesql @Sql
    END
END
ELSE BEGIN
    /* Ha nem létezik a view, akkor létrehozzuk */
    IF (SELECT COUNT(1)
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = N'T_KIRKONTENERELEM' AND TABLE_SCHEMA = 'dbo' AND COLUMN_NAME = 'C_TANEVID' 
            ) > 0
    BEGIN
        SET @AktivTanev = N' C_TANEVID = '+ CAST(@TanevId as nvarchar) +' AND '
    END
    ELSE 
    BEGIN
        SET @AktivTanev = N''
    END

    IF (SELECT COUNT(1)
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = N'T_KIRKONTENERELEM' AND TABLE_SCHEMA = 'dbo' AND COLUMN_NAME = 'C_INTEZMENYID' 
            ) > 0
    BEGIN
        /* T_KIRKONTENERELEM */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_KIRKONTENERELEM
        AS
        SELECT *
        FROM dbo.T_KIRKONTENERELEM
        WHERE '+@AktivTanev+' (TOROLT = ''F'') AND C_INTEZMENYID = '+CAST(@IntezmenyId as nvarchar)
        EXECUTE sp_executesql @Sql

        /* T_KIRKONTENERELEM_OSSZES */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_KIRKONTENERELEM_OSSZES
        AS
        SELECT *
        FROM dbo.T_KIRKONTENERELEM
        WHERE C_INTEZMENYID = '+CAST(@IntezmenyId as nvarchar)
        EXECUTE sp_executesql @Sql
    END
    ELSE 
    BEGIN
        /* T_KIRKONTENERELEM */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_KIRKONTENERELEM
        AS
        SELECT *
        FROM dbo.T_KIRKONTENERELEM
        WHERE '+@AktivTanev+' (TOROLT = ''F'')'
        EXECUTE sp_executesql @Sql

        /* T_KIRKONTENERELEM_OSSZES */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_KIRKONTENERELEM_OSSZES
        AS
        SELECT *
        FROM dbo.T_KIRKONTENERELEM'
        EXECUTE sp_executesql @Sql
    END
END

END

GO


--------------------


DECLARE intezmenytanevekCursor CURSOR LOCAL FOR 
	SELECT T_INTEZMENY.ID, T_INTEZMENY.C_AZONOSITO, T_TANEV.ID
		FROM T_INTEZMENY
		INNER JOIN T_TANEV ON T_TANEV.C_INTEZMENYID = T_INTEZMENY.ID
		WHERE T_TANEV.C_AKTIV = 'T' AND T_TANEV.TOROLT = 'F' AND T_INTEZMENY.TOROLT = 'F'
		ORDER BY T_INTEZMENY.ID

DECLARE @intezmenyId int, @intezmenyAzonosito nvarchar(30), @tanevId int

OPEN intezmenytanevekCursor  
FETCH NEXT FROM intezmenytanevekCursor INTO @intezmenyId, @intezmenyAzonosito, @tanevId

WHILE @@FETCH_STATUS = 0 
BEGIN  
	PRINT 'INTÉZMÉNY ID: ' + CAST(@intezmenyId AS NVARCHAR(10)) + ', INTÉZMÉNY AZONOSÍTÓ: ' + CAST(@intezmenyAzonosito AS NVARCHAR(30)) + ', TANÉV ID: ' + CAST(@tanevId AS NVARCHAR(10))
    EXEC sp_AddNewSchemaViews @IntezmenyId = @intezmenyId, @IntezmenyAzonosito = @intezmenyAzonosito, @TanevId = @tanevId

	FETCH NEXT FROM intezmenytanevekCursor INTO @intezmenyId, @intezmenyAzonosito, @tanevId
END  

CLOSE intezmenytanevekCursor  
DEALLOCATE intezmenytanevekCursor 

GO


--------------------


EXEC dev.sp_Global_GenerateAsyncAuditTriggerAll
GO

EXEC dev.sp_Global_GenerateAsyncAuditProcedureAll
GO

EXEC dev.sp_Global_GenerateAsyncAuditAuditPocessing
GO

ALTER QUEUE auditLog.[AuditQueue]
WITH ACTIVATION
( 
    STATUS = ON,
    PROCEDURE_NAME = auditLog.usp_AuditProcessing,
    MAX_QUEUE_READERS = 10,
    EXECUTE AS 'Kreta_tech_user'
);

GO
