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


GO
ALTER TABLE [dbo].[T_KIZAROCSOPORT] DROP CONSTRAINT [FK_4026005_4026007];


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


GO
ALTER TABLE [dbo].[T_EVFOLYAMTIPUS_OKTATASINEVELE] DROP CONSTRAINT [FK_4026013_4026012];


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


GO
ALTER TABLE [dbo].[T_ORATERV] DROP CONSTRAINT [FK_4025994_4025995];


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


GO
ALTER TABLE [dbo].[T_EVFOLYAMTIPUS] DROP CONSTRAINT [FK_T_21038_4025414];


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


GO
ALTER TABLE [dbo].[T_EVFOLYAMTIPUS] DROP CONSTRAINT [FK_402540502_402540500];


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


GO
ALTER TABLE [dbo].[T_EVFOLYAMTIPUS] DROP CONSTRAINT [FK_402540505_402540503];


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


GO
ALTER TABLE [dbo].[T_TANTERV] DROP CONSTRAINT [FK_4025392_4025394];


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


GO
ALTER TABLE [dbo].[T_TANTERV] DROP CONSTRAINT [FK_4025392_4025395];


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


GO
ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK] DROP CONSTRAINT [FK_4027915_4027917];


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


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


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_EVFOLYAMTIPUS] (
    [ID]                    INT             NOT NULL,
    [C_ALAPORASZAM]         NUMERIC (10, 2) DEFAULT ((0)) NOT NULL,
    [C_MINIMUMORASZAM]      NUMERIC (10, 2) DEFAULT ((0)) NOT NULL,
    [C_VISZONYITASILETSZAM] NUMERIC (10, 2) DEFAULT ((0)) NOT NULL,
    [C_ALINTEZMENYID]       INT             NOT NULL,
    [C_ALTANEVID]           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,
    [ELOZOTANEVIREKORDIDA]  INT             NULL,
    [NNID]                  INT             NULL,
    CONSTRAINT [tmp_ms_xx_constraint_PK_T_40254051] PRIMARY KEY CLUSTERED ([ID] ASC, [C_ALINTEZMENYID] ASC, [C_ALTANEVID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_EVFOLYAMTIPUS])
    BEGIN
        INSERT INTO [dbo].[tmp_ms_xx_T_EVFOLYAMTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [ELOZOTANEVIREKORDID], [ELOZOTANEVIREKORDIDA], [NNID])
        SELECT   [ID],
                 [C_ALINTEZMENYID],
                 [C_ALTANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [ELOZOTANEVIREKORDID],
                 [ELOZOTANEVIREKORDIDA],
                 [NNID]
        FROM     [dbo].[T_EVFOLYAMTIPUS]
        ORDER BY [ID] ASC, [C_ALINTEZMENYID] ASC, [C_ALTANEVID] ASC;
    END

DROP TABLE [dbo].[T_EVFOLYAMTIPUS];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_EVFOLYAMTIPUS]', N'T_EVFOLYAMTIPUS';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40254051]', N'PK_T_4025405', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_EVFOLYAMTIPUS].[I_T_402540502_402540500]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402540502_402540500]
    ON [dbo].[T_EVFOLYAMTIPUS]([C_ALINTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_EVFOLYAMTIPUS].[I_T_402540505_402540503]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402540505_402540503]
    ON [dbo].[T_EVFOLYAMTIPUS]([C_ALTANEVID] ASC);


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


GO
ALTER TABLE [dbo].[T_KIZAROCSOPORT] WITH NOCHECK
    ADD CONSTRAINT [FK_4026005_4026007] FOREIGN KEY ([C_EVFOLYAM], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_EVFOLYAMTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_EVFOLYAMTIPUS_OKTATASINEVELE] WITH NOCHECK
    ADD CONSTRAINT [FK_4026013_4026012] FOREIGN KEY ([C_EVFOLYAMTIPUSID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_EVFOLYAMTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_ORATERV] WITH NOCHECK
    ADD CONSTRAINT [FK_4025994_4025995] FOREIGN KEY ([C_EVFOLYAM], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_EVFOLYAMTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_EVFOLYAMTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_T_21038_4025414] FOREIGN KEY ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]) REFERENCES [dbo].[T_DICTIONARYITEMBASE] ([ID], [C_INTEZMENYID], [C_TANEVID]);


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


GO
ALTER TABLE [dbo].[T_EVFOLYAMTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_402540502_402540500] FOREIGN KEY ([C_ALINTEZMENYID]) REFERENCES [dbo].[T_INTEZMENY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_EVFOLYAMTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_402540505_402540503] FOREIGN KEY ([C_ALTANEVID]) REFERENCES [dbo].[T_TANEV] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANTERV] WITH NOCHECK
    ADD CONSTRAINT [FK_4025392_4025394] FOREIGN KEY ([C_EVFOLYAMIG], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_EVFOLYAMTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_TANTERV] WITH NOCHECK
    ADD CONSTRAINT [FK_4025392_4025395] FOREIGN KEY ([C_EVFOLYAMTOL], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_EVFOLYAMTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK] WITH NOCHECK
    ADD CONSTRAINT [FK_4027915_4027917] FOREIGN KEY ([C_EVFOLYAMTIPUSA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_EVFOLYAMTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_OSZTALYCSOPORT] WITH NOCHECK
    ADD CONSTRAINT [FK_4025296_4025418] FOREIGN KEY ([C_EVFOLYAMTIPUSA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_EVFOLYAMTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


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


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


GO
ALTER TABLE [dbo].[T_KIZAROCSOPORT] WITH CHECK CHECK CONSTRAINT [FK_4026005_4026007];

ALTER TABLE [dbo].[T_EVFOLYAMTIPUS_OKTATASINEVELE] WITH CHECK CHECK CONSTRAINT [FK_4026013_4026012];

ALTER TABLE [dbo].[T_ORATERV] WITH CHECK CHECK CONSTRAINT [FK_4025994_4025995];

ALTER TABLE [dbo].[T_EVFOLYAMTIPUS] WITH CHECK CHECK CONSTRAINT [FK_T_21038_4025414];

ALTER TABLE [dbo].[T_EVFOLYAMTIPUS] WITH CHECK CHECK CONSTRAINT [FK_402540502_402540500];

ALTER TABLE [dbo].[T_EVFOLYAMTIPUS] WITH CHECK CHECK CONSTRAINT [FK_402540505_402540503];

ALTER TABLE [dbo].[T_TANTERV] WITH CHECK CHECK CONSTRAINT [FK_4025392_4025394];

ALTER TABLE [dbo].[T_TANTERV] WITH CHECK CHECK CONSTRAINT [FK_4025392_4025395];

ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK] WITH CHECK CHECK CONSTRAINT [FK_4027915_4027917];

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


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_EVFOLYAMTIPUS', 'T_EVFOLYAMTIPUS_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

