过程:testAlt_item(sAltItem in Alt_item.component%type,OAltItem out varchar2)如何在select中使用?

解决方案 »

  1.   

    过程应该是不可以在select中使用的,最起码我没有用过,但是可以用函数
      

  2.   

    例如:
    SQL> SELECT TRUNC(SYSDATE,'DD')-1 FROM DUAL;TRUNC(SYSD
    ----------
    12-9月 -05TRUNC()是系统函数
      

  3.   

    function可以这样调用
    select * from tablename where id=f_test('test');如果是select f_test from tablename就不行,会当成字符串输出如果是procedure,需要定义返回变量,p_test(in1,in2,out1,out2)
      

  4.   

    waterfirer(水清) 说的中间有错。select function_name(...) from tablename是允许的。
      

  5.   

    to bobfang(匆匆过客):
    你是说把function_name(...) 当成字段吗?好像不行吧,我试过,返回值都是function_name(...) ,表tablename中的记录并没有查到啊。
      

  6.   

    bobfang(匆匆过客) 说的没错,
    测试,创建过程
    CREATE OR REPLACE FUNCTION TEST
    (
    p_PARA1 NUMBER
    )
    RETURN VARCHAR2
    IS
    v_TIME_STR VARCHAR2(20);
    BEGIN
    SELECT TO_CHAR(SYSDATE-p_PARA1,'YYYY-MM-DD') INTO v_TIME_STR FROM DUAL;
    RETURN v_TIME_STR;
    END test;
    执行:
    SQL> SELECT TEST(1) FROM DUAL;TEST(1)
    --------------------------------------------------------------------------------
    2005-09-12SQL>
      

  7.   

    to qiaozhiwei(乔):
    你和bobfang(匆匆过客) 用的都是 DUAL,实际上取的是函数的返回值。这个是可以直接取得的,就像我第一种写法中的where id=f_test('test')也可以写成where id=(select f_test('test') from dual)
    你们的写法都是对的。我说的是把返回值当成字段来查,就像select a,b from tablename就不能写成select upper('a'),upper('b') from tablename一样,不能用select f_test from tablename来查询表中的记录不知道我说的是不是清楚。
      

  8.   

    我写的函数:
    create or replace function sitem(sAlt_item Alt_item.component%type) return varchar2 is
     Result varchar2(200):='';
     v_item alt_item.component%type;
     v_rev alt_item.revision%type;
    cursor c_altitem is select distinct alternative,revision from alt_item where component=sAlt_item;
    begin
      open c_altitem;
      loop
      fetch c_altitem into v_item,v_rev ;
      if c_altitem%rowcount > 0 then
          Result:= Result ||v_item ||chr(91)||v_rev||'chr(93)';
      else  
          Result:= ' ';
      end if;
      end loop;
      close c_altitem;
      return(Result);
    end sitem;执行:
    select SITEM('9366-2110-21-2B') from dual;会出错为什么? 
    ORA-06502: PL/SQL: numeric or value error
    ORA-06512: at "GLOVIA_PROD52.SITEM", line 11
    ORA-06512: at line 1申明一下Alt_item.component%type 是varchar2(30)类型
      

  9.   

    open c_altitem;
      loop
      fetch c_altitem into v_item,v_rev ;
      if c_altitem%rowcount > 0 then
          Result:= Result ||v_item ||chr(91)||v_rev||'chr(93)';
      else  
          Result:= ' ';
      end if;
      end loop;
      close c_altitem;
    换成
      for c_altitem in c_altitem loop ;
          Result:= Result ||c_altitem .alternative||chr(91)||c_altitem .revision||'chr(93)';
      end loop;
      

  10.   

    应该是
    for c_altitem in c_altitem loop 
          Result:= Result ||c_altitem .alternative||chr(91)||c_altitem .revision||'chr(93)';
      end loop;
      

  11.   

    是这个
    create or replace function sitem(sAlt_item Alt_item.component%type) return varchar2 is
     Result varchar2(200):='';
    cursor c_altitem is select distinct alternative,revision from alt_item where component=sAlt_item;
    begin
      for c_altitem in c_altitem loop 
          Result:= Result ||c_altitem .alternative||chr(91)||c_altitem .revision||chr(93);
      end loop;
      return(Result);
    end sitem;
      

  12.   

    to:waterfirer(水清)
    越听越糊涂。: )
      

  13.   

    SQL> select ID from b;        ID
    ----------
             1
             2
             1
             2
             3
             1
             1已选择7行。SQL> select upper('id') from b;UP
    --
    ID
    ID
    ID
    ID
    ID
    ID
    ID已选择7行。SQL>
    用函数来查和上面的效果是一样的,表中的数据出不来,出的全是函数的返回值。这样应该够清楚了吧 :)
      

  14.   

    水清啊,你错了,你用upper('id') 的意思是 id表示一个字符串,而不是一个列名,给你个例子看看
    SQL> select id from test_b;I
    -
    1
    2
    1
    2
    3
    1
    1
    已选择7行。
    SQL> select upper(id) as id,upper('id')  as "ID" from test_b;I ID
    - --
    1 ID
    2 ID
    1 ID
    2 ID
    3 ID
    1 ID
    1 ID已选择7行。
      

  15.   

    噢!有道理。qiaozhiwei(乔)说得对。那自己写的函数怎么做到这样呢?
      

  16.   

    总结一下(不对的地方大家继续更正):function可以这样调用
    select * from tablename where id=f_test('test');
    有些function以列名为参数,将每列的内容通过函数算出后返回
    select f_test(a) from tablename
    如果是procedure,需要定义返回变量
    p_test(in1,in2,out1,out2)