
/*------ ATD 402795 ------*/
IF NOT EXISTS (SELECT 1 FROM SYSOBJECTS WHERE NAME = 'LOG_ITEMCLICOMPLEMENTO_SEM_ITEMCLICAD_TEMP')
 BEGIN
	CREATE TABLE LOG_ITEMCLICOMPLEMENTO_SEM_ITEMCLICAD_TEMP(
		NumPed INT, 
		RItem 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_ItemCliCompleento_Sem_ItemCliCad_Ficha_402795')
	DROP TRIGGER Monitorar_ItemCliCompleento_Sem_ItemCliCad_Ficha_402795
GO

CREATE TRIGGER Monitorar_ItemCliCompleento_Sem_ItemCliCad_Ficha_402795 ON ItemCliComplemento FOR INSERT 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_ITEMCLICOMPLEMENTO_SEM_ITEMCLICAD_TEMP(NumPed, RItem, Data, Comando, Sistema, Estacao, Usuario, Observacao) 
        SELECT I.NumPed, I.RItem, GETDATE(), 
               @Comando, @Sistema, @Estacao, @Usuario, 'ItemCliComplementeo SEM ItemCliCad' 
        FROM Inserted I
        WHERE NOT EXISTS(SELECT 1 FROM ItemCliCad WHERE Item = I.RItem)
	SET NOCOUNT OFF
	RETURN
END
GO
