﻿UPDATE T_FELHASZNALO SET C_NEVSORREND = 'T' WHERE C_NEVSORREND IS NULL;
GO

UPDATE u SET 
TOROLT='T'
,C_FELHASZNALOID = 
	COALESCE
	(
		(SELECT TOP 1 ID FROM T_FELHASZNALO f WHERE C_SZULETESINEV = 'Rendszer Üzenet' AND f.C_TANEVID = u.C_TANEVID)
		,(SELECT TOP 1 ID FROM T_FELHASZNALO f WHERE C_SZULETESINEV = 'Kréta Adminisztrátor' AND f.C_TANEVID = u.C_TANEVID)
		,(SELECT TOP 1 ID FROM T_FELHASZNALO f WHERE f.C_TANEVID = u.C_TANEVID ORDER BY ID
		)
	)
FROM T_TELEFON u WHERE C_FELHASZNALOID IS NULL
GO

UPDATE u SET 
TOROLT='T'
,C_FELHASZNALOID = 
	COALESCE
	(
		(SELECT TOP 1 ID FROM T_FELHASZNALO f WHERE C_SZULETESINEV = 'Rendszer Üzenet' AND f.C_TANEVID = u.C_TANEVID)
		,(SELECT TOP 1 ID FROM T_FELHASZNALO f WHERE C_SZULETESINEV = 'Kréta Adminisztrátor' AND f.C_TANEVID = u.C_TANEVID)
		,(SELECT TOP 1 ID FROM T_FELHASZNALO f WHERE f.C_TANEVID = u.C_TANEVID ORDER BY ID
		)
	)
FROM T_EMAIL u WHERE C_FELHASZNALOID IS NULL
GO

UPDATE u SET 
TOROLT='T'
,C_FELHASZNALOID = 
	COALESCE
	(
		(SELECT TOP 1 ID FROM T_FELHASZNALO f WHERE C_SZULETESINEV = 'Rendszer Üzenet' AND f.C_TANEVID = u.C_TANEVID)
		,(SELECT TOP 1 ID FROM T_FELHASZNALO f WHERE C_SZULETESINEV = 'Kréta Adminisztrátor' AND f.C_TANEVID = u.C_TANEVID)
		,(SELECT TOP 1 ID FROM T_FELHASZNALO f WHERE f.C_TANEVID = u.C_TANEVID ORDER BY ID
		)
	)
FROM T_CIM u WHERE C_FELHASZNALOID IS NULL
GO

UPDATE T_EMAIL SET C_EMAILTIPUSA = 1024 WHERE C_EMAILTIPUSA IS NULL;
UPDATE T_CIM SET C_CIMTIPUSA = 906 WHERE C_CIMTIPUSA IS NULL;
UPDATE T_TELEFON SET C_TELEFONTIPUSA = 1019 WHERE C_TELEFONTIPUSA IS NULL;
GO


PRINT N'Dropping Index [dbo].[T_CIM].[NCI_Cim_FelhasznaloId]...';


GO
DROP INDEX [NCI_Cim_FelhasznaloId]
    ON [dbo].[T_CIM];


GO
PRINT N'Dropping Index [dbo].[T_CIM].[NCI_Cim_FelhasznaloId_Gondviseloid_CimTipusa_IntezmenyId]...';


GO
DROP INDEX [NCI_Cim_FelhasznaloId_Gondviseloid_CimTipusa_IntezmenyId]
    ON [dbo].[T_CIM];


GO
PRINT N'Dropping Index [dbo].[T_CIM].[NCU_Cim_FelhasznaloId_GondviseloId_TanevId]...';


GO
DROP INDEX [NCU_Cim_FelhasznaloId_GondviseloId_TanevId]
    ON [dbo].[T_CIM];


GO
PRINT N'Dropping Index [dbo].[T_EMAIL].[NCI_Email_IntezmenyId_TanevId_EmailTipusa]...';


GO
DROP INDEX [NCI_Email_IntezmenyId_TanevId_EmailTipusa]
    ON [dbo].[T_EMAIL];


GO
PRINT N'Dropping Index [dbo].[T_EMAIL].[NCI_Email_FelhasznaloId]...';


GO
DROP INDEX [NCI_Email_FelhasznaloId]
    ON [dbo].[T_EMAIL];


GO
PRINT N'Dropping Index [dbo].[T_EMAIL].[NCI_Email_FelhasznaloId_GondviseloId_IntezmenyId]...';


GO
DROP INDEX [NCI_Email_FelhasznaloId_GondviseloId_IntezmenyId]
    ON [dbo].[T_EMAIL];


GO
PRINT N'Dropping Index [dbo].[T_EMAIL].[NCU_Email_FelhasznaloId_GondviseloId_TanevId]...';


GO
DROP INDEX [NCU_Email_FelhasznaloId_GondviseloId_TanevId]
    ON [dbo].[T_EMAIL];


GO
PRINT N'Dropping Index [dbo].[T_EMAIL].[NCU_Email_FelhasznaloId_GondviseloId_TanevId_Telefonszam]...';


GO
DROP INDEX [NCU_Email_FelhasznaloId_GondviseloId_TanevId_Telefonszam]
    ON [dbo].[T_EMAIL];


GO
PRINT N'Dropping Index [dbo].[T_EMAIL].[NCU_Email_FelhasznaloId_TanevId]...';


GO
DROP INDEX [NCU_Email_FelhasznaloId_TanevId]
    ON [dbo].[T_EMAIL];


GO
PRINT N'Dropping Index [dbo].[T_FELHASZNALO].[NCI_Felhasznalo_IntezmenyId_Id]...';


GO
DROP INDEX [NCI_Felhasznalo_IntezmenyId_Id]
    ON [dbo].[T_FELHASZNALO];


GO
PRINT N'Dropping Index [dbo].[T_TELEFON].[NCI_Telefon_FelhasznaloId]...';


GO
DROP INDEX [NCI_Telefon_FelhasznaloId]
    ON [dbo].[T_TELEFON];


GO
PRINT N'Dropping Index [dbo].[T_TELEFON].[NCI_Telefon_FelhasznaloId_GondviseloId_IntezmenyId]...';


GO
DROP INDEX [NCI_Telefon_FelhasznaloId_GondviseloId_IntezmenyId]
    ON [dbo].[T_TELEFON];


GO
PRINT N'Dropping Index [dbo].[T_TELEFON].[NCU_Telefon_FelhasznaloId_GondviseloId_TanevId]...';


GO
DROP INDEX [NCU_Telefon_FelhasznaloId_GondviseloId_TanevId]
    ON [dbo].[T_TELEFON];


GO
PRINT N'Dropping Index [dbo].[T_TELEFON].[NCU_Telefon_FelhasznaloId_GondviseloId_TanevId_Telefonszam]...';


GO
DROP INDEX [NCU_Telefon_FelhasznaloId_GondviseloId_TanevId_Telefonszam]
    ON [dbo].[T_TELEFON];


GO
PRINT N'Dropping Index [dbo].[T_TELEFON].[NCU_Telefon_FelhasznaloId_TanevId]...';


GO
DROP INDEX [NCU_Telefon_FelhasznaloId_TanevId]
    ON [dbo].[T_TELEFON];


GO
PRINT N'Dropping Default Constraint unnamed constraint on [dbo].[T_FELHASZNALO]...';


GO
EXEC dev.uspDropDefaultConstraint @pTableName = 'T_FELHASZNALO', @pColumnName = 'C_NEVSORREND'
EXEC dev.uspDropDefaultConstraint @pTableName = 'T_FELHASZNALO', @pColumnName = 'C_SZULETESINEVSORREND'
EXEC dev.uspDropDefaultConstraint @pTableName = 'T_FELHASZNALO', @pColumnName = 'C_ANYJANEVESORREND'
EXEC dev.uspDropDefaultConstraint @pTableName = 'T_CIM', @pColumnName = 'C_CIMTIPUSA'
EXEC dev.uspDropDefaultConstraint @pTableName = 'T_EMAIL', @pColumnName = 'C_EMAILTIPUSA'
EXEC dev.uspDropDefaultConstraint @pTableName = 'T_TELEFON', @pColumnName = 'C_TELEFONTIPUSA'

GO
PRINT N'Dropping Default Constraint unnamed constraint on [dbo].[T_FELHASZNALO]...';




GO
PRINT N'Dropping Default Constraint unnamed constraint on [dbo].[T_FELHASZNALO]...';



GO
PRINT N'Dropping Foreign Key [dbo].[FK_Cim_FelhasznaloId_REF_Felhasznalo]...';


GO
ALTER TABLE [dbo].[T_CIM] DROP CONSTRAINT [FK_Cim_FelhasznaloId_REF_Felhasznalo];


GO
PRINT N'Dropping Foreign Key [dbo].[FK_Email_EmailTipusa_IntezmenyId_TanevId_REF_EmailTipus]...';


GO
ALTER TABLE [dbo].[T_EMAIL] DROP CONSTRAINT [FK_Email_EmailTipusa_IntezmenyId_TanevId_REF_EmailTipus];


GO
PRINT N'Dropping Foreign Key [dbo].[FK_Email_FelhasznaloId_REF_Felhasznalo]...';


GO
ALTER TABLE [dbo].[T_EMAIL] DROP CONSTRAINT [FK_Email_FelhasznaloId_REF_Felhasznalo];


GO
PRINT N'Dropping Foreign Key [dbo].[FK_Telefon_FelhasznaloId_REF_Felhasznalo]...';


GO
ALTER TABLE [dbo].[T_TELEFON] DROP CONSTRAINT [FK_Telefon_FelhasznaloId_REF_Felhasznalo];


GO
PRINT N'Altering Table [dbo].[T_CIM]...';


GO
ALTER TABLE [dbo].[T_CIM] ALTER COLUMN [C_FELHASZNALOID] INT NOT NULL;


GO
PRINT N'Creating Index [dbo].[T_CIM].[NCI_Cim_FelhasznaloId]...';


GO
CREATE NONCLUSTERED INDEX [NCI_Cim_FelhasznaloId]
    ON [dbo].[T_CIM]([C_FELHASZNALOID] ASC);


GO
PRINT N'Creating Index [dbo].[T_CIM].[NCI_Cim_FelhasznaloId_Gondviseloid_CimTipusa_IntezmenyId]...';


GO
CREATE NONCLUSTERED INDEX [NCI_Cim_FelhasznaloId_Gondviseloid_CimTipusa_IntezmenyId]
    ON [dbo].[T_CIM]([C_FELHASZNALOID] ASC, [C_GONDVISELOID] ASC, [C_CIMTIPUSA] ASC, [C_INTEZMENYID] ASC)
    INCLUDE([C_AJTO], [C_EMELET], [C_HAZSZAM], [C_IRANYITOSZAM], [C_KOZTERULET], [C_VAROS], [C_ALAPERTELMEZETT], [C_TANEVID], [C_KOZTERULETJELLEGENEV]) WHERE ([TOROLT]='F') WITH (FILLFACTOR = 80);


GO
PRINT N'Creating Index [dbo].[T_CIM].[NCU_Cim_FelhasznaloId_GondviseloId_TanevId]...';


GO
CREATE UNIQUE NONCLUSTERED INDEX [NCU_Cim_FelhasznaloId_GondviseloId_TanevId]
    ON [dbo].[T_CIM]([C_FELHASZNALOID] ASC, [C_GONDVISELOID] ASC, [C_TANEVID] ASC) WHERE ([TOROLT]='F' AND [C_ALAPERTELMEZETT]='T');


GO
PRINT N'Altering Table [dbo].[T_EMAIL]...';


GO
ALTER TABLE [dbo].[T_EMAIL] ALTER COLUMN [C_EMAILTIPUSA] INT NOT NULL;

ALTER TABLE [dbo].[T_EMAIL] ALTER COLUMN [C_FELHASZNALOID] INT NOT NULL;


GO
PRINT N'Creating Index [dbo].[T_EMAIL].[NCI_Email_IntezmenyId_TanevId_EmailTipusa]...';


GO
CREATE NONCLUSTERED INDEX [NCI_Email_IntezmenyId_TanevId_EmailTipusa]
    ON [dbo].[T_EMAIL]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_EMAILTIPUSA] ASC);


GO
PRINT N'Creating Index [dbo].[T_EMAIL].[NCI_Email_FelhasznaloId]...';


GO
CREATE NONCLUSTERED INDEX [NCI_Email_FelhasznaloId]
    ON [dbo].[T_EMAIL]([C_FELHASZNALOID] ASC);


GO
PRINT N'Creating Index [dbo].[T_EMAIL].[NCI_Email_FelhasznaloId_GondviseloId_IntezmenyId]...';


GO
CREATE NONCLUSTERED INDEX [NCI_Email_FelhasznaloId_GondviseloId_IntezmenyId]
    ON [dbo].[T_EMAIL]([C_FELHASZNALOID] ASC, [C_GONDVISELOID] ASC, [C_INTEZMENYID] ASC)
    INCLUDE([C_EMAILCIM], [C_TANEVID]) WHERE ([TOROLT]='F') WITH (FILLFACTOR = 80);


GO
PRINT N'Creating Index [dbo].[T_EMAIL].[NCU_Email_FelhasznaloId_GondviseloId_TanevId]...';


GO
CREATE UNIQUE NONCLUSTERED INDEX [NCU_Email_FelhasznaloId_GondviseloId_TanevId]
    ON [dbo].[T_EMAIL]([C_FELHASZNALOID] ASC, [C_GONDVISELOID] ASC, [C_TANEVID] ASC) WHERE ([TOROLT]='F' AND [C_ALAPERTELMEZETT]='T');


GO
PRINT N'Creating Index [dbo].[T_EMAIL].[NCU_Email_FelhasznaloId_GondviseloId_TanevId_Telefonszam]...';


GO
CREATE UNIQUE NONCLUSTERED INDEX [NCU_Email_FelhasznaloId_GondviseloId_TanevId_Telefonszam]
    ON [dbo].[T_EMAIL]([C_FELHASZNALOID] ASC, [C_GONDVISELOID] ASC, [C_TANEVID] ASC, [C_EMAILCIM] ASC, [C_ISPUBLIC] ASC) WHERE ([TOROLT]='F');


GO
PRINT N'Creating Index [dbo].[T_EMAIL].[NCU_Email_FelhasznaloId_TanevId]...';


GO
CREATE UNIQUE NONCLUSTERED INDEX [NCU_Email_FelhasznaloId_TanevId]
    ON [dbo].[T_EMAIL]([C_FELHASZNALOID] ASC, [C_TANEVID] ASC) WHERE ([TOROLT]='F' AND [C_ISPUBLIC]='T' AND [C_GONDVISELOID] IS NULL);


GO
PRINT N'Altering Table [dbo].[T_FELHASZNALO]...';


GO
ALTER TABLE [dbo].[T_FELHASZNALO] ALTER COLUMN [C_NEVSORREND] CHAR (1) NOT NULL;


GO
PRINT N'Creating Index [dbo].[T_FELHASZNALO].[NCI_Felhasznalo_IntezmenyId_Id]...';


GO
CREATE NONCLUSTERED INDEX [NCI_Felhasznalo_IntezmenyId_Id]
    ON [dbo].[T_FELHASZNALO]([C_INTEZMENYID] ASC, [ID] ASC)
    INCLUDE([C_NYOMTATASINEV], [C_SZULETESIDATUM], [C_UTONEV], [C_VEZETEKNEV], [C_NEVSORREND], [C_ANYJANEVE], [C_SZULETESIHELY], [C_OKTATASIAZONOSITO]) WHERE ([TOROLT]='F') WITH (FILLFACTOR = 80);


GO
PRINT N'Altering Table [dbo].[T_TELEFON]...';


GO
ALTER TABLE [dbo].[T_TELEFON] ALTER COLUMN [C_FELHASZNALOID] INT NOT NULL;


GO
PRINT N'Creating Index [dbo].[T_TELEFON].[NCI_Telefon_FelhasznaloId]...';


GO
CREATE NONCLUSTERED INDEX [NCI_Telefon_FelhasznaloId]
    ON [dbo].[T_TELEFON]([C_FELHASZNALOID] ASC);


GO
PRINT N'Creating Index [dbo].[T_TELEFON].[NCI_Telefon_FelhasznaloId_GondviseloId_IntezmenyId]...';


GO
CREATE NONCLUSTERED INDEX [NCI_Telefon_FelhasznaloId_GondviseloId_IntezmenyId]
    ON [dbo].[T_TELEFON]([C_FELHASZNALOID] ASC, [C_GONDVISELOID] ASC, [C_INTEZMENYID] ASC)
    INCLUDE([C_TELEFONSZAM], [C_TANEVID]) WHERE ([TOROLT]='F') WITH (FILLFACTOR = 80);


GO
PRINT N'Creating Index [dbo].[T_TELEFON].[NCU_Telefon_FelhasznaloId_GondviseloId_TanevId]...';


GO
CREATE UNIQUE NONCLUSTERED INDEX [NCU_Telefon_FelhasznaloId_GondviseloId_TanevId]
    ON [dbo].[T_TELEFON]([C_FELHASZNALOID] ASC, [C_GONDVISELOID] ASC, [C_TANEVID] ASC) WHERE ([TOROLT]='F' AND [C_ALAPERTELMEZETT]='T');


GO
PRINT N'Creating Index [dbo].[T_TELEFON].[NCU_Telefon_FelhasznaloId_GondviseloId_TanevId_Telefonszam]...';


GO
CREATE UNIQUE NONCLUSTERED INDEX [NCU_Telefon_FelhasznaloId_GondviseloId_TanevId_Telefonszam]
    ON [dbo].[T_TELEFON]([C_FELHASZNALOID] ASC, [C_GONDVISELOID] ASC, [C_TANEVID] ASC, [C_TELEFONSZAM] ASC, [C_ISPUBLIC] ASC) WHERE ([TOROLT]='F');


GO
PRINT N'Creating Index [dbo].[T_TELEFON].[NCU_Telefon_FelhasznaloId_TanevId]...';


GO
CREATE UNIQUE NONCLUSTERED INDEX [NCU_Telefon_FelhasznaloId_TanevId]
    ON [dbo].[T_TELEFON]([C_FELHASZNALOID] ASC, [C_TANEVID] ASC) WHERE ([TOROLT]='F' AND [C_ISPUBLIC]='T' AND [C_GONDVISELOID] IS NULL);


GO
PRINT N'Creating Default Constraint unnamed constraint on [dbo].[T_CIM]...';


GO
ALTER TABLE [dbo].[T_CIM]
    ADD DEFAULT ((906)) FOR [C_CIMTIPUSA];


GO
PRINT N'Creating Default Constraint unnamed constraint on [dbo].[T_EMAIL]...';


GO
ALTER TABLE [dbo].[T_EMAIL]
    ADD DEFAULT ((1024)) FOR [C_EMAILTIPUSA];


GO
PRINT N'Creating Default Constraint unnamed constraint on [dbo].[T_FELHASZNALO]...';


GO
ALTER TABLE [dbo].[T_FELHASZNALO]
    ADD DEFAULT ('T') FOR [C_ANYJANEVESORREND];


GO
PRINT N'Creating Default Constraint unnamed constraint on [dbo].[T_FELHASZNALO]...';


GO
ALTER TABLE [dbo].[T_FELHASZNALO]
    ADD DEFAULT ('T') FOR [C_SZULETESINEVSORREND];


GO
PRINT N'Creating Default Constraint unnamed constraint on [dbo].[T_FELHASZNALO]...';


GO
ALTER TABLE [dbo].[T_FELHASZNALO]
    ADD DEFAULT ('T') FOR [C_NEVSORREND];


GO
PRINT N'Creating Default Constraint unnamed constraint on [dbo].[T_TELEFON]...';


GO
ALTER TABLE [dbo].[T_TELEFON]
    ADD DEFAULT ((1019)) FOR [C_TELEFONTIPUSA];


GO
PRINT N'Creating Foreign Key [dbo].[FK_Cim_FelhasznaloId_REF_Felhasznalo]...';


GO
ALTER TABLE [dbo].[T_CIM] WITH NOCHECK
    ADD CONSTRAINT [FK_Cim_FelhasznaloId_REF_Felhasznalo] FOREIGN KEY ([C_FELHASZNALOID]) REFERENCES [dbo].[T_FELHASZNALO] ([ID]);


GO
PRINT N'Creating Foreign Key [dbo].[FK_Email_EmailTipusa_IntezmenyId_TanevId_REF_EmailTipus]...';


GO
ALTER TABLE [dbo].[T_EMAIL] WITH NOCHECK
    ADD CONSTRAINT [FK_Email_EmailTipusa_IntezmenyId_TanevId_REF_EmailTipus] FOREIGN KEY ([C_EMAILTIPUSA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_EMAILTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


GO
PRINT N'Creating Foreign Key [dbo].[FK_Email_FelhasznaloId_REF_Felhasznalo]...';


GO
ALTER TABLE [dbo].[T_EMAIL] WITH NOCHECK
    ADD CONSTRAINT [FK_Email_FelhasznaloId_REF_Felhasznalo] FOREIGN KEY ([C_FELHASZNALOID]) REFERENCES [dbo].[T_FELHASZNALO] ([ID]);


GO
PRINT N'Creating Foreign Key [dbo].[FK_Telefon_FelhasznaloId_REF_Felhasznalo]...';


GO
ALTER TABLE [dbo].[T_TELEFON] WITH NOCHECK
    ADD CONSTRAINT [FK_Telefon_FelhasznaloId_REF_Felhasznalo] FOREIGN KEY ([C_FELHASZNALOID]) REFERENCES [dbo].[T_FELHASZNALO] ([ID]);


GO
ALTER TABLE [dbo].[T_CIM] WITH CHECK CHECK CONSTRAINT [FK_Cim_FelhasznaloId_REF_Felhasznalo];

ALTER TABLE [dbo].[T_EMAIL] WITH CHECK CHECK CONSTRAINT [FK_Email_EmailTipusa_IntezmenyId_TanevId_REF_EmailTipus];

ALTER TABLE [dbo].[T_EMAIL] WITH CHECK CHECK CONSTRAINT [FK_Email_FelhasznaloId_REF_Felhasznalo];

ALTER TABLE [dbo].[T_TELEFON] WITH CHECK CHECK CONSTRAINT [FK_Telefon_FelhasznaloId_REF_Felhasznalo];





EXEC dev.uspCreateSchemaViews 'T_CIM,T_EMAIL,T_FELHASZNALO,T_TELEFON'
GO

EXEC dev.sp_Global_GenerateAsyncAuditTriggerAll
GO


GO
PRINT N'Update complete.';

GO
