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


GO
CREATE VIEW [dbo].T_KKAMIELEKTROAKUTIPUS_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKAMIELEKTROAKUTIPUS
GO
PRINT N'Creating [dbo].[T_KKAMIJAZZZENETIPUS_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKAMIJAZZZENETIPUS_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKAMIJAZZZENETIPUS
GO
PRINT N'Creating [dbo].[T_KKAMIKLASSZIKUSTIPUS_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKAMIKLASSZIKUSTIPUS_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKAMIKLASSZIKUSTIPUS
GO
PRINT N'Creating [dbo].[T_KKAMINEPZENETIPUS_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKAMINEPZENETIPUS_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKAMINEPZENETIPUS
GO
PRINT N'Creating [dbo].[T_KKAMITERULETTIPUS_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKAMITERULETTIPUS_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKAMITERULETTIPUS
GO
PRINT N'Creating [dbo].[T_KKAMIVEGZETTSEG_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKAMIVEGZETTSEG_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKAMIVEGZETTSEG
GO
PRINT N'Creating [dbo].[T_KKAMIVEGZETTSEGTIPUS_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKAMIVEGZETTSEGTIPUS_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKAMIVEGZETTSEGTIPUS
GO
PRINT N'Creating [dbo].[T_KKAMIZENEMUVKATTIPUS_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKAMIZENEMUVKATTIPUS_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKAMIZENEMUVKATTIPUS
GO
PRINT N'Creating [dbo].[T_KKELEKTROAKUZENE_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKELEKTROAKUZENE_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKELEKTROAKUZENE
GO
PRINT N'Creating [dbo].[T_KKGYOGYPEDTERTIPUS_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKGYOGYPEDTERTIPUS_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKGYOGYPEDTERTIPUS
GO
PRINT N'Creating [dbo].[T_KKGYOGYPEDVEGZETTSEG_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKGYOGYPEDVEGZETTSEG_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKGYOGYPEDVEGZETTSEG
GO
PRINT N'Creating [dbo].[T_KKJAZZZENE_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKJAZZZENE_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKJAZZZENE
GO
PRINT N'Creating [dbo].[T_KKKEPESITESJELLTIPUS_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKKEPESITESJELLTIPUS_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKKEPESITESJELLTIPUS
GO
PRINT N'Creating [dbo].[T_KKKEPESITESTIPUS_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKKEPESITESTIPUS_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKKEPESITESTIPUS
GO
PRINT N'Creating [dbo].[T_KKKLASSZIKUSZENE_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKKLASSZIKUSZENE_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKKLASSZIKUSZENE
GO
PRINT N'Creating [dbo].[T_KKNEPZENE_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKNEPZENE_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKNEPZENE
GO
PRINT N'Creating [dbo].[T_KKSZAKTIPUS_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKSZAKTIPUS_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKSZAKTIPUS
GO
PRINT N'Creating [dbo].[T_KKTANARVEGZETTSEG_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKTANARVEGZETTSEG_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKTANARVEGZETTSEG
GO
PRINT N'Creating [dbo].[T_KKTANITOMUVTERTIPUS_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKTANITOMUVTERTIPUS_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKTANITOMUVTERTIPUS
GO
PRINT N'Creating [dbo].[T_KKTANITOVEZGETTSEG_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKTANITOVEZGETTSEG_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKTANITOVEZGETTSEG
GO
PRINT N'Creating [dbo].[T_KKTANTARGYKATEGORIA_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKTANTARGYKATEGORIA_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKTANTARGYKATEGORIA
GO
PRINT N'Creating [dbo].[T_KKTERULET_OSSZES]...';


GO
CREATE VIEW [dbo].T_KKTERULET_OSSZES
        AS
        SELECT *
        FROM dbo.T_KKTERULET
GO
PRINT N'Creating [dbo].[T_KKVEGZETTSEGTIPUS_OSSZES]...';


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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