数据插入(insert into), 数据导入(使用dmp文件)

sxzgz2008 2008-12-24 10:42:24
问高手2个问题:
1.
我现在有A表id NUMBER(15)(唯一,非自动增长),name VARCHAR2(30)
B表 name VARCHAR2(30);
现在我要将B表的数据插入A表中.其中A中的id非自动增长,但是要求是,它也要随着记录的增加而增加.
比如说目前a表中id最大值为10,我要添加5条记录之后,每添加一条记录,id对应自动增长1,这5条记录添加后在A表中id显示分别为11,12,13,14,15;
我写了这个语句 insert into a(id,name) select max(a.id)+1,b.name from a,b 但是报错"ORA-00918:coumn ambiguously defined "
高手帮我看看

------------------------------------------
2.
或者另外一种情况表如1中所说,但id是自动增长列(不允许为空)
但是我用语句insert into a (name) select name from b 但是报错"id不能为空!!"
高手帮忙... ... ... ... ...
...全文
293 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
耶律火柴 2008-12-29
  • 打赏
  • 举报
回复
为啥不用序列呢。。。。
yf520gn 2008-12-24
  • 打赏
  • 举报
回复
你这个用普通插入不行~要用程序块

declare
maxid number;
begin
select max(id) into maxid from a;
insert into a(select maxid+rownum,name from b);
commit;
end;
范佩西_11 2008-12-24
  • 打赏
  • 举报
回复
SQL> select * from a;

ID NAME
---------------- ------------------------------
9 fddfgh
10 gf
11 jgh
12 jjjk
13 hjkhj
14 m,hj

6 rows selected

SQL> select * from b;

NAME
------------------------------
fddfgh
gf
jgh
jjjk
hjkhj
m,hj

6 rows selected

SQL> insert into a select t.m+rownum ,b.name from b,(select max(id) m from a)t;

6 rows inserted

SQL> select * from a;

ID NAME
---------------- ------------------------------
9 fddfgh
10 gf
11 jgh
12 jjjk
13 hjkhj
14 m,hj
15 fddfgh
16 gf
17 jgh
18 jjjk
19 hjkhj
20 m,hj

12 rows selected

SQL>
范佩西_11 2008-12-24
  • 打赏
  • 举报
回复
1、
insert into a select t.m+rownum ,b.name from b,(select max(id) m from a)t;
jinjazz 2008-12-24
  • 打赏
  • 举报
回复
create sequence SQ_test
minvalue 1
maxvalue 9999999999999999999999999999
start with 11
increment by 1
nocache;

insert into a(id,name)
select SQ_test.NEXTVAL,name from b;
又是违规昵称 2008-12-24
  • 打赏
  • 举报
回复
批量插入可以用rownum:
insert into a(id, name) select rownum, b.* from b;

单条插入可以用sequence
insert into a(id, name) values(mySequence.nextval, 'myname');


zhouxu_hust 2008-12-24
  • 打赏
  • 举报
回复
学习~

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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