﻿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_4027574_4027572]...';


GO
ALTER TABLE [dbo].[T_TOVABBIMUNKAUGYIADATOK] DROP CONSTRAINT [FK_4027574_4027572];


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


GO
ALTER TABLE [dbo].[T_FELADATELLATASIHELY] DROP CONSTRAINT [FK_4026010_4026011];


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


GO
ALTER TABLE [dbo].[T_FELADATELLATASIHELY] DROP CONSTRAINT [FK_4026032_4026030];


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


GO
ALTER TABLE [dbo].[T_FELADATELLATASIHELY] DROP CONSTRAINT [FK_402601002_402601000];


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


GO
ALTER TABLE [dbo].[T_FELADATELLATASIHELY] DROP CONSTRAINT [FK_402601005_402601003];


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


GO
ALTER TABLE [dbo].[T_NEBULO] DROP CONSTRAINT [FK_4027460_4027459];


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


GO
ALTER TABLE [dbo].[T_JOGYAKORLAT] DROP CONSTRAINT [FK_4026037_4026036];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_4026125_4026124];


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


GO
ALTER TABLE [dbo].[T_OSZTALYCSOPORT] DROP CONSTRAINT [FK_4026025_4026024];


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_FELADATELLATASIHELY] (
    [ID]                             INT          IDENTITY (1, 1) NOT NULL,
    [C_OKTATASINEVELESIFELADATTIPUS] INT          NOT NULL,
    [C_ENGEDELYEZETTOSSZLETSZAM]     INT          NULL,
    [C_ENGEDELYEZETTOSZTALY]         INT          NULL,
    [C_FELADATELLATASIHELYKOD]       NVARCHAR (3) NULL,
    [C_MUKODESIHELYID]               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_40260101] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_FELADATELLATASIHELY])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_FELADATELLATASIHELY] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_FELADATELLATASIHELY] ([ID], [C_OKTATASINEVELESIFELADATTIPUS], [C_ENGEDELYEZETTOSSZLETSZAM], [C_ENGEDELYEZETTOSZTALY], [C_MUKODESIHELYID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [ELOZOTANEVIREKORDID], [NNID])
        SELECT   [ID],
                 [C_OKTATASINEVELESIFELADATTIPUS],
                 [C_ENGEDELYEZETTOSSZLETSZAM],
                 [C_ENGEDELYEZETTOSZTALY],
                 [C_MUKODESIHELYID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [ELOZOTANEVIREKORDID],
                 [NNID]
        FROM     [dbo].[T_FELADATELLATASIHELY]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_FELADATELLATASIHELY] OFF;
    END

DROP TABLE [dbo].[T_FELADATELLATASIHELY];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_FELADATELLATASIHELY]', N'T_FELADATELLATASIHELY';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40260101]', N'PK_T_4026010', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_FELADATELLATASIHELY].[I_T_4026010_4026011]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026010_4026011]
    ON [dbo].[T_FELADATELLATASIHELY]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_OKTATASINEVELESIFELADATTIPUS] ASC);


GO
PRINT N'Creating [dbo].[T_FELADATELLATASIHELY].[I_T_4026032_4026030]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026032_4026030]
    ON [dbo].[T_FELADATELLATASIHELY]([C_MUKODESIHELYID] ASC);


GO
PRINT N'Creating [dbo].[T_FELADATELLATASIHELY].[I_T_402601002_402601000]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402601002_402601000]
    ON [dbo].[T_FELADATELLATASIHELY]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_FELADATELLATASIHELY].[I_T_402601005_402601003]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402601005_402601003]
    ON [dbo].[T_FELADATELLATASIHELY]([C_TANEVID] ASC);


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


GO
ALTER TABLE [dbo].[T_TOVABBIMUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4027574_4027572] FOREIGN KEY ([C_FELADATELLATASIHELYID]) REFERENCES [dbo].[T_FELADATELLATASIHELY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_FELADATELLATASIHELY] WITH NOCHECK
    ADD CONSTRAINT [FK_4026010_4026011] FOREIGN KEY ([C_OKTATASINEVELESIFELADATTIPUS], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_OKTATASINEVELESIFELADAT] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_FELADATELLATASIHELY] WITH NOCHECK
    ADD CONSTRAINT [FK_4026032_4026030] FOREIGN KEY ([C_MUKODESIHELYID]) REFERENCES [dbo].[T_MUKODESIHELY] ([ID]);


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_NEBULO] WITH NOCHECK
    ADD CONSTRAINT [FK_4027460_4027459] FOREIGN KEY ([C_FELADATELLATASIHELYID]) REFERENCES [dbo].[T_FELADATELLATASIHELY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_JOGYAKORLAT] WITH NOCHECK
    ADD CONSTRAINT [FK_4026037_4026036] FOREIGN KEY ([C_FELADATELLATASIHELYID]) REFERENCES [dbo].[T_FELADATELLATASIHELY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026125_4026124] FOREIGN KEY ([C_FELADATELLATASIHELYID]) REFERENCES [dbo].[T_FELADATELLATASIHELY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_OSZTALYCSOPORT] WITH NOCHECK
    ADD CONSTRAINT [FK_4026025_4026024] FOREIGN KEY ([C_FELADATELLATASIHELYID]) REFERENCES [dbo].[T_FELADATELLATASIHELY] ([ID]);


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


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


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


GO
ALTER TABLE [dbo].[T_TOVABBIMUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4027574_4027572];

ALTER TABLE [dbo].[T_FELADATELLATASIHELY] WITH CHECK CHECK CONSTRAINT [FK_4026010_4026011];

ALTER TABLE [dbo].[T_FELADATELLATASIHELY] WITH CHECK CHECK CONSTRAINT [FK_4026032_4026030];

ALTER TABLE [dbo].[T_FELADATELLATASIHELY] WITH CHECK CHECK CONSTRAINT [FK_402601002_402601000];

ALTER TABLE [dbo].[T_FELADATELLATASIHELY] WITH CHECK CHECK CONSTRAINT [FK_402601005_402601003];

ALTER TABLE [dbo].[T_NEBULO] WITH CHECK CHECK CONSTRAINT [FK_4027460_4027459];

ALTER TABLE [dbo].[T_JOGYAKORLAT] WITH CHECK CHECK CONSTRAINT [FK_4026037_4026036];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026125_4026124];

ALTER TABLE [dbo].[T_OSZTALYCSOPORT] WITH CHECK CHECK CONSTRAINT [FK_4026025_4026024];


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_FELADATELLATASIHELY', 'T_FELADATELLATASIHELY_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


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


EXEC dev.sp_Global_GenerateAsyncAuditTriggerAll
GO

EXEC dev.sp_Global_GenerateAsyncAuditProcedureAll
GO

EXEC dev.sp_Global_GenerateAsyncAuditAuditPocessing
GO

ALTER QUEUE auditLog.[AuditQueue]
WITH ACTIVATION
( 
    STATUS = ON,
    PROCEDURE_NAME = auditLog.usp_AuditProcessing,
    MAX_QUEUE_READERS = 10,
    EXECUTE AS 'Kreta_tech_user'
);

GO
