Call function in sql

HI all,
If I have function as
create or replace function func(id)
return number
as
begin
loop
loop
......does some calculations....
end loop
amt:=........
end loop
return amt
end
now if ihave a query as
select id,gp from t;
id gp
1 20
2 40
Now iwant the amt calculated in function func to be displayed as another column in the query as:
select id,gp,func(id) from t;
id gp func(id)
1 20 100
2 40 300
How to achieve this?
Thanks

You can simply call the function in your query:
SQL> create table t as
  2  select 1 id, 20 gp from dual union
  3  select 2, 40 from dual;
Table created.
SQL> create or replace function func(p_id in number)
  2  return number
  3  as
  4   amt number;
  5  begin
  6    ---do your own thing here, I just made something up here:
  7    for i in 1..p_id*5
  8    loop
  9      amt := 10*i;
10    end loop;
11    return amt;
12  end;
13  /
Function created.
SQL> select id
  2  ,      gp
  3  ,      func(id)
  4  from   t;
        ID         GP   FUNC(ID)
         1         20         50
         2         40        100
2 rows selected.More explanations:
http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:1547006324238

Similar Messages

  • Calling Function in SQL Server through DBLINK

    Hello
    I need to call a function that exists in other database (*SQL Server*) through DBLINK
    Is It possible? If yes, please give me the syntax
    Thanks

    Hi,
    You can call remote functions on SQL*Server if you create the database link using the Database Gateway for SQL*Server (DG4MSQL).
    You need to install and configure DG4MSQL then you can call remote functions as follows -
    SELECT function1@Remote_DB(7782) FROM dual;
    You need to set the parameter -
    HS_CALL_NAME
    in the DG4MSQL init<sid>.ora file with the owner and name of the function, for example -
    HS_CALL_NAME = "owner1.function1, owner2.function2 "
    and if you want to return values using the function you need to set the parameter in the gateway init file -
    HS_FDS_PROC_IS_FUNC=TRUE
    For full details you need to refer to the documentation -
    Oracle® Database Gateway for SQL Server User’s Guide, 11g Release 2 (11.2)
    Oracle® Database Heterogeneous Connectivity User’s Guide 11g Release 2 (11.2)
    which is available from -
    http://www.oracle.com/pls/db112/homepage
    and click on the 'Integration Information' option on the left.
    The documentation has further details on how to call and use remote functions.
    For information about installing and configuring the gateway and links to further information have a look at the following note -
    Note.1083703.1 Master Note for Oracle Gateway Products
    available in My Oracle Support
    You cannot use the Database Gateway for ODBC (DG4ODBC) to create the database link as it does not supported stored procedures or remote functions.
    Regards,
    Mike
    Edited by: mkirtley on Sep 2, 2011 11:09 AM

  • Calling Function in  SQL

    Hi,
    I wrote a function like below:
    create or replace function test_ref_cur
    return sys_refcursor is
    l_ref_cur sys_refcursor;
    begin
    open l_ref_cur for
    select 'Tom' ename from dual
    union
    select 'John' from dual
    union
    select 'Jim' from dual;
    return l_ref_cur;
    end;
    When I call this function in Sql: Select test_ref_cur from dual; it returns 1 row as below
    Test_REF_CURSOR
    (CURSOR)
    I can see the data only when I double click on (CURSOR).
    Is there a way to show the data directly in the result window: like
    Ename
    Jim
    Tim
    John
    Thanks,
    DD

    dd_ram wrote:
    Hi,
    I wrote a function like below:
    create or replace function test_ref_cur
    return sys_refcursor is
    l_ref_cur sys_refcursor;
    begin
    open l_ref_cur for
    select 'Tom' ename from dual
    union
    select 'John' from dual
    union
    select 'Jim' from dual;
    return l_ref_cur;
    end;
    When I call this function in Sql: Select test_ref_cur from dual; it returns 1 row as below
    Test_REF_CURSOR
    (CURSOR)
    I can see the data only when I double click on (CURSOR).
    Is there a way to show the data directly in the result window: likeYou need to understand what a ref cursor is.
    The reason your result is showing "(CURSOR)" (I assume you're using SQL Developer or TOAD) is that the returned result is a cursor, not a set of data. Only when you double click on it, will it actually use that cursor to fetch the data back.
    For more understanding, read the following article...
    PL/SQL 101 : Understanding Ref Cursors

  • Regarding Restrictions on calling functions from sql expressions

    Hi all,
    While going through the functions of oracle 9i plsql documentation i came across restrictions on calling a function, i that i did not understand the following statement. can anyone explain me with an example. Please.........
    Functions called from SQL statements cannot contain statements that end the transactions.
    Regards,
    Sri Ram.

    Some where from google
    http://www.ucertify.com/article/what-are-the-restrictions-on-a-user-defined-function-that-is-called-from-a-sql-expression.html
    >
    •The function cannot contain statements that end the transaction. For example, the function cannot contain transaction control statements (such as COMMIT), session control statements (such as SET ROLE), or system control statements (such as ALTER SYSTEM). Also, it cannot contain DDL statements (such as CREATE) because they are followed by an automatic COMMIT.

  • Calling function from sql folder vs report

    Hi,
    A report based on a custom sql folder is taking a long time to run. One of the things that I noticed is that when I run the sql in plsql it takes a long time, but if I remove the row where I call a function, it runs pretty quickly.
    In general, is there a difference between running a function from the sql custom folder and calling it from the report itself?
    Thanks.
    Leah

    Hi Tamir,
    I might check out the execution plans, but truthfully, understanding the plans and the meaning of the differences is not my strong point.
    I thought that maybe there was some general rule that, for instance, it is better to keep functions out of the sql folder and use them in a condition in the report itself, or the opposite, or that maybe it makes no difference at all.
    I appreciate the response.
    Thanks.
    Leah

  • Getting session hang When calling Function from SQL query

    Hi All,
    I am using Oracle 8.1.7.4.0. I have a fucntion in a Package and I am calling it from the SQL query. When I am firing the query my oracle session is going to hang position and I am not able to any thing. I have to kill the session.
    But this same thing is working fine in Oracle 9.i.
    There are no out parameter and no DML, DDL and DCL statement in this fucntion.
    Could you please get back me what is the problem on it.
    Regards
    SUN

    Check why your session hangs.
    Just a few ideas:
    * Blocking locks?
    * Endless loop?
    * Performance (maybe it is just slow in orac8i and you have to wait a bit longer). Check the execution plans of the SQL statements in your function.
    * Don't use a function, but direct SQL, it is faster in both versions.

  • Workaround for ORA-14551 WHILE CALLING FUNCTION IN SQL OVER DBLINK

    Hi,
    any idea how to workaround such issue in 9.2.0.8 (I know this is working with 11.2).
    create table mylog(id number, data date , mess clob);
    create or replace function myfunc(id in number, data in date ,mess in varchar2)
    return number is
    pragma autonomous_transaction;
    retval number;
    begin
       insert into mylog values (id , data ,mess);
    commit;
    retval := id;
    return retval;
    end;
    SQL> select schema.myfunc@dblink(2,sysdate,'bbbbbbb') from dual;
    ERROR at line 1:
    ORA-14551: cannot perform a DML operation inside a query
    ORA-06512: at "SCHEMA.MYFUNC", line 6
    ORA-06512: at line 1Regards
    GregG

    DBMS_SQL is documented in what I think 9i calls "Supplied Oracle Packages and Types" manual.
    The basic concept behind this is that it allows you to manually create a cursor and execute it (via an OCI like interface). For example, it can be used to issue remote DDLs via a database link as described in {message:id=10323373}.
    DBMS_SQL provides flexibility and power that is beyond the standard cursor constructs in PL/SQL. The system package version even enables you to execute SQL and PL/SQL code as any other schema in the database.
    You will have however to play around with DBMS_SQL (locally and remotely) to determine how to hide the fact that a local select (which Oracle assumes, correctly, does not change database state), actually changes the state of a remote database.
    Personally I would not bother with such a hack. I would use the problem to enforce an upgrade to the latest Oracle version, or kick out the crappy app that needs to use a SQL select to make database changes.

  • Functions in SQL

    Will calling functions in SQL reduce the performance of a query ?

    yes it is a user defined function. The function is a simple one such as below. This is being called in different SQL with the input parameter passed having different values.
    function getdesc(param in varchar2)
    return varchar2
    is
    begin
    select description
    into empdesc
    from emp
    where codetype = param;
    return empdesc;
    end;
    My question here is this could have been included in the SQL itself but for code maintenance/reusability the function is used. So which one do you suggest - to include in the SQL itself or to call a function ?

  • How to call function which return and out

    My function specifcation is
    FUNCTION GetGoogleScholarActivation (
    pi_clnt_id IN google_scholar_status.clnt_id%TYPE,
    pi_status_type IN ci_status_type_tab_type,
    po_status OUT ci_status_tab_type,
    po_date OUT ci_date_tab_type
    RETURN NUMBER ;
    where type is
    TYPE ci_status_type_tab_type IS TABLE OF VARCHAR2 (1) INDEX BY BINARY_INTEGER;
    TYPE ci_status_tab_type IS TABLE OF VARCHAR2 (10) INDEX BY BINARY_INTEGER;
    TYPE ci_date_tab_type IS TABLE OF VARCHAR2 (8) INDEX BY BINARY_INTEGER;
    I am trying to call function in SQL PLUS for testing as
    declare
    var number ( 10);
    TYPE p_st1 IS TABLE OF VARCHAR2 (1) INDEX BY BINARY_INTEGER;
    p_st p_st1 ;
    type p_status1 IS TABLE OF VARCHAR2 (1) INDEX BY BINARY_INTEGER;
    p_status p_status1 ;
    TYPE p_date1 IS TABLE OF VARCHAR2 (8) INDEX BY BINARY_INTEGER;
    p_date p_date1 ;
    Begin
    p_st(0) := 'A';
    var := PQDKGOSH.GetGoogleScholarActivation(11920 , p_st, p_status ,p_date );
    dbms_output.put_line ('Activation_status '|| p_status(0) );
    dbms_output.put_line ('Activation_date '|| p_date(0) );
    dbms_output.put_line ('Retun Value'|| var );
    End;
    but getting error PLS-00306: wrong number or types of arguments in call to
    'GETGOOGLESCHOLARACTIVATION'
    so how to test from SQL Plus

    HI
    I tried to execute below like this
    declare
    var number ( 10);
    p_st ci_status_type_tab_type ;
    p_status ci_status_tab_type;
    p_date ci_date_tab_type;
    Begin
    p_st(0) := 'A';
    var := PQDKGOSH.GetGoogleScholarActivation(11920 , p_st, p_status ,p_date );
    dbms_output.put_line ('Activation_status '|| p_status(0) );
    dbms_output.put_line ('Activation_date '|| p_date(0) );
    dbms_output.put_line ('Retun Value'|| var );
    End;
    still iget error
    PLS-00201: identifier 'CI_STATUS_TYPE_TAB_TYPE' must be declared
    becuse this declareation i did in package specification .
    and i am trying to execute the function .

  • SQL report region source to call a pl/sql function using DB link

    Hi - I have a pl/sql function fn_dbtype(id NUMBER) defined in database X. The pl/sql function executes couple DML statements and returns a string (a SELECT query). I am able to call this function using SQL Plus (Connected to Database X) as below and it works fine:
    declare
    vSQL VARCHAR2(100);
    begin
    vSQL := fn_dbtype(1);
    end;
    The DML operations completed fine and vSQL contains the "Select" query now.
    In APEX:
    I am trying to create a SQL report in APEX using SQL query(PL/SQL function returning a sql statement) option. I am trying to figure out what to put in the region source so that the output of the "Select" query is displayed in the report.
    Moreover APEX is hosted in a different database instance. So I would need to call this pl/sql function using a DB Link.
    Please let me know what I need to put in the region source to execute the pl/sql function which returns the "Select" query thereby displaying the query output in the report. Thanks.
    Edited by: user709584 on Mar 19, 2009 2:32 PM
    Edited by: user709584 on Mar 19, 2009 2:34 PM

    try something like this:
    return fn_dbtype(1)@dblink;

  • Calling PL/SQL functions in SQL

    Hi,
    I had a question on the behaviour of PL/SQL function when they are embedded in SQL queries.
    I have simple PL/SQl function:
    function get_city_id
    (vCUSTOMER_GROUP_ID IN NUMBER)
    RETURN VARCHAR2 AS
    vIDs VARCHAR2(1000);
    begin
    vIDs := '2,3';
    RETURN vIDs;
    end;
    I use an SQL query call this PL SQL function;
    select equipment_id from equipment where equipment_type_id in (get_city_id(0));
    When I run this query I get this error:
    ERROR at line 1:
    ORA-01722: invalid number
    I understand that the SQL compiler is complaining that get_city_id() function is not returning a number. but isn't the PL/SQL suppose to be replace with whatever string is returned from the function.
    This brings me to the second question: How can we return multiple values from a function to the SQL query, when the function is called in a SQL query. In my above explame, can the get_city_id() function return more than one city id and if yes, the how?
    Thanks for the help in advance
    regards
    Alankar

    How can we return multiple values from a function to the SQL query,Have it return a collection, e.g.
    CREATE OR REPLACE TYPE VARCHAR2_TT AS TABLE OF VARCHAR2(4000)
    CREATE OR REPLACE FUNCTION test_collection
        RETURN VARCHAR2_TT
    AS
    BEGIN
        RETURN VARCHAR2_TT(1,2,3);
    END;
    SELECT *
    FROM   employees
    WHERE  emp_id IN
           ( SELECT column_value
             FROM   TABLE(test_collection) );

  • Calling a PL/SQL function in the SQL statement

    I have a PL/SQL function which i should call in a SQL statement.
    Let's say the function is func1(parameter1,parameter2),
    it should be called like this :- SELECT func1() FROM mytable ;
    Can any one help me out on this ?
    ---Thanx in advance

    you can call pl/sql functions from pl/sql using
    select fn1('param1','param2') from table;
    if you dont want to pass paramters, you have to specify default
    value while creating function like below
    create function fn1(p1 number default null,p2 varchar2 default
    null) return...
    now you can call using
    select fn1 from table;

  • Calling a function in sql prompt

    hi
    i have created one function in pl/sql
    lik following
    create or replace function ff(a number) return number is
    x number;
    begin
    select ann_pct_rate into x from naap30_appproducts where applicant_id = a;
    return x;
    end;
    while i called this in sql prompt i got an error like this
    SQL> select ff(200610000493001) from dual;
    select ff(200610000493001) from dual
    ERROR at line 1:
    ORA-06502: PL/SQL: numeric or value error: character to number conversion error
    is it possible to do like this how?
    please advice
    thanks in advance
    sivasankar

    Hi Radha,
    i am very sorry, mistake is min i have wrongly specified the datatype. thanks for remind me. now it is ok
    i have corrected the table like this
    create or replace function ff(a number) return varchar2 is
    x varchar2(50);
    begin
    select ann_pct_rate into x from appproducts where applicant_id = a;
    return x;
    end;
    out put is
    select ff(200610000493001) from dual;
    FF(200610000493001)
    18.5000%
    thanks
    sivasankar

  • Problem in Calling a function in sql statement.

    hi,
    I am having a function ops_safex_utl.EDIT_ASSC_CNTR_LOG(id number);
    when i am trying to use this inside a sql statement as shown below, it is giving error (exception part inside the function).
    SQL> select ops_safex_utl.EDIT_ASSC_CNTR_LOG(688) from dual;
    OPS_SAFEX_UTL.EDIT_ASSC_CNTR_LOG(688)
    -1 (-- exception )
    when i am trying to call this function using a PL/SQL Block then it is woking fine as shown below.
    SQL> DECLARE
    2
    3 x NUMBER(2);
    4
    5 BEGIN
    6
    7 x := ops_safex_utl.EDIT_ASSC_CNTR_LOG(688);
    8
    9 dbms_output.put_line('x '||' '||x);
    10
    11 END;
    12 /
    hi
    insert into ops_assc_cntr_log
    insert into ops_ac_ex_gratia_log
    insert into ops_ac_sls_dlvry_slab_dtls_log
    insert into ops_ac_sls_dlvry_slab_dtls_log
    insert into ops_ac_sls_dlvry_slab_dtls_log
    insert into ops_ac_sls_dlvry_slab_dtls_log
    insert into ops_ac_sls_dlvry_slab_dtls_log
    insert into ops_ac_spl_acct_dtls_log
    insert into ops_ac_spl_acct_slab_dtls_log
    insert into ops_ac_spl_acct_slab_dtls_log
    insert into ops_ac_spl_acct_slab_dtls_log
    insert into ops_ac_spl_acct_dtls_log
    insert into ops_ac_spl_acct_slab_dtls_log
    insert into ops_ac_spl_acct_slab_dtls_log
    insert into ops_ac_spl_acct_slab_dtls_log
    update ops_assc_cntr
    success
    x 0
    PL/SQL procedure successfully completed.
    when i am trying to run the SQL statement it is returning a exception from the function.
    SELECT ops_safex_utl.EDIT_ASSC_CNTR_LOG(688) from dual --it is returning -1 (i.e exception).
    My sql client version is 9.2.0.1.0. and my data base version is 10.2.0.2.0.
    Please advice.

    Could you post the exception handler within the function.
    It sounds like you return -1 if you experience an error - it would be easier to determine the cause of the problem if you return the Oracle error details, E.g:
    EXCEPTION
       WHEN OTHERS THEN
          RETURN dbms_utility.format_error_backtrace;This will then return a meaningful error, identifying exactly what is causing the error to be generated.

  • Call a PL/SQL procedure or function from applet

    Could anyone please let me know how I could call a PL/SQL procedure
    or function from a JDBC method from applet with Internet Explorer?

    It depends from where you are calling your PLSQL routine. If it is SQL*Plus then you can use & (ampersand) with the variable to be input at run time.
    If you are executing the PLSQL routine from another application (some front end application) then it's not possible. Because when a procedure is executing at server side, the front end application does not have control, and the control is only transfered back to front end application when the PLSQL routine either completes successfully or throws an exception.
    In either case, you can not go back to the PLSQL routine.
    In this case, what you can do is, write code in your front end application to get that variable value from user and then pass that value to PLSQL routine.

Maybe you are looking for