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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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