insert,触发器不执行怎么办,

zhangwuji154 2011-01-13 04:35:43
USE [test]
GO
/****** 对象: Table [dbo].[classes] 脚本日期: 01/13/2011 16:34:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[classes](
[p_weight] [float] NULL,
[classID] [int] IDENTITY(1,1) NOT NULL
) ON [PRIMARY]

新建一个班级表

USE [test]
GO
/****** 对象: Table [dbo].[Student] 脚本日期: 01/13/2011 16:35:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Student](
[StudentID] [int] NOT NULL,
[weight] [float] NULL,
[classid] [int] NULL,
PRIMARY KEY CLUSTERED
(
[StudentID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


新建一个学生表。
想插入一个学生的时候,有体重,修改全班平均体重,可是不成功,现成为了看出效果,直接改简单了,就是班级的体重按最后一个插入的学生的体重算,也不触发。为什么呀
ALTER Trigger triStudent
On Student --在Student表中创建触发器
for insert --为什么事件触发
As --事件触发后所要做的事情

begin
--declare @aaa int;
--select @aaa = count(*) from student where classid=1;
Update classes
Set p_weight=d.weight --(p_weight*@aaa+i.weight)/(@aaa+1)
From Deleted d ,Inserted i --Deleted和Inserted临时表
Where classes.classID=d.classID
end
...全文
422 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yy1987316 2011-01-13
  • 打赏
  • 举报
回复
deleted表何解?
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zhangwuji154 的回复:]

哦,明白了
select @aaa = count(*) from student where classid=1;
这句搜出来的已经插入的学生数是insert以后的,所以不用+1了
[/Quote]
对头!
华夏小卒 2011-01-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zhangwuji154 的回复:]
引用 2 楼 js_szy 的回复:
insert 触发器只有 inserted
update 和 delete 触发器 才有 deleted 和inserted

问题还真是这个问题,不过触发器怎么调试啊,我用的SQL Server 2005,按网上找的那个,找不到"调试"按钮啊
[/Quote]

sql2008 的才有调试按钮的哦,呵呵
dawugui 2011-01-13
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zhangwuji154 的回复:]
哦,明白了
select @aaa = count(*) from student where classid=1;
这句搜出来的已经插入的学生数是insert以后的,所以不用+1了
[/Quote]恭喜.
zhangwuji154 2011-01-13
  • 打赏
  • 举报
回复
哦,明白了
select @aaa = count(*) from student where classid=1;
这句搜出来的已经插入的学生数是insert以后的,所以不用+1了
飘零一叶 2011-01-13
  • 打赏
  • 举报
回复
ALTER Trigger triStudent
On Student --在Student表中创建触发器
for insert --为什么事件触发
As --事件触发后所要做的事情

begin
update c
set p_weight=weight
from classes c join
(select avg(weight) as weight,classid from Student where classid in(select classid from inserted) group by classid) b
on c.classid=b.classid
end
飘零一叶 2011-01-13
  • 打赏
  • 举报
回复
ALTER Trigger triStudent
On Student --在Student表中创建触发器
for insert --为什么事件触发
As --事件触发后所要做的事情

begin
update c
set p_weight=weight
from classes c join
(select avg(weight) as weight,classid from Student where classid in(select classid from inserted) group by classid) b
on c.classid=b.classid
end

try!
zhangwuji154 2011-01-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 js_szy 的回复:]
insert 触发器只有 inserted
update 和 delete 触发器 才有 deleted 和inserted
[/Quote]
问题还真是这个问题,不过触发器怎么调试啊,我用的SQL Server 2005,按网上找的那个,找不到"调试"按钮啊
zhangwuji154 2011-01-13
  • 打赏
  • 举报
回复
其实,就一个学生表,一个班级表,
每个学生有个学号、所在班级ID、以及自己的体重
班级就一个班级ID和全班平均体重。
华夏小卒 2011-01-13
  • 打赏
  • 举报
回复
insert 触发器只有 inserted
update 和 delete 触发器 才有 deleted 和inserted
华夏小卒 2011-01-13
  • 打赏
  • 举报
回复
Update classes
Set p_weight=i.weight --(p_weight*@aaa+i.weight)/(@aaa+1)
From classes,Inserted i --Deleted和Inserted临时表
Where classes.classID=i.classID

22,302

社区成员

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

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