La fonction : /// ALTER FUNCTION dbo.EXT_SelonTVA ( @pincode varchar(10), @pcvid int, @CodeTVA int, @Champ varchar(10) ) RETURNS real AS BEGIN DECLARE @Resultat real DECLARE @TypeDoc int if left(@pincode,3) = ‘AVR’ Set @TypeDoc = -1 Else Set @TypeDoc = 1 Set @Resultat = CASE @Champ WHEN ‘Base’ THEN (Select PTVBASETVA * @TypeDoc FROM PIECEVENTETAXES INNER JOIN PIECEVENTES ON PIECEVENTETAXES.PCVID = PIECEVENTES.PCVID Where PIECEVENTETAXES.PCVID = @pcvid AND PIECEVENTETAXES.CODETAXE = @CODETVA) WHEN ‘Taux’ THEN (Select PTVTAUXTVA*100 FROM PIECEVENTETAXES INNER JOIN PIECEVENTES ON PIECEVENTETAXES.PCVID = PIECEVENTES.PCVID Where PIECEVENTETAXES.PCVID = @pcvid AND PIECEVENTETAXES.CODETAXE = @CODETVA) WHEN ‘TVA’ THEN (Select PTVMNTTVA * @TypeDoc FROM PIECEVENTETAXES INNER JOIN PIECEVENTES ON PIECEVENTETAXES.PCVID = PIECEVENTES.PCVID Where PIECEVENTETAXES.PCVID = @pcvid AND PIECEVENTETAXES.CODETAXE = @CODETVA) END RETURN @Resultat ; END /// La requête : /// SELECT PV.PINCODE, PV.PCVNUM, PV.PCVDATEEFFET, TI.TIRCODE, AD.ADRSOCIETE, AD.ADRL1, AD.ADRCODEPOSTAL, AD.ADRVILLE, AD.ADRPAYS, PV.PCVNUMEXT, CASE LEFT(PV.PINCODE,3) WHEN ‘AVR’ THEN PV.PCVMNTHT*-1 ELSE PV.PCVMNTHT END AS TOTAL_HT, CASE LEFT(PV.PINCODE,3) WHEN ‘AVR’ THEN PV.PCVMNTTTC*-1 ELSE PV.PCVMNTTTC END AS TOTAL_TTC, CASE LEFT(PV.PINCODE,3) WHEN ‘AVR’ THEN PV.PCVMNTESCOMPTE*-1 ELSE PV.PCVMNTESCOMPTE END AS TOTAL_ESCOMPTE, CASE LEFT(PV.PINCODE,3) WHEN ‘AVR’ THEN PV.PCVMNTPORT*-1 ELSE PV.PCVMNTPORT END AS TOTAL_ESCOMPTE, dbo.EXT_SelonTVA(PV.PINCODE, PV.PCVID, 1, ‘Base’) AS Base_TVA_1, dbo.EXT_SelonTVA(PV.PINCODE, PV.PCVID, 1, ‘Taux’) AS Taux_TVA_1, dbo.EXT_SelonTVA(PV.PINCODE, PV.PCVID, 1, ‘TVA’) AS Montant_TVA_1, dbo.EXT_SelonTVA(PV.PINCODE, PV.PCVID, 2, ‘Base’) AS Base_TVA_2, dbo.EXT_SelonTVA(PV.PINCODE, PV.PCVID, 2, ‘Taux’) AS Taux_TVA_2, dbo.EXT_SelonTVA(PV.PINCODE, PV.PCVID, 2, ‘TVA’) AS Montant_TVA_2, dbo.EXT_SelonTVA(PV.PINCODE, PV.PCVID, 3, ‘Base’) AS Base_TVA_3, dbo.EXT_SelonTVA(PV.PINCODE, PV.PCVID, 3, ‘Taux’) AS Taux_TVA_3, dbo.EXT_SelonTVA(PV.PINCODE, PV.PCVID, 3, ‘TVA’) AS Montant_TVA_3, dbo.EXT_SelonTVA(PV.PINCODE, PV.PCVID, 4, ‘Base’) AS Base_TVA_4, dbo.EXT_SelonTVA(PV.PINCODE, PV.PCVID, 4, ‘Taux’) AS Taux_TVA_4, dbo.EXT_SelonTVA(PV.PINCODE, PV.PCVID, 4, ‘TVA’) AS Montant_TVA_4 FROM PIECEVENTES AS PV INNER JOIN ADRESSES AS AD ON PV.ADRID_FAC = AD.ADRID INNER JOIN TIERS AS TI ON PV.TIRID = TI.TIRID WHERE (LEFT(PV.PINCODE, 3) = ‘AVR’) OR (LEFT(PV.PINCODE, 3) = ‘FAC’) ///