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


GO
CREATE VIEW dbo.T_AGAZATTIPUS_OSSZES
	AS
		SELECT *
		FROM dbo.T_AGAZATTIPUS
GO
PRINT N'Creating [dbo].[T_RESZSZAKKEPESITESTIPUS_OSSZES]...';


GO
CREATE VIEW dbo.T_RESZSZAKKEPESITESTIPUS_OSSZES
	AS
		SELECT *
		FROM dbo.T_RESZSZAKKEPESITESTIPUS
GO
PRINT N'Creating [dbo].[T_SZAKKEPESITESTIPUS_OSSZES]...';


GO
CREATE VIEW dbo.T_SZAKKEPESITESTIPUS_OSSZES
	AS
		SELECT *
		FROM dbo.T_SZAKKEPESITESTIPUS
GO
PRINT N'Creating [dbo].[T_SZAKMACSOPORTTIPUS_OSSZES]...';


GO
CREATE VIEW dbo.T_SZAKMACSOPORTTIPUS_OSSZES
	AS
		SELECT *
		FROM dbo.T_SZAKMACSOPORTTIPUS
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)

-- IDE JÖNNEK AZ ÚJ TÁBLÁKHOZ TARTOZÓ VIEW-K

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

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

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

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


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

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

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

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


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

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

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

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


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

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

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

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

