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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_4026116_4026121];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_4026116_4026122];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_4026116_4026343];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_4026116_4026352];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_4026116_4026365];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_4026125_4026124];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_4026129_4026127];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_402611602_402611600];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_402611605_402611603];


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_MUNKAUGYIADATOK] (
    [ID]                            INT             IDENTITY (1, 1) NOT NULL,
    [C_ALKALMAZASKEZDETE]           DATETIME        NULL,
    [C_ALKALMAZASMEGSZUNESE]        DATETIME        NULL,
    [C_KOTELEZOORASZAM]             NUMERIC (10, 2) NOT NULL,
    [C_MUNKAIDOKEDVEZMENYORASZAM]   INT             NULL,
    [C_MUNKAKORTIPUSA]              INT             NULL,
    [C_MUNKAVISZONYTIPUSA]          INT             NULL,
    [C_BETOLTETLENALLASHELY]        CHAR (1)        DEFAULT ('F') NOT NULL,
    [C_MUNKAIDOKEDVEZMENYOKASZOVEG] NVARCHAR (MAX)  NULL,
    [C_TARTOSHELYETTESITES]         CHAR (1)        DEFAULT ('F') NOT NULL,
    [C_PEDAGOGUSSTATUSZA]           INT             NULL,
    [C_BESOROLASIFOKOZAT]           INT             NULL,
    [C_MUNKAIDOKEDVEZMENYOKA]       INT             NULL,
    [C_NYUGDIJAS]                   CHAR (1)        DEFAULT ('F') NOT NULL,
    [C_ISORAADO]                    CHAR (1)        DEFAULT ('F') NULL,
    [C_RESZMUNKAIDOSZAZALEK]        INT             DEFAULT ((100)) NULL,
    [C_FOGLALKOZTATASTIPUS]         INT             NULL,
    [C_FELADATELLATASIHELYID]       INT             NULL,
    [C_ALKALMAZOTTID]               INT             NOT NULL,
    [C_INTEZMENYID]                 INT             NOT NULL,
    [C_TANEVID]                     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_40261161] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_MUNKAUGYIADATOK])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_MUNKAUGYIADATOK] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_MUNKAUGYIADATOK] ([ID], [C_ALKALMAZASKEZDETE], [C_ALKALMAZASMEGSZUNESE], [C_KOTELEZOORASZAM], [C_MUNKAIDOKEDVEZMENYORASZAM], [C_MUNKAKORTIPUSA], [C_MUNKAVISZONYTIPUSA], [C_BETOLTETLENALLASHELY], [C_MUNKAIDOKEDVEZMENYOKASZOVEG], [C_TARTOSHELYETTESITES], [C_PEDAGOGUSSTATUSZA], [C_BESOROLASIFOKOZAT], [C_MUNKAIDOKEDVEZMENYOKA], [C_NYUGDIJAS], [C_ISORAADO], [C_FELADATELLATASIHELYID], [C_ALKALMAZOTTID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR])
        SELECT   [ID],
                 [C_ALKALMAZASKEZDETE],
                 [C_ALKALMAZASMEGSZUNESE],
                 [C_KOTELEZOORASZAM],
                 [C_MUNKAIDOKEDVEZMENYORASZAM],
                 [C_MUNKAKORTIPUSA],
                 [C_MUNKAVISZONYTIPUSA],
                 [C_BETOLTETLENALLASHELY],
                 [C_MUNKAIDOKEDVEZMENYOKASZOVEG],
                 [C_TARTOSHELYETTESITES],
                 [C_PEDAGOGUSSTATUSZA],
                 [C_BESOROLASIFOKOZAT],
                 [C_MUNKAIDOKEDVEZMENYOKA],
                 [C_NYUGDIJAS],
                 [C_ISORAADO],
                 [C_FELADATELLATASIHELYID],
                 [C_ALKALMAZOTTID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR]
        FROM     [dbo].[T_MUNKAUGYIADATOK]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_MUNKAUGYIADATOK] OFF;
    END

DROP TABLE [dbo].[T_MUNKAUGYIADATOK];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_MUNKAUGYIADATOK]', N'T_MUNKAUGYIADATOK';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40261161]', N'PK_T_4026116', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_4026116_4026121]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026116_4026121]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_MUNKAKORTIPUSA] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_4026116_4026122]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026116_4026122]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_MUNKAVISZONYTIPUSA] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_4026116_4026343]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026116_4026343]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_PEDAGOGUSSTATUSZA] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_4026116_4026352]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026116_4026352]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_BESOROLASIFOKOZAT] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_4026116_4026365]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026116_4026365]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_MUNKAIDOKEDVEZMENYOKA] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_4026116_4027631]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026116_4027631]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_FOGLALKOZTATASTIPUS] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_4026125_4026124]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026125_4026124]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_FELADATELLATASIHELYID] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_4026129_4026127]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026129_4026127]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_ALKALMAZOTTID] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_402611602_402611600]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402611602_402611600]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_402611605_402611603]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402611605_402611603]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_TANEVID] ASC);


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


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


GO
PRINT N'Creating [dbo].[T_FOGLALKOZTATASTIPUSA].[I_T_402762902_402762900]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402762902_402762900]
    ON [dbo].[T_FOGLALKOZTATASTIPUSA]([C_ALINTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_FOGLALKOZTATASTIPUSA].[I_T_402762905_402762903]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402762905_402762903]
    ON [dbo].[T_FOGLALKOZTATASTIPUSA]([C_ALTANEVID] ASC);


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


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


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026116_4026121] FOREIGN KEY ([C_MUNKAKORTIPUSA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_MUNKAKORTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026116_4026122] FOREIGN KEY ([C_MUNKAVISZONYTIPUSA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_MUNKAVISZONYTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026116_4026343] FOREIGN KEY ([C_PEDAGOGUSSTATUSZA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_PEDAGOGUSSTATUSZ] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026116_4026352] FOREIGN KEY ([C_BESOROLASIFOKOZAT], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_BESOROLASIFOKOZATTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026116_4026365] FOREIGN KEY ([C_MUNKAIDOKEDVEZMENYOKA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_MUNKAIDOKEDVEZMENYOKA] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026125_4026124] FOREIGN KEY ([C_FELADATELLATASIHELYID]) REFERENCES [dbo].[T_FELADATELLATASIHELY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026129_4026127] FOREIGN KEY ([C_ALKALMAZOTTID]) REFERENCES [dbo].[T_ALKALMAZOTT] ([ID]);


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026116_4027631] FOREIGN KEY ([C_FOGLALKOZTATASTIPUS], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_FOGLALKOZTATASTIPUSA] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


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


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


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


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


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


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


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

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_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026116_4026121];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026116_4026122];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026116_4026343];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026116_4026352];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026116_4026365];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026125_4026124];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026129_4026127];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_402611602_402611600];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_402611605_402611603];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026116_4027631];

ALTER TABLE [dbo].[T_FOGLALKOZTATASTIPUSA] WITH CHECK CHECK CONSTRAINT [FK_T_21038_4027630];

ALTER TABLE [dbo].[T_FOGLALKOZTATASTIPUSA] WITH CHECK CHECK CONSTRAINT [FK_402762902_402762900];

ALTER TABLE [dbo].[T_FOGLALKOZTATASTIPUSA] WITH CHECK CHECK CONSTRAINT [FK_402762905_402762903];


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_MUNKAUGYIADATOK', 'T_MUNKAUGYIADATOK_OSSZES',
  'T_FOGLALKOZTATASTIPUSA', 'T_FOGLALKOZTATASTIPUSA_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
