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;

Similar Messages

  • Function within SELECT statement

    Help please...
    How can I get the my custom RFCal function to work within
    this select statement??
    My Error:
    The value SUM((a2.act_rate + a2.act_gratuity) *
    ap2.actpac_quantity) cannot be converted to a number.
    My Code:
    SELECT outer select blah blah,
    (SELECT #RFCal("SUM((a2.act_rate + a2.act_gratuity) *
    ap2.actpac_quantity)" + p2.pac_serviceFee, p2.pac_occupancy)#
    FROM tablep2 p2 INNER JOIN tableap2 ap2 ON p2.pac_id =
    ap2.pac_id
    INNER JOIN tablea2 a2 ON ap2.act_id = a2.act_id
    WHERE p2.pac_id = p.pac_id) AS myTotal
    FROM tablep p INNER JOIN tableap ap ON p.pac_id = ap.pac_id
    INNER JOIN tablea a ON ap2.act_id = a.act_id
    WHERE outer select blah blah

    Thanks for responding Dan.
    Your option of running the function on the result via a
    cfloop is what I am currently doing... problem is I want to be able
    to sort the function result. So with that, what would be the
    simplest method?
    I will test running an additional query of queries on the
    result.

  • Call function with select arguments

    Hi Gurus,
    I have problem to call function inside select statements as follow:
    select a.ID_ELE2, a.ID_ELE3, a.DT_FIS_YR, c.NU_FIS_PER, c.dt,
    (case
    when c.ld is null then
    GET_LD_CHECK (a.DT_FIS_YR,c.NU_FIS_PER, a.ID_ELE3, a.ID_ELE2) -- 1
    -- GET_LD_CHECK ('2009',7, '8010', '7493') --- 2
    else
    c.ld
    end ) description
    from ACCOUNT a, TRANSACTION c
    where a.DT_FIS_YR ='2009'
    and a.ID_ELE3 <> '0000'
    and c.TY_SRC not in ('CL', 'CN')
    and a.DT_FIS_YR = c.nu_fis_yr
    and a.AK = c.AK_FGCHAR
    and trim(a.ID_ELE3) ='8010'
    and c.NU_FIS_PER <> 14
    order by 1,4,5,6
    the 1 doesn't output result but the 2 it does! How can pass the select result to the function?
    Thanks in advance for your help.
    Ben

    The statement / function call seems to be ok. So there are not much chances left for your call to return different (=non) values.
    1) It could be that you have different values in the column then during your test call.
    2) Maybe your function raises an error and that error is supressed in some ugly WHEN OTHERS EXCEPTION => Solution: Get rid of the error handler.
    3) datatype conversion. For example if a.dt_fis_yr is a number value, then you should test with number values and not with strings. GET_LD_CHECK (2009,7, '8010', '7493'). Same logic goes for the other paramters, make sure the datatype is correct and matches the function parameter.

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

  • 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

  • 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

  • Class not getting called by the Select Statement

    Dear All,
    I have created a new class "Z1_EXIT_VARIABLES" in my Dev system and coded for a new Exit Variable for InfoObject "0FISCPER3" in this class.
    But on debugging the Function module "EXIT_SAPLRRS0_001" (INCLUDE ZXRSRU01), I could find that the Select statement
    <i>select CLSNAME
    into  l_clsname
    from VSEOIMPLEM
    where REFCLSNAME = 'ZBWIF_VAR_EXIT'.
    </i>
    calls ALL the classes mentioned in the table "VSEOIMPLEM" with    REFCLSNAME = ZBWIF_VAR_EXIT, EXCEPT the class "Z1_EXIT_VARIABLES". Therefore, the code written by me is not getting executed.
    Also, after debugging the same function module in Quality system, I could find that not all of the classes mentioned in the table "VSEOIMPLEM"are getting called by the select statement.
    (I do not have authorization for CMOD in my Dev system)
    Can anyone suggest a possible solution to the problem ?
    Regards
    Shalabh Jain

    Hi,
    Still waiting for response.
    The issue is not with finding control and performing action on it.
    The coded UI automation script is able to find the control and performing click on that, and after that is is supposed to display the results, which is not happening.
    I am not able to find out why the results are not displaying with coded UI but it's working as expected with SELENIUM web driver.
    I have to do it with CODED UI VS2013, So i request you to give me some solution for this issue, I have done automation earlier for some similar web applications, but never faced this situation.
    Studio: VS2013
    Scripting Language: C#
    HtmlHyperlink performSearchLink = new HtmlHyperlink(inventoryWindow);
     performSearchLink.SearchProperties.Add(HtmlHyperlink.PropertyNames.InnerText, "Search");
                Mouse.Click(performSearchLink);
    this code is working fine, it's clicking on search but not displaying the results.
    someone reply asap.
    Thanks
    Adrusta

  • 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

  • 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

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

  • Calling function within function as parameter

    We're planning to switch from MS-OLEDB to Oracle OLEDB and are expecting several troubles.
    One of these is that MS supports calling functions with another function within whilst Oralce does not?
    Example (works fine with MS):
    {? = call *<FUNCTION_1>* ( ?, ?, *<FUNCTION_2>* ( ?, ?, ? ) + 1, ?, 1 )}
    This raises ORA-01036: Variablenname/-nummer ungültig with Oracle's OLEDB.
    Any ideas?
    Thanks in advance!
    Edited by: user617919 on 04.11.2011 01:28

    Hi,
    Whenever you have a problem, please post a complete test script that people can run to re-create the problem and test their ideas. In this case, include complete CREATE PACKAGE and CREATE PAGKAGE BODY statements, CREATE TABLE and INSERT statements for any tables needed, some test calls to the the package, and the results you want from those test calls gibven that data.
    Always say which versin of Oracle you're using (e.g. 11.2.0.2.0).
    See the forum FAQ {message:id=9360002}
    LostNoob wrote:
    --Calculate salary minus before tax deductions
    FUNCTION sal_mns_btd (emp_no IN NUMBER)
    RETURN NUMBER
    IS
    tot_mns_btd Number;
    BEGIN
    SELECT e.sal - bef_tax_ded(bef_ded_tot)
    INTO  tot_mns_btd
    FROM  emp
    WHERE empno = emp_no;
    RETURN tot_mns_btd;
    END sal_mns_btd;
    If bef_tax_ded is a function in the same package, then what you posted is a correct way to call it.
    What is bef_ded_tot? If it's a NUMBER column in emp, then what you posted makes sense. If bef_ded_tot is a local variable inside some other function, then it doesn't make sense.. How to do what you want depends on what you want, and I can't tell you how to do it unless I understand what you want.
    Also, the table alias e isn't defined. It doesn't look like you need a table alias.

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

  • 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