如何在 pl sql 脚本中打印 select 语句?
How to print select statment in a plsql script?
我有两个数组,其中存储了 table 的一些有效值,并在 SELECT
语句中传递它们。我这样做是为了让数组 a
和 b
值相应地进入 where 条件。
我正在使用 EXECUTE IMMEDIATE
来打印 SELECT
语句,我尝试将 SELECT
语句的值插入到变量中并通过 dbms_output.put_line
打印它但是它给出了一个错误。
而且我的代码不打印任何东西,它只显示程序已完成。
DECLARE
dest temp_1.destination%type;
type arr1 IS VARRAY(4) OF VARCHAR2(50);
sd arr1;
type arr2 IS VARRAY(4) OF VARCHAR2(50);
sid1 arr2;
total integer;
BEGIN
sd := street_directional('a','b','c','d');
sid1 := street_direction('1','2','3','4');
total := sd.count;
FOR i in 1 .. total LOOP
execute immediate q'[select destination from temp_1 where cond1 =(:var1) and cond2 = (:var2) ]' using sd(i),sid1(i);
END LOOP;
END;
这不会给出任何输出,它只是显示过程已完成。
我试过这样做:
execute immediate q'[select destination from temp_1 where cond1 =(:var1) and cond2 = (:var2) into dest]' using sd(i),sid1(i);
return dest;
-- or printline
dbms_output.put_line(dest);
但是报错了。
我在 oracle 引擎上保留了 serveroutput on
和 im 运行。
如果除了使用 PL/SQL 之外还有一些简单的方法可以得到结果,请告诉我。
尝试设置 dbms_output.enable(null) 如:
execute immediate q'[select destination from temp_1 where cond1 =(:var1) and cond2 = (:var2)]'
into dest
using sd(i),sid1(i);
dbms_output.enable(null);
dbms_output.put_line(dest);
或对文件启用spooling。
spool on to 'output.txt';
execute immediate q'[select destination from temp_1 where cond1 =(:var1) and cond2 = (:var2)]'
into dest
using sd(i),sid1(i);
spool off;
也检查ask tom
即使使用动态 SQL 你也需要 select 到 一些东西,它必须在语句的动态部分之外。事实上,对于动态 SQL,如果你不这样做,那么查询将被解析但不会执行。 (当然,其他 DML 和 DDL 的行为不同。)您的第二个块有 into dest
但在查询字符串内部,它什么也没做;而且你不能 return 来自匿名块的值。
execute immediate q'[select destination from temp_1 where cond1 =(:var1) and cond2 = (:var2)]'
into dest
using sd(i),sid1(i);
dbms_output.put_line(dest);
您仍然需要在客户端或等效程序中安装 set serveroutput on
才能看到 dest
输出。如果在 temp_1
中没有任何一对条件值的匹配行,那么您将得到一个未找到数据的异常,因此您需要决定这是否可能以及您想要做什么如果它发生了。
看起来这并不真的需要动态 - 我在 fiddle 中包含了一个静态版本,使用:
select destination into dest from temp_1 where cond1 = sd(i) and cond2 = sid1(i);
dbms_output.put_line(dest);
...甚至真的 PL/SQL,但大概是一个练习。
为什么要使用立即执行?您可以在 plsql 中打印 select 语句,如下所示。
DECLARE
dest temp_1.destination%type;
type arr1 IS VARRAY(4) OF VARCHAR2(50);
sd arr1;
type arr2 IS VARRAY(4) OF VARCHAR2(50);
sid1 arr2;
total integer;
BEGIN
sd := arr1('a','b','c','d');
sid1 := arr2('1','2','3','4');
total := sd.count;
FOR i in 1 .. total LOOP
select destination into dest from temp_1 where cond1 =sd(i) and cond2 = sid1(i) ;
dbms_output.put_line(dest);
END LOOP;
END;
我有两个数组,其中存储了 table 的一些有效值,并在 SELECT
语句中传递它们。我这样做是为了让数组 a
和 b
值相应地进入 where 条件。
我正在使用 EXECUTE IMMEDIATE
来打印 SELECT
语句,我尝试将 SELECT
语句的值插入到变量中并通过 dbms_output.put_line
打印它但是它给出了一个错误。
而且我的代码不打印任何东西,它只显示程序已完成。
DECLARE
dest temp_1.destination%type;
type arr1 IS VARRAY(4) OF VARCHAR2(50);
sd arr1;
type arr2 IS VARRAY(4) OF VARCHAR2(50);
sid1 arr2;
total integer;
BEGIN
sd := street_directional('a','b','c','d');
sid1 := street_direction('1','2','3','4');
total := sd.count;
FOR i in 1 .. total LOOP
execute immediate q'[select destination from temp_1 where cond1 =(:var1) and cond2 = (:var2) ]' using sd(i),sid1(i);
END LOOP;
END;
这不会给出任何输出,它只是显示过程已完成。
我试过这样做:
execute immediate q'[select destination from temp_1 where cond1 =(:var1) and cond2 = (:var2) into dest]' using sd(i),sid1(i);
return dest;
-- or printline
dbms_output.put_line(dest);
但是报错了。
我在 oracle 引擎上保留了 serveroutput on
和 im 运行。
如果除了使用 PL/SQL 之外还有一些简单的方法可以得到结果,请告诉我。
尝试设置 dbms_output.enable(null) 如:
execute immediate q'[select destination from temp_1 where cond1 =(:var1) and cond2 = (:var2)]'
into dest
using sd(i),sid1(i);
dbms_output.enable(null);
dbms_output.put_line(dest);
或对文件启用spooling。
spool on to 'output.txt';
execute immediate q'[select destination from temp_1 where cond1 =(:var1) and cond2 = (:var2)]'
into dest
using sd(i),sid1(i);
spool off;
也检查ask tom
即使使用动态 SQL 你也需要 select 到 一些东西,它必须在语句的动态部分之外。事实上,对于动态 SQL,如果你不这样做,那么查询将被解析但不会执行。 (当然,其他 DML 和 DDL 的行为不同。)您的第二个块有 into dest
但在查询字符串内部,它什么也没做;而且你不能 return 来自匿名块的值。
execute immediate q'[select destination from temp_1 where cond1 =(:var1) and cond2 = (:var2)]'
into dest
using sd(i),sid1(i);
dbms_output.put_line(dest);
您仍然需要在客户端或等效程序中安装 set serveroutput on
才能看到 dest
输出。如果在 temp_1
中没有任何一对条件值的匹配行,那么您将得到一个未找到数据的异常,因此您需要决定这是否可能以及您想要做什么如果它发生了。
看起来这并不真的需要动态 - 我在 fiddle 中包含了一个静态版本,使用:
select destination into dest from temp_1 where cond1 = sd(i) and cond2 = sid1(i);
dbms_output.put_line(dest);
...甚至真的 PL/SQL,但大概是一个练习。
为什么要使用立即执行?您可以在 plsql 中打印 select 语句,如下所示。
DECLARE
dest temp_1.destination%type;
type arr1 IS VARRAY(4) OF VARCHAR2(50);
sd arr1;
type arr2 IS VARRAY(4) OF VARCHAR2(50);
sid1 arr2;
total integer;
BEGIN
sd := arr1('a','b','c','d');
sid1 := arr2('1','2','3','4');
total := sd.count;
FOR i in 1 .. total LOOP
select destination into dest from temp_1 where cond1 =sd(i) and cond2 = sid1(i) ;
dbms_output.put_line(dest);
END LOOP;
END;