怎么用Oracle 存过中执行多个查询SQL 并返回结果,SQL和返回结果都存在一张表里面 oracle存储过程中循环查询返回多个结果集怎么集合在一起

oracle \u591a\u4e2aselect\u67e5\u8be2\u8bed\u53e5\uff0c\u600e\u4e48\u628a\u67e5\u8be2\u7684\u7ed3\u679c\u5728\u540c\u4e00\u5f20\u8868\u4e2d\u5e76\u6392\u663e\u793a\u554a\uff1f\uff1f\uff1f

insert into D
select A.a, A.b, B.c, B.d, C.f
from A, B, C;

\u4f60\u53ef\u4ee5\u5148\u628a\u6570\u636e\u96c6\u4fdd\u5b58\u5230array\u91cc\u9762\uff0c\u5b8c\u4e86\u4e4b\u540e\u518d\u7528\u4e00\u6b21\u6027\u7684\u5bfc\u51fa\u6765\u3002
\u53c8\u6216\u8005\u4f60\u53ef\u4ee5\u68c0\u67e5\u4f60\u7684\u5faa\u73af\u67e5\u8be2\uff0c\u662f\u5426\u80fd\u7528\u4e00\u6761sql\u6765\u5b8c\u6210\u3002

你这种要求不应该这么设计,首先你十几条的SQL的结果集字段类型和个数不一定一样,所以没法存储,只有你的SQL返回的结果集都一样或者可以枚举且字段类型均一样,这样就可以用自定义类型作为B字段。

解决方案一:单表存储结果

思路:要求所有的SQL返回结果集列数不多于N个,然后创建一个N个字段的TYPE,此处假设N=5,代码如下:

--创建多个字段的type
create or replace type test_column_type as object (
col_1     varchar2(300),
col_2     varchar2(300),
col_3     varchar2(300),
col_4     varchar2(300),
col_5     varchar2(300)
);
--创建引用多个字段的type
create or replace type test_tab_column_type as table of test_column_type;

--创建表
create table test_tab (
a     varchar2(300),
b     test_tab_column_type)
nested table b store as nested_tab return as value;


--写数据的语句
insert into test_tab
values
('1',test_tab_column_type(test_column_type(1,1,1,1,1),test_column_type(2,2,2,2,2)));

解决方案二:多表存储结果

思路:主子表,先创建一个表只用来存SQL,另外一个表存SQL的结果,大致如下:

create t_sql(
id   number,
a    varchar2(3000)
);
create t_sql_result(
id       number,
col1     varchar2(400),
col2     varchar2(400),
...
colN     varchar2(400)
);

如果所有SQL返回的列数都一样,那很简单,直接写到 t_sql_result 表里面即可。

如果SQL返回的列数不一样,则需要判断每个sql返回的列数,然后拼接动态SQL,将结果写入 t_sql_result 表。



set feedback off heading off term off
set pages 0 trim on trims on lines 32767 long 999999
set echo off
spool result.sql
Select 'alter table '||table_name||' allocate extent;' from user_tables where num_rows=0;
spool off
把以上部分放入一个脚本文件中再执行,会在当前目录下生成你要的文件result.sql

直接用spool轻轻松松, 不要把简单的问题想复杂

扩展阅读:数字转换为yyyy-mm-dd ... oracle 合计行 ... oracle 行转列 列转行 ... oracle 日期转换 ... oracle批量insert脚本 ... 序列使用方法 oracle ... oracle日期格式化yyyymmdd ... oracle 用户过期解除 ... oracle 异常处理怎么写 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网