create procedure dbo.%PROC% @sid varchar(30) = null, @rep_type integer as .ID_VAR(@rep_id) select @rep_id=convert(numeric,@sid) select v.viol_name, v.find_date as find_date_d, v.commit_date as commit_date_d, v.clear_date as clear_date_d, v.action_str, v.rep_detect_name, convert(decimal(18,4),v.fact_proc) as fact_proc, convert(decimal(18,4),v.min_proc) as min_proc, convert(decimal(18,4),v.max_proc) as max_proc, (case when v.order_no is null and v.order_date is null then '' else '¹ '+v.order_no+' īņ '+convert(varchar,v.order_date,3) end) as order_str, @rep_type, v.days_qty, convert(decimal(18,4),isnull((select sum(ts.fact_percent) from td_depo_docs t,ts_investment_report ts where t.id=@rep_id and ts.line_id=v.lim_id and ts.srez_id=(select max(s.id) from tp_pif_srez s where t.in_date=s.srez_date and .ITEMS_EXISTS_BY_TYPE(s.id,'PIF_SREZ') and s.fond_id=t.owner_id) GROUP BY ts.line_code_eff),0)) as fact_part from t_sd_violation v where ((v.rep_detect_id=@rep_id and @rep_type=1) or (v.rep_corr_id=@rep_id and @rep_type=2) or (v.rep_uncorr_id=@rep_id and @rep_type=3)) order by find_date_d,viol_name