create procedure dbo.%PROC% @stock_id numeric, /*ЦБ*/ @rate_date datetime, /*Дата*/ @cur_id numeric, /*Валюта*/ @stock_rate numeric(18,8) out as begin declare @rate_id numeric declare @rate_cur_id numeric declare @rate_place_id numeric declare @cur_rate_value decimal(18,8) declare @cur_rate_date datetime declare @class_value_code varchar(255) declare @class_comment varchar(255) declare @stock_rate_val numeric(18,8) select @stock_rate = 0 if @stock_id is null return if @rate_date is null return if @cur_id is null select @cur_id = .CUR('RUR') -- Площадка select @rate_place_id = (select max(tb_stock_rates.rate_place_id) from tb_stock_rates where tb_stock_rates.rate_date <= @rate_date and tb_stock_rates.stock_id = @stock_id and tb_stock_rates.stock_rate is not null and tb_stock_rates.stock_rate <> 0 and .ITEMS_EXISTS(tb_stock_rates.id)) if @rate_place_id is null return -- Котировка select @rate_id = (select max(tb_stock_rates.id) from tb_stock_rates where tb_stock_rates.rate_date <= @rate_date and tb_stock_rates.rate_place_id = @rate_place_id and tb_stock_rates.stock_id = @stock_id and tb_stock_rates.stock_rate is not null and tb_stock_rates.stock_rate <> 0 and .ITEMS_EXISTS(tb_stock_rates.id)) -- Валюта котировки select @rate_cur_id = tb_stock_rates.rate_cur_id from tb_stock_rates where id=@rate_id if @rate_cur_id is null -- Валюта по умолчанию select @rate_cur_id = td_rate_places.cur_id from td_rate_places where id=@rate_place_id -- Котировка declare @coupon_rate money select @stock_rate_val= tb_stock_rates.stock_rate, @coupon_rate = tb_stock_rates.coupon_rate from tb_stock_rates where id=@rate_id if @coupon_rate is not null select @stock_rate_val = isnull(@stock_rate_val, 0) + isnull(@coupon_rate,0) if @stock_rate_val is null select @stock_rate = 0 else if @cur_id = @rate_cur_id select @stock_rate = @stock_rate_val else begin .CONVERT_SUM_DEC(@rate_cur_id, @cur_id, @rate_date, 0, @stock_rate_val, @stock_rate, @cur_rate_value, @cur_rate_date) end end