简体   繁体   中英

Select in PL-SQL Errors: INTO After Select

I've the following query in a test script window

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


Here is the error :

ORA-06550: line 9, column 3: PLS-00428: an INTO clause is expected in this SELECT statement


I've been using T-SQL for a long time and I'm new to PL/SQL.

What's wrong here?

These columns need to be stored in some type structure. like given in this example

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

Assuming that what you really want is quering the db with some parameters, then you have some options:
1- use sqlplus or plsql developer "command window" or "sql window" with the query like this:

  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;

You'll be prompted to give values to the parameters.

2- You can use plsql (though I don't see why) but then you need an explicit cursor
For example if you're using a "test window":

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

Note that you need to add a variable in the table below with the name "src" and type "cursor", after running the plsql block it will hold the resultset

You can try this solution:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM