再提问一下 关于触发器的问题

lonly 2002-06-26 05:23:08
说明一下我的a,b 是举个例子,正式的表不止2列,我要修改的也不是prmiary key 。

我的要求就是,当表中的a列的第n行的值被改变,则该表中的b列的第n行的值也发生相应的改变,其他行的b列值不改变。。。

也可以这么说,怎么知道我拿一行被改变了。。。
...全文
19 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzsswz 2002-06-26
  • 打赏
  • 举报
回复

CREATE TRIGGER [goto_account_relhand] ON dbo.unify_goods
FOR INSERT,update
AS
declare @money money
declare @price money
declare @invoice_num varchar(16)
declare @sub_number varchar(16)
declare @point_reg varchar(6)
select @point_reg = inserted.point_reg from inserted
select @invoice_num = invoice_num,@sub_number = sub_number,@money = amount*price
from inserted where inserted.point_reg = @point_reg
if @point_reg = '0'
begin
select @invoice_num = invoice_num,@sub_number = sub_number,@money = amount*price
from inserted where inserted.point_reg = @point_reg
update dbo.unify_goods set money = convert(decimal(12,2),@money) from dbo.unify_goods aa,inserted where aa.invoice_num = @invoice_num
and aa.sub_number = @sub_number and aa.point_reg = @point_reg
end
else if @point_reg = '2'
begin
select @invoice_num = invoice_num,@sub_number = sub_number, @price = case amount when 0 then 0.00 else money/amount end
from inserted where inserted.point_reg = '2'
update dbo.unify_goods set price = convert(decimal(12,2),@price) from dbo.unify_goods aa,inserted
where aa.invoice_num = @invoice_num and aa.sub_number = @sub_number and aa.point_reg = '2'
end
hele 2002-06-26
  • 打赏
  • 举报
回复
a,b都定义主键id,其中a中的主键id为自增长类型。
在a中创建触发器,当填加一条记录时,自动填加一条与该id一样的记录。
即两个表中有相同id的数据。
在a中创建更新触发器,当a中的某个数据改变时,相应改变b中同id的记录数据即可。
Good Luck!
lonelyghost 2002-06-26
  • 打赏
  • 举报
回复
我主观地认为触发器不能完成这项任务。如果写一个 trigger 在 a 被更新时,更新 b,难道不会导致无限地递归下去吗?

再从 normal form 的角度来看,如果 a 改变,b 也跟着边,那么 b 是否函数依赖于 a?如果是,这样的设计就不符合某种(第2或者3)normal form。
bqj 2002-06-26
  • 打赏
  • 举报
回复
被修改的行在insterd表中,如果要判断修改的是哪一列,就在触发器中用update(columnname)来判断

34,575

社区成员

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

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