
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [REKLAMA_UPDATE] ON [dbo].[REKLAMA] 
FOR UPDATE
AS
DECLARE @id INT
DECLARE @id1 VARCHAR(100)
DECLARE @opis VARCHAR(4000)

SELECT @id1=[ID REKLAMY], @id=ReklamaID  FROM DELETED

SET @opis=''

IF UPDATE([ID REKLAMY])
BEGIN
    SET @opis=@opis+' ID_REKLAMY: '+@id1+' na '+(SELECT [ID REKLAMY] FROM INSERTED)
END
 
IF UPDATE(SZER)
BEGIN
    SET @opis=@opis+' SZER: '+(SELECT   CONVERT(varchar(100),SZER) FROM DELETED)+' na '+(SELECT CONVERT(varchar(100),SZER) FROM INSERTED)
END

IF UPDATE(waluta_kurs)
BEGIN
    SET @opis=@opis+' waluta_kurs: '+(SELECT CONVERT(varchar(100), COALESCE(waluta_kurs,'')) FROM DELETED)+' na '+(SELECT CONVERT(varchar(100), COALESCE(waluta_kurs,'')) FROM INSERTED)
END

IF UPDATE(Brutto_Euro_Miano)
BEGIN
	SET @opis=@opis+' waluta_miano: '+(SELECT CONVERT(varchar(100),COALESCE(Brutto_Euro_Miano,'')) FROM DELETED)+' na '+(SELECT CONVERT(varchar(100),COALESCE(Brutto_Euro_Miano,'')) FROM INSERTED)
END
    
IF UPDATE(WYS)
BEGIN
	SET @opis=@opis+' WYS: '+(SELECT CONVERT(varchar(100),WYS) FROM DELETED)+' na '+(SELECT CONVERT(varchar(100),WYS) FROM INSERTED)
END
    
IF UPDATE(MOD_TYP)
BEGIN
	SET @opis=@opis+' MOD_TYP: '+(SELECT CONVERT(varchar(100),MOD_TYP) FROM DELETED)+' na '+(SELECT CONVERT(varchar(100),MOD_TYP) FROM INSERTED)
END
    
IF UPDATE([KROTNOŚĆ])
BEGIN
    SET @opis=@opis+' KROTNOŚĆ: '+(SELECT CONVERT(varchar(100),KROTNOŚĆ) FROM DELETED)+' na '+(SELECT CONVERT(varchar(100),KROTNOŚĆ) FROM INSERTED)
END

IF UPDATE(RABAT)
BEGIN
    SET @opis=@opis+' RABAT: '+(SELECT CONVERT(varchar(100),RABAT) FROM DELETED)+' na '+(SELECT CONVERT(varchar(100),RABAT) FROM INSERTED)
END

IF UPDATE([ZATWIERDZONO DO DRUKU])
BEGIN
    SET @opis=@opis+' ZATWIERDZONO DO DRUKU: '+(SELECT CONVERT(varchar(100),[ZATWIERDZONO DO DRUKU]) FROM DELETED)+' na '+(SELECT CONVERT(varchar(100),[ZATWIERDZONO DO DRUKU]) FROM INSERTED)
END

IF UPDATE(NETTO)
BEGIN
    SET @opis=@opis+' NETTO: '+(SELECT CONVERT(varchar(100),NETTO) FROM DELETED)+' na '+(SELECT CONVERT(varchar(100),NETTO) FROM INSERTED)
END

IF UPDATE([FAKTURA WYSTAWIONO])
BEGIN
    SET @opis=@opis+' FAKTURA WYSTAWIONO: '+(SELECT CONVERT(varchar(100),[FAKTURA WYSTAWIONO]) FROM DELETED)+' na '+(SELECT CONVERT(varchar(100),[FAKTURA WYSTAWIONO]) FROM INSERTED)
END

IF UPDATE([FAKTURA DATA WYSTAWIENIA])
BEGIN
	SET @opis=@opis+' FAKTURA data wystawienia: '+(SELECT CONVERT(varchar(100),COALESCE([FAKTURA DATA WYSTAWIENIA],'')) FROM DELETED)+' na '+(SELECT CONVERT(varchar(100),COALESCE([FAKTURA DATA WYSTAWIENIA],'')) FROM INSERTED)
END

IF UPDATE([FAKTURA TERMIN ZAPŁATY])
BEGIN
	SET @opis=@opis+' FAKTURA TERMIN ZAPŁATY: '+(SELECT CONVERT(varchar(100),COALESCE([FAKTURA TERMIN ZAPŁATY],'')) FROM DELETED)+' na '+(SELECT CONVERT(varchar(100),COALESCE([FAKTURA TERMIN ZAPŁATY],'')) FROM INSERTED)
END
    
IF UPDATE(CustomerID)
BEGIN
	SET @opis=@opis+' Klient Agencji: '+(SELECT CONVERT(varchar(100),CustomerID) FROM DELETED)+' na '+(SELECT CONVERT(varchar(100),CustomerID) FROM INSERTED)
END

-- zapis do logu
INSERT INTO LOG(OPERACJA,TABELA,OPIS,SZCZEGOLY)
VALUES('UPDATE','REKLAMA',@id1,@opis)
 
UPDATE REKLAMA
SET [DATA OSTATNIEJ MODYFIKACJI]=CURRENT_TIMESTAMP
WHERE ReklamaID=@id
GO

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO

