不知道这个问题如何解决

fstao 2010-09-05 09:38:50
有一个表t1,如下:
id amount sign audit
1 10 1 1
2 20 1 0
3 30 23 1
4 40 23 0


字段:id(int),amount(int),sign(int),audit(bit)

,我想实现,要对表t1操作时,只要是sign=23和audit=1时,则改不了amount的值。
如id=3时,把amount=30改为amount=60时,则恢复30,是不是在触发器或什么约束里写吗?请问怎么写?


...全文
43 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
mfqmsj 2010-09-05
  • 打赏
  • 举报
回复
yuyizhao198 2010-09-05
  • 打赏
  • 举报
回复
update t1 set amount="?" where sign != 23 and audit != 1
你需要的情况不修改不就行了
duanzhi1984 2010-09-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fstao 的回复:]
2楼,你这样写也不行的。
我还是重复我的想法,只要sign=23 and audit=1,不管amount是什么值,都不能修改,这个触发器怎么写?而且id是多少,也不是固定的。
[/Quote]


--那ID=3时,也不需要更改amount啊。本来就是30
水族杰纶 2010-09-05
  • 打赏
  • 举报
回复
create trigger tri_name on t1
for update
as
if update(amount)
if exists(select 1 from inserted where sign=23 and audit=1)
rollback
duanzhi1984 2010-09-05
  • 打赏
  • 举报
回复
create trigger tr_t1
on t1 for insert ,update
as
begin
update t1 set amount=case when sign=23 and audit=1 then i.amount else 30 end
from inserted i where i.id=t1.id
and i.id=3 and i.amount=60


end
fstao 2010-09-05
  • 打赏
  • 举报
回复
2楼,你这样写也不行的。
我还是重复我的想法,只要sign=23 and audit=1,不管amount是什么值,都不能修改,这个触发器怎么写?而且id是多少,也不是固定的。
fstao 2010-09-05
  • 打赏
  • 举报
回复
1楼的,你这样写不行的,因为amount是任何值的,不是固定值,我的想法是,只要是sign=23 and audit=1时,则不能修改amount的值,这个amount值是多少,录入多少是多少,比如amount有可能是:79、98、76等等。
duanzhi1984 2010-09-05
  • 打赏
  • 举报
回复
create trigger tr_t1
on t1 for insert ,update
as
begin
update t1 set amount=case when sign=23 and audit=1 then amount else 30 end
where exists(select 1 from inserted i where i.id=t1.id
and i.id=3 and i.amount=60)


end


没测试,试试看
duanzhi1984 2010-09-05
  • 打赏
  • 举报
回复
update set amount=case when sign=23 and audit=1 then amount  else 30 end 
where id=3 and amount=60


方在触发器中就最好的,不过要考虑考虑性能

34,589

社区成员

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

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