22,302
社区成员




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
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
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