以下触发器运行错误,是何原因

cpu 2011-09-06 11:58:41
set @types=(select ccprec_sub_type from Inserted)
set @subtypes=(select ccprec_sub_subtype from Inserted)
if update(CcpRec_Sub_S_Zjr)
begin
if @subtypes='销售出库'
begin
BEGIN TRANSACTION c1
update CCPrec_Sub set CcpRec_Sub_S_Zjr_BHS=b.CcpRec_Sub_S_Zjr/1.17,CcpRec_Sub_S_Zjr_BWB_BHS=b.CcpRec_Sub_S_Zjr_BWB/1.17 from CCPrec_Sub a join inserted b on a.ID=b.ID
COMMIT TRANSACTION c1
end
else
begin
BEGIN TRANSACTION d1
update CCPrec_Sub set CcpRec_Sub_S_Zjr_BHS=b.CcpRec_Sub_S_Zjr,CcpRec_Sub_S_Zjr_BWB_BHS=b.CcpRec_Sub_S_Zjr_BWB from CCPrec_Sub a join inserted b on a.ID=b.ID
COMMIT TRANSACTION d1
end
end
...全文
58 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
-晴天 2011-09-06
  • 打赏
  • 举报
回复
set @types=(select top 1 ccprec_sub_type from Inserted)
set @subtypes=(select top 1 ccprec_sub_subtype from Inserted)

另,建议取消自定义事务,对一条语句,系统会自动设置一个事务的.
jyh070207 2011-09-06
  • 打赏
  • 举报
回复
set @types=(select ccprec_sub_type from Inserted)
set @subtypes=(select ccprec_sub_subtype from Inserted)
Inserted有多个ccprec_sub_type或ccprec_sub_subtype
改为
set @types=(select top 1 ccprec_sub_type from Inserted)
set @subtypes=(select top 1 ccprec_sub_subtype from Inserted)

怪众生太美丽 2011-09-06
  • 打赏
  • 举报
回复

set @types=(select ccprec_sub_type from Inserted)
set @subtypes=(select ccprec_sub_subtype from Inserted)
---你可以先print输出查看下其返回值,再操作..
怪众生太美丽 2011-09-06
  • 打赏
  • 举报
回复

查询语句返回值不只一个时不能直接赋值..
cpu 2011-09-06
  • 打赏
  • 举报
回复
变量已定义
DECLARE @types varchar(1)
DECLARE @subtypes varchar(30)
错误是子查询返回的值不止一个,请指正,谢谢
NBDBA 2011-09-06
  • 打赏
  • 举报
回复
还有个条件

if update(CcpRec_Sub_S_Zjr) 
update a set CcpRec_Sub_S_Zjr_BHS=b.CcpRec_Sub_S_Zjr/ case when b.ccprec_sub_subtype='销售出库' then 1.17 else 1 end,CcpRec_Sub_S_Zjr_BWB_BHS=b.CcpRec_Sub_S_Zjr_BWB/case when b.ccprec_sub_subtype='销售出库' then 1.17 else 1 end
from CCPrec_Sub a join inserted b on a.ID=b.ID
NBDBA 2011-09-06
  • 打赏
  • 举报
回复
触发器处理不能这样写,这样写只能处理一条数据,触发器内也不要再开事务

可笑这么多星星建议改成top 1避免错误

你的触发器贴出的部分代码改为如下即可

update a set CcpRec_Sub_S_Zjr_BHS=b.CcpRec_Sub_S_Zjr/ case when b.ccprec_sub_subtype='销售出库' then 1.17 else 1 end,CcpRec_Sub_S_Zjr_BWB_BHS=b.CcpRec_Sub_S_Zjr_BWB/case when b.ccprec_sub_subtype='销售出库' then 1.17 else 1 end
from CCPrec_Sub a join inserted b on a.ID=b.ID

oO寒枫Oo 2011-09-06
  • 打赏
  • 举报
回复

update CCPrec_Sub
set CcpRec_Sub_S_Zjr_BHS=b.CcpRec_Sub_S_Zjr/1.17,CcpRec_Sub_S_Zjr_BWB_BHS=b.CcpRec_Sub_S_Zjr_BWB/1.17
from CCPrec_Sub a join inserted b on a.ID=b.ID
where b.ccprec_sub_subtype='销售出库'

update CCPrec_Sub
set CcpRec_Sub_S_Zjr_BHS=b.CcpRec_Sub_S_Zjr,CcpRec_Sub_S_Zjr_BWB_BHS=b.CcpRec_Sub_S_Zjr_BWB
from CCPrec_Sub a join inserted b on a.ID=b.ID
where b.ccprec_sub_subtype<>'销售出库'
cd731107 2011-09-06
  • 打赏
  • 举报
回复
set @types=(select ccprec_sub_type from Inserted)
set @subtypes=(select ccprec_sub_subtype from Inserted)
set赋值时只能是一个值,不能是一组值,上面的select ccprec_sub_type from Inserted
或者select ccprec_sub_subtype from Inserted的返回值太多,
这说明你修改或插入时,一次性有多个记录,所以修改你的程序改为一次性只允许一条记录修改或插入即可
,这样可不修改触发器
--小F-- 2011-09-06
  • 打赏
  • 举报
回复
返回top1就没问题了
set @types=(select top 1 ccprec_sub_type from Inserted)
set @subtypes=(select top 1 ccprec_sub_subtype from Inserted)

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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