在存储过程中简单选择

时间:2015-06-10 18:19:15

标签: oracle select stored-procedures

我有这个SP,并且它不起作用的选择语句。它总是说我INTO期待。

CREATE OR REPLACE PROCEDURE GETDATANEWSP (
  P_ID NUMBER
  )
AS
BEGIN
 SELECT ID as d_id,
       USUARIO as d_usuario,
       SPAEJECUTAR as d_spaejecutar,
       NOMBREREPORTE as d_nombrereporte,
       XMLPARAMETROS as d_xmlparam,
       FECHA as d_fecha
  FROM REPORTESBATCH WHERE ID = P_ID;
END;
/

我想知道在哪里或哪里出错,即使有不同的方法也可以。

2 个答案:

答案 0 :(得分:0)

正如错误消息所示,您需要一个变量/记录或数组来将查询结果放入。你的代码应该是

CREATE OR REPLACE PROCEDURE GETDATANEWSP (   P_ID NUMBER   ) AS 
v_row REPORTESBATCH %ROWTYPE; 
BEGIN  SELECT ID as d_id,
       USUARIO as d_usuario,
       SPAEJECUTAR as d_spaejecutar,
       NOMBREREPORTE as d_nombrereporte,
       XMLPARAMETROS as d_xmlparam,
       FECHA as d_fecha  
       INTO v_row   
       FROM REPORTESBATCH 
       WHERE ID = P_ID; END;

但如果您需要进行进一步处理,您可能会发现在视觉上更容易使用每个项目的变量

CREATE OR REPLACE PROCEDURE GETDATANEWSP (   P_ID NUMBER   ) AS 
--declare each variable here 
   BEGIN  SELECT ID as d_id,
           USUARIO as d_usuario,
           SPAEJECUTAR as d_spaejecutar,
           NOMBREREPORTE as d_nombrereporte,
           XMLPARAMETROS as d_xmlparam,
           FECHA as d_fecha   
           INTO v_id, v_d_usuario, v_d_spaejecutar,
           v_d_nombrereporte,v_d_xmlparam, v_d_fecha
           FROM REPORTESBATCH 
           WHERE ID = P_ID; 
END;

答案 1 :(得分:0)

解决方案就是这个。

 CREATE OR REPLACE PROCEDURE GETDATANEWSP (
      P_ID NUMBER,
      P_CURSOR OUT SYSREFCURSOR 
     )
    AS
    BEGIN
     OPEN P_CURSOR FOR
     SELECT ID as d_id,
           USUARIO as d_usuario,
           SPAEJECUTAR as d_spaejecutar,
           NOMBREREPORTE as d_nombrereporte,
           XMLPARAMETROS as d_xmlparam,
           FECHA as d_fecha
      FROM REPORTESBATCH WHERE ID = P_ID;
    EXCEPTION WHEN NO_DATA_FOUND THEN
    NULL;
    END;

/