﻿GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;

SET NUMERIC_ROUNDABORT OFF;


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


GO
ALTER TABLE [dbo].[T_IKTATOTTDOKUMENTUM]
    ADD [C_DOKUMENTUMELEMISZINT] INT NOT NULL;


GO
PRINT N'Creating [dbo].[T_IKTATOTTDOKUMENTUM].[I_T_4028544_4028651]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4028544_4028651]
    ON [dbo].[T_IKTATOTTDOKUMENTUM]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_DOKUMENTUMELEMISZINT] ASC);


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


GO
CREATE TABLE [dbo].[T_DOKUMENTUMELEMISZINT] (
    [ID]                   INT      NOT NULL,
    [C_ALINTEZMENYID]      INT      NOT NULL,
    [C_ALTANEVID]          INT      NOT NULL,
    [TOROLT]               CHAR (1) NOT NULL,
    [SERIAL]               INT      NOT NULL,
    [LASTCHANGED]          DATETIME NOT NULL,
    [CREATED]              DATETIME NOT NULL,
    [MODIFIER]             INT      NULL,
    [CREATOR]              INT      NULL,
    [ELOZOTANEVIREKORDID]  INT      NULL,
    [ELOZOTANEVIREKORDIDA] INT      NULL,
    [NNID]                 INT      NULL,
    CONSTRAINT [PK_T_4028652] PRIMARY KEY CLUSTERED ([ID] ASC, [C_ALINTEZMENYID] ASC, [C_ALTANEVID] ASC)
);


GO
PRINT N'Creating [dbo].[T_DOKUMENTUMELEMISZINT].[I_T_402865202_402865200]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402865202_402865200]
    ON [dbo].[T_DOKUMENTUMELEMISZINT]([C_ALINTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_DOKUMENTUMELEMISZINT].[I_T_402865205_402865203]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402865205_402865203]
    ON [dbo].[T_DOKUMENTUMELEMISZINT]([C_ALTANEVID] ASC);


GO
PRINT N'Creating unnamed constraint on [dbo].[T_DOKUMENTUMELEMISZINT]...';


GO
ALTER TABLE [dbo].[T_DOKUMENTUMELEMISZINT]
    ADD DEFAULT (getdate()) FOR [CREATED];


GO
PRINT N'Creating unnamed constraint on [dbo].[T_DOKUMENTUMELEMISZINT]...';


GO
ALTER TABLE [dbo].[T_DOKUMENTUMELEMISZINT]
    ADD DEFAULT (getdate()) FOR [LASTCHANGED];


GO
PRINT N'Creating unnamed constraint on [dbo].[T_DOKUMENTUMELEMISZINT]...';


GO
ALTER TABLE [dbo].[T_DOKUMENTUMELEMISZINT]
    ADD DEFAULT ('F') FOR [TOROLT];


GO
PRINT N'Creating unnamed constraint on [dbo].[T_DOKUMENTUMELEMISZINT]...';


GO
ALTER TABLE [dbo].[T_DOKUMENTUMELEMISZINT]
    ADD DEFAULT ((0)) FOR [SERIAL];


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


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


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_IKTATOTTDOKUMENTUM] WITH NOCHECK
    ADD CONSTRAINT [FK_4028544_4028651] FOREIGN KEY ([C_DOKUMENTUMELEMISZINT], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_DOKUMENTUMELEMISZINT] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


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


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


GO
CREATE VIEW [dbo].T_DOKUMENTUMELEMISZINT_OSSZES
        AS
        SELECT *
        FROM dbo.T_DOKUMENTUMELEMISZINT
GO
PRINT N'Checking existing data against newly created constraints';


GO
ALTER TABLE [dbo].[T_DOKUMENTUMELEMISZINT] WITH CHECK CHECK CONSTRAINT [FK_T_21038_4028653];

ALTER TABLE [dbo].[T_DOKUMENTUMELEMISZINT] WITH CHECK CHECK CONSTRAINT [FK_402865202_402865200];

ALTER TABLE [dbo].[T_DOKUMENTUMELEMISZINT] WITH CHECK CHECK CONSTRAINT [FK_402865205_402865203];

ALTER TABLE [dbo].[T_IKTATOTTDOKUMENTUM] WITH CHECK CHECK CONSTRAINT [FK_4028544_4028651];


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_IKTATOTTDOKUMENTUM', 'T_IKTATOTTDOKUMENTUM_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


ALTER QUEUE auditLog.[AuditQueue]
WITH ACTIVATION
( 
    STATUS = ON,
    PROCEDURE_NAME = auditLog.usp_AuditProcessing,
    MAX_QUEUE_READERS = 10,
    EXECUTE AS 'Kreta_tech_user'
);

GO

