在PL-SQL错误中选择:选择后INTO

时间:2012-10-16 09:22:58

标签: sql database oracle select plsql

我在测试脚本窗口

中进行了以下查询
declare
  -- Local variables here
  p_StartDate date := to_date('10/15/2012');
  p_EndDate   date := to_date('10/16/2012');
  p_ClientID  integer := 000192;
begin
  -- Test statements here
  select d.r                          "R",
         e.amount                     "Amount",
         e.inv_da                     "InvoiceData",
         e.product                    "ProductId",
         d.system_time                "Date",
         d.action_code                "Status",
         e.term_rrn                   "IRRN",
         d.commiount                  "Commission",
         0                            "CardStatus"
    from docs d
   inner join ext_inv e on d.id = e.or_document
   inner join term t on t.id = d.term_id
   where d.system_time >= p_StartDate
     and d.system_time <= p_EndDate
     and e.need_r = 1
     and t.term_gr_id = p_ClientID;
end


以下是错误

  

ORA-06550:第9行,第3列:PLS-00428:此SELECT语句中需要INTO子句


我已经使用T-SQL很长一段时间了,我是PL / SQL的新手。

这里有什么问题?

3 个答案:

答案 0 :(得分:0)

这些列需要存储在某种类型的结构中。 就像在这个例子中给出的那样

DECLARE
  deptid        employees.department_id%TYPE;
  jobid         employees.job_id%TYPE;
  emp_rec       employees%ROWTYPE;
**Create type structure**
  TYPE emp_tab IS TABLE OF employees%ROWTYPE INDEX BY PLS_INTEGER;
  all_emps      emp_tab;
BEGIN
  SELECT department_id, job_id INTO deptid, jobid 
     FROM employees WHERE employee_id = 140;
  IF SQL%FOUND THEN 
    DBMS_OUTPUT.PUT_LINE('Dept Id: ' || deptid || ', Job Id: ' || jobid);
  END IF;
  SELECT * INTO emp_rec FROM employees WHERE employee_id = 105;
  SELECT * INTO all_emps FROM employees;  **//storing into all_emp type structure**
  DBMS_OUTPUT.PUT_LINE('Number of rows: ' || SQL%ROWCOUNT);
END;
/

答案 1 :(得分:0)

假设您真正想要的是使用一些参数查询数据库,那么您有一些选择:
1-使用sqlplus或plsql developer“命令窗口”或“sql窗口”,查询如下:

  select d.r                          "R",
         e.amount                     "Amount",
         e.inv_da                     "InvoiceData",
         e.product                    "ProductId",
         d.system_time                "Date",
         d.action_code                "Status",
         e.term_rrn                   "IRRN",
         d.commiount                  "Commission",
         0                            "CardStatus"
    from docs d
   inner join ext_inv e on d.id = e.or_document
   inner join term t on t.id = d.term_id
   where d.system_time >= &p_StartDate
     and d.system_time <= &p_EndDate
     and e.need_r = 1
     and t.term_gr_id = &p_ClientID;

系统会提示您为参数赋值。

2-你可以使用plsql(虽然我不明白为什么)但是你需要一个明确的光标
例如,如果您使用的是“测试窗口”:

declare
  -- Local variables here
  p_StartDate date := to_date('10/15/2012');
  p_EndDate   date := to_date('10/16/2012');
  p_ClientID  integer := 000192;
begin
  -- Test statements here
  OPEN :src FOR select d.r                          "R",
         e.amount                     "Amount",
         e.inv_da                     "InvoiceData",
         e.product                    "ProductId",
         d.system_time                "Date",
         d.action_code                "Status",
         e.term_rrn                   "IRRN",
         d.commiount                  "Commission",
         0                            "CardStatus"
    from docs d
   inner join ext_inv e on d.id = e.or_document
   inner join term t on t.id = d.term_id
   where d.system_time >= p_StartDate
     and d.system_time <= p_EndDate
     and e.need_r = 1
     and t.term_gr_id = p_ClientID;
end

请注意,您需要在下表中添加一个名为“src”的变量并键入“cursor”,运行plsql块后它将保存结果集

答案 2 :(得分:0)

您可以尝试此解决方案:

set serveroutput on
declare
  -- Local variables here
  p_StartDate date := to_date('10/15/2012');
  p_EndDate   date := to_date('10/16/2012');
  p_ClientID  integer := 000192;
begin
  for cur in ( select d.r                          "R",
                      e.amount                     "Amount",
                      e.inv_da                     "InvoiceData",
                      e.product                    "ProductId",
                      d.system_time                "Date",
                      d.action_code                "Status",
                      e.term_rrn                   "IRRN",
                      d.commiount                  "Commission",
                      0                            "CardStatus"
                 from docs d
                 inner join ext_inv e on d.id = e.or_document
                 inner join term t on t.id = d.term_id
                 where d.system_time >= p_StartDate
                   and d.system_time <= p_EndDate
                   and e.need_r = 1
                   and t.term_gr_id = p_ClientID)
   LOOP
       dbms_output.put_line('R: '||cur.R||'Amount:  '||cur.Amount/*...*/);
   END LOOP;

end;
/