SET NOCOUNT ON
SET XACT_ABORT ON
BEGIN TRAN
BEGIN TRY

	--Cria Procedure para Desabilitar a fila do ETL ao executar manualmente comando Update ou Insert
	If Object_id ('LX_LJ_ETL_DESABILITA_FILA', 'P') Is Not Null 
	  EXEC ('Drop Procedure LX_LJ_ETL_DESABILITA_FILA') 

	EXEC ('Create Procedure LX_LJ_ETL_DESABILITA_FILA 
					@LigaGeracaoFila Bit = 0 
			As 
			Begin 
				Declare @TipoExecucao Varchar(30) 

				If( @LigaGeracaoFila = 0 ) 
				Begin 
					Set CONTEXT_INFO 0X53454D5F46494C415F45544C 
					Set @TipoExecucao = ''DESABILITADA''
				End 
				Else 
				Begin 
					Set CONTEXT_INFO 0X 
					Set @TipoExecucao = ''HABILITADA'' 
				End 

				Insert Into Lj_etl_log 
							(Metodo, 
							Erro, 
							Data_hora_log) 

				Select ''[LX_LJ_ETL_DESABILITA_FILA - Fila: '' + @TipoExecucao + '']'', 
						''Máquina: "'' + Cast(Host_name() As Varchar(254)) + 
						''" - Aplicação: "'' + Cast(app_name() As Varchar(255)) + 
						''" - Usuário: "'' + Cast(system_user As Varchar(255)) + 
						''" - Id Sessão: "'' + Cast(@@SPID As Varchar(255)) + ''"'',			 
						Getdate() 

			End')

	PRINT 'COMMIT';
	COMMIT;

END TRY
BEGIN CATCH

	PRINT 'ROLLBACK';
	THROW;
	ROLLBACK;

	SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_MESSAGE() AS ErrorMessage

END CATCH