学习使用临时表展开BOM,写的一个存储过程,结果如果值小于零,则结果不是为小于1的小数,而直接等于零,不知道为什么,
比如说算出来的结果应该是0.00112,但算出来的值却为零,好奇怪.
ALTER procedure [dbo].[brpbom] (@socode1 varchar(20),@socode2 varchar(20))
as 
--第一步建立一个临时表#re,如果已经存在,就删除,重新建
if object_id('tempdb.dbo.#re') is not null
drop table #re
CREATE TABLE #re
(id int NULL,
socode varchar(50) NULL,
cinvcode varchar(50) NULL,
intb [decimal](18, 4) NULL,
free1 bit NULL,
free2 bit NULL,
sta int NULL)
-- 第二步 定义一个变量
DECLARE @Level int
SET @Level = 1
--第三步,追加查询把订单资料追加到临时表
insert into #re(a.id,a.socode,a.cinvcode,a.intb,b.free1,b.free2,sta)
select a.id,a.socode,a.cinvcode,a.intb,b.free1,b.free2,sta=@level 
from dsaleson a left join ainventory b on a.cinvcode=b.cinvcode where a.socode between @socode1 and @socode2
--循环增加
while @@rowcount > 0
begin
--如果level超过了10,表示有问题,因为嵌套15的情况很少见,除非是出错了
if @Level>15
return
SET @Level = @Level + 1 
--追加查询,把上一层性质是自制属性的,添加进去.(1+d.de3/100-d.de4/100) de3为增加损耗率,de4为扣除率
insert into #re(id,socode,cinvcode,intb,free1,free2,sta)
select a.id,a.socode,d.cinvcode,case b.ifint when 0 then a.intb*d.de1/d.de2*(1+d.de3/100-d.de4/100) 
when 1 then ceiling(a.intb*d.de1/d.de2*(1+d.de3/100-d.de4/100)) end as sumquan,b.free1,b.free2,sta=@Level
from #re a left join cbom c on a.cinvcode = c.cinvcode left join cbomson d on c.bomid=d.bomid 
left join ainventory b on d.cinvcode=b.cinvcode
where a.sta = @Level-1 and a.free1 = 1
end
--第四步,添加到永久表cbrp中
insert into cbrp(id,socode,cinvcode,intb,free1,free2,sta)
select id,socode,cinvcode,intb,free1,free2,sta from #re where sta>1
--第五步,有的时间出现NULL,不知道为什么会这样子,以后找到原因会删掉这一条
delete cbrp where cinvcode is null
--第六步,得到BRP运算结果
select count(0) from cbrp where socode between @socode1 and @socode2

解决方案 »

  1.   


    (a.intb*d.de1/d.de2*(1+d.de3/100.0000 - d.de4/100.0000)) 
      

  2.   

    你可以试下 select 1/3和select 1.0/3
    第一个是INT型  第二个是浮点数了
      

  3.   

    那我该如何修改这个SQL语句呢,
      

  4.   

    (a.intb*1.*d.de1/d.de2*(1+d.de3/100-d.de4/100))
      

  5.   

    (a.intb*1.00*d.de1/d.de2*(1+d.de3/100-d.de4/100))
    如上方式,就可以了吗
      

  6.   

    (a.intb*1.00*d.de1/d.de2*(1.0+d.de3/100.0-d.de4/100.0))
      

  7.   

    问题是我改成如下代码,还是不行呀
    insert into #re(id,socode,cinvcode,intb,free1,free2,sta)
    select a.id,a.socode,d.cinvcode,case b.ifint when 0 then a.intb*1.000000*d.de1/d.de2*(1.00+d.de3/100.00-d.de4/100.00) 
    when 1 then ceiling(a.intb*1.00*d.de1/d.de2*(1.00+d.de3/100.00-d.de4/100.00)) end as sumquan,b.free1,b.free2,sta=@Level
    from #re a left join cbom c on a.cinvcode = c.cinvcode left join cbomson d on c.bomid=d.bomid 
    left join ainventory b on d.cinvcode=b.cinvcode
    where a.sta = @Level-1 and a.free1 = 1
    都不知道问题在那里哦。
      

  8.   

    会不会是因为下面的一句
    --第三步,追加查询把订单资料追加到临时表
    insert into #re(a.id,a.socode,a.cinvcode,a.intb,b.free1,b.free2,sta)
    select a.id,a.socode,a.cinvcode,a.intb,b.free1,b.free2,sta=@level  
    from dsaleson a left join ainventory b on a.cinvcode=b.cinvcode where a.socode between @socode1 and @socode2
    我建立的临时表 intb字段是[decimal](18, 4) NULL,追加到里面的数据时是整数,会不会改变了intb字段的字符类型,从decimal 变成了 int格式了。