sqlserver2005存储过程中限制被除数不为0,局部代码如下
第 139 行
insert into #lyg
select b.sStationNO,
case when nSaleAmount=0 then 0 else substring(convert (varchar(20),(a.nMargin/a.nSaleAmount)*100),1,5)+'%' end --限制被除数不为零
from tSaleDaily a,#ly b where a.sStationNO=b.sStationNO and a.dReportDate between dateadd(dd,-392,@BeginDate) and dateadd(dd,-364,@BeginDate)
group by b.sStationNO,a.nMargin,a.nSaleAmount
update #ly set nThisMonthMaoLiLv=a.nMaoLiLv from #lyh a,#ly b where a.sStationNO=b.sStationNO
但是执行存储过程的时候总是报错,错误如下
过程 usp_OilStationSaleMonth,第 139 行 在将 varchar 值 '21.42%' 转换成数据类型 int 时失败。
insert into #lyg
select b.sStationNO,
case when nSaleAmount=0 then '0' else substring(convert (varchar(20),(a.nMargin/a.nSaleAmount)*100),1,5)+'%' end --限制被除数不为零
from tSaleDaily a,#ly b where a.sStationNO=b.sStationNO and a.dReportDate between dateadd(dd,-392,@BeginDate) and dateadd(dd,-364,@BeginDate)
group by b.sStationNO,a.nMargin,a.nSaleAmount
update #ly set nThisMonthMaoLiLv=a.nMaoLiLv from #lyh a,#ly b where a.sStationNO=b.sStationNO
substring(convert (varchar(50),(a.nMargin/a.nSaleAmount)*100),1,5)+'%' end --限制被除数不为零
#7
改成这样:
insert into #lyg
select b.sStationNO,
case when nSaleAmount=0 then '0' else substring(convert (varchar(20),(a.nMargin/a.nSaleAmount)*100),1,5)+'%' end --限制被除数不为零
from tSaleDaily a,#ly b where a.sStationNO=b.sStationNO and a.dReportDate between dateadd(dd,-392,@BeginDate) and dateadd(dd,-364,@BeginDate)
group by b.sStationNO,a.nMargin,a.nSaleAmount
update #ly set nThisMonthMaoLiLv=a.nMaoLiLv from #lyh a,#ly b where a.sStationNO=b.sStationNO
insert into #lyg
select b.sStationNO,
case when nSaleAmount=0 then '0' else substring(convert (varchar(20),(a.nMargin/a.nSaleAmount)*100),1,5)+'%' end --限制被除数不为零
from tSaleDaily a,#ly b where a.sStationNO=b.sStationNO and a.dReportDate between dateadd(dd,-392,@BeginDate) and dateadd(dd,-364,@BeginDate)
group by b.sStationNO,a.nMargin,a.nSaleAmount
update #ly set nThisMonthMaoLiLv=a.nMaoLiLv from #lyh a,#ly b where a.sStationNO=b.sStationNO
insert into #ly
select a.sStationNO,a.sStationName,0,0,
case when nLastMonthSale=0 then '0' else substring(convert (varchar(50),((a.nThisMonthSale-a.nLastMonthSale)/a.nLastMonthSale)*100),1,5)+'%' end,0,0,0,0,0,0,0,0,0,0,0
from #ly a
Group by a.sStationNO,a.sStationName,a.nThisMonthSale,a.nLastMonthSale
insert into #lyg
select b.sStationNO,
case when nSaleAmount=0 then '0' else substring(convert (varchar(20),(a.nMargin/a.nSaleAmount)*100),1,5)+'%' end --限制被除数不为零
from tSaleDaily a,#ly b where a.sStationNO=b.sStationNO and a.dReportDate between dateadd(dd,-392,@BeginDate) and dateadd(dd,-364,@BeginDate)
group by b.sStationNO,a.nMargin,a.nSaleAmount
update #ly set nThisMonthMaoLiLv=a.nMaoLiLv from #lyh a,#ly b where a.sStationNO=b.sStationNO
insert into #lyg
select b.sStationNO,
case when nSaleAmount=0 then '0' else substring(convert (varchar(20),(a.nMargin/a.nSaleAmount)*100),1,5)+'%' end --限制被除数不为零
from tSaleDaily a,#ly b where a.sStationNO=b.sStationNO and a.dReportDate between dateadd(dd,-392,@BeginDate) and dateadd(dd,-364,@BeginDate)
group by b.sStationNO,a.nMargin,a.nSaleAmount
update #ly set nThisMonthMaoLiLv=a.nMaoLiLv from #lyh a,#ly b where a.sStationNO=b.sStationNO
insert into #lyg
select b.sStationNO,
case when nSaleAmount=0 then '0' else substring(convert (varchar(20),(a.nMargin/a.nSaleAmount)*100),1,5)+'%' end --限制被除数不为零
from tSaleDaily a,#ly b where a.sStationNO=b.sStationNO and a.dReportDate between dateadd(dd,-392,@BeginDate) and dateadd(dd,-364,@BeginDate)
group by b.sStationNO,a.nMargin,a.nSaleAmount
update #ly set nThisMonthMaoLiLv=a.nMaoLiLv from #lyh a,#ly b where a.sStationNO=b.sStationNO
insert into #lyg
select b.sStationNO,
case when nSaleAmount=0 then '0' else substring(convert (varchar(20),(a.nMargin/a.nSaleAmount)*100),1,5)+'%' end --限制被除数不为零
from tSaleDaily a,#ly b where a.sStationNO=b.sStationNO and a.dReportDate between dateadd(dd,-392,@BeginDate) and dateadd(dd,-364,@BeginDate)
group by b.sStationNO,a.nMargin,a.nSaleAmount
update #ly set nThisMonthMaoLiLv=a.nMaoLiLv from #lyh a,#ly b where a.sStationNO=b.sStationNO
insert into #lyg
select b.sStationNO,
case when nSaleAmount=0 then '0' else substring(convert (varchar(20),(a.nMargin/a.nSaleAmount)*100),1,5)+'%' end --限制被除数不为零
from tSaleDaily a,#ly b where a.sStationNO=b.sStationNO and a.dReportDate between dateadd(dd,-392,@BeginDate) and dateadd(dd,-364,@BeginDate)
group by b.sStationNO,a.nMargin,a.nSaleAmount
update #ly set nThisMonthMaoLiLv=a.nMaoLiLv from #lyh a,#ly b where a.sStationNO=b.sStationNO
substring(convert (varchar(50),(a.nMargin/a.nSaleAmount)*100),1,5)+'%' end --限制被除数不为零
#7
改成这样:
insert into #lyg
select b.sStationNO,
case when nSaleAmount=0 then '0' else substring(convert (varchar(20),(a.nMargin/a.nSaleAmount)*100),1,5)+'%' end --限制被除数不为零
from tSaleDaily a,#ly b where a.sStationNO=b.sStationNO and a.dReportDate between dateadd(dd,-392,@BeginDate) and dateadd(dd,-364,@BeginDate)
group by b.sStationNO,a.nMargin,a.nSaleAmount
update #ly set nThisMonthMaoLiLv=a.nMaoLiLv from #lyh a,#ly b where a.sStationNO=b.sStationNO
insert into #lyg
select b.sStationNO,
case when nSaleAmount=0 then '0' else substring(convert (varchar(20),(a.nMargin/a.nSaleAmount)*100),1,5)+'%' end --限制被除数不为零
from tSaleDaily a,#ly b where a.sStationNO=b.sStationNO and a.dReportDate between dateadd(dd,-392,@BeginDate) and dateadd(dd,-364,@BeginDate)
group by b.sStationNO,a.nMargin,a.nSaleAmount
update #ly set nThisMonthMaoLiLv=a.nMaoLiLv from #lyh a,#ly b where a.sStationNO=b.sStationNO
insert into #ly
select a.sStationNO,a.sStationName,0,0,
case when nLastMonthSale=0 then '0' else substring(convert (varchar(50),((a.nThisMonthSale-a.nLastMonthSale)/a.nLastMonthSale)*100),1,5)+'%' end,0,0,0,0,0,0,0,0,0,0,0
from #ly a
Group by a.sStationNO,a.sStationName,a.nThisMonthSale,a.nLastMonthSale
insert into #lyg
select b.sStationNO,
case when nSaleAmount=0 then '0' else substring(convert (varchar(20),(a.nMargin/a.nSaleAmount)*100),1,5)+'%' end --限制被除数不为零
from tSaleDaily a,#ly b where a.sStationNO=b.sStationNO and a.dReportDate between dateadd(dd,-392,@BeginDate) and dateadd(dd,-364,@BeginDate)
group by b.sStationNO,a.nMargin,a.nSaleAmount
update #ly set nThisMonthMaoLiLv=a.nMaoLiLv from #lyh a,#ly b where a.sStationNO=b.sStationNO
insert into #lyg
select b.sStationNO,
case when nSaleAmount=0 then '0' else substring(convert (varchar(20),(a.nMargin/a.nSaleAmount)*100),1,5)+'%' end --限制被除数不为零
from tSaleDaily a,#ly b where a.sStationNO=b.sStationNO and a.dReportDate between dateadd(dd,-392,@BeginDate) and dateadd(dd,-364,@BeginDate)
group by b.sStationNO,a.nMargin,a.nSaleAmount
update #ly set nThisMonthMaoLiLv=a.nMaoLiLv from #lyh a,#ly b where a.sStationNO=b.sStationNO