在对象上存储sql查询

时间:2013-01-22 13:33:22

标签: sql oracle plsql cursor dynamic-sql

有没有办法在对象中存储查询,以便您可以在游标中使用它或作为更大查询的子查询?所有这些都没有使用execute immediate?

让我们想要这个:

set serveroutput on;
DECLARE
  CNT NUMBER; 
  v1 varchar2(4000);
  SQL_QUERY view := SELECT table_name FROM USER_TABLES;
  CURSOR C1 IS 
    SQL_QUERY;
BEGIN
  OPEN C1;
    FETCH C1 INTO V1;
    dbms_output.put_line('name of the first table: '||v1);
  CLOSE C1;
  SELECT COUNT(*) INTO CNT FROM SQL_QUERY;
  dbms_output.put_line('Count: '|| cnt);
end;

有可能吗?

1 个答案:

答案 0 :(得分:0)

  

“将查询存储在对象中”

这是视图的定义。

create view foo as select table_name from user_tables;

然后

DECLARE
  CNT NUMBER; 
  v1 varchar2(4000);
  CURSOR C1 IS 
    select * from foo;
BEGIN
  OPEN C1;
    FETCH C1 INTO V1;
    dbms_output.put_line('name of the first table: '||v1);
  CLOSE C1;
  SELECT COUNT(*) INTO CNT FROM (select null from foo);
  dbms_output.put_line('Count: '|| cnt);
end;

不是你想要的吗?