我有一些具有相同结构的表,我想在一组中进行选择.
我想在主查询的FROM之后放置一个子查询,而不是仅仅对所有这些表进行循环.
它可能会失败吗?
谢谢!
(使用Oracle)
附加信息:我没有马上的名字!它们存储在另一个表中.是否有可能在我的主查询的FROM之后放置一个子查询?
APC*_*APC 11
"我没有马上的名字!他们存放在另一张桌子里"
Oracle在SQL中不做这种事情.您需要使用PL/SQL并组装动态查询.
create or replace function get_dynamic_rows
return sys_refcursor
is
stmt varchar2(32767) := null;
return_value sys_refcursor;
begin
for r in ( select table_name from your_table )
loop
if stmt is not null then
stmt := stmt||' union all ';
end if;
stmt := stmt||'select * from '||r.table_name;
end loop;
open return_value for stmt;
return return_value;
end;
/
Run Code Online (Sandbox Code Playgroud)
这将组合这样的查询
select * from table_1 union all select * from table_2
Run Code Online (Sandbox Code Playgroud)
UNION ALL是一个集合运算符,它将单个结果集中的多个查询的输出组合在一起,而不会删除重复项.每个查询中的列必须与数字和数据类型匹配.
因为生成的语句将自动执行,所以格式化它没有实际价值(除非查询的实际位更复杂,您可能需要调试它).
Ref Cursors是PL/SQL构造,等同于JDBC或.Net ResultSet. 了解更多.
归档时间: |
|
查看次数: |
253 次 |
最近记录: |