将一个表的数据插入到另一个表,存储过程

arameter 2011-12-29 09:43:09
有一个user表,有uid和uname字段。有一个customer表,有cid,cname,cpass字段,现在需要写一个存储过程:
要把select c.cname from customer c查出来的人名插入加到user表,uid为可以用序列。

存储过程怎么写?


实际项目中select c.cname from customer c是一个复杂的查询,只投影名字字段,如查出张三、李四,在向user添加两条记录:
1 张三
2 李四
...全文
1422 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
raymonshi 2012-01-01
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 zhaohongbo84 的回复:]

begin
insert into user(id,name,age)
select 序列,c.cname,0 from customer c;
commit;
end
[/Quote]
11楼这,有点雷同了吧,
看看8楼怎么写的,
一口盐水
zhaohongbo84 2011-12-31
  • 打赏
  • 举报
回复
begin
insert into user(id,name,age)
select 序列,c.cname,0 from customer c;
commit;
end

zlb_chen 2011-12-31
  • 打赏
  • 举报
回复
--测试系列
create sequence UISSEQ
minvalue 0
maxvalue 999999999999
start with 1
increment by 1
cache 100;

-- 这种写法游标的定义有点局限,不能带参数条件
create or replace procedure testAdd
as
sSql varchar2(2000);
iNextValue number;
cursor c1 is select distinct c.cname from customer c;
begin
for c2 in c1 loop
sSql := 'begin select UISSEQ.NEXTVAL into :1 from dual; end;';
execute immediate sSql using out iNextValue;

sSql := 'begin insert into user(uId,uname) values (:1,:2); end;';
execute immediate sSql using in iNextValue,in c2.cname
end loop;
commit;
end;

--这种写法游标的定义可以带参数条件
create or replace procedure testAdd
as
sSql varchar2(2000);
sSql1 varchar2(2000);
iNextValue number;
vName varchar2(32);
type cur_type is ref cursor;
c1 cur_type
begin
sSql1 := 'select distinct c.cname from customer c ';
open c1 for sSql1
loop
fetch c1 into vName;
exit when c1%notfound;
sSql := 'begin select UISSEQ.NEXTVAL into :1 from dual; end;';
execute immediate sSql using out iNextValue;

sSql := 'begin insert into user(uId,uname) values(:1,:2); end;';
execute immediate sSql using in iNextValue,in vName;

end loop;
close c1;
commit;

end;

应这样。。晕。。写错表名了。
zlb_chen 2011-12-31
  • 打赏
  • 举报
回复
--测试系列
create sequence UISSEQ
minvalue 0
maxvalue 999999999999
start with 1
increment by 1
cache 100;

-- 这种写法游标的定义有点局限,不能带参数条件
create or replace procedure testAdd
as
sSql varchar2(2000);
iNextValue number;
cursor c1 is select distinct c.cname from customer c;
begin
for c2 in c1 loop
sSql := 'begin select UISSEQ.NEXTVAL into :1 from dual; end;';
execute immediate sSql using out iNextValue;

sSql := 'begin insert into customer(uId,uname) values (:1,:2); end;';
execute immediate sSql using in iNextValue,in c2.cname
end loop;
commit;
end;

--这种写法游标的定义可以带参数条件
create or replace procedure testAdd
as
sSql varchar2(2000);
sSql1 varchar2(2000);
iNextValue number;
vName varchar2(32);
type cur_type is ref cursor;
c1 cur_type
begin
sSql1 := 'select distinct c.cname from customer c ';
open c1 for sSql1
loop
fetch c1 into vName;
exit when c1%notfound;
sSql := 'begin select UISSEQ.NEXTVAL into :1 from dual; end;';
execute immediate sSql using out iNextValue;

sSql := 'begin insert into customer(uId,uname) values(:1,:2); end;';
execute immediate sSql using in iNextValue,in vName;

end loop;
close c1;
commit;

end;

valid13 2011-12-30
  • 打赏
  • 举报
回复
我像这样写为何不行?
insert into user2 (uid,uname) values
(select copyt.nextval,c.cname from customer c);

copyt是序列
valid13 2011-12-30
  • 打赏
  • 举报
回复
楼上的写法可以,但我说掉了一点:

user2表中还有的字段customer表中没有,比如user2表还有number型字段age,我想插入时让age为空,但我不想写null,因为有还很多字段,要写很多null不好看。

我像这样写为何不行:
insert into user2 (ID,name) values
(select copyt.nextval,c.cus_name from customer c);
arameter 2011-12-30
  • 打赏
  • 举报
回复
SELECT copyt.nextval,c.cname INTO vUID, vUName
FROM customer;

可是我的customer表里有多条数据,SELECT INTO要求select from的查询结果只有一行

我心飞翔 2011-12-30
  • 打赏
  • 举报
回复
那就调整一下罢,非得用一条语句吗,呵呵。
代码参考:

DECLARE
vUID user2.uid%TYPE;
vUNme user2.uname%TYPE;
BEGIN
SELECT copyt.nextval,c.cname INTO vUID, vUName
FROM customer;
INSERT INTO user2(uid, uname) VALUES(vUID, vUName);
END;
raymonshi 2011-12-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 valid13 的回复:]

楼上的写法可以,但我说掉了一点:

user2表中还有的字段customer表中没有,比如user2表还有number型字段age,我想插入时让age为空,但我不想写null,因为有还很多字段,要写很多null不好看。

我像这样写为何不行:
insert into user2 (ID,name) values
(select copyt.nextval,c.cus_name f……
[/Quote]


想让没有插入的字段不为空,可以在建表的时候,让number字段默认为0.
或者插入sql这样写:
begin
insert into user(id,name,age)
select 序列,c.cname,0 from customer c;
commit;
end


raymonshi 2011-12-29
  • 打赏
  • 举报
回复


begin
insert into user
select 序列,c.cname from customer c;
commit;
end
arameter 2011-12-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 luiseradl 的回复:]
分太少了,要不就给你写一写了,呵呵。
其实非常简单。
[/Quote]

呵呵,已经加了帖子加分了

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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