create procedure dbo.%PROC% @s_cur_id numeric, @t_cur_id numeric, @convert_date datetime, @plus_percent decimal(38,8), @s_sum decimal(38,8), @t_sum decimal(38,8) out, @rate_value decimal(38,8) out, @rate_date datetime out as begin declare @s_rate_value decimal(38,8), @s_rate_date datetime, @t_rate_value decimal(38,8), @t_rate_date datetime, @mess varchar(255), @rate_value_long decimal(36,16) if @s_cur_id = @t_cur_id begin select @t_sum = @s_sum select @rate_value = 1 select @rate_date = null end else begin if @convert_date is null begin raiserror 40000 'Задайте дату конвертации.' return end if @s_sum is null begin raiserror 40000 'Задайте исходную сумму для конвертации.' return end if @s_cur_id is null begin raiserror 40000 'Задайте исходную валюту' return end if @t_cur_id is null begin raiserror 40000 'Задайте целевую валюту' return end /*--------S---------*/ select @s_rate_date = (select max(rate_date) from t_rates r,t_items i,t_states s where r.cur_id=@s_cur_id and r.rate_date <=@convert_date and r.id = i.id and i.state_id = s.id and s.class_id !=2) if @s_rate_date is null begin select @mess = 'Не задан курс для исходной валюты ' + isnull(cur_name,'Не задана') + ' на '+ isnull(convert(varchar,@convert_date,3),' неопределенную дату')+'.' from t_currencies where id = @s_cur_id if @mess is null select @mess = 'Исходная валюта не найдена' select @t_sum = 0 select @rate_value = 0 select @rate_date = null raiserror 40000 @mess return end select @s_rate_value = (select rate_value from t_rates r,t_items i,t_states s where r.cur_id=@s_cur_id and r.rate_date =@s_rate_date and r.id = i.id and i.state_id = s.id and s.class_id !=2) if (@s_rate_value is null) or (@s_rate_value <= 0 ) begin select @mess = 'Не задан курс для исходной валюты ' + isnull(cur_name,'Не задана') + ' на '+ isnull(convert(varchar,@convert_date,3),' неопределенную дату')+'.' from t_currencies where id = @s_cur_id select @t_sum = 0 select @rate_value = 0 select @rate_date = null raiserror 40000 @mess return end /*-------------------T-----------------*/ select @t_rate_date = (select max(rate_date) from t_rates r,t_items i,t_states s where r.cur_id=@t_cur_id and r.rate_date <=@convert_date and r.id = i.id and i.state_id = s.id and s.class_id !=2) if @t_rate_date is null begin select @mess = 'Не задан курс для целевой валюты ' + isnull(cur_name,'Не задана') + ' на '+ isnull(convert(varchar,@convert_date,3),' неопределенную дату')+'.' from t_currencies where id = @t_cur_id if @mess is null select @mess = 'Целевая валюта не найдена' select @t_sum = 0 select @rate_value = 0 select @rate_date = null raiserror 40000 @mess return end select @t_rate_value = (select rate_value from t_rates r,t_items i,t_states s where r.cur_id=@t_cur_id and r.rate_date =@t_rate_date and r.id = i.id and i.state_id = s.id and s.class_id !=2) if (@t_rate_value is null) or (@t_rate_value <= 0 ) begin select @mess = 'Не задан курс для исходной валюты ' + isnull(cur_name,'Не задана') + ' на '+ isnull(convert(varchar,@convert_date,3),' неопределенную дату')+'.' from t_currencies where id = @t_cur_id select @t_sum = 0 select @rate_value = 0 select @rate_date = null raiserror 40000 @mess return end /*----------------------Date ---------------*/ /* Начало Богаковский 29/12/2004, поскольку точность обратного курса 8 знаков - не достаточна ************/ --select @rate_value = round(@s_rate_value / @t_rate_value, 8) select @rate_value_long = round(@s_rate_value / @t_rate_value, 16) --select @t_sum = round(@s_sum * @rate_value * (100.0 + @plus_percent)/100.0,4) select @t_sum = round(@s_sum * @rate_value_long * (100.0 + @plus_percent)/100.0,4) --Добавил округление для того, чтобы выходная переменная шаблона была как раньше select @rate_value = round(@rate_value_long,8) /* Конец Богаковский 29/12/2004, поскольку точность обратного курса 8 знаков - не достаточна *************/ if @s_rate_date >= @t_rate_date select @rate_date = @s_rate_date else select @rate_date = @t_rate_date end end