﻿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].[T_MEROHELY].[I_T_4028189_4028188]...';


GO
DROP INDEX [I_T_4028189_4028188]
    ON [dbo].[T_MEROHELY];


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


GO
ALTER TABLE [dbo].[T_KOZMUSZAMLA] DROP CONSTRAINT [FK_4028171_4028178];


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


GO
ALTER TABLE [dbo].[T_KOZMUSZAMLA] DROP CONSTRAINT [FK_4028171_4028180];


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


GO
ALTER TABLE [dbo].[T_KOZMUSZAMLA] DROP CONSTRAINT [FK_402817102_402817100];


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


GO
ALTER TABLE [dbo].[T_KOZMUSZAMLA] DROP CONSTRAINT [FK_402817105_402817103];


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


GO
ALTER TABLE [dbo].[T_MEROHELY] DROP CONSTRAINT [FK_4028189_4028188];


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_KOZMUSZAMLA] (
    [ID]                   INT             IDENTITY (1, 1) NOT NULL,
    [C_BEFIZETESDATUMA]    DATETIME        NULL,
    [C_FIZETESIHATARIDO]   DATETIME        NULL,
    [C_FIZETVE]            CHAR (1)        DEFAULT ('F') NOT NULL,
    [C_INVALIDITASOKA]     NVARCHAR (MAX)  NULL,
    [C_KELTE]              DATETIME        NULL,
    [C_KIBOCSATO]          NVARCHAR (MAX)  NULL,
    [C_KOZMUSZAMLATIPUSID] INT             NULL,
    [C_OSSZEG]             NUMERIC (10, 2) NULL,
    [C_PENZNEMID]          INT             NULL,
    [C_VALID]              CHAR (1)        DEFAULT ('F') NOT NULL,
    [C_VALIDALASALATT]     CHAR (1)        DEFAULT ('F') NOT NULL,
    [C_MEROHELYID]         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_40281711] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_KOZMUSZAMLA])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_KOZMUSZAMLA] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_KOZMUSZAMLA] ([ID], [C_BEFIZETESDATUMA], [C_FIZETESIHATARIDO], [C_FIZETVE], [C_INVALIDITASOKA], [C_KELTE], [C_KIBOCSATO], [C_KOZMUSZAMLATIPUSID], [C_OSSZEG], [C_PENZNEMID], [C_VALID], [C_VALIDALASALATT], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [ELOZOTANEVIREKORDID], [NNID])
        SELECT   [ID],
                 [C_BEFIZETESDATUMA],
                 [C_FIZETESIHATARIDO],
                 [C_FIZETVE],
                 [C_INVALIDITASOKA],
                 [C_KELTE],
                 [C_KIBOCSATO],
                 [C_KOZMUSZAMLATIPUSID],
                 [C_OSSZEG],
                 [C_PENZNEMID],
                 [C_VALID],
                 [C_VALIDALASALATT],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [ELOZOTANEVIREKORDID],
                 [NNID]
        FROM     [dbo].[T_KOZMUSZAMLA]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_KOZMUSZAMLA] OFF;
    END

DROP TABLE [dbo].[T_KOZMUSZAMLA];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_KOZMUSZAMLA]', N'T_KOZMUSZAMLA';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40281711]', N'PK_T_4028171', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_KOZMUSZAMLA].[I_T_4028171_4028178]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4028171_4028178]
    ON [dbo].[T_KOZMUSZAMLA]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_KOZMUSZAMLATIPUSID] ASC);


GO
PRINT N'Creating [dbo].[T_KOZMUSZAMLA].[I_T_4028171_4028180]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4028171_4028180]
    ON [dbo].[T_KOZMUSZAMLA]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_PENZNEMID] ASC);


GO
PRINT N'Creating [dbo].[T_KOZMUSZAMLA].[I_T_4028190_4028188]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4028190_4028188]
    ON [dbo].[T_KOZMUSZAMLA]([C_MEROHELYID] ASC);


GO
PRINT N'Creating [dbo].[T_KOZMUSZAMLA].[I_T_402817102_402817100]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402817102_402817100]
    ON [dbo].[T_KOZMUSZAMLA]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_KOZMUSZAMLA].[I_T_402817105_402817103]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402817105_402817103]
    ON [dbo].[T_KOZMUSZAMLA]([C_TANEVID] ASC);


GO
PRINT N'Altering [dbo].[T_MEROHELY]...';


GO
ALTER TABLE [dbo].[T_MEROHELY] DROP COLUMN [C_KOZMUSZAMLAID];


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


GO
ALTER TABLE [dbo].[T_KOZMUSZAMLA] WITH NOCHECK
    ADD CONSTRAINT [FK_4028171_4028178] FOREIGN KEY ([C_KOZMUSZAMLATIPUSID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_KOZMUSZAMLATIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_KOZMUSZAMLA] WITH NOCHECK
    ADD CONSTRAINT [FK_4028171_4028180] FOREIGN KEY ([C_PENZNEMID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_PENZNEMTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_KOZMUSZAMLA] WITH NOCHECK
    ADD CONSTRAINT [FK_4028190_4028188] FOREIGN KEY ([C_MEROHELYID]) REFERENCES [dbo].[T_MEROHELY] ([ID]);


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_KOZMUSZAMLA] WITH CHECK CHECK CONSTRAINT [FK_4028171_4028178];

ALTER TABLE [dbo].[T_KOZMUSZAMLA] WITH CHECK CHECK CONSTRAINT [FK_4028171_4028180];

ALTER TABLE [dbo].[T_KOZMUSZAMLA] WITH CHECK CHECK CONSTRAINT [FK_402817102_402817100];

ALTER TABLE [dbo].[T_KOZMUSZAMLA] WITH CHECK CHECK CONSTRAINT [FK_402817105_402817103];

ALTER TABLE [dbo].[T_KOZMUSZAMLA] WITH CHECK CHECK CONSTRAINT [FK_4028190_4028188];


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_KOZMUSZAMLA', 'T_KOZMUSZAMLA_OSSZES', 'T_MEROHELY', 'T_MEROHELY_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
