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

SET NUMERIC_ROUNDABORT OFF;


DECLARE @Command NVARCHAR(max),
  @table_name NVARCHAR(max) = 'T_TANULOERTEKELES'

select @Command = N'ALTER TABLE ' + @table_name + N' drop constraint ' + d.name
 from sys.tables t
  join    sys.default_constraints d
   on d.parent_object_id = t.object_id
  join    sys.columns c
   on c.object_id = t.object_id
    and c.column_id = d.parent_column_id
 where t.name = @table_name
  and c.name = N'C_LATTAMOZVA'

execute (@Command)

GO

DECLARE @Command NVARCHAR(max),
  @table_name NVARCHAR(max) = 'T_TANULOESEMENY'

select @Command = N'ALTER TABLE ' + @table_name + N' drop constraint ' + d.name
 from sys.tables t
  join    sys.default_constraints d
   on d.parent_object_id = t.object_id
  join    sys.columns c
   on c.object_id = t.object_id
    and c.column_id = d.parent_column_id
 where t.name = @table_name
  and c.name = N'C_LATTAMOZVA'

execute (@Command)

GO
PRINT N'Dropping [dbo].[T_TANULOERTEKELESLATTAMOZAS].[I_T_4028060_4028058]...';


GO
DROP INDEX [I_T_4028060_4028058]
    ON [dbo].[T_TANULOERTEKELESLATTAMOZAS];


GO
PRINT N'Dropping [dbo].[T_TANULOERTEKELESLATTAMOZAS].[I_T_4028054_4028052]...';


GO
DROP INDEX [I_T_4028054_4028052]
    ON [dbo].[T_TANULOERTEKELESLATTAMOZAS];


GO
PRINT N'Dropping [dbo].[T_TANULOESEMENYLATTAMOZAS].[I_T_4028056_4028055]...';


GO
DROP INDEX [I_T_4028056_4028055]
    ON [dbo].[T_TANULOESEMENYLATTAMOZAS];


GO
PRINT N'Dropping [dbo].[T_TANULOESEMENYLATTAMOZAS].[I_T_4028078_4028076]...';


GO
DROP INDEX [I_T_4028078_4028076]
    ON [dbo].[T_TANULOESEMENYLATTAMOZAS];


GO
PRINT N'Dropping [dbo].[T_TANULOESEMENYLATTAMOZAS].[I_T_4028075_4028073]...';


GO
DROP INDEX [I_T_4028075_4028073]
    ON [dbo].[T_TANULOESEMENYLATTAMOZAS];


GO
PRINT N'Dropping [dbo].[T_TANULOMULASZTASLATTAMOZAS].[I_T_4028063_4028061]...';


GO
DROP INDEX [I_T_4028063_4028061]
    ON [dbo].[T_TANULOMULASZTASLATTAMOZAS];


GO
PRINT N'Dropping [dbo].[T_TANULOMULASZTASLATTAMOZAS].[I_T_4028066_4028064]...';


GO
DROP INDEX [I_T_4028066_4028064]
    ON [dbo].[T_TANULOMULASZTASLATTAMOZAS];


GO
PRINT N'Dropping [dbo].[T_TANULOERTEKELESLATTAMOZAS].[I_T_4028072_4028070]...';


GO
DROP INDEX [I_T_4028072_4028070]
    ON [dbo].[T_TANULOERTEKELESLATTAMOZAS];


GO
PRINT N'Dropping [dbo].[T_TANULOMULASZTASLATTAMOZAS].[I_T_4028069_4028067]...';


GO
DROP INDEX [I_T_4028069_4028067]
    ON [dbo].[T_TANULOMULASZTASLATTAMOZAS];


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELESLATTAMOZAS] DROP CONSTRAINT [FK_4028060_4028058];


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELESLATTAMOZAS] DROP CONSTRAINT [FK_4028054_4028052];


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENYLATTAMOZAS] DROP CONSTRAINT [FK_4028056_4028055];


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENYLATTAMOZAS] DROP CONSTRAINT [FK_4028078_4028076];


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENYLATTAMOZAS] DROP CONSTRAINT [FK_4028075_4028073];


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


GO
ALTER TABLE [dbo].[T_TANULOMULASZTASLATTAMOZAS] DROP CONSTRAINT [FK_4028063_4028061];


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


GO
ALTER TABLE [dbo].[T_TANULOMULASZTASLATTAMOZAS] DROP CONSTRAINT [FK_4028066_4028064];


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELESLATTAMOZAS] DROP CONSTRAINT [FK_4028072_4028070];


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


GO
ALTER TABLE [dbo].[T_TANULOMULASZTASLATTAMOZAS] DROP CONSTRAINT [FK_4028069_4028067];


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


GO
ALTER TABLE [dbo].[T_ORAREND] DROP COLUMN [C_ORARENDIORAGROUPID];


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


GO
ALTER TABLE [dbo].[T_TANORA] DROP COLUMN [C_ORARENDIORAGROUPID];


GO
ALTER TABLE [dbo].[T_TANORA] ALTER COLUMN [C_NAPLOZASIDEJE] DATETIME NULL;


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP COLUMN [C_LATTAMOZVA];


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELESLATTAMOZAS] DROP COLUMN [C_TANULOID];


GO
ALTER TABLE [dbo].[T_TANULOERTEKELESLATTAMOZAS] ALTER COLUMN [C_GONDVISELOID] INT NOT NULL;

ALTER TABLE [dbo].[T_TANULOERTEKELESLATTAMOZAS] ALTER COLUMN [C_LATTAMOZASDATUMA] DATETIME NOT NULL;

ALTER TABLE [dbo].[T_TANULOERTEKELESLATTAMOZAS] ALTER COLUMN [C_TANULOERTEKELESID] INT NOT NULL;


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELESLATTAMOZAS].[I_T_4028060_4028058]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4028060_4028058]
    ON [dbo].[T_TANULOERTEKELESLATTAMOZAS]([C_GONDVISELOID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELESLATTAMOZAS].[I_T_4028054_4028052]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4028054_4028052]
    ON [dbo].[T_TANULOERTEKELESLATTAMOZAS]([C_TANULOERTEKELESID] ASC);


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENY] DROP COLUMN [C_LATTAMOZVA];


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENYLATTAMOZAS] ALTER COLUMN [C_GONDVISELOID] INT NOT NULL;

ALTER TABLE [dbo].[T_TANULOESEMENYLATTAMOZAS] ALTER COLUMN [C_LATTAMOZASDATUMA] DATETIME NOT NULL;

ALTER TABLE [dbo].[T_TANULOESEMENYLATTAMOZAS] ALTER COLUMN [C_TANULOESEMENYID] INT NOT NULL;

ALTER TABLE [dbo].[T_TANULOESEMENYLATTAMOZAS] ALTER COLUMN [C_TANULOID] INT NOT NULL;


GO
PRINT N'Creating [dbo].[T_TANULOESEMENYLATTAMOZAS].[I_T_4028056_4028055]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4028056_4028055]
    ON [dbo].[T_TANULOESEMENYLATTAMOZAS]([C_GONDVISELOID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOESEMENYLATTAMOZAS].[I_T_4028078_4028076]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4028078_4028076]
    ON [dbo].[T_TANULOESEMENYLATTAMOZAS]([C_TANULOESEMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOESEMENYLATTAMOZAS].[I_T_4028075_4028073]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4028075_4028073]
    ON [dbo].[T_TANULOESEMENYLATTAMOZAS]([C_TANULOID] ASC);


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


GO
ALTER TABLE [dbo].[T_TANULOMULASZTASLATTAMOZAS] DROP COLUMN [C_TANULOID];


GO
ALTER TABLE [dbo].[T_TANULOMULASZTASLATTAMOZAS] ALTER COLUMN [C_GONDVISELOID] INT NOT NULL;

ALTER TABLE [dbo].[T_TANULOMULASZTASLATTAMOZAS] ALTER COLUMN [C_LATTAMOZASDATUMA] DATETIME NOT NULL;

ALTER TABLE [dbo].[T_TANULOMULASZTASLATTAMOZAS] ALTER COLUMN [C_TANULOMULASZTASID] INT NOT NULL;


GO
PRINT N'Creating [dbo].[T_TANULOMULASZTASLATTAMOZAS].[I_T_4028063_4028061]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4028063_4028061]
    ON [dbo].[T_TANULOMULASZTASLATTAMOZAS]([C_GONDVISELOID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOMULASZTASLATTAMOZAS].[I_T_4028066_4028064]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4028066_4028064]
    ON [dbo].[T_TANULOMULASZTASLATTAMOZAS]([C_TANULOMULASZTASID] ASC);


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELESLATTAMOZAS] WITH NOCHECK
    ADD CONSTRAINT [FK_4028060_4028058] FOREIGN KEY ([C_GONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELESLATTAMOZAS] WITH NOCHECK
    ADD CONSTRAINT [FK_4028054_4028052] FOREIGN KEY ([C_TANULOERTEKELESID]) REFERENCES [dbo].[T_TANULOERTEKELES] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENYLATTAMOZAS] WITH NOCHECK
    ADD CONSTRAINT [FK_4028056_4028055] FOREIGN KEY ([C_GONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENYLATTAMOZAS] WITH NOCHECK
    ADD CONSTRAINT [FK_4028078_4028076] FOREIGN KEY ([C_TANULOESEMENYID]) REFERENCES [dbo].[T_TANULOESEMENY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENYLATTAMOZAS] WITH NOCHECK
    ADD CONSTRAINT [FK_4028075_4028073] FOREIGN KEY ([C_TANULOID]) REFERENCES [dbo].[T_TANULO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANULOMULASZTASLATTAMOZAS] WITH NOCHECK
    ADD CONSTRAINT [FK_4028063_4028061] FOREIGN KEY ([C_GONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANULOMULASZTASLATTAMOZAS] WITH NOCHECK
    ADD CONSTRAINT [FK_4028066_4028064] FOREIGN KEY ([C_TANULOMULASZTASID]) REFERENCES [dbo].[T_TANULOMULASZTAS] ([ID]);


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELESLATTAMOZAS] WITH CHECK CHECK CONSTRAINT [FK_4028060_4028058];

ALTER TABLE [dbo].[T_TANULOERTEKELESLATTAMOZAS] WITH CHECK CHECK CONSTRAINT [FK_4028054_4028052];

ALTER TABLE [dbo].[T_TANULOESEMENYLATTAMOZAS] WITH CHECK CHECK CONSTRAINT [FK_4028056_4028055];

ALTER TABLE [dbo].[T_TANULOESEMENYLATTAMOZAS] WITH CHECK CHECK CONSTRAINT [FK_4028078_4028076];

ALTER TABLE [dbo].[T_TANULOESEMENYLATTAMOZAS] WITH CHECK CHECK CONSTRAINT [FK_4028075_4028073];

ALTER TABLE [dbo].[T_TANULOMULASZTASLATTAMOZAS] WITH CHECK CHECK CONSTRAINT [FK_4028063_4028061];

ALTER TABLE [dbo].[T_TANULOMULASZTASLATTAMOZAS] WITH CHECK CHECK CONSTRAINT [FK_4028066_4028064];


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_TANULOERTEKELESLATTAMOZAS', 'T_TANULOERTEKELESLATTAMOZAS_OSSZES',
	'T_TANULOESEMENYLATTAMOZAS', 'T_TANULOESEMENYLATTAMOZAS_OSSZES',
	'T_TANULOMULASZTASLATTAMOZAS', 'T_TANULOMULASZTASLATTAMOZAS_OSSZES',
	'T_TANORA', 'T_TANORA_OSSZES',
	'T_ORAREND', 'T_ORAREND_OSSZES',
	'T_TANULOERTEKELES', 'T_TANULOERTEKELES_OSSZES',
	'T_TANULOESEMENY', 'T_TANULOESEMENY_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


