﻿PRINT N'Creating [dbo].[T_ERETTSEGIEREDMENY_OSSZES]...';


GO
CREATE VIEW dbo.T_ERETTSEGIEREDMENY_OSSZES
	AS
		SELECT *
		FROM dbo.T_ERETTSEGIEREDMENY
GO
PRINT N'Creating [dbo].[T_ERETTSEGISZINT_OSSZES]...';


GO
CREATE VIEW dbo.T_ERETTSEGISZINT_OSSZES
	AS
		SELECT *
		FROM dbo.T_ERETTSEGISZINT
GO
PRINT N'Creating [dbo].[T_ERETTSEGITANTARGY_OSSZES]...';


GO
CREATE VIEW dbo.T_ERETTSEGITANTARGY_OSSZES
	AS
		SELECT *
		FROM dbo.T_ERETTSEGITANTARGY
GO
PRINT N'Creating [dbo].[T_ERETTSEGITANTARGYADATOK_OSSZES]...';


GO
CREATE VIEW dbo.T_ERETTSEGITANTARGYADATOK_OSSZES
	AS
		SELECT *
		FROM dbo.T_ERETTSEGITANTARGYADATOK
GO
PRINT N'Creating [dbo].[T_ERETTSEGITIPUS_OSSZES]...';


GO
CREATE VIEW dbo.T_ERETTSEGITIPUS_OSSZES
	AS
		SELECT *
		FROM dbo.T_ERETTSEGITIPUS
GO
PRINT N'Checking existing data against newly created constraints';


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)

-- CREATE SCHEMA VIEW - START

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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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

-- CREATE SCHEMA VIEW - 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
