ITPub博客

首页 > 数据库 > 数据库开发技术 > 一条 Insert 语句插入多张表

一条 Insert 语句插入多张表

原创 数据库开发技术 作者:zhangxu052012 时间:2009-11-05 15:09:45 0 删除 编辑

昨天去笔试,遇到一道题,比较有趣。有三张表:t、t1和t2,它们的表结构都一样 (id int , name varchar(20)),要求把 t 表中 id 为 1的数据插入到 t1 表中, id 为 2 的数据插入到 t2 表中,并且只能用一条 insert 语句实现。

[@more@]

下面我们来做下测试:

create table t (id int , name varchar(20));

create table t1 (id int , name varchar(20));

create table t2 (id int , name varchar(20));

insert into t values(1 , '张三');

insert into t values(1 , '李四');

insert into t values(2 , '王五');

insert into t values(2 , '王二麻子');

commit;

------insert statement------

insert all

when id = 1 then into t1 values (id, name)

when id = 2 then into t2 values (id, name)

select id, name from t;

commit;

------test result------
SQL> select * from t1;

ID NAME
--------------------------------------- --------------------
1 张三
1 李四

SQL> select * from t2;

ID NAME
--------------------------------------- --------------------
2 王五
2 王二麻子

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/14758327/viewspace-1028464/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-11-25

  • 博文量
    4
  • 访问量
    23729