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


GO
ALTER TABLE [dbo].[T_HITTANADATSZOLGALTATAS] DROP CONSTRAINT [FK_4028135_4028137];


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


GO
ALTER TABLE [dbo].[T_HITTANADATSZOLGALTATAS] DROP CONSTRAINT [FK_4028139_4028138];


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


GO
ALTER TABLE [dbo].[T_HITTANADATSZOLGALTATAS] DROP CONSTRAINT [FK_402813502_402813500];


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


GO
ALTER TABLE [dbo].[T_HITTANADATSZOLGALTATAS] DROP CONSTRAINT [FK_402813505_402813503];


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_HITTANADATSZOLGALTATAS] (
    [ID]                  INT             IDENTITY (1, 1) NOT NULL,
    [C_EDDIGIORASZAM]     NUMERIC (10, 2) NULL,
    [C_HITTANTIPUSID]     INT             NULL,
    [C_CSOPORTLETSZAM]    INT             DEFAULT ((0)) NOT NULL,
    [C_SORSZAM]           INT             NULL,
    [C_OSZTALYCSOPORTID]  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_40281351] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_HITTANADATSZOLGALTATAS])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_HITTANADATSZOLGALTATAS] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_HITTANADATSZOLGALTATAS] ([ID], [C_EDDIGIORASZAM], [C_HITTANTIPUSID], [C_CSOPORTLETSZAM], [C_OSZTALYCSOPORTID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [ELOZOTANEVIREKORDID], [NNID])
        SELECT   [ID],
                 [C_EDDIGIORASZAM],
                 [C_HITTANTIPUSID],
                 [C_CSOPORTLETSZAM],
                 [C_OSZTALYCSOPORTID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [ELOZOTANEVIREKORDID],
                 [NNID]
        FROM     [dbo].[T_HITTANADATSZOLGALTATAS]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_HITTANADATSZOLGALTATAS] OFF;
    END

DROP TABLE [dbo].[T_HITTANADATSZOLGALTATAS];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_HITTANADATSZOLGALTATAS]', N'T_HITTANADATSZOLGALTATAS';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40281351]', N'PK_T_4028135', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_HITTANADATSZOLGALTATAS].[I_T_4028135_4028137]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4028135_4028137]
    ON [dbo].[T_HITTANADATSZOLGALTATAS]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_HITTANTIPUSID] ASC);


GO
PRINT N'Creating [dbo].[T_HITTANADATSZOLGALTATAS].[I_T_4028139_4028138]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4028139_4028138]
    ON [dbo].[T_HITTANADATSZOLGALTATAS]([C_OSZTALYCSOPORTID] ASC);


GO
PRINT N'Creating [dbo].[T_HITTANADATSZOLGALTATAS].[I_T_402813502_402813500]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402813502_402813500]
    ON [dbo].[T_HITTANADATSZOLGALTATAS]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_HITTANADATSZOLGALTATAS].[I_T_402813505_402813503]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402813505_402813503]
    ON [dbo].[T_HITTANADATSZOLGALTATAS]([C_TANEVID] ASC);


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


GO
ALTER TABLE [dbo].[T_HITTANADATSZOLGALTATAS] WITH NOCHECK
    ADD CONSTRAINT [FK_4028135_4028137] FOREIGN KEY ([C_HITTANTIPUSID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_HITTANTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


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


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


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


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


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

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


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


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


GO
ALTER TABLE [dbo].[T_HITTANADATSZOLGALTATAS] WITH CHECK CHECK CONSTRAINT [FK_4028135_4028137];

ALTER TABLE [dbo].[T_HITTANADATSZOLGALTATAS] WITH CHECK CHECK CONSTRAINT [FK_4028139_4028138];

ALTER TABLE [dbo].[T_HITTANADATSZOLGALTATAS] WITH CHECK CHECK CONSTRAINT [FK_402813502_402813500];

ALTER TABLE [dbo].[T_HITTANADATSZOLGALTATAS] WITH CHECK CHECK CONSTRAINT [FK_402813505_402813503];


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_HITTANADATSZOLGALTATAS', 'T_HITTANADATSZOLGALTATAS_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
