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


GO
ALTER TABLE [dbo].[T_FELHASZNALOBELEPESTORTENET] DROP CONSTRAINT [FK_3195831_3195830];


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


GO
ALTER TABLE [dbo].[T_FELHASZNALOBELEPESTORTENET] DROP CONSTRAINT [FK_319582802_319582800];


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


GO
ALTER TABLE [dbo].[T_FELHASZNALOBELEPESTORTENET] DROP CONSTRAINT [FK_319582805_319582803];


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_FELHASZNALOBELEPESTORTENET] (
    [ID]                  INT           IDENTITY (1, 1) NOT NULL,
    [C_SESSIONID]         NVARCHAR (36) NULL,
    [C_SERVERNAME]        NVARCHAR (50) NULL,
    [C_USERIP]            NVARCHAR (15) NULL,
    [C_LOGINDATE]         DATETIME      NULL,
    [C_LASTACTIVITY]      DATETIME      NULL,
    [C_GUID]              NVARCHAR (36) NULL,
    [C_IDP]               CHAR (1)      DEFAULT ('F') NOT NULL,
    [C_FELHASZNALOID]     INT           NOT NULL,
    [C_GONDVISELOID]      INT           NULL,
    [C_INTEZMENYID]       INT           NULL,
    [C_TANEVID]           INT           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_31958281] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_FELHASZNALOBELEPESTORTENET])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_FELHASZNALOBELEPESTORTENET] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_FELHASZNALOBELEPESTORTENET] ([ID], [C_SESSIONID], [C_SERVERNAME], [C_USERIP], [C_LOGINDATE], [C_LASTACTIVITY], [C_FELHASZNALOID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [ELOZOTANEVIREKORDID], [NNID])
        SELECT   [ID],
                 [C_SESSIONID],
                 [C_SERVERNAME],
                 [C_USERIP],
                 [C_LOGINDATE],
                 [C_LASTACTIVITY],
                 [C_FELHASZNALOID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [ELOZOTANEVIREKORDID],
                 [NNID]
        FROM     [dbo].[T_FELHASZNALOBELEPESTORTENET]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_FELHASZNALOBELEPESTORTENET] OFF;
    END

DROP TABLE [dbo].[T_FELHASZNALOBELEPESTORTENET];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_FELHASZNALOBELEPESTORTENET]', N'T_FELHASZNALOBELEPESTORTENET';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_31958281]', N'PK_T_3195828', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_FELHASZNALOBELEPESTORTENET].[I_T_3195831_3195830]...';


GO
CREATE NONCLUSTERED INDEX [I_T_3195831_3195830]
    ON [dbo].[T_FELHASZNALOBELEPESTORTENET]([C_FELHASZNALOID] ASC);


GO
PRINT N'Creating [dbo].[T_FELHASZNALOBELEPESTORTENET].[I_T_4028159_4028157]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4028159_4028157]
    ON [dbo].[T_FELHASZNALOBELEPESTORTENET]([C_GONDVISELOID] ASC);


GO
PRINT N'Creating [dbo].[T_FELHASZNALOBELEPESTORTENET].[I_T_319582802_319582800]...';


GO
CREATE NONCLUSTERED INDEX [I_T_319582802_319582800]
    ON [dbo].[T_FELHASZNALOBELEPESTORTENET]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_FELHASZNALOBELEPESTORTENET].[I_T_319582805_319582803]...';


GO
CREATE NONCLUSTERED INDEX [I_T_319582805_319582803]
    ON [dbo].[T_FELHASZNALOBELEPESTORTENET]([C_TANEVID] ASC);


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


GO
ALTER TABLE [dbo].[T_FELHASZNALOBELEPESTORTENET] WITH NOCHECK
    ADD CONSTRAINT [FK_3195831_3195830] FOREIGN KEY ([C_FELHASZNALOID]) REFERENCES [dbo].[T_FELHASZNALO] ([ID]);


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


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


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


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


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_FELHASZNALOBELEPESTORTENET] WITH CHECK CHECK CONSTRAINT [FK_3195831_3195830];

ALTER TABLE [dbo].[T_FELHASZNALOBELEPESTORTENET] WITH CHECK CHECK CONSTRAINT [FK_319582802_319582800];

ALTER TABLE [dbo].[T_FELHASZNALOBELEPESTORTENET] WITH CHECK CHECK CONSTRAINT [FK_319582805_319582803];

ALTER TABLE [dbo].[T_FELHASZNALOBELEPESTORTENET] WITH CHECK CHECK CONSTRAINT [FK_4028159_4028157];


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_FELHASZNALOBELEPESTORTENET', 'T_FELHASZNALOBELEPESTORTENET_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
