Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在plsql脚本中打印select语句?_Sql_Oracle_Plsql_Dynamic Sql - Fatal编程技术网

如何在plsql脚本中打印select语句?

如何在plsql脚本中打印select语句?,sql,oracle,plsql,dynamic-sql,Sql,Oracle,Plsql,Dynamic Sql,我有两个数组,其中存储了表的一些有效值,并在SELECT语句中传递它们。我这样做是为了让数组a和b值相应地输入where条件 我正在使用executeimmediate打印SELECT语句,我尝试在变量中插入SELECT语句的值,并通过dbms\u输出打印它。put\u line但它给出了一个错误 我的代码并没有打印任何东西,它只是显示过程已经完成 DECLARE dest temp_1.destination%type; type arr1 IS VARRAY(4) OF VARCHA

我有两个数组,其中存储了表的一些有效值,并在
SELECT
语句中传递它们。我这样做是为了让数组
a
b
值相应地输入where条件

我正在使用
executeimmediate
打印
SELECT
语句,我尝试在变量中插入
SELECT
语句的值,并通过
dbms\u输出打印它。put\u 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引擎上运行它

如果除了使用PL/SQL之外,还有其他简单的方法可以获得结果,请告诉我。

尝试如下设置:

或启用一个文件

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;

还要检查

即使使用动态SQL,您也需要选择一些内容,这些内容必须在语句的动态部分之外。事实上,对于动态SQL,如果不这样做,则会解析查询,但不会执行查询。(其他DML和DDL的行为当然不同。)第二个块已
进入dest
,但在查询字符串中,它没有做任何事情;并且不能从匿名块返回值

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);

您仍然需要在客户端或等效程序中启用
设置serveroutput,才能查看
dest
输出。如果在
temp_1
中没有匹配任何一对条件值的行,那么您将得到一个“未找到数据”异常,因此您需要确定这是否可能,以及如果发生这种情况,您要做什么

看起来这并不需要是动态的-我在小提琴中包括了一个静态版本,使用:

select destination into dest from temp_1 where cond1 = sd(i) and cond2 = sid1(i);

dbms_output.put_line(dest);

。。。甚至是真正的PL/SQL,但可能是一个练习。

为什么要使用Execute IMMEDIATE?您可以在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;

这与其说是一个答案,不如说是一个评论,因为所有的细节都在其他网站(以及另一个城堡)的链接后面。如果包含相关部分,并给出如何将其应用于OP情况的示例,则会更好。您的spool doc链接指向不同的Oracle产品,而不是数据库。Spool是,也没有那么好的语法;并且不能在PL/SQL块中。客户机必须打开假脱机,然后执行块-块仍然必须使用
dbms\u输出
。所以,我认为短管在这里有点像是在转移视线。
    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;