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


GO
ALTER TABLE [dbo].[T_OSZTALY] DROP CONSTRAINT [FK_4025294_4027546];


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


GO
ALTER TABLE [dbo].[T_SZAKKEPESITESTIPUS] DROP CONSTRAINT [FK_T_21038_4027551];


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


GO
ALTER TABLE [dbo].[T_SZAKKEPESITESTIPUS] DROP CONSTRAINT [FK_402753902_402753900];


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


GO
ALTER TABLE [dbo].[T_SZAKKEPESITESTIPUS] DROP CONSTRAINT [FK_402753905_402753903];


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


GO
ALTER TABLE [dbo].[T_TANULO] DROP CONSTRAINT [FK_4025178_4027542];


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


GO
ALTER TABLE [dbo].[T_FELHASZNALOBELEPES] ALTER COLUMN [C_GUID] NVARCHAR (36) NOT NULL;


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_SZAKKEPESITESTIPUS] (
    [ID]                   INT      NOT NULL,
    [C_SZAKKEPESITESSZINT] INT      NULL,
    [C_TANULMANYITERULET]  INT      NULL,
    [C_TERULETSORSZAM]     INT      NULL,
    [C_ALINTEZMENYID]      INT      NOT NULL,
    [C_ALTANEVID]          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_40275391] PRIMARY KEY CLUSTERED ([ID] ASC, [C_ALINTEZMENYID] ASC, [C_ALTANEVID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_SZAKKEPESITESTIPUS])
    BEGIN
        INSERT INTO [dbo].[tmp_ms_xx_T_SZAKKEPESITESTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR])
        SELECT   [ID],
                 [C_ALINTEZMENYID],
                 [C_ALTANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR]
        FROM     [dbo].[T_SZAKKEPESITESTIPUS]
        ORDER BY [ID] ASC, [C_ALINTEZMENYID] ASC, [C_ALTANEVID] ASC;
    END

DROP TABLE [dbo].[T_SZAKKEPESITESTIPUS];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_SZAKKEPESITESTIPUS]', N'T_SZAKKEPESITESTIPUS';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40275391]', N'PK_T_4027539', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_SZAKKEPESITESTIPUS].[I_T_402753902_402753900]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402753902_402753900]
    ON [dbo].[T_SZAKKEPESITESTIPUS]([C_ALINTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_SZAKKEPESITESTIPUS].[I_T_402753905_402753903]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402753905_402753903]
    ON [dbo].[T_SZAKKEPESITESTIPUS]([C_ALTANEVID] ASC);


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


GO
ALTER TABLE [dbo].[T_OSZTALY] WITH NOCHECK
    ADD CONSTRAINT [FK_4025294_4027546] FOREIGN KEY ([C_SZAKKEPESITES], [C_ALINTEZMENYID], [C_ALTANEVID]) REFERENCES [dbo].[T_SZAKKEPESITESTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


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


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_TANULO] WITH NOCHECK
    ADD CONSTRAINT [FK_4025178_4027542] FOREIGN KEY ([C_SZAKKEPESITES], [C_ALINTEZMENYID], [C_ALTANEVID]) REFERENCES [dbo].[T_SZAKKEPESITESTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


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


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


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


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


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


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_OSZTALY] WITH CHECK CHECK CONSTRAINT [FK_4025294_4027546];

ALTER TABLE [dbo].[T_SZAKKEPESITESTIPUS] WITH CHECK CHECK CONSTRAINT [FK_T_21038_4027551];

ALTER TABLE [dbo].[T_SZAKKEPESITESTIPUS] WITH CHECK CHECK CONSTRAINT [FK_402753902_402753900];

ALTER TABLE [dbo].[T_SZAKKEPESITESTIPUS] WITH CHECK CHECK CONSTRAINT [FK_402753905_402753903];

ALTER TABLE [dbo].[T_TANULO] WITH CHECK CHECK CONSTRAINT [FK_4025178_4027542];


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_SZAKKEPESITESTIPUS', 'T_SZAKKEPESITESTIPUS_OSSZES',
  'T_FELHASZNALOBELEPES', 'T_FELHASZNALOBELEPES_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
