Home » SQL & PL/SQL » SQL & PL/SQL » Generic Function
Generic Function [message #181734] Tue, 11 July 2006 05:40 Go to next message
junaidsystems
Messages: 30
Registered: June 2006
Member
I am having probelm in creating a generic fnunction, called from a stored procedure, which receive three parameters a column name, a table name, and a value.
Function searches for value given in third parameter in a table, 2nd parameter, for a particular column, 1st parameter.

Function job is to return 'T', if value is found else return 'F'.

example should be something like this:

---------------------------------------------
create or replace procedure proc1
begin

select function search_value('empno','EMP',7788) into result from dual;

if result = 'T' then
.......
end if;

end proc1;

---------------------
problem is how to use table and column name as variable.
---------------------
create or replace function search value(eno,tbl,num) return varchar2 as
begin

select eno from tbl where eno = num;
return 'T';

exception
when no_data_found then
return 'F';

end search_value;


thanks in advance

[Updated on: Tue, 11 July 2006 08:17]

Report message to a moderator

Re: Generic Function [message #181771 is a reply to message #181734] Tue, 11 July 2006 08:18 Go to previous messageGo to next message
Art Metzer
Messages: 2480
Registered: December 2002
Senior Member
This looks like a job for native dynamic SQL:
SQL> CREATE OR REPLACE FUNCTION search_value (
  2      p_column_name     IN  sys.user_tab_columns.column_name%TYPE
  3  ,   p_table_name      IN  sys.user_tables.table_name%TYPE
  4  ,   p_column_value    IN  NUMBER
  5  )
  6  RETURN VARCHAR2
  7  IS
  8      l_dummy         VARCHAR2(1);
  9      l_sql           VARCHAR2(4000) := 'SELECT ''T'' FROM sys.dual '
 10                                     || 'WHERE EXISTS ('
 11                                     || 'SELECT NULL FROM # WHERE ! = :b1)';
 12      l_return_value  VARCHAR2(1);
 13  BEGIN
 14      BEGIN
 15          SELECT NULL
 16          INTO   l_dummy
 17          FROM   sys.user_tables      ut
 18          WHERE  ut.table_name = UPPER(p_table_name);
 19      EXCEPTION
 20          WHEN NO_DATA_FOUND THEN
 21              RAISE_APPLICATION_ERROR (-20202
 22              ,                        'Table "'
 23                                       || USER
 24                                       || '"."'
 25                                       || UPPER(p_table_name)
 26                                       || '" does not exist.');
 27      END;
 28
 29      BEGIN
 30          SELECT NULL
 31          INTO   l_dummy
 32          FROM   sys.user_tab_columns utc
 33          WHERE  utc.table_name = UPPER(p_table_name)
 34          AND    utc.column_name = UPPER(p_column_name);
 35      EXCEPTION
 36          WHEN NO_DATA_FOUND THEN
 37              RAISE_APPLICATION_ERROR (-20204
 38              ,                        'Column "'
 39                                       || UPPER(p_column_name)
 40                                       || '" does not exist in table "'
 41                                       || USER
 42                                       || '"."'
 43                                       || UPPER(p_table_name)
 44                                       || '".');
 45      END;
 46
 47      l_sql := REPLACE(l_sql,'#',USER || '.' || p_table_name);
 48      l_sql := REPLACE(l_sql,'!',p_column_name);
 49      BEGIN
 50          EXECUTE IMMEDIATE l_sql
 51                       INTO l_return_value
 52                      USING p_column_value;
 53      EXCEPTION
 54          WHEN NO_DATA_FOUND THEN
 55              l_return_value := 'F';
 56      END;
 57      RETURN (l_return_value);
 58  END search_value;
 59  /
 
Function created.
 
SQL> COL does_exist FOR A10
SQL> SELECT search_value('empno','emp',7788) does_exist FROM DUAL
  2  /
 
DOES_EXIST
----------
T
 
SQL> SELECT search_value('empno','emp',12345) does_exist FROM DUAL
  2  /
 
DOES_EXIST
----------
F
 
SQL> SELECT search_value('empno','nonexistent_table',7788) does_exist FROM DUAL
  2  /
SELECT search_value('empno','nonexistent_table',7788) does_exist FROM DUAL
       *
ERROR at line 1:
ORA-20202: Table "SCOTT"."NONEXISTENT_TABLE" does not exist.
ORA-06512: at "SCOTT.SEARCH_VALUE", line 21
 
 
SQL> SELECT search_value('nonexistent_column','emp',7788) does_exist FROM DUAL
  2  /
SELECT search_value('nonexistent_column','emp',7788) does_exist FROM DUAL
       *
ERROR at line 1:
ORA-20204: Column "NONEXISTENT_COLUMN" does not exist in table "SCOTT"."EMP".
ORA-06512: at "SCOTT.SEARCH_VALUE", line 37
 
 
SQL>
Re: Generic Function [message #181824 is a reply to message #181734] Tue, 11 July 2006 14:32 Go to previous messageGo to next message
hackin
Messages: 3
Registered: July 2006
Location: VA - USA
Junior Member
I would try to do it like this:
create or replace function search_value(eno,tbl,num) return NUMBER as 

strQry VARCHAR2(150);
cntVar NUMBER;

begin

  strQry := 'select count(*) from '||tbl||' where '||eno||' = '||num;
  EXECUTE IMMEDIATE strQry into cntVar;

  return(cntVar);
end search_value;

Re: Generic Function [message #181826 is a reply to message #181824] Tue, 11 July 2006 14:54 Go to previous messageGo to next message
Art Metzer
Messages: 2480
Registered: December 2002
Senior Member
As you can see, I prefer to err on the side of robustness, meaningful error messages, scalability and providing what the OP asked for. Other than that, your solution should work just fine (with some minor tweaks to make it compile, of course).
Re: Generic Function [message #181842 is a reply to message #181826] Tue, 11 July 2006 21:30 Go to previous messageGo to next message
rleishman
Messages: 3728
Registered: October 2005
Location: Melbourne, Australia
Senior Member
Laughing
Re: Generic Function [message #182508 is a reply to message #181734] Sat, 15 July 2006 18:05 Go to previous message
William Robertson
Messages: 1643
Registered: August 2003
Location: London, UK
Senior Member
Why would the interface care about the name of a table, of all things? That just seems seriously wrong from a design point of view.

[Updated on: Sat, 15 July 2006 18:05]

Report message to a moderator

Previous Topic: Dynamically Join Tables
Next Topic: PL/SQL DOUBT regarding ULT_FILE
Goto Forum:
  


Current Time: Sun Aug 03 13:30:32 CDT 2025