2013-09-24 62 views
0

我有這樣一個PL/SQL語句(它不是一個存儲過程或函數):PLSQL收益表中設置

DECLARE 
    curstatus VARCHAR(2); 
    person_id varchar(6) := 100; 
BEGIN 
    SELECT cur_status INTO curstatus FROM Persons WHERE PID=cast(person_id as number(6)); 
    IF curstatus='F' THEN 
    SELECT PID FROM Reports WHERE Report_Type='F' AND PID = person_id; 
    END IF; 
END; 

我想從報告(它們顯示在屏幕上)返回所有PID,但是,我收到一個錯誤:an INTO clause is expected

請注意,這不是一個存儲過程或函數。我怎樣才能退回桌子?

+0

無法從匿名PL/SQL塊返回結果集。 –

+1

爲什麼要在純SQL中編寫PL/SQL?然後你可以獲得免費的返回位。 –

回答

4

當然純粹的SQL解決方案,類似於以下,將工作?

SELECT r.PID 
    FROM Reports r, Persons p 
    WHERE r.Report_Type = 'F' AND r.PID = p.PID 
    AND p.cur_status = 'F' 
    AND p.PID = 100; 
+0

謝謝+1。這將工作,但它不會很好地集成到應用程序中。我很快就可以接受這個答案。 – w0051977

1

錯誤消息的原因是第二個select語句中沒有into

DECLARE 
    curstatus VARCHAR(16); 
    person_id varchar(6) := 100; 
    p_pid VARCHAR(32); 
BEGIN 
    SELECT cur_status INTO curstatus FROM Persons WHERE PID=cast(person_id as number(6)); 
    IF curstatus='F' THEN 
    SELECT PID into p_pid FROM Reports WHERE Report_Type='F' AND PID = person_id; 
    END IF; 
END; 

您可以使用它返回sys_refcursor

例如函數

CREATE OR REPLACE FUNCTION testfunc 
    RETURN SYS_REFCURSOR 
AS 
    curstatus persons.cur_status%TYPE; 
    person_id VARCHAR (6) := 100; 
    r_cursor SYS_REFCURSOR; 
BEGIN 
    SELECT cur_status 
     INTO curstatus 
     FROM persons 
    WHERE pid = CAST (person_id AS NUMBER (6)); 

    IF curstatus = 'F' 
    THEN 
     OPEN r_cursor FOR 
      SELECT pid 
       INTO p_pid 
       FROM reports 
      WHERE report_type = 'F' AND pid = person_id; 
    END IF; 

    RETURN r_cursor; 
END; 
0

試試這個:

DECLARE 
    CURSTATUS VARCHAR (2); 
    PERSON_ID VARCHAR (6) := 100; 
    RESULTS REPORTS.PID%TYPE; 
BEGIN 
    SELECT 
      CUR_STATUS 
    INTO 
      CURSTATUS 
    FROM 
      PERSONS 
    WHERE 
      PID = CAST (PERSON_ID AS NUMBER (6)); 

    IF CURSTATUS = 'F' 
    THEN 
     SELECT 
       PID 
     INTO 
       RESULTS 
     FROM 
       REPORTS 
     WHERE 
       REPORT_TYPE = 'F' 
       AND PID = PERSON_ID; 
     DBMS_OUTPUT.PUT_LINE ('Result is:'||RESULTS); 
    END IF; 

END; 
0

您不能直接從PL/SQL塊中調用查詢,因爲oracle對SQL和PL/SQL使用兩個不同的引擎。

所有SQL查詢都在SQL引擎中執行,所有PL/SQL操作都在PL/SQL引擎中處理。當你在PL/SQL塊中運行查詢時,數據集必須從sql傳遞到pl/sql引擎。而在PL/SQL引擎中,它需要作爲變量或集合綁定到內存區域。對於這個綁定過程,我們需要INTO子句。一旦聲明瞭變量並使用INTO子句綁定數據,PL/SQL引擎就可以使用這些數據。