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


GO
CREATE VIEW [dbo].T_OGEXPORTORAREND_OSSZES
        AS
        SELECT *
        FROM dbo.T_OGEXPORTORAREND
GO
PRINT N'Creating [dbo].[T_OGFOGLALKOZAS_OSSZES]...';


GO
CREATE VIEW [dbo].T_OGFOGLALKOZAS_OSSZES
        AS
        SELECT *
        FROM dbo.T_OGFOGLALKOZAS
GO
PRINT N'Creating [dbo].[T_OGFOGLALKOZAS_TEREM_OSSZES]...';


GO
CREATE VIEW [dbo].T_OGFOGLALKOZAS_TEREM_OSSZES
        AS
        SELECT *
        FROM dbo.T_OGFOGLALKOZAS_TEREM
GO
PRINT N'Creating [dbo].[T_OGFOGLALKOZASFOGLALTSAG_OSSZES]...';


GO
CREATE VIEW [dbo].T_OGFOGLALKOZASFOGLALTSAG_OSSZES
        AS
        SELECT *
        FROM dbo.T_OGFOGLALKOZASFOGLALTSAG
GO
PRINT N'Creating [dbo].[T_OGFOGLALTSAGTIPUS_OSSZES]...';


GO
CREATE VIEW [dbo].T_OGFOGLALTSAGTIPUS_OSSZES
        AS
        SELECT *
        FROM dbo.T_OGFOGLALTSAGTIPUS
GO
PRINT N'Creating [dbo].[T_OGGENERATORALAP_OSSZES]...';


GO
CREATE VIEW [dbo].T_OGGENERATORALAP_OSSZES
        AS
        SELECT *
        FROM dbo.T_OGGENERATORALAP
GO
PRINT N'Creating [dbo].[T_OGINTEZMENYFOGLALTSAG_OSSZES]...';


GO
CREATE VIEW [dbo].T_OGINTEZMENYFOGLALTSAG_OSSZES
        AS
        SELECT *
        FROM dbo.T_OGINTEZMENYFOGLALTSAG
GO
PRINT N'Creating [dbo].[T_OGORARENDIORA_OSSZES]...';


GO
CREATE VIEW [dbo].T_OGORARENDIORA_OSSZES
        AS
        SELECT *
        FROM dbo.T_OGORARENDIORA
GO
PRINT N'Creating [dbo].[T_OGOSZTALYCSOPORT_OSSZES]...';


GO
CREATE VIEW [dbo].T_OGOSZTALYCSOPORT_OSSZES
        AS
        SELECT *
        FROM dbo.T_OGOSZTALYCSOPORT
GO
PRINT N'Creating [dbo].[T_OGOSZTALYCSOPORTFOGLALTSAG_OSSZES]...';


GO
CREATE VIEW [dbo].T_OGOSZTALYCSOPORTFOGLALTSAG_OSSZES
        AS
        SELECT *
        FROM dbo.T_OGOSZTALYCSOPORTFOGLALTSAG
GO
PRINT N'Creating [dbo].[T_OGTANARFOGLALTSAG_OSSZES]...';


GO
CREATE VIEW [dbo].T_OGTANARFOGLALTSAG_OSSZES
        AS
        SELECT *
        FROM dbo.T_OGTANARFOGLALTSAG
GO
PRINT N'Creating [dbo].[T_OGTANTARGYFOGLALTSAG_OSSZES]...';


GO
CREATE VIEW [dbo].T_OGTANTARGYFOGLALTSAG_OSSZES
        AS
        SELECT *
        FROM dbo.T_OGTANTARGYFOGLALTSAG
GO
PRINT N'Creating [dbo].[T_OGTEREM_TANTARGY_OSSZES]...';


GO
CREATE VIEW [dbo].T_OGTEREM_TANTARGY_OSSZES
        AS
        SELECT *
        FROM dbo.T_OGTEREM_TANTARGY
GO
PRINT N'Creating [dbo].[T_OGTEREMFOGLALTSAG_OSSZES]...';


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


PRINT '--- intézményi view-k (dbo táblákkal megegyező nevű és a tábla_OSSZES nevűek) frissítése ---'

DECLARE  @viewname nvarchar(100), @pureview nvarchar(100)

DECLARE semaviewk CURSOR LOCAL FOR 
  SELECT '['+TABLE_SCHEMA + '].[' + TABLE_NAME + ']', TABLE_NAME
  FROM INFORMATION_SCHEMA.TABLES
  WHERE TABLE_TYPE = 'VIEW' and TABLE_SCHEMA LIKE 'KR[_]%[_]Schema'
  AND TABLE_NAME in (
  'T_ALKALMAZOTT', 'T_ALKALMAZOTT_OSSZES'
  ) -- ha csak néhány VIEW frissült, akkor a gyorsabb futásért ide fel lehet sorolni
  ORDER BY TABLE_SCHEMA, TABLE_NAME

OPEN semaviewk
FETCH NEXT FROM semaviewk INTO @viewname, @pureview

WHILE @@FETCH_STATUS = 0 BEGIN
	PRINT @viewname
	EXEC sp_refreshview @viewname

    FETCH NEXT FROM semaviewk INTO @viewname, @pureview
END             

CLOSE semaviewk
DEALLOCATE semaviewk
GO


-- Triggerek inicializálás az összes táblára
DECLARE tableCursor CURSOR LOCAL FOR 
  SELECT DISTINCT TABLE_NAME
  FROM INFORMATION_SCHEMA.COLUMNS 
  WHERE COLUMN_NAME IN ('CREATOR', 'MODIFIER')
	AND TABLE_NAME NOT IN ('T_ENTITYHISTORY', 'T_ENTITYATTRIBUTEHISTORY', 'T_LOG', 'T_GLOBALLOCK', 'T_USERPROFILE', 'T_OLDALLATOGATOTTSAG')
	AND TABLE_NAME IN (
		SELECT t.name 
		FROM sys.tables t
		INNER JOIN sys.schemas s ON s.schema_id = t.schema_id
		WHERE s.name ='dbo' 
	)

DECLARE @table nvarchar(35)

OPEN tableCursor  
FETCH NEXT FROM tableCursor INTO @table  

WHILE @@FETCH_STATUS = 0 BEGIN  
  EXEC sp_Global_CreateTriggers @table
  FETCH NEXT FROM tableCursor INTO @table  
END  

CLOSE tableCursor  
DEALLOCATE tableCursor 


EXEC dev.sp_Global_GenerateAsyncAuditTriggerAll 1
GO

EXEC dev.sp_Global_GenerateAsyncAuditProcedureAll
GO

EXEC dev.sp_Global_GenerateAsyncAuditAuditPocessing
GO

ALTER QUEUE auditlog.AuditQueue WITH STATUS=OFF, ACTIVATION 
(STATUS = OFF)
GO

ALTER QUEUE auditlog.AuditQueue WITH STATUS=ON, ACTIVATION 
(STATUS = ON, MAX_QUEUE_READERS = 2)
GO
