Domanda

I have several Tables that contains just id and description in that particular schema, i wonder if it is possible to write one generic function which will read something like:

create or replace FUNCTION tab_lookup (key_field char,key_value char,from_table char,return_field char) RETURN char IS
a varchar2(1000);
BEGIN

select &return_field into a
from &from_table  
where &key_field=key_value;
return(a);
exception 
    when others
    then 
    return('*ERR*');
END;

i want to use it at inside package application which only 50 users will be using.

È stato utile?

Soluzione

Modified your version, by changing it using Dynamic SQL and changed the Input parameters' Datatype as VARCHAR2

CREATE OR REPLACE FUNCTION tab_lookup (key_field VARCHAR2,
                                       key_value VARCHAR2,
                                       from_table VARCHAR2,
                                       return_field VARCHAR2,
                                       return_type VARCHAR2)
RETURN VARCHAR2 IS
result_a varchar2(1000);
query_string VARCHAR2(4000);
/*version 0.1*/
BEGIN

  query_string := 'SELECT '||return_field|| 
                  'FROM '||from_table||  
                  'WHERE '||key_field || ' = :key_value ';

  IF(return_type = 'SQL') THEN
     result_a := query_string;
  ELSE

       //this line will not work in forms 6i remove the using key_value word
       EXECUTE IMMEDIATE query_string USING key_value  into result_a;
  END IF;

  RETURN (result_a);
EXCEPTION 

 // add DBMS_ASSERT Exceptions
WHEN 
    NO_DATA_FOUND THEN 
       RETURN(NULL);
WHEN
    TOO_MANY_ROWS THEN 
       RETURN('**ERR_DUPLICATE**');
WHEN OTHERS
THEN 
        RETURN('*ERR_'||SQLERRM);
END;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top