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


GO
CREATE VIEW [dbo].T_ORAREND_OSSZES
        AS
        SELECT *
        FROM dbo.T_ORAREND
GO
PRINT N'Creating [dbo].[T_ORARENDORATULAJDONSAG_OSSZES]...';


GO
CREATE VIEW [dbo].T_ORARENDORATULAJDONSAG_OSSZES
        AS
        SELECT *
        FROM dbo.T_ORARENDORATULAJDONSAG
GO
PRINT N'Creating [dbo].[T_ORARENDSABLON_OSSZES]...';


GO
CREATE VIEW [dbo].T_ORARENDSABLON_OSSZES
        AS
        SELECT *
        FROM dbo.T_ORARENDSABLON
GO
PRINT N'Creating [dbo].[T_ORATULAJDONSAGTIPUS_OSSZES]...';


GO
CREATE VIEW [dbo].T_ORATULAJDONSAGTIPUS_OSSZES
        AS
        SELECT *
        FROM dbo.T_ORATULAJDONSAGTIPUS
GO
PRINT N'Creating [dbo].[T_TANORA_OSSZES]...';


GO
CREATE VIEW [dbo].T_TANORA_OSSZES
        AS
        SELECT *
        FROM dbo.T_TANORA
GO
PRINT N'Creating [dbo].[T_TANORAORATULAJDONSAG_OSSZES]...';


GO
CREATE VIEW [dbo].T_TANORAORATULAJDONSAG_OSSZES
        AS
        SELECT *
        FROM dbo.T_TANORAORATULAJDONSAG

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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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