﻿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_TANITASIORA].[IDX_TANITASIORA_NAPTARINAPID_ORASZAM]...';


GO
DROP INDEX [IDX_TANITASIORA_NAPTARINAPID_ORASZAM]
    ON [dbo].[T_TANITASIORA];


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


GO
ALTER TABLE [dbo].[T_ORARENDIORATULAJDONSAG] DROP CONSTRAINT [FK_4027998_4027999];


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


GO
ALTER TABLE [dbo].[T_ORARENDIORATULAJDONSAG] DROP CONSTRAINT [FK_402799802_402799800];


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


GO
ALTER TABLE [dbo].[T_ORARENDIORATULAJDONSAG] DROP CONSTRAINT [FK_402799805_402799803];


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


GO
ALTER TABLE [dbo].[T_TANITASIORATULAJDONSAG] DROP CONSTRAINT [FK_402795405_402795403];


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


GO
ALTER TABLE [dbo].[T_TANITASIORATULAJDONSAG] DROP CONSTRAINT [FK_4027954_4027955];


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


GO
ALTER TABLE [dbo].[T_TANITASIORATULAJDONSAG] DROP CONSTRAINT [FK_402795402_402795400];


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_ORARENDIORATULAJDONSAG] (
    [ID]                  INT      IDENTITY (1, 1) NOT NULL,
    [C_ORATULAJDONSAGID]  INT      NULL,
    [C_ORARENDIORAID]     INT      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_40279981] PRIMARY KEY CLUSTERED ([ID] ASC)
);

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

DROP TABLE [dbo].[T_ORARENDIORATULAJDONSAG];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_ORARENDIORATULAJDONSAG]', N'T_ORARENDIORATULAJDONSAG';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40279981]', N'PK_T_4027998', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_ORARENDIORATULAJDONSAG].[I_T_4027998_4027999]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027998_4027999]
    ON [dbo].[T_ORARENDIORATULAJDONSAG]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_ORATULAJDONSAGID] ASC);


GO
PRINT N'Creating [dbo].[T_ORARENDIORATULAJDONSAG].[I_T_4028143_4028141]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4028143_4028141]
    ON [dbo].[T_ORARENDIORATULAJDONSAG]([C_ORARENDIORAID] ASC);


GO
PRINT N'Creating [dbo].[T_ORARENDIORATULAJDONSAG].[I_T_402799802_402799800]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402799802_402799800]
    ON [dbo].[T_ORARENDIORATULAJDONSAG]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_ORARENDIORATULAJDONSAG].[I_T_402799805_402799803]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402799805_402799803]
    ON [dbo].[T_ORARENDIORATULAJDONSAG]([C_TANEVID] ASC);


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] ALTER COLUMN [C_ORASZAM] INT NULL;


GO
PRINT N'Creating [dbo].[T_TANITASIORA].[IDX_TANITASIORA_NAPTARINAPID_ORASZAM]...';


GO
CREATE NONCLUSTERED INDEX [IDX_TANITASIORA_NAPTARINAPID_ORASZAM]
    ON [dbo].[T_TANITASIORA]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_NAPTARINAPID] ASC, [C_ORASZAM] ASC);


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_TANITASIORATULAJDONSAG] (
    [ID]                  INT      IDENTITY (1, 1) NOT NULL,
    [C_ORATULAJDONSAGID]  INT      NULL,
    [C_TANITASIORAID]     INT      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_40279541] PRIMARY KEY CLUSTERED ([ID] ASC)
);

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

DROP TABLE [dbo].[T_TANITASIORATULAJDONSAG];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_TANITASIORATULAJDONSAG]', N'T_TANITASIORATULAJDONSAG';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40279541]', N'PK_T_4027954', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_TANITASIORATULAJDONSAG].[I_T_4027954_4027955]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027954_4027955]
    ON [dbo].[T_TANITASIORATULAJDONSAG]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_ORATULAJDONSAGID] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORATULAJDONSAG].[I_T_4028146_4028144]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4028146_4028144]
    ON [dbo].[T_TANITASIORATULAJDONSAG]([C_TANITASIORAID] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORATULAJDONSAG].[I_T_402795402_402795400]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402795402_402795400]
    ON [dbo].[T_TANITASIORATULAJDONSAG]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORATULAJDONSAG].[I_T_402795405_402795403]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402795405_402795403]
    ON [dbo].[T_TANITASIORATULAJDONSAG]([C_TANEVID] ASC);


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


GO
ALTER TABLE [dbo].[T_ORARENDIORATULAJDONSAG] WITH NOCHECK
    ADD CONSTRAINT [FK_4027998_4027999] FOREIGN KEY ([C_ORATULAJDONSAGID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ORATULAJDONSAGTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_ORARENDIORATULAJDONSAG] WITH NOCHECK
    ADD CONSTRAINT [FK_4028143_4028141] FOREIGN KEY ([C_ORARENDIORAID]) REFERENCES [dbo].[T_ORARENDIORA] ([ID]);


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


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


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


GO
ALTER TABLE [dbo].[T_TANITASIORATULAJDONSAG] WITH NOCHECK
    ADD CONSTRAINT [FK_4027954_4027955] FOREIGN KEY ([C_ORATULAJDONSAGID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ORATULAJDONSAGTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


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


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


GO
ALTER TABLE [dbo].[T_TANITASIORATULAJDONSAG] WITH NOCHECK
    ADD CONSTRAINT [FK_4028146_4028144] FOREIGN KEY ([C_TANITASIORAID]) REFERENCES [dbo].[T_TANITASIORA] ([ID]);


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


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


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_ORARENDIORATULAJDONSAG] WITH CHECK CHECK CONSTRAINT [FK_4027998_4027999];

ALTER TABLE [dbo].[T_ORARENDIORATULAJDONSAG] WITH CHECK CHECK CONSTRAINT [FK_402799802_402799800];

ALTER TABLE [dbo].[T_ORARENDIORATULAJDONSAG] WITH CHECK CHECK CONSTRAINT [FK_402799805_402799803];

ALTER TABLE [dbo].[T_ORARENDIORATULAJDONSAG] WITH CHECK CHECK CONSTRAINT [FK_4028143_4028141];

ALTER TABLE [dbo].[T_TANITASIORATULAJDONSAG] WITH CHECK CHECK CONSTRAINT [FK_402795405_402795403];

ALTER TABLE [dbo].[T_TANITASIORATULAJDONSAG] WITH CHECK CHECK CONSTRAINT [FK_4027954_4027955];

ALTER TABLE [dbo].[T_TANITASIORATULAJDONSAG] WITH CHECK CHECK CONSTRAINT [FK_402795402_402795400];

ALTER TABLE [dbo].[T_TANITASIORATULAJDONSAG] WITH CHECK CHECK CONSTRAINT [FK_4028146_4028144];


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_ORARENDIORATULAJDONSAG', 'T_ORARENDIORATULAJDONSAG_OSSZES', 'T_TANITASIORATULAJDONSAG', 'T_TANITASIORATULAJDONSAG_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


UPDATE T_TANITASIORA SET C_ORASZAM = NULL
WHERE C_TANORANKIVULIFOGLALKOZAS = 'T'
GO
