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


GO
CREATE VIEW [dbo].T_KOZMUSZAMLA_OSSZES
        AS
        SELECT *
        FROM dbo.T_KOZMUSZAMLA
GO
PRINT N'Creating [dbo].[T_KOZMUSZAMLATIPUS_OSSZES]...';


GO
CREATE VIEW [dbo].T_KOZMUSZAMLATIPUS_OSSZES
        AS
        SELECT *
        FROM dbo.T_KOZMUSZAMLATIPUS
GO
PRINT N'Creating [dbo].[T_MEROALLAS_OSSZES]...';


GO
CREATE VIEW [dbo].T_MEROALLAS_OSSZES
        AS
        SELECT *
        FROM dbo.T_MEROALLAS
GO
PRINT N'Creating [dbo].[T_MEROHELY_OSSZES]...';


GO
CREATE VIEW [dbo].T_MEROHELY_OSSZES
        AS
        SELECT *
        FROM dbo.T_MEROHELY
GO
PRINT N'Creating [dbo].[T_MEROHELYTIPUS_OSSZES]...';


GO
CREATE VIEW [dbo].T_MEROHELYTIPUS_OSSZES
        AS
        SELECT *
        FROM dbo.T_MEROHELYTIPUS
GO
PRINT N'Creating [dbo].[T_PENZNEMTIPUS_OSSZES]...';


GO
CREATE VIEW [dbo].T_PENZNEMTIPUS_OSSZES
        AS
        SELECT *
        FROM dbo.T_PENZNEMTIPUS


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_PENZNEMTIPUS                                                          */
/*============================================================================*/
/* Package: Kreta */
IF OBJECT_ID('['+@Schema+'].T_PENZNEMTIPUS', 'V') IS NOT NULL BEGIN
    SET @Sql = N'EXEC sp_refreshview ''['+@Schema+'].T_PENZNEMTIPUS'''
    EXEC sp_executesql @Sql

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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