2017-02-27 36 views
0

我想獲取有關用戶在我的數據庫中的信息。如何獲取存儲過程以輸出從SQL * Plus中的select語句返回的表?

這是我有:

表:

create table Users(
userID int CHECK (userID > 0), 
email varchar2(30) NOT NULL, 
age varchar2(30) NOT NULL, 
numberBookedSoFar int, 
primary key(userID) 
); 

步驟:

CREATE or REPLACE PROCEDURE user_info() AS 
BEGIN 
SELECT age, COUNT(userid) AS numberofusers, 
AVG(numberbookedsofar) AS avgbooked 
FROM users 
GROUP BY age; 
END; 

但是,什麼也沒有發生。

+1

這將得到一個編譯錯誤,而不是一無所獲。您使用的是哪個版本的Oracle? (以及爲什麼使用子查詢?) –

+0

請更清楚,你有什麼資源以及你需要實現什麼 –

+0

你的過程運行一個查詢,但你沒有要求它做更多的事情。 – BobC

回答

1

編輯

據我瞭解,你需要從存儲功能

CREATE TYPE users_obj IS OBJECT (age varchar2(30), numberofusers int, avgbooked numeric); 

CREATE TYPE users_t IS TABLE OF users_obj; 

CREATE or REPLACE FUNCTION user_info RETURN users_t 
IS 
     users_table users_t := users_t(); 
     n INT := 0; 
BEGIN 
     FOR i IN (SELECT age, COUNT(userid) AS numberofusers, AVG(numberbookedsofar) AS avgbooked FROM users GROUP BY age) -- < here is your query 
     LOOP 
      users_table.EXTEND; 
      n := n + 1; 
      users_table(n) := users_obj(i.age, i.numberofusers, i.avgbooked); 
     END LOOP; 
    RETURN users_table; 
END; 


-- So, now you can call function which returns result as table 
SELECT * FROM TABLE (user_info); 
+0

是的,它的確如此。我自己運行查詢,但是當我嘗試運行這個時沒有任何反應。 –

+0

我的意思是這並不能真正回答我的問題... –

+0

@ Ana-GabrielPerez - 請參閱編輯答案 –

0

我喜歡用在包中定義的類型返回表。這使我可以將類型組合在一個緊湊的命名空間中。我不會稱這比其他答案更好,只是不同而已。這裏是一個例子:

CREATE OR REPLACE PACKAGE deleteme_pkg 
AS 
    TYPE example_t IS RECORD 
    (
     text   VARCHAR2 (10) 
     , textlength INTEGER 
    ); 

    TYPE example_tt IS TABLE OF example_t; 

    FUNCTION sample 
     RETURN example_tt 
     PIPELINED; 
END deleteme_pkg; 

CREATE OR REPLACE PACKAGE BODY deleteme_pkg 
AS 
    -- Very contrived, in practice, the rows will be from a select statement 
    FUNCTION sample 
     RETURN example_tt 
     PIPELINED 
    AS 
     l_rec example_t; 
    BEGIN 
     l_rec.text   := 'This'; 
     l_rec.textlength := LENGTH (l_rec.text); 
     PIPE ROW (l_rec); 
     l_rec.text   := 'is'; 
     l_rec.textlength := LENGTH (l_rec.text); 
     PIPE ROW (l_rec); 
     l_rec.text   := 'a'; 
     l_rec.textlength := LENGTH (l_rec.text); 
     PIPE ROW (l_rec); 
     l_rec.text   := 'test'; 
     l_rec.textlength := LENGTH (l_rec.text); 
     PIPE ROW (l_rec); 
    END sample; 
END deleteme_pkg; 

-- now select from the package function 
select * from table(deleteme_pkg.sample); 
0

只是以上答案的變種。 Usin BULK COLLECT。希望這可以幫助。

CREATE OR REPLACE type CUSTOM_OBJ 
IS 
    object 
    (
    AGE  NUMBER, 
    USER_ID NUMBER, 
    AVG_BOOKED NUMBER 
); 

CREATE OR REPLACE TYPE custom_tab 
IS 
    TABLE OF CUSTOM_OBJ; 


CREATE OR REPLACE 
    FUNCTION USER_INFO 
    RETURN CUSTOM_TAB 
    AS 
    tab custom_tab; 
    BEGIN 
    SELECT custom_obj(age,numberofusers,AVGBOOKED) BULK COLLECT 
    INTO tab 
    FROM 
     (SELECT age, 
     COUNT(userid)   AS numberofusers, 
     AVG(NUMBERBOOKEDSOFAR) AS AVGBOOKED 
     FROM users 
    ); 
    RETURN TAB; 
    END; 
相關問題