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

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

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

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

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


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

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

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

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



/* VIEW: T_FELTOLTOTTFAJL_HAZIFELADATOK */

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

    IF OBJECT_ID('['+@Schema+'].T_FELTOLTOTTFAJL_HAZIFELADATOK_OSSZES', 'V') IS NOT NULL BEGIN
        SET @Sql = N'EXEC sp_refreshview ''['+@Schema+'].T_FELTOLTOTTFAJL_HAZIFELADATOK_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 = 'T_FELTOLTOTTFAJL_HAZIFELADATOK' AND TABLE_SCHEMA = 'dbo' AND COLUMN_NAME IN ( 'C_INTEZMENYID', 'C_TANEVID' )
            ) > 0
    BEGIN
        SET @AktivTanev = N' C_TANEVID = '+ CAST(@TanevId as nvarchar) +' AND '

        /* T_FELTOLTOTTFAJL_HAZIFELADATOK */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_FELTOLTOTTFAJL_HAZIFELADATOK
        AS
        SELECT *
        FROM dbo.T_FELTOLTOTTFAJL_HAZIFELADATOK
        WHERE '+@AktivTanev+' C_INTEZMENYID = '+CAST(@IntezmenyId as nvarchar)
        EXECUTE sp_executesql @Sql

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

/* VIEW: T_FELTOLTOTTFAJL_ORARENDIORA */

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

    IF OBJECT_ID('['+@Schema+'].T_FELTOLTOTTFAJL_ORARENDIORA_OSSZES', 'V') IS NOT NULL BEGIN
        SET @Sql = N'EXEC sp_refreshview ''['+@Schema+'].T_FELTOLTOTTFAJL_ORARENDIORA_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 = 'T_FELTOLTOTTFAJL_ORARENDIORA' AND TABLE_SCHEMA = 'dbo' AND COLUMN_NAME IN ( 'C_INTEZMENYID', 'C_TANEVID' )
            ) > 0
    BEGIN
        SET @AktivTanev = N' C_TANEVID = '+ CAST(@TanevId as nvarchar) +' AND '

        /* T_FELTOLTOTTFAJL_ORARENDIORA */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_FELTOLTOTTFAJL_ORARENDIORA
        AS
        SELECT *
        FROM dbo.T_FELTOLTOTTFAJL_ORARENDIORA
        WHERE '+@AktivTanev+' C_INTEZMENYID = '+CAST(@IntezmenyId as nvarchar)
        EXECUTE sp_executesql @Sql

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

/* VIEW: T_TAVOLLETJELENTO_ALKALMAZOTT */

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

    IF OBJECT_ID('['+@Schema+'].T_TAVOLLETJELENTO_ALKALMAZOTT_OSSZES', 'V') IS NOT NULL BEGIN
        SET @Sql = N'EXEC sp_refreshview ''['+@Schema+'].T_TAVOLLETJELENTO_ALKALMAZOTT_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 = 'T_TAVOLLETJELENTO_ALKALMAZOTT' AND TABLE_SCHEMA = 'dbo' AND COLUMN_NAME IN ( 'C_INTEZMENYID', 'C_TANEVID' )
            ) > 0
    BEGIN
        SET @AktivTanev = N' C_TANEVID = '+ CAST(@TanevId as nvarchar) +' AND '

        /* T_TAVOLLETJELENTO_ALKALMAZOTT */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_TAVOLLETJELENTO_ALKALMAZOTT
        AS
        SELECT *
        FROM dbo.T_TAVOLLETJELENTO_ALKALMAZOTT
        WHERE '+@AktivTanev+' C_INTEZMENYID = '+CAST(@IntezmenyId as nvarchar)
        EXECUTE sp_executesql @Sql

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

/* VIEW: T_TAVOLLETJELENTO_IKTATOTTDOKU */

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

    IF OBJECT_ID('['+@Schema+'].T_TAVOLLETJELENTO_IKTATOTTDOKU_OSSZES', 'V') IS NOT NULL BEGIN
        SET @Sql = N'EXEC sp_refreshview ''['+@Schema+'].T_TAVOLLETJELENTO_IKTATOTTDOKU_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 = 'T_TAVOLLETJELENTO_IKTATOTTDOKU' AND TABLE_SCHEMA = 'dbo' AND COLUMN_NAME IN ( 'C_INTEZMENYID', 'C_TANEVID' )
            ) > 0
    BEGIN
        SET @AktivTanev = N' C_TANEVID = '+ CAST(@TanevId as nvarchar) +' AND '

        /* T_TAVOLLETJELENTO_IKTATOTTDOKU */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_TAVOLLETJELENTO_IKTATOTTDOKU
        AS
        SELECT *
        FROM dbo.T_TAVOLLETJELENTO_IKTATOTTDOKU
        WHERE '+@AktivTanev+' C_INTEZMENYID = '+CAST(@IntezmenyId as nvarchar)
        EXECUTE sp_executesql @Sql

        /* T_TAVOLLETJELENTO_IKTATOTTDOKU_OSSZES */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_TAVOLLETJELENTO_IKTATOTTDOKU_OSSZES
        AS
        SELECT *
        FROM dbo.T_TAVOLLETJELENTO_IKTATOTTDOKU
        WHERE C_INTEZMENYID = '+CAST(@IntezmenyId as nvarchar)
        EXECUTE sp_executesql @Sql
    END
    ELSE 
    BEGIN
        /* T_TAVOLLETJELENTO_IKTATOTTDOKU */
        SET @Sql = N'CREATE VIEW ['+@Schema+'].T_TAVOLLETJELENTO_IKTATOTTDOKU
        AS
        SELECT *
        FROM dbo.T_TAVOLLETJELENTO_IKTATOTTDOKU'
        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

