触发器

树先生1991 2011-04-27 09:26:21
怎样定义一个 当插入某列时,改变该表的另外一列 的触发器?

我是这么写的:
create trigger TRIGGER_EP
after insert on EP of Gdianliang
。。。。。。。。。。


sql server 2005 这样提示错误

消息 102,级别 15,状态 1,过程 TRIGGER_EP,第 2 行
'after' 附近有语法错误。

这是怎么回事呢?
...全文
91 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
树先生1991 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 jyh070207 的回复:]

--检查指定列,符合条件才触发,在where中加条件
CREATE TRIGGER TRIGGER_EP on EP
FOR insert
AS
begin
if exists(select 1 from inserted where column_name is not null --此处写指定的条件)
begin
..................
end
end
……
[/Quote]

CREATE TRIGGER TRIGGER_EP
ON EP
for insert
as
begin
if exists(select * from inserted where Gdianliang is not null)
begin
declare @gudianliang int,@fengdianliang int,@gudianjia int,@fengdianjia int,@pno int,@dianfei float
select @gudianliang=Gdianliang from inserted
select @fengdianliang=Fdianliang from inserted
select @pno=Pno from inserted
select @gudianjia=Plow,@fengdianjia=Phigh from price where Pno=@pno
set @dianfei=@gudianliang * @gudianjia + @fengdianjia * @fengdianliang
insert into EP(Dianfei) values(@dianfei)
end
end


为什么提示 EP不存在呢?数据库中确实有这个表啊
jyh070207 2011-04-27
  • 打赏
  • 举报
回复
--检查指定列,符合条件才触发,在where中加条件
CREATE TRIGGER TRIGGER_EP on EP
FOR insert
AS
begin
if exists(select 1 from inserted where column_name is not null --此处写指定的条件)
begin
..................
end
end
investruth 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 rjw1900 的回复:]
引用 6 楼 investruth 的回复:

引用 4 楼 rjw1900 的回复:
引用 2 楼 investruth 的回复:

create trigger TRIGGER_EP
on 表名
after insert
as


我想实现插入某列时 执行触发器,好像不对


你的意思是插入指定列的时候执行触发器 其他列不执行?



[/Quote]

等高手来解答...
树先生1991 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 investruth 的回复:]

引用 4 楼 rjw1900 的回复:
引用 2 楼 investruth 的回复:

create trigger TRIGGER_EP
on 表名
after insert
as


我想实现插入某列时 执行触发器,好像不对


你的意思是插入指定列的时候执行触发器 其他列不执行?
[/Quote]

investruth 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 rjw1900 的回复:]
引用 2 楼 investruth 的回复:

create trigger TRIGGER_EP
on 表名
after insert
as


我想实现插入某列时 执行触发器,好像不对
[/Quote]

你的意思是插入指定列的时候执行触发器 其他列不执行?
jyh070207 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 rjw1900 的回复:]
引用 1 楼 jyh070207 的回复:

CREATE TRIGGER TRIGGER_EP on EP
FOR insert
AS
begin
......
end


之前我也是这么写的,但是怎么让 插入一列发生变化时,执行触发器呢?
[/Quote]

你是不是想在数据更新是触发其它修改,那用更新触发器
CREATE TRIGGER TRIGGER_EP on EP
FOR update
AS
begin
......
end

树先生1991 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 investruth 的回复:]

create trigger TRIGGER_EP
on 表名
after insert
as
[/Quote]

我想实现插入某列时 执行触发器,好像不对
树先生1991 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jyh070207 的回复:]

CREATE TRIGGER TRIGGER_EP on EP
FOR insert
AS
begin
......
end
[/Quote]

之前我也是这么写的,但是怎么让 插入一列发生变化时,执行触发器呢?
investruth 2011-04-27
  • 打赏
  • 举报
回复
create trigger TRIGGER_EP
on 表名
after insert
as
jyh070207 2011-04-27
  • 打赏
  • 举报
回复
CREATE TRIGGER TRIGGER_EP on EP
FOR insert
AS
begin
......
end

34,873

社区成员

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

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