2013-12-09 17 views
0

我已經嘗試了一些答案,並且無法讓我的代碼工作: 以下是我想要的,這裏是我所做的: 我有一個表,它是兩列類型,我想查詢該表使用函數返回完全相同的表路由到VB.NET,以便我可以在DatagridView控件中顯示它。 我是PL/SQL的新手,這對我來說是個問題。 我打算解決的第一個問題是創建函數。如何從oracle中的函數返回值表

-- DECLARE A RECORD TYPE 
create or replace type shipper_type AS OBJECT 
(
shipper_id number, shipper_name varchar2(7) 
); 
/

CREATE TYPE t_shipper as table of shipper_type; 
/

    create or replace function get_shipper return t_shipper is 
    temp_list t_shipper:= t_shipper(); 
    is 
    -- shipper_record shipper_type; 
    begin 
     for i in ( (select shipper.shipper_id, shipper.shipper_name) list from shipper) 
     loop 
     temp_list.extend; 
     temp_list(temp_list.last):= t_shipper(list); 
    end loop; 

    return(temp_list); 
    end get_shipper; 
/

當我嘗試編譯此代碼我收到以下錯誤: 誤差的功能GET_SHIPPER:

LINE/COL ERROR 
-------- ----------------------------------------------------------------- 
3/1  PLS-00103: Encountered the symbol "IS" when expecting one of the 
     following: 
     begin function pragma procedure subtype type <an identifier> 
     <a double-quoted delimited-identifier> current cursor delete 
     exists prior 

6/63  PLS-00103: Encountered the symbol ")" when expecting one of the 
     following: 
     . (, * @ % & - +/at mod remainder rem <an identifier> 
     <a double-quoted delimited-identifier> <an exponent (**)> as 
     from || multiset 

回答

2

,因爲你必須在你的代碼的幾個語法錯誤,你得到的編譯消息。兩個IS的實例,缺少select中的類型,return中的那些不必要的括號。你可以改正這些,但你也應該簡化你的代碼。

填充嵌套表最簡單的方法是使用批量收集。

create or replace function get_shipper return t_shipper is 
    temp_list t_shipper:= t_shipper(); 
begin 
    select shipper_type(shipper.shipper_id, shipper.shipper_name) 
    bulk collect into temp_list 
    from shipper; 

    return temp_list ; 
end get_shipper; 
/
+0

非常感謝它的工作。 – Surya

+0

如果這解決了你的問題,你應該接受它作爲正確的解決方案。 [瞭解如何](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – APC