SQL存储过程执行出错~~

COMPUTERZZZ 2007-06-07 04:14:49
declare @begindate varchar(10)
set @begindate='2007-05-01'

delete yj where convert(char(7),yjdate,120)=left(@begindate,7)
--复制上月月结数据
insert into yj(yjdate,bomid,qty_a_l,amount_a_l,in_qtys,amount_m,out_qtys,aunitprice,amount_o,
qty_a,amount_a)
select @begindate,bomid,qty_a,amount_a,in_qtys,amount_m,out_qtys,aunitprice,amount_o,
qty_a,amount_a from yj where convert(char(7),yjdate,120)=
left(convert(char(7),dateadd(month,-1,convert(datetime,@begindate,120)),120),7)
--对COPY的数据进行修改
--更新 入,出库数,入库金额
update yj set in_qtys=b.in_qty,out_qtys=b.out_qty,amount_m=b.amount_m
from yj inner join (
select bomid,isnull(sum(in_qty),0) in_qty,
isnull(sum(amount),0) amount_m,isnull(sum(out_qty),0) out_qty
from delivery_view where convert(char(7),datetime,120)=left(@begindate,7) group by bomid ) b
on yj.bomid=b.bomid
where convert(char(7),yj.yjdate,120)=left(@begindate,7)


--当上月结存数为0(即本月期初数为0)并且本月入库也为0时,平均单价为0
update yj set aunitprice=0, --平均单价
amount_o=out_qtys*0, --本月出库金额
qty_a=qty_a_l+in_qtys-out_qtys, --结存数
amount_a=amount_a_l+amount_m-(out_qtys*0), --结存金额
where yj.qty_a_l=0 and yj.in_qtys=0 and convert(char(7),yjdate,120)=left(@begindate,7)

--当上月结存数不为(即本月期初数量不为0)0或本月入库数不为0时
--这一段出错,提示:
服务器: 消息 8134,级别 16,状态 1,行 31
Divide by zero error encountered.
The statement has been terminated.
被0除.可是这个我已经把为0的记录去除了.而且我把它COPY到一个新查询中运行是没问题的.很奇怪!!
update yj set aunitprice=(amount_a_l+amount_m)/(qty_a_l+in_qtys), --平均单价
amount_o=out_qtys*convert(numeric(18,2),(amount_a_l+amount_m)/(qty_a_l+in_qtys)), --本月出库金额
qty_a=qty_a_l+in_qtys-out_qtys, --结存数
amount_a=amount_a_l+amount_m-
(out_qtys*convert(numeric(18,2),(amount_a_l+amount_m)/(qty_a_l+in_qtys))) --结存金额
where (qty_a_l<>0 or in_qtys>0) and convert(char(7),yjdate,120)=left(@begindate,7)

--上月月结中的物料在本月无出入记录
update yj set in_qtys=0,amount_m=0,out_qtys=0,
aunitprice=0, --平均单价
amount_o=0, --本月出库金额
qty_a=qty_a_l, --结存数
amount_a=amount_a_l --结存金额
where convert(char(7),yjdate,120)=
left(@begindate,7) and yj.bomid not in
(
select bomid from delivery_view where convert(char(7),datetime,120)=convert(char(7),@begindate,120)
)

----处理yj中不存在的物料-----
--当入库数据量>0时
insert into yj(yjdate,bomid,qty_a_l,amount_a_l,in_qtys,amount_m,out_qtys,aunitprice,amount_o,
qty_a,amount_a)
select convert(datetime,@begindate,120),bomid,0 qty_a_l,0 amount_a_l,
sum(in_qty) in_qty,sum(amount) amount,sum(out_qty) out_qty,
convert(numeric(18,2),sum(amount)/sum(in_qty)) aunitprice, --平均单价
sum(out_qty)*convert(numeric(18,2),sum(amount)/sum(in_qty)) as amount_o,
sum(in_qty)-sum(out_qty) qty_a,
sum(amount)-sum(out_qty)*convert(numeric(18,2),sum(amount)/sum(in_qty)) amount_a
from (
select * from delivery_view where bomid not in
( select bomid from yj where convert(char(7),yjdate,120)=convert(char(7),@begindate,120))
and convert(char(7),datetime,120)=convert(char(7),@begindate,120)
) a
group by bomid
having sum(in_qty)>0

--当入库数量=0时,平均单价按0计算
insert into yj(yjdate,bomid,qty_a_l,amount_a_l,in_qtys,amount_m,out_qtys,aunitprice,amount_o,
qty_a,amount_a)
select convert(datetime,@begindate,120),bomid,0 qty_a_l,0 amount_a_l,
sum(in_qty) in_qty,sum(amount) amount,
sum(out_qty) out_qty,0 aunitprice, --平均单价
sum(out_qty)*0 as amount_o,
sum(in_qty)-sum(out_qty) qty_a,
sum(amount)-sum(out_qty)*0 amount_a
from (
select * from delivery_view where bomid not in
( select bomid from yj where convert(char(7),yjdate,120)=convert(char(7),@begindate,120) )
and convert(char(7),datetime,120)=convert(char(7),@begindate,120)
) a
group by bomid
having sum(in_qty)=0
...全文
341 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lwl0606 2007-06-07
  • 打赏
  • 举报
回复
上面粘错了吧,应该是
where (qty_a_l<>0 or in_qtys>0) and convert(char(7),yjdate,120)=left(@begindate,7)

,你qty_a_l 和 in_qtys 会不会有 负值, (-1)+1 =0 ,
先用 select 把你的数据查出来看一下,因该还是数据的问题
COMPUTERZZZ 2007-06-07
  • 打赏
  • 举报
回复
我已经能过条件去掉了分母为0的可能(where yj.qty_a_l=0 and yj.in_qtys=0 and convert(char(7),yjdate,120)=left(@begindate,7) )
可是它还是提示:被0除.

~~~
COMPUTERZZZ 2007-06-07
  • 打赏
  • 举报
回复
就是这个问题分母为0,可是我单独把它COPY出来,到一个新查询中执行却没问题!是为什么呢?
zhj92lxs 2007-06-07
  • 打赏
  • 举报
回复
看看
lwl0606 2007-06-07
  • 打赏
  • 举报
回复
你测试的数据何存储过程里面的数据一样的嘛?
应该是表里面有的数据 会使 分母 为 0 ,你可能没有测试,如果分母不是0 ,那段代码就没问题
COMPUTERZZZ 2007-06-07
  • 打赏
  • 举报
回复
但是我单独把它COPY出来,到一个新查询中执行却没问题!是为什么呢?
lwl0606 2007-06-07
  • 打赏
  • 举报
回复

--当上月结存数不为(即本月期初数量不为0)0或本月入库数不为0时
--这一段出错,提示:
update yj set aunitprice=( case when qty_a_l+in_qtys=0 then 0 else (amount_a_l+amount_m)/(qty_a_l+in_qtys) end ), --平均单价
amount_o=out_qtys*convert(numeric(18,2),( case when qty_a_l+in_qtys=0 then 0 else (amount_a_l+amount_m)/(qty_a_l+in_qtys) end )), --本月出库金额
qty_a=qty_a_l+in_qtys-out_qtys, --结存数
amount_a=amount_a_l+amount_m-
(out_qtys*convert(numeric(18,2),( case when qty_a_l+in_qtys=0 then 0 else (amount_a_l+amount_m)/(qty_a_l+in_qtys) end))) --结存金额
where (qty_a_l<>0 or in_qtys>0) and convert(char(7),yjdate,120)=left(@begindate,7)


这样试试看行不行
肥胖的柠檬 2007-06-07
  • 打赏
  • 举报
回复
update yj set aunitprice=0, --平均单价
amount_o=out_qtys*0, --本月出库金额
qty_a=qty_a_l+in_qtys-out_qtys, --结存数
amount_a=amount_a_l+amount_m-(out_qtys*0), --结存金额
where yj.qty_a_l=0 and yj.in_qtys=0 and convert(char(7),yjdate,120)=left(@begindate,7)

--当上月结存数不为(即本月期初数量不为0)0或本月入库数不为0时


amount_a=amount_a_l+amount_m-(out_qtys*0), --结存金额
这里多了个,号

不过LZ想说的是执行出错,是什么错呢?

内容概要:本文针对无刷直流电机驱动的电子机械制动(EMB)执行器,建立了考虑Stribeck摩擦特性的非线性耦合动力学模型,并在Simulink环境中完成了系统级仿真分析。研究综合集成了电机动力学、齿轮传动机构与制动执行机构的动力学特性,构建了高保真的机电一体化系统模型。重点引入Stribeck摩擦模型以精确描述低速工况下执行器内部存在的静摩擦、粘滞摩擦与库仑摩擦之间的过渡行为,有效提升了系统在启停、反向运动等瞬态过程中的动态响应仿真精度。通过多工况仿真验证了模型的有效性,能够准确反映摩擦引起的爬行、滞后与定位误差等非线性现象,为EMB系统的高性能控制算法设计(如摩擦补偿、滑模控制)与结构优化提供了高可信度的仿真平台。; 适合人群:从事汽车电子制动系统、电机驱动控制、机电系统建模与仿真研究的研究生、科研人员及工程技术人员,需具备扎实的机械动力学、自动控制理论基础和MATLAB/Simulink仿真能力。; 使用场景及目标:①用于高精度电子机械制动系统的设计验证与性能预测;②为消除摩擦非线性影响的先进控制策略(如自适应控制、智能控制)提供精确的被控对象模型;③深入探究Stribeck摩擦等非线性因素对系统动态性能(如响应延迟、稳态误差)的作用机理; 阅读建议:读者应结合提供的Simulink模型文件,深入剖析Stribeck摩擦模块的数学实现与参数辨识方法,建议通过改变输入指令(如阶跃、正弦)和负载条件进行对比仿真,以直观理解非线性摩擦对系统动态特性的影响。

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧