﻿-- ================================================================================================================================== 
-- CoreAPI DB változások 
-- ================================================================================================================================== 
IF COL_LENGTH('dbo.T_INTEZMENYADATOK', 'CORE_CIMID') IS NULL BEGIN
  ALTER TABLE T_INTEZMENYADATOK
  ADD CORE_CIMID INT NULL
END
GO

IF COL_LENGTH('dbo.T_MUKODESIHELY', 'CORE_CIMID') IS NULL BEGIN
  ALTER TABLE T_MUKODESIHELY
  ADD CORE_CIMID INT NULL
END
GO

IF COL_LENGTH('dbo.T_INTEZMENYADATOK', 'CORE_ELERHETOSEGID') IS NULL BEGIN
  ALTER TABLE T_INTEZMENYADATOK
  ADD CORE_ELERHETOSEGID INT NULL
END
GO

IF COL_LENGTH('dbo.T_MUKODESIHELY', 'CORE_ELERHETOSEGID') IS NULL BEGIN
  ALTER TABLE T_MUKODESIHELY
  ADD CORE_ELERHETOSEGID INT NULL
END
GO

DROP SEQUENCE IF EXISTS dbo.SeqCoreMunkaugyiAdatok
GO

CREATE SEQUENCE dbo.SeqCoreMunkaugyiAdatok
  AS INT
  START WITH 1
  INCREMENT BY 1
GO

IF COL_LENGTH('dbo.T_MUNKAUGYIADATOK', 'CORE_ID') IS NULL BEGIN
  ALTER TABLE T_MUNKAUGYIADATOK
  ADD CORE_ID INT NOT NULL DEFAULT NEXT VALUE FOR dbo.SeqCoreMunkaugyiAdatok
END
GO

IF COL_LENGTH('dbo.T_TOVABBIMUNKAUGYIADATOK', 'CORE_ID') IS NULL BEGIN
  ALTER TABLE T_TOVABBIMUNKAUGYIADATOK
  ADD CORE_ID INT NOT NULL DEFAULT NEXT VALUE FOR dbo.SeqCoreMunkaugyiAdatok
END
GO

IF COL_LENGTH('dbo.T_KEPESITES_VEGZETTSEG', 'CORE_ID') IS NULL BEGIN
  ALTER TABLE T_KEPESITES_VEGZETTSEG
  ADD CORE_ID INT NOT NULL IDENTITY(1,1)
END
GO

IF COL_LENGTH('dbo.T_FELHASZNALO_SZEREPKOR', 'CORE_ID') IS NULL BEGIN
  ALTER TABLE T_FELHASZNALO_SZEREPKOR
  ADD CORE_ID INT NOT NULL IDENTITY(1,1)
END
GO

IF COL_LENGTH('dbo.T_DICTIONARYITEMBASE', 'CORE_KATEGORIAID') IS NULL BEGIN
  ALTER TABLE T_DICTIONARYITEMBASE
  ADD CORE_KATEGORIAID INT NOT NULL DEFAULT 0
END
GO

IF COL_LENGTH('dbo.T_CIM', 'CORE_PUBLIKUS') IS NULL BEGIN
  ALTER TABLE T_CIM
  ADD CORE_PUBLIKUS CHAR(1) NOT NULL DEFAULT 'F'
END
GO

-- Struktúrális változás miatt szükséges, hogy nullable legyen. 
-- Tranzakcióban kezeljük, nem fog tőlünk hibás rekord keletkezni.
ALTER TABLE dbo.T_GONDVISELO
ALTER COLUMN C_TANULOID INT NULL
GO

ALTER TABLE dbo.T_GONDVISELO
ALTER COLUMN C_ROKONSAGFOKA INT NULL
GO

ALTER TABLE dbo.T_FELHASZNALOBELEPES
ALTER COLUMN C_FELHASZNALOID INT NULL
GO

ALTER TABLE dbo.T_FELHASZNALOBELEPESTORTENET
ALTER COLUMN C_FELHASZNALOID INT NULL
GO

-- ================================================================================================================================== 
-- Előző Tanév Rekord ID Betétele, ha még nincs 
-- ================================================================================================================================== 
DECLARE @sql nvarchar(max) = '';
SELECT  @sql += 'ALTER TABLE dbo.' +  T.TABLE_NAME + '  ADD ELOZOTANEVIREKORDID INT;' + char(13) + char(10)
FROM INFORMATION_SCHEMA.TABLES T
WHERE NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS C WHERE C.TABLE_NAME = T.TABLE_NAME AND C.COLUMN_NAME = 'ELOZOTANEVIREKORDID')
  AND T.TABLE_TYPE = 'BASE TABLE' 
  AND TABLE_SCHEMA = 'dbo';
 
EXEC sp_executesql @sql;  
GO

DECLARE @sql nvarchar(max) = '';
SELECT  @sql += 'ALTER TABLE dbo.' +  T.TABLE_NAME + '  ADD ELOZOTANEVIREKORDID INT;' + char(13) + char(10)
FROM INFORMATION_SCHEMA.TABLES T
WHERE EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS C WHERE C.TABLE_SCHEMA = T.TABLE_SCHEMA AND C.TABLE_NAME = T.TABLE_NAME AND C.COLUMN_NAME = 'C_INTEZMENYID')
  AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS C WHERE C.TABLE_SCHEMA = T.TABLE_SCHEMA AND C.TABLE_NAME = T.TABLE_NAME AND C.COLUMN_NAME = 'ELOZOTANEVIREKORDID')
  AND T.TABLE_TYPE = 'BASE TABLE'
  AND TABLE_SCHEMA = 'dbo';

--PRINT @sql;  
EXEC sp_executesql @sql;  
GO

DECLARE @sql nvarchar(max) = '';
SELECT  @sql += 'ALTER TABLE dbo.' +  T.TABLE_NAME + '  ADD ELOZOTANEVIREKORDIDA INT;' + char(13) + char(10)
FROM INFORMATION_SCHEMA.TABLES T
WHERE EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS C WHERE C.TABLE_SCHEMA = T.TABLE_SCHEMA AND C.TABLE_NAME = T.TABLE_NAME AND C.COLUMN_NAME = 'C_ALINTEZMENYID')
  AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS C WHERE C.TABLE_SCHEMA = T.TABLE_SCHEMA AND C.TABLE_NAME = T.TABLE_NAME AND C.COLUMN_NAME = 'ELOZOTANEVIREKORDIDA')
  AND T.TABLE_TYPE = 'BASE TABLE'
  AND TABLE_SCHEMA = 'dbo';

--PRINT @sql;   
EXEC sp_executesql @sql;  
GO

