0

我在测试脚本窗口中有以下查询

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 的新手。

这里有什么问题?

4

3 回答 3

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;
/
于 2012-10-16T20:46:55.573 回答
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;
/
于 2012-10-16T10:58:27.613 回答
0

假设您真正想要的是使用一些参数查询数据库,那么您有一些选择:
1- 使用 sqlplus 或 plsql 开发人员“命令窗口”或“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 块后它将保存结果集

于 2012-10-16T11:00:28.533 回答