﻿GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;

SET NUMERIC_ROUNDABORT OFF;


GO

PRINT N'';
PRINT N'';
PRINT N'DELETE TanuloErtekeles, ahol nincs se OsztalyfonokiErtekeles se OsztalyzatErtekeles.';

GO
DELETE TanuloErtekeles
FROM 
	T_TANULOERTEKELES TanuloErtekeles
LEFT JOIN 
	T_OSZTALYFONOKIERTEKELES OsztalyfonokiErtekeles ON OsztalyfonokiErtekeles.ID = TanuloErtekeles.ID
LEFT JOIN 
	T_OSZTALYZATERTEKELES OsztalyzatErtekeles ON OsztalyzatErtekeles.ID = TanuloErtekeles.ID
WHERE
	OsztalyfonokiErtekeles.ID IS NULL AND
	OsztalyzatErtekeles.ID IS NULL

GO
PRINT N'DELETE complete.';

GO
PRINT N'';
PRINT N'';
PRINT N'DELETE TanuloErtekeles, ahol egyszerre van OsztalyfonokiErtekeles és OsztalyzatErtekeles.';


GO
DECLARE @tanuloErtekelesIdList TABLE (ID INT)

INSERT INTO @tanuloErtekelesIdList
SELECT 
	TanuloErtekeles.ID
FROM 
	T_TANULOERTEKELES TanuloErtekeles
LEFT JOIN 
	T_OSZTALYFONOKIERTEKELES OsztalyfonokiErtekeles ON OsztalyfonokiErtekeles.ID = TanuloErtekeles.ID
LEFT JOIN 
	T_OSZTALYZATERTEKELES OsztalyzatErtekeles ON OsztalyzatErtekeles.ID = TanuloErtekeles.ID
WHERE
	OsztalyfonokiErtekeles.ID = TanuloErtekeles.ID AND
	OsztalyzatErtekeles.ID = TanuloErtekeles.ID
	

DELETE OsztalyfonokiErtekeles
FROM 
	T_OSZTALYFONOKIERTEKELES OsztalyfonokiErtekeles
WHERE
	OsztalyfonokiErtekeles.ID IN (SELECT ID FROM @tanuloErtekelesIdList)

DELETE OsztalyzatErtekeles
FROM 
	T_OSZTALYZATERTEKELES OsztalyzatErtekeles
WHERE
	OsztalyzatErtekeles.ID IN (SELECT ID FROM @tanuloErtekelesIdList)

DELETE TanuloErtekeles
FROM 
	T_TANULOERTEKELES TanuloErtekeles
WHERE
	TanuloErtekeles.ID IN (SELECT ID FROM @tanuloErtekelesIdList)

GO
PRINT N'DELETE complete.';
PRINT N'';
PRINT N'';


GO
PRINT N'Dropping [dbo].[FK_4025662_4025660]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_4025662_4025660];


GO
PRINT N'Dropping [dbo].[FK_4025665_4025663]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_4025665_4025663];


GO
PRINT N'Dropping [dbo].[FK_4025668_4025666]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_4025668_4025666];


GO
PRINT N'Dropping [dbo].[FK_4025670_4025669]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_4025670_4025669];


GO
PRINT N'Dropping [dbo].[FK_4025938_4025937]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_4025938_4025937];


GO
PRINT N'Dropping [dbo].[FK_402564102_402564100]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_402564102_402564100];


GO
PRINT N'Dropping [dbo].[FK_402564105_402564103]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_402564105_402564103];


GO
PRINT N'Dropping [dbo].[FK_4025641_4025644]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_4025641_4025644];


GO
PRINT N'Dropping [dbo].[FK_4025641_4026317]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_4025641_4026317];


GO
PRINT N'Dropping [dbo].[FK_4028054_4028052]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELESLATTAMOZAS] DROP CONSTRAINT [FK_4028054_4028052];


GO
PRINT N'Dropping [dbo].[FK_T_4025641_4025742]...';


GO
ALTER TABLE [dbo].[T_OSZTALYZATERTEKELES] DROP CONSTRAINT [FK_T_4025641_4025742];


GO
PRINT N'Dropping [dbo].[FK_T_4025641_4025743]...';


GO
ALTER TABLE [dbo].[T_OSZTALYFONOKIERTEKELES] DROP CONSTRAINT [FK_T_4025641_4025743];


GO
PRINT N'Starting rebuilding table [dbo].[T_TANULOERTEKELES]...';


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_TANULOERTEKELES] (
    [ID]                         INT            IDENTITY (1, 1) NOT NULL,
    [C_DATUM]                    DATETIME       NOT NULL,
    [C_TIPUSID]                  INT            NOT NULL,
    [C_ERTEKELESSZOVEG]          NVARCHAR (MAX) NULL,
    [C_ROGZITESDATUM]            DATETIME       NOT NULL,
    [C_ERTEKELESOSZTALYZATID]    INT            NULL,
    [C_ERTEKELESSZOVEGROVIDNEV]  NVARCHAR (3)   NULL,
    [C_ERTEKELESMODID]           INT            NULL,
    [C_ERTEKELESTEMA]            NVARCHAR (255) NULL,
    [C_ERTEKELESSZAZALEK]        INT            NULL,
    [C_ISMAGATARTASSZORGALOM]    CHAR (1)       NOT NULL,
    [C_MAGATARTASERTEKID]        INT            NULL,
    [C_MAGATARTASOSZTALYZATID]   INT            NULL,
    [C_MAGATARTASSZOVEG]         NVARCHAR (MAX) NULL,
    [C_MAGATARTASSZOVEGROVIDNEV] NVARCHAR (3)   NULL,
    [C_SZORGALOMERTEKID]         INT            NULL,
    [C_SZORGALOMOSZTALYZATID]    INT            NULL,
    [C_SZORGALOMSZOVEG]          NVARCHAR (MAX) NULL,
    [C_SZORGALOMSZOVEGROVIDNEV]  NVARCHAR (3)   NULL,
    [C_TANTARGYID]               INT            NULL,
    [C_TANULOID]                 INT            NOT NULL,
    [C_OSZTALYCSOPORTID]         INT            NULL,
    [C_ERTEKELOID]               INT            NOT NULL,
    [C_TANORAID]                 INT            NULL,
    [C_INTEZMENYID]              INT            NOT NULL,
    [C_TANEVID]                  INT            NOT NULL,
    [TOROLT]                     CHAR (1)       DEFAULT ('F') NOT NULL,
    [SERIAL]                     INT            DEFAULT ((0)) NOT NULL,
    [LASTCHANGED]                DATETIME       DEFAULT (getdate()) NOT NULL,
    [CREATED]                    DATETIME       DEFAULT (getdate()) NOT NULL,
    [MODIFIER]                   INT            NULL,
    [CREATOR]                    INT            NULL,
    [ELOZOTANEVIREKORDID]        INT            NULL,
    [NNID]                       INT            NULL,
    CONSTRAINT [tmp_ms_xx_constraint_PK_T_40256411] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_TANULOERTEKELES])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_TANULOERTEKELES] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_TANULOERTEKELES] ([ID], [C_DATUM], [C_TIPUSID], [C_ERTEKELESOSZTALYZATID], [C_ERTEKELESSZOVEGROVIDNEV], [C_ISMAGATARTASSZORGALOM],[C_ERTEKELESSZOVEG], [C_ROGZITESDATUM], [C_TANTARGYID], [C_TANULOID], [C_OSZTALYCSOPORTID], [C_ERTEKELOID], [C_TANORAID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [ELOZOTANEVIREKORDID], [NNID])
        SELECT   [ID],
				 [C_ERTEKELESDATUM],
				 [C_ERTEKELESTIPUSA],
				 [C_OSZTALYZAT],
				 [C_SZOVEGROVIDNEV],
				 'F',
                 [C_ERTEKELESSZOVEG],
                 [C_ROGZITESDATUM],
                 [C_TANTARGYID],
                 [C_TANULOID],
                 [C_OSZTALYCSOPORTID],
                 [C_ERTEKELOID],
                 [C_TANORAID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [ELOZOTANEVIREKORDID],
                 [NNID]
        FROM     [dbo].[T_TANULOERTEKELES]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_TANULOERTEKELES] OFF;
    END

DROP TABLE [dbo].[T_TANULOERTEKELES];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_TANULOERTEKELES]', N'T_TANULOERTEKELES';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40256411]', N'PK_T_4025641', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025641_4025644]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025641_4025644]
    ON [dbo].[T_TANULOERTEKELES]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_TIPUSID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025641_4026317]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025641_4026317]
    ON [dbo].[T_TANULOERTEKELES]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_ERTEKELESOSZTALYZATID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025641_4028155]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025641_4028155]
    ON [dbo].[T_TANULOERTEKELES]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_ERTEKELESMODID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025641_4028159]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025641_4028159]
    ON [dbo].[T_TANULOERTEKELES]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_MAGATARTASERTEKID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025641_4028160]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025641_4028160]
    ON [dbo].[T_TANULOERTEKELES]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_MAGATARTASOSZTALYZATID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025641_4028163]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025641_4028163]
    ON [dbo].[T_TANULOERTEKELES]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_SZORGALOMERTEKID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025641_4028164]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025641_4028164]
    ON [dbo].[T_TANULOERTEKELES]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_SZORGALOMOSZTALYZATID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025662_4025660]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025662_4025660]
    ON [dbo].[T_TANULOERTEKELES]([C_TANTARGYID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025665_4025663]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025665_4025663]
    ON [dbo].[T_TANULOERTEKELES]([C_TANULOID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025668_4025666]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025668_4025666]
    ON [dbo].[T_TANULOERTEKELES]([C_OSZTALYCSOPORTID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025670_4025669]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025670_4025669]
    ON [dbo].[T_TANULOERTEKELES]([C_ERTEKELOID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025938_4025937]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025938_4025937]
    ON [dbo].[T_TANULOERTEKELES]([C_TANORAID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_402564102_402564100]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402564102_402564100]
    ON [dbo].[T_TANULOERTEKELES]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_402564105_402564103]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402564105_402564103]
    ON [dbo].[T_TANULOERTEKELES]([C_TANEVID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[IDX_TANULOERTEKELES_TANULOID_TANTARGYID_ERTEKELESTIPUSA]...';


GO
CREATE NONCLUSTERED INDEX [IDX_TANULOERTEKELES_TANULOID_TANTARGYID_ERTEKELESTIPUSA]
    ON [dbo].[T_TANULOERTEKELES]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_TANULOID] ASC, [C_TANTARGYID] ASC, [C_TIPUSID] ASC);


GO
PRINT N'Creating [dbo].[FK_4025662_4025660]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025662_4025660] FOREIGN KEY ([C_TANTARGYID]) REFERENCES [dbo].[T_TANTARGY] ([ID]);


GO
PRINT N'Creating [dbo].[FK_4025665_4025663]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025665_4025663] FOREIGN KEY ([C_TANULOID]) REFERENCES [dbo].[T_TANULO] ([ID]);


GO
PRINT N'Creating [dbo].[FK_4025668_4025666]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025668_4025666] FOREIGN KEY ([C_OSZTALYCSOPORTID]) REFERENCES [dbo].[T_OSZTALYCSOPORT] ([ID]);


GO
PRINT N'Creating [dbo].[FK_4025670_4025669]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025670_4025669] FOREIGN KEY ([C_ERTEKELOID]) REFERENCES [dbo].[T_ALKALMAZOTT] ([ID]);


GO
PRINT N'Creating [dbo].[FK_4025938_4025937]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025938_4025937] FOREIGN KEY ([C_TANORAID]) REFERENCES [dbo].[T_TANITASIORA] ([ID]);


GO
PRINT N'Creating [dbo].[FK_402564102_402564100]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_402564102_402564100] FOREIGN KEY ([C_INTEZMENYID]) REFERENCES [dbo].[T_INTEZMENY] ([ID]);


GO
PRINT N'Creating [dbo].[FK_402564105_402564103]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_402564105_402564103] FOREIGN KEY ([C_TANEVID]) REFERENCES [dbo].[T_TANEV] ([ID]);


GO
PRINT N'Creating [dbo].[FK_4025641_4025644]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025641_4025644] FOREIGN KEY ([C_TIPUSID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ERTEKELESTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


GO
PRINT N'Creating [dbo].[FK_4025641_4026317]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025641_4026317] FOREIGN KEY ([C_ERTEKELESOSZTALYZATID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_OSZTALYZATTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


GO
PRINT N'Creating [dbo].[FK_4028054_4028052]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELESLATTAMOZAS] WITH NOCHECK
    ADD CONSTRAINT [FK_4028054_4028052] FOREIGN KEY ([C_TANULOERTEKELESID]) REFERENCES [dbo].[T_TANULOERTEKELES] ([ID]);


GO
PRINT N'Creating [dbo].[FK_T_4025641_4025742]...';


GO
ALTER TABLE [dbo].[T_OSZTALYZATERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_T_4025641_4025742] FOREIGN KEY ([ID]) REFERENCES [dbo].[T_TANULOERTEKELES] ([ID]);


GO
PRINT N'Creating [dbo].[FK_T_4025641_4025743]...';


GO
ALTER TABLE [dbo].[T_OSZTALYFONOKIERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_T_4025641_4025743] FOREIGN KEY ([ID]) REFERENCES [dbo].[T_TANULOERTEKELES] ([ID]);


GO
PRINT N'Creating [dbo].[FK_4025641_4028155]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025641_4028155] FOREIGN KEY ([C_ERTEKELESMODID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ERTEKELESMOD] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


GO
PRINT N'Creating [dbo].[FK_4025641_4028159]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025641_4028159] FOREIGN KEY ([C_MAGATARTASERTEKID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_MAGATARTASERTEKELESTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


GO
PRINT N'Creating [dbo].[FK_4025641_4028160]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025641_4028160] FOREIGN KEY ([C_MAGATARTASOSZTALYZATID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_OSZTALYZATTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


GO
PRINT N'Creating [dbo].[FK_4025641_4028163]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025641_4028163] FOREIGN KEY ([C_SZORGALOMERTEKID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_SZORGALOMERTEKELESTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


GO
PRINT N'Creating [dbo].[FK_4025641_4028164]...';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025641_4028164] FOREIGN KEY ([C_SZORGALOMOSZTALYZATID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_OSZTALYZATTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


GO
PRINT N'Refreshing [dbo].[T_TANULOERTEKELES_OSSZES]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[T_TANULOERTEKELES_OSSZES]';


GO
PRINT N'Checking existing data against newly created constraints';


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025662_4025660];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025665_4025663];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025668_4025666];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025670_4025669];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025938_4025937];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_402564102_402564100];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_402564105_402564103];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025641_4025644];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025641_4026317];

ALTER TABLE [dbo].[T_TANULOERTEKELESLATTAMOZAS] WITH CHECK CHECK CONSTRAINT [FK_4028054_4028052];

ALTER TABLE [dbo].[T_OSZTALYZATERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_T_4025641_4025742];

ALTER TABLE [dbo].[T_OSZTALYFONOKIERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_T_4025641_4025743];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025641_4028155];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025641_4028159];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025641_4028160];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025641_4028163];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025641_4028164];


GO
PRINT N'Update complete.';


GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;

SET NUMERIC_ROUNDABORT OFF;


GO
PRINT N'Dropping [dbo].[FK_4025957_4025959]...';


GO
ALTER TABLE [dbo].[T_TANARIATLAGSULY] DROP CONSTRAINT [FK_4025957_4025959];


GO
PRINT N'Dropping [dbo].[FK_402595702_402595700]...';


GO
ALTER TABLE [dbo].[T_TANARIATLAGSULY] DROP CONSTRAINT [FK_402595702_402595700];


GO
PRINT N'Dropping [dbo].[FK_402595705_402595703]...';


GO
ALTER TABLE [dbo].[T_TANARIATLAGSULY] DROP CONSTRAINT [FK_402595705_402595703];


GO
PRINT N'Starting rebuilding table [dbo].[T_TANARIATLAGSULY]...';


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_TANARIATLAGSULY] (
    [ID]                  INT      IDENTITY (1, 1) NOT NULL,
    [C_SULY]              INT      NOT NULL,
    [C_ERTEKELESMODID]    INT      NOT NULL,
    [C_INTEZMENYID]       INT      NOT NULL,
    [C_TANEVID]           INT      NOT NULL,
    [TOROLT]              CHAR (1) DEFAULT ('F') NOT NULL,
    [SERIAL]              INT      DEFAULT ((0)) NOT NULL,
    [LASTCHANGED]         DATETIME DEFAULT (getdate()) NOT NULL,
    [CREATED]             DATETIME DEFAULT (getdate()) NOT NULL,
    [MODIFIER]            INT      NULL,
    [CREATOR]             INT      NULL,
    [ELOZOTANEVIREKORDID] INT      NULL,
    [NNID]                INT      NULL,
    CONSTRAINT [tmp_ms_xx_constraint_PK_T_40259571] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_TANARIATLAGSULY])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_TANARIATLAGSULY] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_TANARIATLAGSULY] ([ID], [C_SULY], [C_ERTEKELESMODID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [ELOZOTANEVIREKORDID], [NNID])
        SELECT   [ID],
                 [C_SULY],
                 [C_ERTEKELESMODJA],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [ELOZOTANEVIREKORDID],
                 [NNID]
        FROM     [dbo].[T_TANARIATLAGSULY]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_TANARIATLAGSULY] OFF;
    END

DROP TABLE [dbo].[T_TANARIATLAGSULY];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_TANARIATLAGSULY]', N'T_TANARIATLAGSULY';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40259571]', N'PK_T_4025957', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_TANARIATLAGSULY].[I_T_4025957_4025959]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025957_4025959]
    ON [dbo].[T_TANARIATLAGSULY]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_ERTEKELESMODID] ASC);


GO
PRINT N'Creating [dbo].[T_TANARIATLAGSULY].[I_T_402595702_402595700]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402595702_402595700]
    ON [dbo].[T_TANARIATLAGSULY]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_TANARIATLAGSULY].[I_T_402595705_402595703]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402595705_402595703]
    ON [dbo].[T_TANARIATLAGSULY]([C_TANEVID] ASC);


GO
PRINT N'Creating [dbo].[FK_4025957_4025959]...';


GO
ALTER TABLE [dbo].[T_TANARIATLAGSULY] WITH NOCHECK
    ADD CONSTRAINT [FK_4025957_4025959] FOREIGN KEY ([C_ERTEKELESMODID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ERTEKELESMOD] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


GO
PRINT N'Creating [dbo].[FK_402595702_402595700]...';


GO
ALTER TABLE [dbo].[T_TANARIATLAGSULY] WITH NOCHECK
    ADD CONSTRAINT [FK_402595702_402595700] FOREIGN KEY ([C_INTEZMENYID]) REFERENCES [dbo].[T_INTEZMENY] ([ID]);


GO
PRINT N'Creating [dbo].[FK_402595705_402595703]...';


GO
ALTER TABLE [dbo].[T_TANARIATLAGSULY] WITH NOCHECK
    ADD CONSTRAINT [FK_402595705_402595703] FOREIGN KEY ([C_TANEVID]) REFERENCES [dbo].[T_TANEV] ([ID]);


GO
PRINT N'Refreshing [dbo].[T_TANARIATLAGSULY_OSSZES]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[T_TANARIATLAGSULY_OSSZES]';


GO
PRINT N'Checking existing data against newly created constraints';


GO
ALTER TABLE [dbo].[T_TANARIATLAGSULY] WITH CHECK CHECK CONSTRAINT [FK_4025957_4025959];

ALTER TABLE [dbo].[T_TANARIATLAGSULY] WITH CHECK CHECK CONSTRAINT [FK_402595702_402595700];

ALTER TABLE [dbo].[T_TANARIATLAGSULY] WITH CHECK CHECK CONSTRAINT [FK_402595705_402595703];


GO
PRINT N'Update complete.';


GO


--------------------


PRINT '--- intézményi view-k (dbo táblákkal megegyezõ nevû és a tábla_OSSZES nevûek) frissítése ---'

DECLARE  @viewname nvarchar(100), @pureview nvarchar(100)

DECLARE semaviewk CURSOR LOCAL FOR 
  SELECT '['+TABLE_SCHEMA + '].[' + TABLE_NAME + ']', TABLE_NAME
  FROM INFORMATION_SCHEMA.TABLES
  WHERE TABLE_TYPE = 'VIEW' and TABLE_SCHEMA LIKE 'KR[_]%[_]Schema'
  AND TABLE_NAME in ('T_TANULOERTEKELES', 'T_TANULOERTEKELES_OSSZES', 'T_TANARIATLAGSULY', 'T_TANARIATLAGSULY_OSSZES') -- ha csak néhány VIEW frissült, akkor a gyorsabb futásért ide fel lehet sorolni
  ORDER BY TABLE_SCHEMA, TABLE_NAME

OPEN semaviewk
FETCH NEXT FROM semaviewk INTO @viewname, @pureview

WHILE @@FETCH_STATUS = 0 BEGIN
    PRINT @viewname
    EXEC sp_refreshview @viewname

    FETCH NEXT FROM semaviewk INTO @viewname, @pureview
END             

CLOSE semaviewk
DEALLOCATE semaviewk


GO
