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


GO
CREATE VIEW [dbo].T_UZENET_OSSZES
        AS
        SELECT *
        FROM dbo.T_UZENET
GO
PRINT N'Creating [dbo].[T_UZENETCIMZETT_OSSZES]...';


GO
CREATE VIEW [dbo].T_UZENETCIMZETT_OSSZES
        AS
        SELECT *
        FROM dbo.T_UZENETCIMZETT
GO
PRINT N'Creating [dbo].[T_UZENETJOGKORTIPUS_OSSZES]...';


GO
CREATE VIEW [dbo].T_UZENETJOGKORTIPUS_OSSZES
        AS
        SELECT *
        FROM dbo.T_UZENETJOGKORTIPUS
GO
PRINT N'Creating [dbo].[T_UZENETJOGOK_OSSZES]...';


GO
CREATE VIEW [dbo].T_UZENETJOGOK_OSSZES
        AS
        SELECT *
        FROM dbo.T_UZENETJOGOK
		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_UZENETCIMZETT                                                         */
/*============================================================================*/
/* Package: Kreta */
IF OBJECT_ID('['+@Schema+'].T_UZENETCIMZETT', 'V') IS NOT NULL BEGIN
    SET @Sql = N'EXEC sp_refreshview ''['+@Schema+'].T_UZENETCIMZETT'''
    EXEC sp_executesql @Sql

    IF OBJECT_ID('['+@Schema+'].T_UZENETCIMZETT_OSSZES', 'V') IS NOT NULL BEGIN
        SET @Sql = N'EXEC sp_refreshview ''['+@Schema+'].T_UZENETCIMZETT_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_UZENETCIMZETT' 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_UZENETCIMZETT' AND TABLE_SCHEMA = 'dbo' AND COLUMN_NAME = 'C_INTEZMENYID' 
            ) > 0
    BEGIN
        /* T_UZENETCIMZETT */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_UZENETCIMZETT
        AS
        SELECT *
        FROM dbo.T_UZENETCIMZETT
        WHERE '+@AktivTanev+' (TOROLT = ''F'') AND C_INTEZMENYID = '+CAST(@IntezmenyId as nvarchar)
        EXECUTE sp_executesql @Sql

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

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


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

    IF OBJECT_ID('['+@Schema+'].T_UZENETJOGKORTIPUS_OSSZES', 'V') IS NOT NULL BEGIN
        SET @Sql = N'EXEC sp_refreshview ''['+@Schema+'].T_UZENETJOGKORTIPUS_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_UZENETJOGKORTIPUS' AND TABLE_SCHEMA = 'dbo' AND COLUMN_NAME = 'C_ALTANEVID' 
            ) > 0
    BEGIN
        SET @AktivTanev = N' C_ALTANEVID = '+ 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_UZENETJOGKORTIPUS' AND TABLE_SCHEMA = 'dbo' AND COLUMN_NAME = 'C_ALINTEZMENYID' 
            ) > 0
    BEGIN
        /* T_UZENETJOGKORTIPUS */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_UZENETJOGKORTIPUS
        AS
        SELECT *
        FROM dbo.T_UZENETJOGKORTIPUS
        WHERE '+@AktivTanev+' (TOROLT = ''F'') AND C_ALINTEZMENYID = '+CAST(@IntezmenyId as nvarchar)
        EXECUTE sp_executesql @Sql

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

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


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

    IF OBJECT_ID('['+@Schema+'].T_UZENET_OSSZES', 'V') IS NOT NULL BEGIN
        SET @Sql = N'EXEC sp_refreshview ''['+@Schema+'].T_UZENET_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_UZENET' 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_UZENET' AND TABLE_SCHEMA = 'dbo' AND COLUMN_NAME = 'C_INTEZMENYID' 
            ) > 0
    BEGIN
        /* T_UZENET */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_UZENET
        AS
        SELECT *
        FROM dbo.T_UZENET
        WHERE '+@AktivTanev+' (TOROLT = ''F'') AND C_INTEZMENYID = '+CAST(@IntezmenyId as nvarchar)
        EXECUTE sp_executesql @Sql

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

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


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

    IF OBJECT_ID('['+@Schema+'].T_UZENETJOGOK_OSSZES', 'V') IS NOT NULL BEGIN
        SET @Sql = N'EXEC sp_refreshview ''['+@Schema+'].T_UZENETJOGOK_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_UZENETJOGOK' 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_UZENETJOGOK' AND TABLE_SCHEMA = 'dbo' AND COLUMN_NAME = 'C_INTEZMENYID' 
            ) > 0
    BEGIN
        /* T_UZENETJOGOK */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_UZENETJOGOK
        AS
        SELECT *
        FROM dbo.T_UZENETJOGOK
        WHERE '+@AktivTanev+' (TOROLT = ''F'') AND C_INTEZMENYID = '+CAST(@IntezmenyId as nvarchar)
        EXECUTE sp_executesql @Sql

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

        /* T_UZENETJOGOK_OSSZES */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_UZENETJOGOK_OSSZES
        AS
        SELECT *
        FROM dbo.T_UZENETJOGOK'
        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_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


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


DROP PROCEDURE [dbo].[sp_AddNewSchemaViews]
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
