﻿--------------------

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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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

