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


GO
ALTER TABLE [dbo].[T_GONDVISELOERTESITES] DROP CONSTRAINT [FK_4025878_4025876];


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


GO
ALTER TABLE [dbo].[T_GONDVISELO] DROP CONSTRAINT [FK_4025181_4025180];


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


GO
ALTER TABLE [dbo].[T_GONDVISELO] DROP CONSTRAINT [FK_402516902_402516900];


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


GO
ALTER TABLE [dbo].[T_GONDVISELO] DROP CONSTRAINT [FK_402516905_402516903];


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


GO
ALTER TABLE [dbo].[T_GONDVISELO] DROP CONSTRAINT [FK_4025169_4025177];


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


GO
ALTER TABLE [dbo].[T_CIM] DROP CONSTRAINT [FK_4025184_4025183];


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


GO
ALTER TABLE [dbo].[T_OLDALLATOGATOTTSAG] DROP CONSTRAINT [FK_4026047_4026046];


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


GO
ALTER TABLE [dbo].[T_EMAIL] DROP CONSTRAINT [FK_4025224_4025222];


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


GO
ALTER TABLE [dbo].[T_FALIUJSAGLATOGATTOTSAG] DROP CONSTRAINT [FK_4025824_4025822];


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


GO
ALTER TABLE [dbo].[T_TELEFON] DROP CONSTRAINT [FK_4025221_4025219];


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


GO
ALTER TABLE [dbo].[T_DIAKIGAZOLVANYIGENYLES] DROP CONSTRAINT [FK_4025785_4025784];


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


GO
ALTER TABLE [dbo].[T_IKTATOTTDOKUMENTUMOK] DROP CONSTRAINT [FK_4026222_4026221];


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


GO
ALTER TABLE [dbo].[T_IKTATOTTDOKUMENTUMOK] DROP CONSTRAINT [FK_4026219_4026218];


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


GO
ALTER TABLE [dbo].[T_JOGOSULTGONDVISELOK_HOZZAREN] DROP CONSTRAINT [FK_4026216_4026215];


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


GO
ALTER TABLE [dbo].[T_FELHASZNALOBELEPES] DROP CONSTRAINT [FK_4025187_4025186];


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_GONDVISELO] (
    [ID]                       INT            IDENTITY (1, 1) NOT NULL,
    [C_AKTIV]                  CHAR (1)       DEFAULT ('T') NULL,
    [C_GONDVISELESKEZDETE]     DATETIME       NULL,
    [C_GONDVISELESVEGE]        DATETIME       NULL,
    [C_GYERMEKETEGYEDULNEVELO] CHAR (1)       DEFAULT ('F') NULL,
    [C_NEV]                    NVARCHAR (200) NOT NULL,
    [C_NYUGDIJAS]              CHAR (1)       DEFAULT ('F') NULL,
    [C_ROKONSAGFOKA]           INT            NOT NULL,
    [C_ISEMAILERTESITES]       CHAR (1)       DEFAULT ('F') NULL,
    [C_TANULOID]               INT            NOT NULL,
    [C_INTEZMENYID]            INT            NOT NULL,
    [C_TANEVID]                INT            NOT NULL,
    [TOROLT]                   CHAR (1)       DEFAULT ('F') NULL,
    [SERIAL]                   INT            NULL,
    [LASTCHANGED]              DATETIME       DEFAULT (getdate()) NULL,
    [CREATED]                  DATETIME       DEFAULT (getdate()) NULL,
    [MODIFIER]                 INT            NULL,
    [CREATOR]                  INT            NULL,
    CONSTRAINT [tmp_ms_xx_constraint_PK_T_40251691] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_GONDVISELO])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_GONDVISELO] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_GONDVISELO] ([ID], [C_AKTIV], [C_GONDVISELESKEZDETE], [C_GONDVISELESVEGE], [C_GYERMEKETEGYEDULNEVELO], [C_NEV], [C_NYUGDIJAS], [C_ROKONSAGFOKA], [C_TANULOID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR])
        SELECT   [ID],
                 [C_AKTIV],
                 [C_GONDVISELESKEZDETE],
                 [C_GONDVISELESVEGE],
                 [C_GYERMEKETEGYEDULNEVELO],
                 [C_NEV],
                 [C_NYUGDIJAS],
                 [C_ROKONSAGFOKA],
                 [C_TANULOID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR]
        FROM     [dbo].[T_GONDVISELO]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_GONDVISELO] OFF;
    END

DROP TABLE [dbo].[T_GONDVISELO];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_GONDVISELO]', N'T_GONDVISELO';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40251691]', N'PK_T_4025169', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_GONDVISELO].[I_T_4025169_4025177]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025169_4025177]
    ON [dbo].[T_GONDVISELO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_ROKONSAGFOKA] ASC);


GO
PRINT N'Creating [dbo].[T_GONDVISELO].[I_T_4025181_4025180]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025181_4025180]
    ON [dbo].[T_GONDVISELO]([C_TANULOID] ASC);


GO
PRINT N'Creating [dbo].[T_GONDVISELO].[I_T_402516902_402516900]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402516902_402516900]
    ON [dbo].[T_GONDVISELO]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_GONDVISELO].[I_T_402516905_402516903]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402516905_402516903]
    ON [dbo].[T_GONDVISELO]([C_TANEVID] ASC);


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


GO
ALTER TABLE [dbo].[T_GONDVISELOERTESITES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025878_4025876] FOREIGN KEY ([C_GONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


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


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_GONDVISELO] WITH NOCHECK
    ADD CONSTRAINT [FK_4025169_4025177] FOREIGN KEY ([C_ROKONSAGFOKA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ROKONSAGIFOK] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_CIM] WITH NOCHECK
    ADD CONSTRAINT [FK_4025184_4025183] FOREIGN KEY ([C_GONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_OLDALLATOGATOTTSAG] WITH NOCHECK
    ADD CONSTRAINT [FK_4026047_4026046] FOREIGN KEY ([C_GONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_EMAIL] WITH NOCHECK
    ADD CONSTRAINT [FK_4025224_4025222] FOREIGN KEY ([C_GONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_FALIUJSAGLATOGATTOTSAG] WITH NOCHECK
    ADD CONSTRAINT [FK_4025824_4025822] FOREIGN KEY ([C_GONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TELEFON] WITH NOCHECK
    ADD CONSTRAINT [FK_4025221_4025219] FOREIGN KEY ([C_GONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_DIAKIGAZOLVANYIGENYLES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025785_4025784] FOREIGN KEY ([C_IGENYLOGONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_IKTATOTTDOKUMENTUMOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026222_4026221] FOREIGN KEY ([C_FELTOLTOGONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_IKTATOTTDOKUMENTUMOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026219_4026218] FOREIGN KEY ([C_DOKPARTNERGONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_JOGOSULTGONDVISELOK_HOZZAREN] WITH NOCHECK
    ADD CONSTRAINT [FK_4026216_4026215] FOREIGN KEY ([C_JOGOSULTGONDVISELOKID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_FELHASZNALOBELEPES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025187_4025186] FOREIGN KEY ([C_GONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


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


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


GO
ALTER TABLE [dbo].[T_GONDVISELOERTESITES] WITH CHECK CHECK CONSTRAINT [FK_4025878_4025876];

ALTER TABLE [dbo].[T_GONDVISELO] WITH CHECK CHECK CONSTRAINT [FK_4025181_4025180];

ALTER TABLE [dbo].[T_GONDVISELO] WITH CHECK CHECK CONSTRAINT [FK_402516902_402516900];

ALTER TABLE [dbo].[T_GONDVISELO] WITH CHECK CHECK CONSTRAINT [FK_402516905_402516903];

ALTER TABLE [dbo].[T_GONDVISELO] WITH CHECK CHECK CONSTRAINT [FK_4025169_4025177];

ALTER TABLE [dbo].[T_CIM] WITH CHECK CHECK CONSTRAINT [FK_4025184_4025183];

ALTER TABLE [dbo].[T_OLDALLATOGATOTTSAG] WITH CHECK CHECK CONSTRAINT [FK_4026047_4026046];

ALTER TABLE [dbo].[T_EMAIL] WITH CHECK CHECK CONSTRAINT [FK_4025224_4025222];

ALTER TABLE [dbo].[T_FALIUJSAGLATOGATTOTSAG] WITH CHECK CHECK CONSTRAINT [FK_4025824_4025822];

ALTER TABLE [dbo].[T_TELEFON] WITH CHECK CHECK CONSTRAINT [FK_4025221_4025219];

ALTER TABLE [dbo].[T_DIAKIGAZOLVANYIGENYLES] WITH CHECK CHECK CONSTRAINT [FK_4025785_4025784];

ALTER TABLE [dbo].[T_IKTATOTTDOKUMENTUMOK] WITH CHECK CHECK CONSTRAINT [FK_4026222_4026221];

ALTER TABLE [dbo].[T_IKTATOTTDOKUMENTUMOK] WITH CHECK CHECK CONSTRAINT [FK_4026219_4026218];

ALTER TABLE [dbo].[T_JOGOSULTGONDVISELOK_HOZZAREN] WITH CHECK CHECK CONSTRAINT [FK_4026216_4026215];

ALTER TABLE [dbo].[T_FELHASZNALOBELEPES] WITH CHECK CHECK CONSTRAINT [FK_4025187_4025186];


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_GONDVISELO', 'T_GONDVISELO_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


-- Triggerek inicializálás az összes táblára
DECLARE tableCursor CURSOR LOCAL FOR 
  SELECT DISTINCT TABLE_NAME
  FROM INFORMATION_SCHEMA.COLUMNS 
  WHERE COLUMN_NAME IN ('CREATOR', 'MODIFIER')
	AND TABLE_NAME NOT IN ('T_ENTITYHISTORY', 'T_ENTITYATTRIBUTEHISTORY', 'T_LOG', 'T_GLOBALLOCK', 'T_USERPROFILE', 'T_OLDALLATOGATOTTSAG')
	AND TABLE_NAME IN (
		SELECT t.name 
		FROM sys.tables t
		INNER JOIN sys.schemas s ON s.schema_id = t.schema_id
		WHERE s.name ='dbo' 
	)

DECLARE @table nvarchar(35)

OPEN tableCursor  
FETCH NEXT FROM tableCursor INTO @table  

WHILE @@FETCH_STATUS = 0 BEGIN  
  EXEC sp_Global_CreateTriggers @table
  FETCH NEXT FROM tableCursor INTO @table  
END  

CLOSE tableCursor  
DEALLOCATE tableCursor 


EXEC dev.sp_Global_GenerateAsyncAuditTriggerAll 1
GO

EXEC dev.sp_Global_GenerateAsyncAuditProcedureAll
GO

EXEC dev.sp_Global_GenerateAsyncAuditAuditPocessing
GO

ALTER QUEUE auditlog.AuditQueue WITH STATUS=OFF, ACTIVATION 
(STATUS = OFF)
GO

ALTER QUEUE auditlog.AuditQueue WITH STATUS=ON, ACTIVATION 
(STATUS = ON, MAX_QUEUE_READERS = 2)
GO