一个关于触发器中用case when 的问题
时间:2011-12-16
来源:互联网
表a,b,c,d结构相同,其中表a中已经有多条记录如下:
id name age
1 令狐冲 22
2 乔峰 33
3 张无忌 19
4 段誉 18
5 虚竹 21
6 郭靖 42
如下写成三个语句
create trigger xxx on a for insert as
begin
insert into b select * from a where a.id = inserted.id and age <20
insert into c select * from a where a.id = inserted.id and age >= 20 and age <30
insert into d select * from a where a.id = inserted.id and age >= 30
end
这样固然能够完成目标,但是我想简化成一个语句,简化如下:
create trigger xxx on a for insert as
declare @age int
select @age = age from a
begin
insert into case when @age < 20 then b
when @age >= 20 and @age < 30 then c
else d end
select * from a
where a.id = inserted.id
end
执行,提示错误【关键字 'case' 附近有语法错误。】
我的问题是:这个错误如何纠正?能简化成一个语句吗?
菜鸟第一次发问题,浅陋勿笑
作者: ganggangd 发布时间: 2011-12-16
另外,触发器没有考虑到多行的情况,还需修改
SQL code
create trigger xxx on a for insert as declare @age int select @age = age from insertd a begin if @age < 20 begin insert into b select * from a where a.id = inserted.id end else begin if @age >= 20 and @age < 30 begin insert into c select * from a where a.id = inserted.id end else begin insert into d select * from a where a.id = inserted.id end end end
作者: jyh070207 发布时间: 2011-12-16
create trigger xxx on t1 for insert as begin declare @age int select @age = age from inserted if @age<20 insert into b select * from inserted else if (@age>20 and @age<30) insert into c select * from inserted else if @age>30 insert into d select * from inserted end
作者: kaikai_kk 发布时间: 2011-12-16
SQL code
create trigger xxx on t1 for insert as
begin
declare @age int
select @age = age from inserted
if @age<20
insert into b select * from inserted
else if (@age>20……
.
作者: fredrickhu 发布时间: 2011-12-16
create trigger xxx on a for insert as begin insert into b select * from a where a.id = inserted.id and age <20 insert into c select * from a where a.id = inserted.id and age >= 20 and age <30 insert into d select * from a where a.id = inserted.id and age >= 30 end
插入不同表,这是最简单明了的了,case when实现不了,不要改了
作者: Haiwer 发布时间: 2011-12-16
作者: Haiwer 发布时间: 2011-12-16
作者: enhydraboy 发布时间: 2011-12-16
sql server沒有oracle的insert all
如果是Oracle可以實現
作者: roy_88 发布时间: 2011-12-16
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28