
/*------ ATD 398492 ------*/
IF NOT EXISTS (SELECT 1 FROM SYSOBJECTS WHERE NAME = 'LOG_EXCLUSAO_VENDASSCAD_Ficha_398492')
 BEGIN
	CREATE TABLE LOG_EXCLUSAO_VENDASSCAD_Ficha_398492(
		NumPed INT, 
		NumOrd INT,
		Data DATETIME, 
		Comando VARCHAR(4000), 
		Sistema VARCHAR(100), 
		Estacao VARCHAR(100), 
		Usuario VARCHAR(100),
		Observacao VARCHAR(100) )
 END
GO

IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE NAME = 'Monitorar_Exclusao_VendassCad_Ficha_398492')
	DROP TRIGGER Monitorar_Exclusao_VendassCad_Ficha_398492
GO


CREATE TRIGGER Monitorar_Exclusao_VendassCad_Ficha_398492 ON VendassCad FOR DELETE AS
BEGIN
	SET NOCOUNT ON 
	Declare @Comando varchar(4000), @ExecStr varchar(50), @Estacao varchar(100), 
			@Usuario varchar(100), @Sistema varchar(100)
			
	CREATE TABLE #inputbuffer (  EventType varchar(30), Paramet int, EventInfo varchar(4000) ) 
	SET @ExecStr = 'DBCC INPUTBUFFER(' + STR(@@SPID) + ')' 
	INSERT INTO #inputbuffer
			
	EXEC (@ExecStr)
			
	SET @Comando = (SELECT EventInfo FROM #inputbuffer)
	DROP TABLE #inputbuffer
			
	SELECT @Estacao = LEFT(host_name,100), 
			@Usuario = LEFT(login_name,100), 
			@Sistema = LEFT(program_name,100)
	FROM sys.dm_exec_sessions 
	where session_id = @@spid
	INSERT INTO LOG_EXCLUSAO_VENDASSCAD_Ficha_398492(NumPed, NumOrd, Data, Comando, Sistema, Estacao, Usuario, Observacao) 
        SELECT D.NumPed, D.NumOrd, GETDATE(), 
               @Comando, @Sistema, @Estacao, @Usuario, 'Excluiu VendassCad confirmado'
        FROM Deleted D
        WHERE D.Situacao = '5' AND NOT EXISTS (SELECT 1 FROM Inserted I WHERE I.NumOrd = D.NumOrd) 
	SET NOCOUNT OFF
	RETURN
END
GO
