﻿/*Töröltre állítjuk a törölt adatszótarelemhey tartozó altábla elemét.*/

   ;DISABLE TRIGGER ALL ON dbo.T_DICTIONARYITEMBASENYELV;
    UPDATE d SET 
       d.TOROLT = 'T'
      ,d.MODIFIER = 0
      ,LASTCHANGED = GETDATE()
    FROM dbo.T_DICTIONARYITEMBASENYELV d
      INNER JOIN T_DICTIONARYITEMBASE dib ON dib.ID = d.ID AND dib.C_TANEVID = d.C_TANEVID AND dib.TOROLT = 'T'
    WHERE d.TOROLT = 'F';
    ENABLE TRIGGER ALL ON dbo.T_DICTIONARYITEMBASENYELV;
     

    DISABLE TRIGGER ALL ON dbo.T_OKTATASINEVELESIFELADAT;
    UPDATE d SET 
       d.TOROLT = 'T'
      ,d.MODIFIER = 0
      ,LASTCHANGED = GETDATE()
    FROM dbo.T_OKTATASINEVELESIFELADAT d
      INNER JOIN T_DICTIONARYITEMBASE dib ON dib.ID = d.ID AND dib.C_TANEVID = d.C_ALTANEVID AND dib.TOROLT = 'T'
    WHERE d.TOROLT = 'F';
    ENABLE TRIGGER ALL ON dbo.T_OKTATASINEVELESIFELADAT;
     

    DISABLE TRIGGER ALL ON dbo.T_OKTNEVELESIKATEGORIA;
    UPDATE d SET 
       d.TOROLT = 'T'
      ,d.MODIFIER = 0
      ,LASTCHANGED = GETDATE()
    FROM dbo.T_OKTNEVELESIKATEGORIA d
      INNER JOIN T_DICTIONARYITEMBASE dib ON dib.ID = d.ID AND dib.C_TANEVID = d.C_ALTANEVID AND dib.TOROLT = 'T'
    WHERE d.TOROLT = 'F';
    ENABLE TRIGGER ALL ON dbo.T_OKTNEVELESIKATEGORIA;
    

    DECLARE 
       @tableName nvarchar(200)
      ,@sql nvarchar(max)

    DECLARE tableCursor CURSOR FAST_FORWARD LOCAL FOR 
    SELECT DISTINCT OBJECT_NAME(parent_object_id) 
    FROM sys.foreign_key_columns 
    WHERE referenced_object_id = OBJECT_ID('T_DICTIONARYITEMBASE') 
      AND OBJECT_NAME(parent_object_id) NOT IN ('T_OKTNEVELESIKATEGORIA', 'T_OKTATASINEVELESIFELADAT','T_DICTIONARYITEMBASENYELV')

    OPEN tableCursor

    FETCH NEXT FROM tableCursor 
    INTO @tableName

    WHILE @@FETCH_STATUS = 0 BEGIN
      SET @sql = '
        DISABLE TRIGGER ALL ON dbo.' + @tableName + ';
        UPDATE d SET 
           d.TOROLT = ''T''
          ,d.MODIFIER = 0
          ,LASTCHANGED = GETDATE()
        FROM dbo.' + @tableName + ' d
          INNER JOIN T_DICTIONARYITEMBASE dib ON dib.ID = d.ID AND dib.C_TANEVID = d.C_ALTANEVID AND dib.TOROLT = ''T''
        WHERE d.TOROLT = ''F'';
          
        ENABLE TRIGGER ALL ON dbo.' + @tableName + ';
        '
      PRINT @tableName
      EXEC sys.sp_executesql @sql

      FETCH NEXT FROM tableCursor 
      INTO @tableName
    END

    CLOSE tableCursor
    DEALLOCATE tableCursor