1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| CREATE FUNCTION FCT_STK_detail (@art_id int)
RETURNS @PAMPTable TABLE (stk_id int primary key,
stk_type varchar(50),
stk_quantite decimal(18,2),
stk_prix decimal(18,2),
stk_date datetime,
ble_numero varchar(50),
ble_fournisseur_nom varchar(50),
stock decimal(18,2),
pmp decimal(18,2)
)
AS
begin
declare @stk_id int
declare @stk_type varchar(50)
declare @stk_quantite decimal(18,2)
declare @stk_prix decimal(18,2)
declare @stk_date datetime
declare @ble_numero varchar(50)
declare @ble_fournisseur_nom varchar(50)
declare @pmp decimal(18,2)
declare @stock decimal(18,2)
set @pmp=0
set @stock=0
declare PAMP cursor scroll for
SELECT stk_id, stk_type, stk_quantite, stk_prix, stk_date, ble_numero, ble_fournisseur_nom
FROM dbo.STK_stock LEFT OUTER JOIN
dbo.BLL_bon_livraison_ligne ON dbo.STK_stock.BLL_id = dbo.BLL_bon_livraison_ligne.BLL_id LEFT OUTER JOIN
dbo.BLE_bon_livraison ON dbo.BLL_bon_livraison_ligne.BLE_id = dbo.BLE_bon_livraison.BLE_id
where stk_stock.art_id=@art_id
order by stk_date
open PAMP
fetch next from PAMP into @stk_id, @stk_type, @stk_quantite, @stk_prix, @stk_date, @ble_numero, @ble_fournisseur_nom
while @@fetch_status = 0
begin
if (@stock + @stk_quantite) = 0
set @pmp = 0
else if @stk_quantite > 0
set @pmp = ((@stock * @pmp) + (@stk_prix*@stk_quantite)) / (@stock + @stk_quantite)
set @stock=@stock + @stk_quantite
insert @PAMPTable select @stk_id, @stk_type, @stk_quantite, @stk_prix, @stk_date, @ble_numero, @ble_fournisseur_nom, @stock, @pmp
fetch next from PAMP into @stk_id, @stk_type, @stk_quantite, @stk_prix, @stk_date, @ble_numero, @ble_fournisseur_nom
end
close PAMP
deallocate PAMP
return
END |
Partager