Unable to call local function in select statement.

Hi all,
I am unable to call the local function in select statement in below scenario.
DECLARE
l_cnt NUMBER;
FUNCTION FUN1 RETURN NUMBER
AS
BEGIN RETURN 2;
END;
BEGIN
SELECT FUN1 INTO l_cnt FROM DUAL;
DBMS_OUTPUT.PUT_LINE(l_cnt );
END;
/Any alternate way to call local function in select statement?
Thanks
Ram.

Hi,
Sorry, you can't call a locally defined function in a SQL statement, even if that SQL statement is in a PL/SQL block where the function is in scope.
This applies to packages, also. If a function is not declared in the package spec, but only in the package body, then you can't call it from SQL statements in the package body.
Why do you want a locally defined function? Why not a function defined outside the procedure?

Similar Messages

  • PL/SQL Function in Select statement

    Hi
    I am calling a function in select statement. The query works in Toad and PL/SQL developer except SQL plus and disconnecting Oracle connection.
    The error is “ERROR at line 1: ORA-03113: end-of-file on communication channel”.
    When I called the same query from BC4J View Object the error message is “java.sql.SQLException: No more data to read from socket”.
    Can any one advise me please?
    Thanks
    Srini

    Srini
    I've seen similar cases in the past with 9.2.0.x (x <= 5). What Oracle version are you using? It's worth checking the bug database (I can't just now - I don't have a valid support id in my current contract). And as Warren says, post your SQL query.
    HTH
    Regards nigel

  • How to call user fuction in select statement.

    hi,
    i am facing some problem.How to call user functions in select statement.please send me answer with example.
    Thanks
    Gopal

    Locations to Call User-Defined Functions
    • Select list of a SELECT command
    • Condition of the WHERE and HAVING clauses
    • CONNECT BY, START WITH, ORDER BY, and GROUP
    BY clauses
    • VALUES clause of the INSERT command
    • SET clause of the UPDATE command
    Restrictions on Calling Functions from SQL Expressions
    To be callable from SQL expressions, a user-defined
    function must:
    • Be a stored function
    • Accept only IN parameters
    • Accept only valid SQL data types, not PL/SQL
    specific types, as parameters
    • Return data types that are valid SQL data types,
    not PL/SQL specific types
    • Functions called from SQL expressions cannot
    contain DML statements.
    • Functions called from UPDATE/DELETE statements
    on a table T cannot contain DML on the same table
    T.
    • Functions called from an UPDATE or a DELETE
    statement on a table T cannot query the same table.
    • Functions called from SQL statements cannot
    contain statements that end the transactions.
    • Calls to subprograms that break the previous
    restriction are not allowed in the function.
    jeneesh                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Can we call a procedure in select statement?

    Can we call a procedure in select statement?

    Hi,
    Raghu_appsdba wrote:
    Can we call a procedure in select statement?No. You can call functions, but not procedures.
    If the procedure does not change the database state (for example, it doesn't update any tables), then you can wrap it in a function, or re-write it as a function.
    Here's an example of wrapping.
    CREATE OR REPLACE FUNCTION fun_x (in_txt IN VARCHAR2)
    RETURN  VARCHAR2
    IS
    BEGIN
            proc_y (in_txt);
            RETURN  in_txt
    END     fun_x;

  • Using java function in select statement

    Hi,
    I am trying to use java function in select statement.
    public class ClassA{
         private static String MyConst = "foo";
         public static String functionA(){
              return MyConst;
    in my query I have:
    select
         ClassA.functionA() AS id,
         groupId AS newID,
    from
         myChannel[now]
    ClassA is part of the application (no need to import).
    I get and error of Invalid Expression on ClassA.functionA().
    I also tried to declare the function in the processor element:
    <wlevs:processor id="proc">
         <wlevs:function function-name="A" exec-methode="functionA">
              <bean class="mtPackage.ClassA"/>
         </wlevs:function>
    <wlevs:processor>
    but then I get a different error in the processor XML file:  "An InvocationTargetException was encoutered while attemting to register the user defind function A. The message was null"
    What am I missing here?

    Hi,
    From the above description, you have tried two manners to call method functionA() in the user defined  class ClassA. One uses java cartridge manner directly and the other try to use user defined function manner.
    For the java cartridge manner, the following CQL query should work if the ClassA is really included in the OEP app. I have done similar test before, it works.
    select
         ClassA.functionA() AS id,
         groupId AS newID,
    from
         myChannel[now]
    For user defined function manner, I think two things you need to change:
    1. Need to declare the function in the EPN assembly file(under META-INF/spring/), not component configuration file(under META-INF/wlevs/). The following is an example:
    <wlevs:processor id="proc">
         <wlevs:function function-name="A" exec-methode="functionA">
              <bean class="mtPackage.ClassA"/>
         </wlevs:function>
    </wlevs:processor>
    2. Call the user defined function in the CQL query in the component configuration file under processor. For example:
    select A() from myChannel
    Regards,
    XiYing

  • Call C function in SQL statement

    dear all
    I want to know how i can call C function in SQL statement where clause

    http://asktom.oracle.com/pls/ask/f?p=4950:8:80100593788949622::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:934029542973

  • How to pass rowtype argument to a function from select statement?

    Hi all!
    I have function that takes mytable%rowtype as in parameter. can I pass entire row of mytable to the function from select statement? kind of
    select myfunction(mytable.*) from mytable where ....
    Thanks in advance

    The function can be used in a SQL statement only if it accepts SQL types and returns SQL type. %ROWTYPE being PL/SQL construct and not a SQL datatype, can not be used in this context.
    http://download-west.oracle.com/docs/cd/B14117_01/server.101/b10743/datatype.htm#i2093

  • Function in select statement need to be called only for the last record.

    Select state,
    local,
    fun_state_loc_other_details(19) details
    from state_local
    where pm_key=19
    resuts:_
    State Local Details
    AP APlocal details1
    UP UPLocal details1
    MP MPLocal details1
    i) The above query returns 100 records
    ii) fun_state_loc_other_details is also getting called 100 times. But I want this function to be called only for the last record that is 100th record.
    is there any way to do that?

    Thanks amatu.
    One more small query. Can I do it on condition based.
    Select state,
    local,
    fun_state_loc_other_details(19) details
    from state_local
    where pm_key=19
    Like if one state it need to be called once.
    AP -- 50 records
    UP - 20 records
    MP -- 10 records.
    fyi: this record no. varies
    I want the function to be called for AP once, UP once, MP once.

  • Call function within select statement

    Hi
    I would appreciate if someone help me with the below package. I need to call the function in my select statement and have to insert the results into the table
    Thanks in advance
    Regards
    Sriram
    create or replace package my_tables
    AS
    procedure pop_staff_info_tab;
    -- FUNCTION get_emergency_contact(pn_person_id IN NUMBER);
    end my_tables;
    create or replace package body pop_staff_info_tab
    is
    cursor crs_pri_staff is
    select
    papf.party_id
    ,papf.person_id
    ,papf.employee_number
    ,papf.first_name
    ,papf.middle_names
    ,papf.last_name
    ,papf.known_as
    ,papf.full_name
    ,papf.previous_last_name
    ,papf.title
    ,get_emergency_contact(pn_person_id) as PRIMARY_EMERGENCY_CONTACT_NAME
    from per_all_people_f papf
    where trunc(sysdate) between papf.effective_start_date and
    papf.effective_end_date;
    procedure pop_staff_info_tab is
    begin
    for my_cursor_staff in crs_pri_staff
    loop
    insert into staff_info
    PARTY_ID
    ,PERSON_ID
    ,EMPLOYEE_NUMBER
    ,FIRST_NAME
    ,MIDDLE_NAME
    ,LAST_NAME
    ,PREFERRED_NAME
    ,FULL_NAME
    ,PREVIOUS_LAST_NAME
    ,TITLE_CODE
    ,PRIMARY_EMERGENCY_CONTACT_NAME
    values
    my_cursor_staff.party_id
    ,my_cursor_staff.person_id
    ,my_cursor_staff.employee_number
    ,my_cursor_staff.first_name
    ,my_cursor_staff.middle_names
    ,my_cursor_staff.last_name
    ,my_cursor_staff.known_as
    ,my_cursor_staff.full_name
    ,my_cursor_staff.previous_last_name
    ,my_cursor_staff.title
    ,my_cursor_staff.PRIMARY_EMERGENCY_CONTACT_NAME);
    end loop;
    end pop_staff_info_tab;
    FUNCTION get_emergency_contact(pn_person_id IN NUMBER)
    RETURN VARCHAR2 IS
    lv_emergency_contact_name VARCHAR2(2000);
    BEGIN
    SELECT ppf.full_name
    INTO lv_emergency_contact_name
    FROM per_person_types_tl pttl
    ,per_person_types pt
    ,per_all_people_f ppf
    ,per_contact_relationships con
    ,hr_lookups hl
    WHERE con.contact_person_id = ppf.person_id
    AND ppf.person_type_id = pt.person_type_id
    AND pt.person_type_id = pttl.person_type_id
    AND pt.user_person_type = 'Emergency Contact'
    AND con.contact_type = hl.lookup_code
    AND hl.lookup_type(+) = 'CONTACT'
    AND con.contact_type = 'EMRG'
    AND con.primary_contact_flag = 'Y'
    AND trunc(SYSDATE) BETWEEN
    nvl(con.date_start,
    trunc(SYSDATE) - 1) AND
    nvl(con.date_end,
    trunc(SYSDATE) + 1)
    AND
    trunc(SYSDATE) BETWEEN nvl(ppf.effective_start_date,
    SYSDATE - 1) AND
    nvl(ppf.effective_end_date,
    SYSDATE + 1)
    AND con.person_id = pn_person_id;
    RETURN lv_emergency_contact_name;
    END get_emergency_contact;*/
    END my_tables;

    hi,
    You don't have to call the function inside the select statement. You can do as below:
    procedure pop_staff_info_tab is
    m_emrncy_cntct varchar2(1000); /*Added by Sri_Raghav*/
    begin
    for my_cursor_staff in crs_pri_staff
    loop
    m_emrncy_cntct := get_emergency_contact(my_cursor_staff.person_id); /*Added by Sri_Raghav*/
    /* This above memory varaible(m_emrncy_cntct) can be used to insert into ur new table */ /*Comment added by Sri_Raghav */
    insert into staff_info
    PARTY_ID
    ,PERSON_ID
    ,EMPLOYEE_NUMBER
    ,FIRST_NAME
    ,MIDDLE_NAME
    ,LAST_NAME
    ,PREFERRED_NAME
    ,FULL_NAME
    ,PREVIOUS_LAST_NAME
    ,TITLE_CODE
    ,PRIMARY_EMERGENCY_CONTACT_NAME
    values
    my_cursor_staff.party_id
    ,my_cursor_staff.person_id
    ,my_cursor_staff.employee_number
    ,my_cursor_staff.first_name
    ,my_cursor_staff.middle_names
    ,my_cursor_staff.last_name
    ,my_cursor_staff.known_as
    ,my_cursor_staff.full_name
    ,my_cursor_staff.previous_last_name
    ,my_cursor_staff.title
    ,my_cursor_staff.PRIMARY_EMERGENCY_CONTACT_NAME);
    end loop;
    end pop_staff_info_tab;

  • Call a C function in select statement

    I have a C function FUN1(int,int) in a c file utility.c.
    I want to use this function in PL/SQL or Pro*c in the select statement as
    Select col1,col2,fun1(col3,col4)r_fun1
    from table_1;
    Can any one tell the way to do this.
    Rergards,

    You need to implement the C routine as an external procedure. There is a whole chapter on this in the online docs here.
    But in outline...
    (1) Copy the compiled C code to a suitable location. Note that in 9.2 or lower Oracle is pretty fussy about this, and you might have configuration issues - check the docs.
    (2) Create an Oracle LIBRARY object for this routine.
    (3) Create a PL/SQL wrapper for the routine using the library.
    Cheers, APC

  • 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.

  • Functions in select statements - time consuming?!

    Hi All,
    I have a fairly complex view that uses a char based date (last business day) as one of the selection criteria. We have a report (based on the view) that needs to be run on a daily basis but the date will be different each day. I don't want to have to change the view every day to modify the date. The date is part of a larger text column and we're using a 'LIKE %dd/mm/yyyy%' to identify it in the select statement.
    I wrote a small function that returns the char based date with the %'s attached and have tested it on a small table. It works fine, however when I tried it on a larger table (50,000 rows) the difference between the response times of the following statements was unusable
    select count(*) from table where x like '%dd/mm/yyyy%' (420msec)
    and
    select count(*) from table where x like get_date_function (52 sec)
    I imagine that this is due to the function being called once for every row of the table. This isn't really going to be any good for me as the table I need to run the complex view against has nearly 750k rows and is growing every day.
    Any thoughts or suggestions as to how I can solve this problem (or work around it) would be much appreciated.
    rgds
    John

    Thanks Andrew,
    The rebuild the view on a daily basis seems to be the favorite solution at the moment. Its easy to do and will solve the problem, not elegantly but it will work.
    My understanding of calling functions inline from SQL is that the function will be called for every row that the SQL will reference regardless of what the function does.
    So if I wanted to do...
    select * from tablea where cola = sysdate-1
    on a table with 500 rows, sysdate would be called 500 times.
    If I was writing a procedure/package I would assign sysdate-1 to a variable and then do the select against the variable resulting in the sysdate function being called once and result in the query executing much faster.
    I can't help thinking that there must be a way of doing this, but then maybe I'm just being optimistic.
    I suppose what I am really trying to get is a way of calling a function once in some SQL regardless of how many rows are being referenced by the query.
    rgds
    John
    PS the code for the function is very simple, the performance hit is being caused by it being called once for every row in the table. The table it needs to run on is just under 1 million rows, which causes a significant lag ;).

  • Calling stored function within select

    Hi,
    I am working with ODP.NET and Oracle 8.1.7 for two years now and I solved a lot of problems. But now I am really in trouble:
    I need to call a stored function (that is within a package) from a select statement:
    Here is a very simple test case (the real select is much more complicated)
    select PFM.P_COSTING.CALC ( 0 ) from dual;
    this works fine in TOAD, but When I try to do the same from C#/ODP.NET, I always get the following error:
    ORA-00904: Invalid column name
    Can anybody help?
    Here my package (scheme is “PFM”):
    CREATE OR REPLACE PACKAGE BODY P_Costing AS
    FUNCTION Calc ( x number)
    return number IS
    BEGIN
    RETURN 1;
    END Calc;
    END P_Costing;
    thanks in advance
    markus

    It works fine for me, but you might not want to use "PFM.P_COSTING.CALC ( 0 )" as a column name.
    Try aliasing the expression
    select PFM.P_COSTING.CALC ( 0 ) result from dual;
    David

  • Using function in select statement

    Hi,
    CREATE FUNCTION [dbo].[udf_testFunction] 
    @value int
    RETURNS int
    AS
    BEGIN
    -- Declare the return variable here
    declare @returnValue int
    set @returnValue = @value*2;
    return @returnValue;
    END
    GO
    create table #Temp
        EmpID int, 
        EmpName Varchar(50)
    insert into #Temp(EmpID,EmpName) values(1,'Name1');
    insert into #Temp(EmpID,EmpName) values(2,'Name2');
    insert into #Temp(EmpID,EmpName) values(3,'Name3');
    insert into #Temp(EmpID,EmpName) values(4,'Name4');
    insert into #Temp(EmpID,EmpName) values(5,'Name5');
    select EmpID,EmpName, [dbo].[udf_testFunction](EmpID), [dbo].[udf_testFunction](EmpID)*EmpID,[dbo].[udf_testFunction](EmpID)+2 from #Temp
    In the above select statement i used [dbo].[udf_testFunction]() function 3 times. But i want to use only once and reuse it.
    Something like 
    select EmpID,EmpName, testfunctionvalue,testfunctionvalue*EmpID,testfunctionvalue+2 from #Temp
    Please advise me.... Thanks in Advance...

    You can use this code: 
    WITH cte
    AS ( SELECT EmpID ,
    EmpName ,
    [dbo].[udf_testFunction](EmpID) AS testfunctionvalue
    FROM #Temp
    SELECT EmpID ,
    EmpName ,
    testfunctionvalue ,
    testfunctionvalue * EmpID ,
    testfunctionvalue + 2
    FROM cte
    But using scalar functions in select clause can hurt the performance. Please see this link: 
    SQL Server Scalar User Defined Function Performance
    T-SQL Articles
    T-SQL e-book by TechNet Wiki Community
    T-SQL blog

  • Using TRIM function in select statement

    Hi All,
    I'm using the TRIM function in my select statement to eliminate the white spaces.
    But while using in select the TRIM function is not working in SQL*PLUS client(The query returns the white spaces also)
    Kindly provide some pointers regarding the issue.........
    I want to get only the data without the spaces in select statement
    Regards,
    Mohan

    Hi, Mohan,
    SQL*Plus always pads columns to make them line up nicely.
    If you have a column declared as VARCHAR2 (20), then SQL*Plus will normally display 20 characters for that column, even in the maximum actual length is, say, 5 (or even if the column always happens to be NULL).
    If you want the output to include only the actual data, without the padding that SQL*Plus adds, then concatenate all the columns into one big string column.
    People often do something like the following to generate a CSV file, with no exta spaces:
    SELECT       TO_CHAR (empno)
    || ',' || ename
    || ',' || job
    || ',' || TO_CHAR (deptno)
    || ',' || TO_CHAR (hiredate, 'DD-Mon-YYYY')     AS all_data
    FROM          scott.emp;

Maybe you are looking for