Sql语句 与 触发器的问题

程序小海 2011-03-09 08:49:13
一条sql语句
update bjmshelfmj set bjmshelfmj_test1=0
where mjbj_id=23 and moju_id=15

这条能触发表中的触发器

另一条sql语句:

update bjmshelfmj set bjmshelfmj_test1=0 where moju_id in
(1,15)
and mjbj_id=23

这条却不能触发表中的触发器

触发器为:

create trigger Tri_UpdateMjbjMf
on bjmshelfmj
after update
as
if(select count(*) from bjmshelfmj,inserted as i where
bjmshelfmj.mjbj_id=i.mjbj_id and
bjmshelfmj.mshelf_id=i.mshelf_id and
bjmshelfmj.bjmshelfmj_test1=1 )=0
begin
update bjmshelf set bjmsshelf_test1=0 from inserted
where bjmshelf.mjbj_id=inserted.mjbj_id and bjmshelf.mshelf_id=inserted.mshelf_id
end


请教各位是什么原因呢?

是不是 因为 in 这个范围的影响造成的?
...全文
142 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Shawn 2011-03-10
  • 打赏
  • 举报
回复
#1. in语句提交前会翻译成or语句.
#2. 看一下2楼的解释。就会明白:
update bjmshelfmj set bjmshelfmj_test1=0 where moju_id in (15,1) and mjbj_id=23 --这条语句执行后
select count(*) from bjmshelfmj,inserted as i where
bjmshelfmj.mjbj_id=i.mjbj_id and
bjmshelfmj.mshelf_id=i.mshelf_id and
bjmshelfmj.bjmshelfmj_test1=1
这条语句执行结果会<>0,所以不会触发update.
#3. 楼主只要弄清楚inserted里面存储的值,我想就能搞清楚了.
程序小海 2011-03-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wwwwgou 的回复:]

#1. in语句提交前会翻译成or语句.
#2. 看一下2楼的解释。就会明白:
update bjmshelfmj set bjmshelfmj_test1=0 where moju_id in (15,1) and mjbj_id=23 --这条语句执行后
select count(*) from bjmshelfmj,inserted as i where
bjmshel……
[/Quote]

看了 inserted中的值 还真的是那样 不为0的

那我想问 有解决的方法吗?就是 把两条语句写在一起,却能达到分开时的效果呢?
程序小海 2011-03-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sysuleiw 的回复:]

我觉得in就是or操作了,既然是or操作,默认会不会去和1匹配,从而合并执行相当于单单执行的第一种情况?
楼主试一下mjbj_id=23 and moju_id=1 or moju_id=15
或者 mjbj_id=23 and moju_id=15 or moju_id=1
试试这两种情况
[/Quote]
这个 我试了 结果是不一样~
sysuleiw 2011-03-09
  • 打赏
  • 举报
回复
我觉得in就是or操作了,既然是or操作,默认会不会去和1匹配,从而合并执行相当于单单执行的第一种情况?
楼主试一下mjbj_id=23 and moju_id=1 or moju_id=15
或者 mjbj_id=23 and moju_id=15 or moju_id=1
试试这两种情况
程序小海 2011-03-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wwwwgou 的回复:]

#1. 你update时,inserted表中存储的是你更新后的行集记录
#2. 看一下你表中的数据,关键在于trigger中的if判断
select count(*) from bjmshelfmj,inserted as i where
bjmshelfmj.mjbj_id=i.mjbj_id and
bjmshelfmj.mshelf_id=i.mshelf_i……
[/Quote]
恩 我现在发现的问题是:单单执行

--触发器中的if语句:
if(select count(*) from bjmshelfmj,inserted as i where
bjmshelfmj.mjbj_id=i.mjbj_id and
bjmshelfmj.mshelf_id=i.mshelf_id and
bjmshelfmj.bjmshelfmj_test1=1 )=0


update bjmshelfmj set bjmshelfmj_test1=0 where moju_id=
1 and mjbj_id=23
--这条语句不符合if要求,不会执行begin里面的东西


update bjmshelfmj set bjmshelfmj_test1=0 where moju_id=
15 and mjbj_id=23
--这条语句,执行后会触发触发器, 满足if要求会执行begin中的代码


--但是我两个整合在一起写
update bjmshelfmj set bjmshelfmj_test1=0 where moju_id in
(15,1) and mjbj_id=23
--这个时候,这条sql语句,却不能达到分开写的效果



不知道 这个是什么原因造成的? 我该如何解决?
Shawn 2011-03-09
  • 打赏
  • 举报
回复
#1. 你update时,inserted表中存储的是你更新后的行集记录
#2. 看一下你表中的数据,关键在于trigger中的if判断
select count(*) from bjmshelfmj,inserted as i where
bjmshelfmj.mjbj_id=i.mjbj_id and
bjmshelfmj.mshelf_id=i.mshelf_id and
bjmshelfmj.bjmshelfmj_test1=1
是否表中存在如此的数据.
create table tb1
(
id int,
[name] varchar(10)
)
insert tb1
select 1, 'a' union all
select 2, 'b'

create table bak
(
id int,
[name] varchar(10)
)
--SQL:
update tb1
set [name] = 'c'
output
inserted.id,
inserted.name
into bak
where id in (1, 2)

select * from tb1
select * from bak

--trigger:
create trigger Tri_UpdateMjbjMf
on bjmshelfmj
after update
as
if(select count(*) from bjmshelfmj,inserted as i where
bjmshelfmj.mjbj_id=i.mjbj_id and
bjmshelfmj.mshelf_id=i.mshelf_id and
bjmshelfmj.bjmshelfmj_test1=1 )=0
begin
print '0'
update bjmshelf set bjmsshelf_test1=0 from inserted
where bjmshelf.mjbj_id=inserted.mjbj_id and bjmshelf.mshelf_id=inserted.mshelf_id
end
else
print '1'

34,575

社区成员

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

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