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


GO
ALTER TABLE [dbo].[T_CSOPORTTIPUS_FOGLALKOZASTIPU] DROP CONSTRAINT [FK_4026541_4026540];


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


GO
ALTER TABLE [dbo].[T_KIZAROCSOPORT] DROP CONSTRAINT [FK_4026005_4026006];


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


GO
ALTER TABLE [dbo].[T_CSOPORT] DROP CONSTRAINT [FK_4025277_4025300];


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


GO
ALTER TABLE [dbo].[T_CSOPORTTIPUS] DROP CONSTRAINT [FK_T_21038_4025243];


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


GO
ALTER TABLE [dbo].[T_CSOPORTTIPUS] DROP CONSTRAINT [FK_402519102_402519100];


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


GO
ALTER TABLE [dbo].[T_CSOPORTTIPUS] DROP CONSTRAINT [FK_402519105_402519103];


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


GO
ALTER TABLE [dbo].[T_TANTERV] DROP CONSTRAINT [FK_4025392_4025393];


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_CSOPORTTIPUS] (
    [ID]                   INT      NOT NULL,
    [C_ISTANORAICELU]      CHAR (1) DEFAULT ('F') NOT NULL,
    [C_ORAPERC]            INT      NULL,
    [C_ALINTEZMENYID]      INT      NOT NULL,
    [C_ALTANEVID]          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,
    [ELOZOTANEVIREKORDIDA] INT      NULL,
    [NNID]                 INT      NULL,
    CONSTRAINT [tmp_ms_xx_constraint_PK_T_40251911] PRIMARY KEY CLUSTERED ([ID] ASC, [C_ALINTEZMENYID] ASC, [C_ALTANEVID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_CSOPORTTIPUS])
    BEGIN
        INSERT INTO [dbo].[tmp_ms_xx_T_CSOPORTTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID], [C_ISTANORAICELU], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [ELOZOTANEVIREKORDID], [ELOZOTANEVIREKORDIDA], [NNID])
        SELECT   [ID],
                 [C_ALINTEZMENYID],
                 [C_ALTANEVID],
                 [C_ISTANORAICELU],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [ELOZOTANEVIREKORDID],
                 [ELOZOTANEVIREKORDIDA],
                 [NNID]
        FROM     [dbo].[T_CSOPORTTIPUS]
        ORDER BY [ID] ASC, [C_ALINTEZMENYID] ASC, [C_ALTANEVID] ASC;
    END

DROP TABLE [dbo].[T_CSOPORTTIPUS];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_CSOPORTTIPUS]', N'T_CSOPORTTIPUS';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40251911]', N'PK_T_4025191', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_CSOPORTTIPUS].[I_T_402519102_402519100]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402519102_402519100]
    ON [dbo].[T_CSOPORTTIPUS]([C_ALINTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_CSOPORTTIPUS].[I_T_402519105_402519103]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402519105_402519103]
    ON [dbo].[T_CSOPORTTIPUS]([C_ALTANEVID] ASC);


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


GO
ALTER TABLE [dbo].[T_CSOPORTTIPUS_FOGLALKOZASTIPU] WITH NOCHECK
    ADD CONSTRAINT [FK_4026541_4026540] FOREIGN KEY ([C_CSOPORTTIPUSID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_CSOPORTTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_KIZAROCSOPORT] WITH NOCHECK
    ADD CONSTRAINT [FK_4026005_4026006] FOREIGN KEY ([C_CSOPORTOKTIPUSA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_CSOPORTTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_CSOPORT] WITH NOCHECK
    ADD CONSTRAINT [FK_4025277_4025300] FOREIGN KEY ([C_TIPUSA], [C_ALINTEZMENYID], [C_ALTANEVID]) REFERENCES [dbo].[T_CSOPORTTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_CSOPORTTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_T_21038_4025243] FOREIGN KEY ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]) REFERENCES [dbo].[T_DICTIONARYITEMBASE] ([ID], [C_INTEZMENYID], [C_TANEVID]);


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


GO
ALTER TABLE [dbo].[T_CSOPORTTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_402519102_402519100] FOREIGN KEY ([C_ALINTEZMENYID]) REFERENCES [dbo].[T_INTEZMENY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_CSOPORTTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_402519105_402519103] FOREIGN KEY ([C_ALTANEVID]) REFERENCES [dbo].[T_TANEV] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANTERV] WITH NOCHECK
    ADD CONSTRAINT [FK_4025392_4025393] FOREIGN KEY ([C_CSOPORTTIPUSA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_CSOPORTTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


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

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


GO
ALTER TABLE [dbo].[T_CSOPORTTIPUS_FOGLALKOZASTIPU] WITH CHECK CHECK CONSTRAINT [FK_4026541_4026540];

ALTER TABLE [dbo].[T_KIZAROCSOPORT] WITH CHECK CHECK CONSTRAINT [FK_4026005_4026006];

ALTER TABLE [dbo].[T_CSOPORT] WITH CHECK CHECK CONSTRAINT [FK_4025277_4025300];

ALTER TABLE [dbo].[T_CSOPORTTIPUS] WITH CHECK CHECK CONSTRAINT [FK_T_21038_4025243];

ALTER TABLE [dbo].[T_CSOPORTTIPUS] WITH CHECK CHECK CONSTRAINT [FK_402519102_402519100];

ALTER TABLE [dbo].[T_CSOPORTTIPUS] WITH CHECK CHECK CONSTRAINT [FK_402519105_402519103];

ALTER TABLE [dbo].[T_TANTERV] WITH CHECK CHECK CONSTRAINT [FK_4025392_4025393];


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_CSOPORTTIPUS', 'T_CSOPORTTIPUS_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
