关于触发器问题

DST_good 2003-08-22 02:05:41
CREATE TRIGGER Tr_gather ON dbo.grade_table
FOR UPDATE
AS

------声明变量
declare @num char(15),@chinese decimal(18, 0),@math decimal(18, 0),@english decimal(18, 0),@gather decimal(18,0)
select @num=num,@chinese=chinese,@math=math,@english=english from inserted
------求出总分
set @gather=case
when (@chinese<0 and @math<0 and @english<0) then 0
when (@chinese<0 and @math<0) then @english
when (@chinese<0 and @english<0) then @math
when (@math<0 and @english<0) then @chinese
when @chinese<0 then @math+@english
when @math<0 then @chinese+@english
when @english<0 then @chinese+@jckcj
else @chinese+@math+@english
end
----更新总分
update grade_table
set gather=@gather
where num=@num
grade_table的结构如下:
num char(15)
chinese decimal(18, 0)
math decimal(18, 0)
english decimal(18, 0)
gather decimal(18, 0)
写这个触发器的目的就是当输入一个学生的语文、数学、外语三门成绩之后,自动算出总分,成绩小于0的表示缺考或舞弊等原因,不加到总分里
我看了这个触发器没什么错误,但是我输入成绩之后,总分(gather)变为空值了,但是有时候得出来的总分又是对的,不知道是什么原因,请各位大虾看看

...全文
17 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
DST_good 2003-08-22
  • 打赏
  • 举报
回复
我试过了,前面两位也说了,我写的那个还是没用,我把三门的值都改写成大于零的数也没有用
yujohny 2003-08-22
  • 打赏
  • 举报
回复
你的代码没错,不知你有没有加了ISNULL后再试过
因为设默认值为零,前台的一些控件如DBGRID也会将值更新为NULL
如果是加了ISNULL的代码你也试过,那我就不知道原因了。
kfcn 2003-08-22
  • 打赏
  • 举报
回复
为什么是 @xxx<0呢
没考又没看到你说默认为某个数,你干脆默认为一个负数,
然后直接判断@xxx=-2,
提个建议.
DST_good 2003-08-22
  • 打赏
  • 举报
回复
to yujohny(踏网无痕)
我写的也是只能更新一行啊,但是现在用的你这个又可以了,你能告诉我那个错在哪里吗,算法是一样的啊
yujohny 2003-08-22
  • 打赏
  • 举报
回复
这样写代码的前提是只能修改一条记录,否则就不对!
select @num=num,
@chinese = CASE WHEN ISNULL(chinese,0) <= 0 THEN 0 ELSE chinese END,
@math = CASE WHEN ISNULL(math,0) <= 0 THEN 0 ELSE math END,
@english = CASE WHEN ISNULL(english,0) <= 0 THEN 0 ELSE english END
from inserted
------求出总分
SET @gather = @chinese + @math + @english
DST_good 2003-08-22
  • 打赏
  • 举报
回复
我在这个触发器中用了一个print @num之后
我在表中更新一行值之后没有提示,是不是print @num不是这样写的
DST_good 2003-08-22
  • 打赏
  • 举报
回复
上面的语句中when @english<0 then @chinese+@jckcj是
when @english<0 then @chinese+@math
DST_good 2003-08-22
  • 打赏
  • 举报
回复
刚才我试了,还是没有用啊,
我在建表的时候已经把chinese,math,english的初试值设为0了。
添加一行数据后还是把gather设为空了
caiyunxia 2003-08-22
  • 打赏
  • 举报
回复
select @num=num,@chinese=isnull(chinese,0),@math=isnull(math,0),@english=isnull(english,0) from inserted
------求出总分
set @gather=case
when (@chinese<0 and @math<0 and @english<0) then 0
when (@chinese<0 and @math<0) then @english
when (@chinese<0 and @english<0) then @math
when (@math<0 and @english<0) then @chinese
when @chinese<0 then @math+@english
when @math<0 then @chinese+@english
when @english<0 then @chinese+@jckcj
else @chinese+@math+@english
end
----更新总分
update grade_table
set gather=@gather
where num=@num
愉快的登山者 2003-08-22
  • 打赏
  • 举报
回复
分数可能有NULL值存在,使用ISNULL(@math,0)函数。

愉快的登山者


◢◣◢◣◢◣

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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