存储过程求教!

lyj5635285 2018-01-09 04:32:15
现有6条sql,每个sql都返回一个结果,我想把这6个结果插入到一张表中该怎么做?
比如有:select sum(a) num1 from table1 where 1=1;
select sum(a) num2 from table2 where 1=1;
...........................
select sum(a) num6 from table6 where 1=1;
上面6个sql 返回6个结果,然后我要把他们插入到一张表中,比如
insert into table(a1,a2,.....a6) values(num1,num2,.....num6);

请教大神如何用存储过程实现。
...全文
364 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyj5635285 2018-01-10
  • 打赏
  • 举报
回复
解决了,刚执行了下,没出现昨天的问题了,谢谢各位大佬的耐心解答
lyj5635285 2018-01-10
  • 打赏
  • 举报
回复
比如这种sql :select count(t.XG_POWER_ID) num from scyw.t_yj_gzqx_qxd t,xggdzhzx.v_isc_org organ where t.xg_power_id=organ.id and t.qxdzt in ('01','02','03','04','05','06','07','08','11','12','13','14','20',' 21','22') and t.ly='04' and t.xg_power_id in(select id from XGGDZHZX.v_isc_org o where o.id!='2c9a829a5a36db63015aab99111104e4') and to_char(t.bxsj, 'yyyy/mm/dd') between to_char(trunc(sysdate, 'yyyy'), 'yyyy/mm/dd') and to_char(sysdate, 'yyyy/mm/dd') 写在存储过程里无法执行
Diza1986 2018-01-10
  • 打赏
  • 举报
回复
写错了,编译执行时都是有错误信息的,要学会自查才行, 另外,select可以把into字句去掉单独调一下
jdsnhan 2018-01-10
  • 打赏
  • 举报
回复
引用 4 楼 lyj5635285 的回复:
感谢大佬指导,已解决,然后还有个问题比如我的where 条件里带了条件,比如 select sum(a) num from table where table.id='2323242' 这种就会无法编译, to_char(sysdate,'yyyy-mm-dd') 这种也不行,该如何解决?
把你的需求和问题描述清楚了,太泛泛,无法分析
碧水幽幽泉 2018-01-09
  • 打赏
  • 举报
回复
to_char(sysdate,'yyyy-mm-dd') 是打算用来做什么?
碧水幽幽泉 2018-01-09
  • 打赏
  • 举报
回复
select sum(a) num from table where table.id='2323242' 这种就会无法编译, table是关键字,不要使用。你直接where 字段=xxx就行了。
lyj5635285 2018-01-09
  • 打赏
  • 举报
回复
感谢大佬指导,已解决,然后还有个问题比如我的where 条件里带了条件,比如 select sum(a) num from table where table.id='2323242' 这种就会无法编译, to_char(sysdate,'yyyy-mm-dd') 这种也不行,该如何解决?
Diza1986 2018-01-09
  • 打赏
  • 举报
回复
大概这样

create or replace procedure test1
is
  num1 number(10);
  num2 number(10);
  num3 number(10);
  --...
begin
  select sum(a) into num1 from table1 where 1=1;
  select sum(a) into num2 from table2 where 1=1;
  select sum(a) into num3 from table3 where 1=1;
  --...
  insert into table(a1,a2,.....a6) values(num1,num2,.....num6);

  commit;
end;
lyj5635285 2018-01-09
  • 打赏
  • 举报
回复
能说的详细点吗?我第一次搞存储过程,一脸懵笔
Diza1986 2018-01-09
  • 打赏
  • 举报
回复
select ... into VAL1 ... 到变量里就可以在下面使用了

17,086

社区成员

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

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