触发器问题

free___sky 2011-09-08 09:54:22
表修改时 执行触发器,怎么判断 其中2个字段修改时 不触发
...全文
99 22 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
koumingjie 2011-09-08
  • 打赏
  • 举报
回复
我感觉楼主可以把问题简化一下,当销售表中的价格发生改变时,插入或者跟新价格表中的价格
free___sky 2011-09-08
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 jyh070207 的回复:]

SQL code

销售表sales,主键:id,与价格表关联:item_code
价格表prices

--插入触发器
create trigger tinsert_sales on sales
for insert
as
begin
if exists(select 1 from prices where prices.item_code = inserted.item_code……
[/Quote]

呜呜 ···
jyh070207 2011-09-08
  • 打赏
  • 举报
回复
更新的有点问题,更正一下


--更新触发器
create trigger tupdate_sales on sales
for update
as
begin
--更新指定的字段不执行
if update(a) and update(b)
and exists(select 1 from inserted i,deleted d where i.id = d.id
and i.a <> d.a
and i.b <> d.b)
begin
return
end

--更改价格,更新价格表
if update(price) and exists(select 1 from prices,inserted i,deleted d
where i.id = d.id
and prices.item_code = i.item_code
)
begin
--存在时更新
update prices
set price = i.price
from inserted i,deleted d
where prices.item_code = i.item_code
and i.id = d.id
and i.price <> d.price
end
else
begin
insert into prices(item_code,price)
select i.price,i.item_code
from inserted i
where not exists(select 1 from prices where prices.item_code = i.item_code)
end
end
jyh070207 2011-09-08
  • 打赏
  • 举报
回复

销售表sales,主键:id,与价格表关联:item_code
价格表prices

--插入触发器
create trigger tinsert_sales on sales
for insert
as
begin
if exists(select 1 from prices where prices.item_code = inserted.item_code)
begin
--存在时更新
update prices
set price = i.price
from inserted
where prices.item_code = inserted.item_code
end
else
begin
insert into prices(item_code,price)
select i.price,i.item_code
from inserted i
where not exists(select 1 from prices where prices.item_code = i.item_code)
end
end



--更新触发器
create trigger tupdate_sales on sales
for update
as
begin
--更新指定的字段不执行
if update(a) and update(b)
and exists(select 1 from inserted i,deleted d where i.id = d.id
and i.a <> d.a
and i.b <> d.b)
begin
return
end

--更改价格,更新价格表
if update(price) and exists(select 1 from prices where prices.item_code = inserted.item_code)
begin
--存在时更新
update prices
set price = i.price
from inserted i,deleted d
where prices.item_code = i.item_code
and i.id = d.id
and i.price <> d.price
end
else
begin
insert into prices(item_code,price)
select i.price,i.item_code
from inserted i
where not exists(select 1 from prices where prices.item_code = i.item_code)
end
end
free___sky 2011-09-08
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 koumingjie 的回复:]

你发的问题跟你的需求一样吗?我感觉我理解的没有错,你把需求再描述清楚一点吧
[/Quote]

我的触发器是 销售表增加修改时 更新价格表 如果有就修改 没有就新增,
但是 销售表下推销售发票时销售表也会更新,这里有2个字段更新,
我想这两个字段同时修改时 触发器不执行
然后 按那个if update()and if update()
执行后 增加销售表 价格表增加
就是这样了
koumingjie 2011-09-08
  • 打赏
  • 举报
回复
create trigger tb_twocolumnupdate
on 销售表
after update
as
begin

if update(A) and update(B) --当销售表中的两个字段同时修改时,不更新价格表
begin
rollback
end
else
begin
update 价格表....
end


end
koumingjie 2011-09-08
  • 打赏
  • 举报
回复
上班上不了qq,你等等吧,你把需求说明白,或者再开个新帖,一定有人帮你解答
koumingjie 2011-09-08
  • 打赏
  • 举报
回复
你发的问题跟你的需求一样吗?我感觉我理解的没有错,你把需求再描述清楚一点吧
free___sky 2011-09-08
  • 打赏
  • 举报
回复
恩恩
是啊 你QQ多少啊 我加你
koumingjie 2011-09-08
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 free___sky 的回复:]
引用 9 楼 koumingjie 的回复:

引用 7 楼 free___sky 的回复:
引用 5 楼 koumingjie 的回复:

SQL code

create table tb
(
ID int identity(1,1),
A varchar(10),
B varchar(10),
C varchar(10)
)

insert into tb
s……
[/Quote]

你是在销售表里写触发器啊?
free___sky 2011-09-08
  • 打赏
  • 举报
回复
if 还要跟else 吗 还是不用
我有两个if啊 一个判断是否执行触发器 一个判断是增加还是修改 都不用else吗
Gemini Dean 2011-09-08
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 geniuswjt 的回复:]
呵呵

引用 7 楼 free___sky 的回复:

引用 5 楼 koumingjie 的回复:

SQL code

create table tb
(
ID int identity(1,1),
A varchar(10),
B varchar(10),
C varchar(10)
)

insert into tb
select 'a1','b1','c……
[/Quote]
+1
free___sky 2011-09-08
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 koumingjie 的回复:]

引用 7 楼 free___sky 的回复:
引用 5 楼 koumingjie 的回复:

SQL code

create table tb
(
ID int identity(1,1),
A varchar(10),
B varchar(10),
C varchar(10)
)

insert into tb
select 'a1','b1','c1' unio……
[/Quote]
我的触发器是当销售表修改后 更新价格表 但是 销售表增加了 价格表没有增加
koumingjie 2011-09-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 free___sky 的回复:]
引用 5 楼 koumingjie 的回复:

SQL code

create table tb
(
ID int identity(1,1),
A varchar(10),
B varchar(10),
C varchar(10)
)

insert into tb
select 'a1','b1','c1' union all
select 'a2','b2','……
[/Quote]

不执行是什么意思?
geniuswjt 2011-09-08
  • 打赏
  • 举报
回复
呵呵[Quote=引用 7 楼 free___sky 的回复:]

引用 5 楼 koumingjie 的回复:

SQL code

create table tb
(
ID int identity(1,1),
A varchar(10),
B varchar(10),
C varchar(10)
)

insert into tb
select 'a1','b1','c1' union all
select 'a2','b2',……
[/Quote]
free___sky 2011-09-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 koumingjie 的回复:]

SQL code

create table tb
(
ID int identity(1,1),
A varchar(10),
B varchar(10),
C varchar(10)
)

insert into tb
select 'a1','b1','c1' union all
select 'a2','b2','c2'


create trigger tb_two……
[/Quote]


我这样写了 不执行呢
jyh070207 2011-09-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 free___sky 的回复:]
引用 1 楼 jyh070207 的回复:

更新a,b字段不触发
if update(a) or update (b) return

return? 不是禁用触发器吗
两个同时修改的时候不触发 就and吗
[/Quote]
你不是只要改其中一个不触发,用or ,两个同时修改的时候不触发 就and
放在触发器第一行
CREATE TRIGGER tupdate_tb ON tb
FOR update
AS
begin
--下面这条语句,指定如有更新a或者b字段,直接退出触发器,即后面的正常处理代码不执行
if update(a) or update (b) return


--下面是正常的处理
--
end
koumingjie 2011-09-08
  • 打赏
  • 举报
回复

create table tb
(
ID int identity(1,1),
A varchar(10),
B varchar(10),
C varchar(10)
)

insert into tb
select 'a1','b1','c1' union all
select 'a2','b2','c2'


create trigger tb_twocolumnupdate
on tb
after update
as
begin

if update(A) and update(B) --AND 或者 OR 看你的需求
rollback


end



update tb set A='1111' where ID=1
(1 行受影响)

update tb set A='1111',B='2222' where ID=1

事务在触发器中结束。批处理已中止。
koumingjie 2011-09-08
  • 打赏
  • 举报
回复

create table tb
(
ID int identity(1,1),
A varchar(10),
B varchar(10),
C varchar(10)
)

insert into tb
select 'a1','b1','c1' union all
select 'a2','b2','c2'


create trigger tb_twocolumnupdate
on tb
after update
as
begin

if update(A) or update(B)
rollback


end


update tb set A='1111' where ID=1

事务在触发器中结束。批处理已中止。




free___sky 2011-09-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jyh070207 的回复:]

更新a,b字段不触发
if update(a) or update (b) return
[/Quote]
return? 不是禁用触发器吗
两个同时修改的时候不触发 就and吗
加载更多回复(2)

34,871

社区成员

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

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