﻿IF OBJECT_ID('dev.sp_Global_GenerateAsyncAuditTriggerAll') IS NOT NULL BEGIN
  DROP PROCEDURE dev.sp_Global_GenerateAsyncAuditTriggerAll
END
GO

CREATE PROCEDURE dev.sp_Global_GenerateAsyncAuditTriggerAll
  @disabled bit = 0
AS
BEGIN
  DECLARE tableCursor CURSOR LOCAL FOR 
    SELECT DISTINCT TABLE_NAME
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE COLUMN_NAME IN ('CREATOR', 'MODIFIER')
  	AND TABLE_NAME NOT IN ('T_ENTITYHISTORY', 'T_ENTITYATTRIBUTEHISTORY', 'T_LOG', 'T_GLOBALLOCK', 'T_OLDALLATOGATOTTSAG', 'T_FELHASZNALOBELEPESTORTENET', 'T_EVFOLYAMTIPUS_OKTATASINEVELE')
  	AND TABLE_NAME IN (
  		SELECT t.name 
  		FROM sys.tables t
  		INNER JOIN sys.schemas s ON s.schema_id = t.schema_id
  		WHERE s.name ='dbo' 
  	)

  DECLARE @tableName nvarchar(50)

  OPEN tableCursor  
  FETCH NEXT FROM tableCursor INTO @tableName  

  WHILE @@FETCH_STATUS = 0 BEGIN  

    EXEC dev.sp_Global_GenerateAsyncAuditTrigger @tableName, @disabled
    
    FETCH NEXT FROM tableCursor INTO @tableName  
  END  
  CLOSE tableCursor  
  DEALLOCATE tableCursor 
END
GO

