CREATE TABLE LogTrocaEndereco(
	rEndereco           INT,
	rItemAnterior       INT,
	rItemAtual          INT,
	RuaAnterior         VARCHAR(254),
	RuaAtual            VARCHAR(254),
	NumeroAnterior      VARCHAR(254),
	NumeroAtual         VARCHAR(254),
	ComplementoAnterior VARCHAR(254),
	ComplementoAtual    VARCHAR(254),
	BairroAnterior      VARCHAR(254),
	BairroAtual         VARCHAR(254),
	DataHora            DATETIME,
	Estacao             CHAR(20))
GO

CREATE TRIGGER Endereco_U ON Endereco FOR UPDATE AS

	DECLARE @Estacao CHAR(20)

	IF UPDATE(rItem)
	BEGIN
		SELECT @Estacao = HostName FROM sys.sysprocesses WHERE spid=@@spid 

		INSERT INTO LogTrocaEndereco 
			(rEndereco, rItemAnterior, rItemAtual, RuaAnterior, RuaAtual, NumeroAnterior, NumeroAtual, ComplementoAnterior, ComplementoAtual, BairroAnterior, BairroAtual, DataHora, Estacao)
		SELECT  INSERTED.OID, DELETED.rItem, INSERTED.rItem, DELETED.Rua, INSERTED.Rua, DELETED.Numero, INSERTED.Numero, DELETED.Complemento, INSERTED.Complemento, DELETED.Bairro, INSERTED.Bairro, GETDATE(), @ESTACAO  
		FROM INSERTED, DELETED 
	END 
GO


