﻿IF ((SELECT is_disabled FROM sys.triggers tr where tr.name = N'tr_AsyncAuditEMAILHIBALOG') <> 1)
  DISABLE TRIGGER tr_AsyncAuditEMAILHIBALOG ON T_EMAILHIBALOG;

DECLARE @tableName nvarchar(max),
  @triggerName nvarchar(max),
  @query nvarchar(max)

DECLARE cur cursor FOR 
  SELECT DISTINCT t.name, tr.name
  FROM sys.tables t
  INNER JOIN sys.columns c ON c.object_id = t.object_id
    AND c.name IN (N'SERIAL', N'LASTCHANGED', N'CREATED')
  INNER JOIN sys.triggers tr ON tr.parent_id = t.object_id
    AND tr.name LIKE N'tr_%Log'
    AND tr.name NOT LIKE N'tr_AsyncAudit%LOG'
  WHERE t.name NOT LIKE N'%_STAGE'
	AND t.name LIKE N'T_[A-G]%'

OPEN cur

FETCH NEXT FROM cur
INTO @tableName, @triggerName

WHILE @@FETCH_STATUS = 0  
BEGIN

  SET @query = N'
    DISABLE TRIGGER ' + @triggerName + N' ON ' + @tableName + N';

    UPDATE ' + @tableName + N' SET
      TOROLT = N''F''
    WHERE TOROLT IS NULL;

    UPDATE ' + @tableName + N' SET
      SERIAL = 0
    WHERE SERIAL IS NULL;

    UPDATE ' + @tableName + N' SET
      CREATED = N''20000101''
    WHERE CREATED IS NULL;

    UPDATE ' + @tableName + N' SET
      LASTCHANGED = CREATED
    WHERE LASTCHANGED IS NULL;
    
    ENABLE TRIGGER ' + @triggerName + N' ON ' + @tableName + N';'

  EXEC sp_executesql @query

  FETCH NEXT FROM cur
  INTO @tableName, @triggerName
END

CLOSE cur;  
DEALLOCATE cur;

  DECLARE cur0 cursor FOR 
  SELECT DISTINCT t.name, tr.name
  FROM sys.tables t
  INNER JOIN sys.columns c ON c.object_id = t.object_id
    AND c.name IN (N'SERIAL', N'LASTCHANGED', N'CREATED')
  INNER JOIN sys.triggers tr ON tr.parent_id = t.object_id
    AND tr.name NOT LIKE N'tr_%Log'
  WHERE t.name NOT LIKE N'%_STAGE'
	AND t.name LIKE N'T_[A-G]%'

OPEN cur0

FETCH NEXT FROM cur0
INTO @tableName, @triggerName

WHILE @@FETCH_STATUS = 0  
BEGIN

  SET @query = N'
    DISABLE TRIGGER ' + @triggerName + N' ON ' + @tableName + N';

    UPDATE ' + @tableName + N' SET
      TOROLT = N''F''
    WHERE TOROLT IS NULL;

    UPDATE ' + @tableName + N' SET
      SERIAL = 0
    WHERE SERIAL IS NULL;

    UPDATE ' + @tableName + N' SET
      CREATED = N''20000101''
    WHERE CREATED IS NULL;

    UPDATE ' + @tableName + N' SET
      LASTCHANGED = CREATED
    WHERE LASTCHANGED IS NULL;'

  EXEC sp_executesql @query

  FETCH NEXT FROM cur0
  INTO @tableName, @triggerName
END

CLOSE cur0;
DEALLOCATE cur0;

GO

    UPDATE T_ENTITYATTRIBUTEHISTORY SET
      TOROLT = N'F'
    WHERE TOROLT IS NULL;

    UPDATE T_ENTITYATTRIBUTEHISTORY SET
      SERIAL = 0
    WHERE SERIAL IS NULL;

    UPDATE T_ENTITYATTRIBUTEHISTORY SET
      CREATED = N'20000101'
    WHERE CREATED IS NULL;

    UPDATE T_ENTITYATTRIBUTEHISTORY SET
      LASTCHANGED = CREATED
    WHERE LASTCHANGED IS NULL;


	UPDATE T_ENTITYHISTORY SET
      TOROLT = N'F'
    WHERE TOROLT IS NULL;

    UPDATE T_ENTITYHISTORY SET
      SERIAL = 0
    WHERE SERIAL IS NULL;

    UPDATE T_ENTITYHISTORY SET
      CREATED = N'20000101'
    WHERE CREATED IS NULL;

    UPDATE T_ENTITYHISTORY SET
      LASTCHANGED = CREATED
    WHERE LASTCHANGED IS NULL;


	UPDATE T_LOGEVENTTYPE SET
      TOROLT = N'F'
    WHERE TOROLT IS NULL;

    UPDATE T_LOGEVENTTYPE SET
      SERIAL = 0
    WHERE SERIAL IS NULL;

    UPDATE T_LOGEVENTTYPE SET
      CREATED = N'20000101'
    WHERE CREATED IS NULL;

    UPDATE T_LOGEVENTTYPE SET
      LASTCHANGED = CREATED
    WHERE LASTCHANGED IS NULL;


	UPDATE T_LOGLEVELTYPE SET
      TOROLT = N'F'
    WHERE TOROLT IS NULL;

    UPDATE T_LOGLEVELTYPE SET
      SERIAL = 0
    WHERE SERIAL IS NULL;

    UPDATE T_LOGLEVELTYPE SET
      CREATED = N'20000101'
    WHERE CREATED IS NULL;

    UPDATE T_LOGLEVELTYPE SET
      LASTCHANGED = CREATED
    WHERE LASTCHANGED IS NULL;


	UPDATE T_TANTARGYNEVHEZKATEGORIA SET
      TOROLT = N'F'
    WHERE TOROLT IS NULL;

    UPDATE T_TANTARGYNEVHEZKATEGORIA SET
      SERIAL = 0
    WHERE SERIAL IS NULL;

    UPDATE T_TANTARGYNEVHEZKATEGORIA SET
      CREATED = N'20000101'
    WHERE CREATED IS NULL;

    UPDATE T_TANTARGYNEVHEZKATEGORIA SET
      LASTCHANGED = CREATED
    WHERE LASTCHANGED IS NULL;

GO
