RT 知道表名tablename 关键字KEYID为keyid_no 要求返回 字段NUM的值
   SQL语句为 SELECT num FROM tablename WHERE KEYID=keyid_no
   但是希望做成一个function 带入参数为字段名,关键字,和表名 让此函数返回值
   我写了函数
    create or replace
    function fun_forlog(name varchar2, keyid_no int,tablenamec varchar2) return varchar2 as
     test varchar2(40);
    begin 
     select name 
      into test 
      from count2    
      where keyid=2; 
     return test;
    end fun_forlog;
 但是返还回来的不是我所要求的值 

解决方案 »

  1.   

    你的name既然是名称的字符串,那只能是使用动态语句执行
    execute immediate 'select ' || name ||'';
      

  2.   

     create   or   replace 
            function   fun_forlog(name   varchar2,   keyid_no   int,tablenamec   varchar2)   return   varchar2   as 
              test   varchar2(40); 
            begin   
              execute immediate 'select '||   name   
                || ' from  ' || tablenamec || ' where   keyid= ' || Keyid_no 
              returning into test;   
              return   test; 
            end   fun_forlog; 
      

  3.   

    In the following example, we create a function (count_in_table) which can be used to count records that satisfy a certain condition in a table whose name is unknown at the time of the creation of the function. attr:
    The name of the column whose value must match attrval in order for the record to be counted 
    attrval:
    The value that attr is compared against 
    tbl:
    The name of the table. set feedback off
    set linesize 120
    set pagesize 0create or replace function count_in_table
      (attr in varchar2, attrval in varchar2, tbl in varchar2)
      return number
    is
      cnt number;
    begin
      execute immediate 'select count(1) from ' || tbl || ' where ' || attr || ' = :a' into cnt using attrval;
      return cnt;
    end;
    /
      

  4.   

    In the following example, we create a function (count_in_table) which can be used to count records that satisfy a certain condition in a table whose name is unknown at the time of the creation of the function. attr:
    The name of the column whose value must match attrval in order for the record to be counted 
    attrval:
    The value that attr is compared against 
    tbl:
    The name of the table. set feedback off
    set linesize 120
    set pagesize 0create or replace function count_in_table
      (attr in varchar2, attrval in varchar2, tbl in varchar2)
      return number
    is
      cnt number;
    begin
      execute immediate 'select count(1) from ' || tbl || ' where ' || attr || ' = :a' into cnt using attrval;
      return cnt;
    end;
    /
      

  5.   

    47522341  谢谢你回答我哦 你2楼发的代码 编译可以通过,但是执行的时候却报错,不可执行 我用的是ORACLE8I 是不是又版本限值呀 ?
     
    SQL>exec dbms_output.put_line(fun_forlog('num',3,'count2'))begin dbms_output.put_line(fun_forlog('num',3,'count2')); end;ORA-06547: INSERT?UPDATE?? DELETE  ?????? RETURNING ??
    ORA-06512: ?"TEST.FUN_FORLOG", line 4
    ORA-06512: ?line 1
      

  6.   

    47522341的帖子内容如下:create       or       replace   
                    function       fun_forlog(name       varchar2,       keyid_no       int,tablenamec       varchar2)       return       varchar2       as   
                        test       varchar2(40);   
                    begin       
                        execute   immediate   'select   ' ¦ ¦       name       
                            ¦ ¦   '   from     '   ¦ ¦   tablenamec   ¦ ¦   '   where       keyid=   '   ¦ ¦   Keyid_no   
                        returning   into   test;       
                        return       test;   
                    end       fun_forlog;   
    其中粗体的returning应该去掉。select的结果直接into test就可以,而其他如update,insert,delete需要返回值即returning into test.