2012-10-16 25 views
0

我在一個測試腳本窗口
選擇在PL-SQL的錯誤:INTO選擇

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

這裏有什麼問題?

+0

你會期待什麼?查詢的結果集作爲塊的輸出? PL/SQL不能像那樣工作... –

+0

我只想查看選定的數據... – levi

+0

@levi:您是使用toad還是pl sql開發人員? –

回答

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

假設你真正想要的是quering一些參數數據庫,那麼你有一些選項給出:
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」,然後鍵入「光標」,運行PLSQL塊後,將持有的ResultSet

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