Returning a set of registers in a PL/SQL function

Does anybody knows if there is some way to return a set of registers (like a cursor) in a PL/SQL function without using a temporary table?
Thank you

To: [email protected]
Sub: Returning a set of registers in a PL/SQL function
Hi: Nei S
Create a Package first:
CREATE OR REPLACE PACKAGE sp_xxxPKG AS
TYPE RT1 IS RECORD (
sp_col1 emp.emp_no%TYPE,
sp_col2 emp.emp_name%TYPE);
TYPE RCT1 IS REF CURSOR RETURN RT1;
END;
Then Create a Procedure:
CREATE OR REPLACE PROCEDURE sp_xxx (
RC1 IN OUT sp_xxxPKG.RCT1) AS
BEGIN
OPEN RC1 FOR
SELECT emp_no, emp_name FROM emp;
RETURN;
END;
null

Similar Messages

  • PL/SQL Function to convert fields & set max length

    I am having a problem with the following assignment:
    Write a stored PL-SQL function called namefct that will convert two varchar2 fields (first and last names) into the format: lastname, firstname with a maximum length of 10 chars. If the combined size of the two names is greater than 10 characters, you will issue an error message indicating the name has been shortened to 10 characters and return only the first ten characters, otherwise return the entire name. Test the namefct function by using the staff table created in Part 1 (above). Turn in: the SQL statements to build the function, execute the sample code and the resulting output.
    This is the start of my attempt, excluding the error message that is to be issued:
    CREATE OR REPLACE FUNCTION namefct (fname VARCHAR2, lname VARCHAR2) IS
    BEGIN
    column "full name" format a10;
    SELECT namefct(fname, lname) as "full name" from staff;
    END namefct
    Any advice or assistance would be greatly appreciated, since my Professor seems unwilling to assist me. Thanks!

    Write a stored PL-SQL function called namefct that
    will convert two varchar2 fields (first and last
    names) into the format: lastname, firstname with a
    maximum length of 10 chars. If the combined size of
    the two names is greater than 10 characters, you will
    issue an error message indicating the name has been
    shortened to 10 characters and return only the first
    ten characters, otherwise return the entire name.
    Test the namefct function by using the staff table
    created in Part 1 (above). Turn in: the SQL
    statements to build the function, execute the sample
    code and the resulting output.Firstly you need to consider what you have already been taught as you need to ensure that you code your function according to the purpose of the assignment. i.e. are you supposed to be demonstrating the use of returning values in a function or are you supposed to be demonstrating the use of exception handling etc.
    The main tricky bit with this assignment is the requirement to show an error message and still return a value. If you were to just raise an exception with the error message (e.g. raise application error) then you won't be able to actually return a value. Because it's a function and not a procedure that has been specified you aren't returning the value in an OUT parameter which would have made passing the value back and raising an exception easier.
    So, the only real way to have a function and allow it to show an error message would be something like...
    SQL> ed
    Wrote file afiedt.buf
      1  CREATE OR REPLACE FUNCTION f_name(p_forename IN VARCHAR2, p_surname IN VARCHAR2) RETURN VARCHAR2 IS
      2    name_too_long EXCEPTION;
      3  BEGIN
      4    IF LENGTH(p_surname||', '||p_forename) > 10 THEN
      5      RAISE name_too_long;
      6    END IF;
      7    RETURN p_surname||', '||p_forename;
      8  EXCEPTION
      9    WHEN name_too_long THEN
    10      DBMS_OUTPUT.PUT_LINE('Name Too Long. Truncating to 10 chrs.');
    11      RETURN SUBSTR(p_surname||', '||p_forename, 1, 10);
    12* END;
    SQL> /
    Function created.
    SQL> select f_name('FRED','SIMS') from dual;
    F_NAME('FRED','SIMS')
    SIMS, FRED
    SQL> select f_name('FRED','SIMKINSON') from dual;
    F_NAME('FRED','SIMKINSON')
    SIMKINSON,
    Name Too Long. Truncating to 10 chrs.
    SQL>Note: Tutors/Professors are very wise to checking your work on the internet to make sure it hasn't been copied, so I would suggest you don't just copy this verbatim, but actually read it and understand how it's working and write your own version, taking account of what you have been taught so far and not using things you haven't been taught.
    ;)

  • How to use stored procedure which returns result set in OBIEE

    Hi,
    I hav one stored procedure (one parameter) which returns a result set. Can we use this stored procedure in OBIEE? If so, how we hav to use.
    I know we hav the Evaluate function but not sure whether I can use for my SP which returns result set. Is there any other way where I can use my SP?
    Pls help me in solving this.
    Thanks

    Hi Radha,
    If you want to cache the results in the Oracle BI Server, you should check that option. When you run a query the Oracle BI Server will get its results from the cache, based on the persistence time you define. If the cache is expired, the Oracle BI Server will go to the database to get the results.
    If you want to use caching, you should enable caching in the nqsconfig.ini file.
    Cheers,
    Daan Bakboord

  • How do you have Google searches appear in new tab? This was previously not a problem, but it changed and I cannot return the setting. Any ideas?

    How do you have Google searches appear in new tab? This was previously not a problem, but it changed and I cannot return the setting. Any ideas?

    I recently purchased a second hand new macbook air, although it was second hand to me the previous owner had never actually turned it on.
    Something doesn't make sense here, though I'm not saying the previous owner is lying....
    Time to send your serial # to iTS and let them see what's happening here.
    iTunes Store Support
    http://www.apple.com/emea/support/itunes/contact.html

  • How to return a set of all objects

    hi,
    i have a simple POJO called Items, for example. In my POJO all I have is two fields, the id and the price of the item which comprise to be two fields in the corresponding table in the database. then i have the getters and setters for these two fields.
    i want to have a getItems() method which would essentially return a set of all items. i'm not sure as to how to do this, should there be a loop in the getItems() method which would go through the rows in the table and keep on adding the rows to the set until there are no more rows?
    what's the correct way to do this? any ideas, suggestions will be helpful. thanks!!

    What do you mean "return a set of all items"? You mean all the entries of this type in the DB? Then run query "select * from whatever_that_table_is", iterate over the result set, and and one object to a List for each row. Of course, this is not generally a good approach, as it's quite possible for the number of rows in the DB to exceed what you can hold in memory at one time.
    What part are you having trouble with?

  • How to return a set of vlaues.

    Hello All,
    I need to return a set of vlaues from a table.The values will be used by other application.
    For example I need all the department names to be retuned . How can we do this. can we return more than one value in a function.
    Please let me know the syntax for this.
    Kind Regards,
    Kumar.

    I will show you two ways.
    1. Using a Table type.
    SQL> create or replace type my_dname_type as table of varchar2(14)
      2  /
    Type created.
    SQL> create or replace function my_dname_fn return my_dname_type
      2  as
      3     ldname my_dname_type;
      4  begin
      5     select dname bulk collect into ldname from hx_dept;
      6     return ldname;
      7  end;
      8  /
    Function created.
    SQL> select * from table(my_dname_fn())
      2  /
    COLUMN_VALUE
    ACCOUNTING
    RESEARCH
    SALES
    OPERATIONS2. using a ref cursor
    SQL> drop type my_dname_type
      2  /
    Type dropped.
    SQL> drop function my_dname_fn
      2  /
    Function dropped.
    SQL> create or replace function my_dname_fn return sys_refcursor
      2  as
      3     ldname sys_refcursor;
      4  begin
      5     open ldname for 'select dname from hx_dept';
      6     return ldname;
      7  end;
      8  /
    Function created.
    SQL> var ldname refcursor
    SQL> exec :ldname := my_dname_fn;
    PL/SQL procedure successfully completed.
    SQL> print :ldname
    DNAME
    ACCOUNTING
    RESEARCH
    SALES
    OPERATIONSto know more about this read oracle document.
    Thanks,
    Karthick.

  • How to return a set of data  by using webservice ?

    Hi.
    I am finding how to return a set of data by using java webservice that serves clients written by others such as VB. Net. Please help me !
    Thanks in advance.

    Check the how to on Accessing Oracle9iAS Java Web Service from a .NET Client
    http://otn.oracle.com/sample_code/tech/java/codesnippet/webservices/index.html
    Chandar

  • Using CASE to return a set of columns

    Good day all,
    Many times I have used the CASE construct to return a value based on a set of conditions but now I have a need to return a set of columns based on a set of condition.
    Look at this
    A
    =====
    id
    name
    account
    zip_code
    status
    B
    =====
    id
    name
    account
    zip_code
    C
    =======
    id
    name
    account
    zip_code
    D
    ======
    id
    name
    account
    zip_code
    I want a query that would return
    id - of A
    name, - of A
    beneficiary, - name column of either, A, B, C or D depending on the value of A's status (A, B, C, D)
    account, - account column of either, A, B, C or D depending on the value of A's status (A, B, C, D)
    zip_code - zip_code column of either, A, B, C or D depending on the value of A's status (A, B, C, D)
    Can anyone help with construct that could achieve this with queries only. I don't feel like this warrants PLSQL.
    regards

    Hi,
    Is this what youa re looking for?
    As Frank has said not sure what is the relationship between your tables and what Output you expect.
    Sample Data used.Avoid using reserved words for column like ID and NAME
    create table a (id1 varchar2(10),name1 varchar2(10),account1 varchar2(10),zip_code varchar2(10),status varchar2(10))
    create table b (id1 varchar2(10),name1 varchar2(10),account1 varchar2(10),zip_code varchar2(10))
    create table c (id1 varchar2(10),name1 varchar2(10),account1 varchar2(10),zip_code varchar2(10))
    create table d (id1 varchar2(50),name1 varchar2(50),account1 varchar2(50),zip_code varchar2(50))
    insert into A values (0, 'michael', '000000', '213401', 'A');
    insert into A values (1, 'johnson', '63423', '093401', 'B');
    insert into A values (2, 'fred', '23221', '203901', 'D');
    insert into B values (1, 'brother', '1129812', '093083');
    insert into B values (2, 'stuggard', '0093732', '087366');
    insert into B values (3, 'jekins', '9009387', '329838');
    insert into C values (1, 'mary', '009300', '093083');
    insert into C values (2, 'lori', '112212', '654032');
    insert into C values (3, 'helen', '909083', '938292');
    insert into D values (1, 'mother & father', '093291', '329232');
    insert into D values (2, 'Uncles & Aunties', '098762', '742326');
    insert into D values (3, 'Grannies', '237643', '864433');
    Query
    select 
    case when a.status='A' then
    a.id1
    when a.status='B' then
    b.id1
    when a.status='C' then
    c.id1
    when a.status='D' then
    d.id1
    end ID1,
    case when a.status='A' then
    a.name1
    when a.status='B' then
    b.name1
    when a.status='C' then
    c.name1
    when a.status='D' then
    d.name1
    end ID1
    ,case when a.status='A' then
    a.account1
    when a.status='B' then
    b.account1
    when a.status='C' then
    c.account1
    when a.status='D' then
    d.account1
    end account1,
    a.status status,
    case when a.status='A' then
    a.zip_code
    when a.status='B' then
    b.zip_code
    when a.status='C' then
    c.zip_code
    when a.status='D' then
    d.zip_code
    end zip_code
    from a ,b,c,d
    where a.id1=b.id1 and b.id1=c.id1 and c.id1=d.id1Cheers!!!
    Bhushan

  • Returning result sets from PL/SQL procedure to client app.

    I was wondering if its possible in Oracle PL/SQL to DECLARE, OPEN a cursor and exit
    the procedure without closing the cursor
    and then retrieve the resultset from
    the client.
    Pl let me know..
    null

    Yes, you need to use one OUT parameter in your PL/SQL procedure
    to pass the cursor variable to
    Java code. You can also return that as a return variable of
    PL/SQL function. Get the cursor variable from the resultset using
    Types.CURSOR data type and then proceed as usual.
    Enrique (guest) wrote:
    : Thank you Rajib for your prompt reply. I have been programming
    a
    : lot in Transact SQL( MSSQL ), but I am new to Oracle and I need
    : to migrate MSSQL procedures to Oracle. I will try to use the
    : refCursors. One more question, how do I pass the cursors back?
    : With OUT parameters? In MSSQL you do not need to specify OUT
    : parameters if you are returning a result set.
    : Once Again,
    : Thank you
    : Rajib (guest) wrote:
    : : You can return a variable of refcursor type from your PL/SQL
    : : procedure (PL/SQL 2.3 or higher) to Java code. Oracle JDBC
    has
    : a
    : : refcursor data type. Now you can use this cursor as a
    : resultset.
    : : Enrique (guest) wrote:
    : : : Hi All,
    : : : I am trying to write some store procedures( PL/SQL )
    : : that
    : : : will select rows from my tables, and then pass then back to
    : my
    : : : JDBC application as result sets....Does anyone know how can
    I
    : : do
    : : : that? Do I need to use output parameters? Or Return
    : functions?
    : : : Any help or hint wourl be be gladly appreciate it.
    : : : Enrique
    : : : Thank you.
    null

  • [svn:fx-trunk] 11655: Added logic to allow getStyle to return locally set style values early on  (before the instance has been added to DOM).

    Revision: 11655
    Author:   [email protected]
    Date:     2009-11-11 08:27:42 -0800 (Wed, 11 Nov 2009)
    Log Message:
    Added logic to allow getStyle to return locally set style values early on (before the instance has been added to DOM).
    QE notes:  None
    Doc notes:  None
    Bugs: SDK-22198
    Reviewer: Glenn
    Tests run: Checkin
    Is noteworthy for integration: No
    Ticket Links:
        http://bugs.adobe.com/jira/browse/SDK-22198
    Modified Paths:
        flex/sdk/trunk/frameworks/projects/framework/src/mx/core/UIComponent.as

    Please can u answer it fast

  • Can I set camera registers in version 1.1 of the IMAQ 1394 driver to values outside of the DCAM 1.3 specs?

    I have an imaging system that includes a Pt Grey Dragonfly camera. I know that using the manufactures SDK, I can set different camera parameters outside of the DCAM specs by setting camera registers. If I am using version 1.1 of the IMAQ 1394 driver can I set, say the frame rate to a value outside the DCAM specs?

    http://exchange.ni.com/servlet/ProcessRequest?RHIVEID=101&RNAME=ViewQuestion&HOID=5065000000080000009B580000&ECategory=Vision

  • PL/SQL function body returning SQL query - ORA-06502: PL/SQL: numeric or value error

    I'm attempting to dynamically generate a rather large SQL query via the "PL/SQL function body returning SQL query" report region option.  The SQL query generated will possibly be over 32K.  When I execute my page, I sometimes receive the "ORA-06502: PL/SQL: numeric or value error" which points to a larger than 32K query that was generated.  I've seen other posts in the forum related to this dynamic SQL size limitation issue, but they are older (pre-2010) and point to the 32K limit of the DNS (EXECUTE IMMEDIATE) and DBMS_SQL.  I found this post (dynamic sql enhancements in 11g) which discusses 11g no longer having the 32K size limitation for generating dynamic SQL.  Our environment is on 11gR2 and using ApEx 4.2.1.  I do not know which dynamic SQL method -- DNS or DBMS_SQL -- ApEx 4.2.1 is using.  Can someone clarify for me which dynamic SQL method ApEx uses to implement the "PL/SQL function body returning SQL query" option?
    As a test, I created a page on apex.oracle.com with a report region with the following source:
    declare
      l_stub varchar2(25) := 'select * from sys.dual ';
      l_sql  clob := l_stub || 'union all ';
      br     number(3) := 33;
    begin
      while length ( l_sql ) < 34000 loop
        l_sql := l_sql || l_stub || 'union all ';
      end loop;
      l_sql := l_sql || l_stub;
      for i in 1 .. ceil ( length ( l_sql ) / br ) loop
        dbms_output.put_line ( dbms_lob.substr ( l_sql, br, ( ( i - 1 ) * br ) + 1 ) );
      end loop;
      return l_sql;
    end;
    The dbms_output section is there to be able to run this code in SQL*Plus and confirm the size of the SQL is indeed larger than 32K.  When running this in SQL*Plus, the procedure is successful and produces a proper SQL statement which can be executed.  When I put this into the report region on apex.oracle.com, I get the ORA-06502 error.
    I can certainly implement a work-around for my issue by creating a 'Before Header' process on the page which populates an ApEx collection with the data I am returning and then the report can simply select from the collection, but according to documentation, the above 32K limitation should be resolved in 11g.  Thoughts?
    Shane.

    What setting do you use in your report properties - especially in Type and in Region Source?
    If you have Type="SQL Query", then you should have a SELECT statement in the Region Source. Something like: SELECT .... FROM ... WHERE
    According to the ERR-1101 error message, you have probably set Type to "SQL Query (PL/SQL function body returning SQL query)". In this situation APEX expects you to write a body of a PL/SQL function, that will generate the text of a SQL query that APEX should run. So it can be something like:
    declare
    mycond varchar2(4000);
    begin
    if :P1_REPORT_SEARCH is not null THEN
    mycond:='WHERE LAST_NAME like :P1_REPORT_SEARCH ||''%''';
    end if;
    return 'select EMPLOYEE_ID, FIRST_NAME, LAST_NAME from EMPLOYEES ' ||mycond;
    end;
    And for escaping - are you interested in escaping the LIKE wildcards, or the quotes?
    For escaping the wildcards in LIKE function so that when the user enters % you will find a record with % and not all functions, look into the SQL Reference:
    http://download-uk.oracle.com/docs/cd/B19306_01/server.102/b14200/conditions007.htm
    (You would than need to change the code of your function accordingly).
    If you are interested in escaping the quotes, try to avoid concatenating the values entered by the user into the SQL. If you can, use bind variables instead - as I have in my example above. If you start concatenating the values into the text of SQL, you are open to SQLInjection - user can enter anything, even things that will break your SQL. If you really need to allow users to choose the operator, I would probably give them a separate combo for operators and a textfield for values, than you could check if the operator is one of the allowed ones and create the condition accordingly - and than still use bind variable for inserting the filtering value into the query.

  • Region source (PL/SQL function body returning SQL query)

    Hi, guys.
    Here is what i try to do:
    Create a region of type SQL Query (PL/SQL function body returning SQL query). In the source area i tried to put this:
    DECLARE
    v_new VARCHAR2(10);
    v_SQL varchar2(32000);
    BEGIN
    v_new := :P102_HDN_NEW;
    -- htp.p(v_new);
    IF v_new = 'N-Set' THEN
    v_SQL := 'select ' ||
    ELSIF v_new = 'Y-Set' THEN
    v_SQL := 'select ' ||
    END IF;
    RETURN v_SQL;
    END;
    And here is the reply from APEX:
    1 error has occurred
    Query cannot be parsed within the Builder. If you believe your query is syntactically correct, check the ''generic columns'' checkbox below the region source to proceed without parsing. The query can not be parsed, the cursor is not yet open or a function returning a SQL query returned without a value.
    Now.
    1. Variable is set with the right value.
    2. Each statement (separately) returns SQL that works with no problems
    3. Problem occures if i try to put IF statement around the SQL creation.
    4. If i select "Use Generic Column Names (parse query at runtime only)" instead of "Use Query-Specific Column Names and Validate Query" then the script returns SQL properly, however report's column names are set to Col1, Col2,Col3 ......
    Thnks in advence
    Mike

    OK. Here is enire statement:
    DECLARE
    v_new VARCHAR2(10);
    v_SQL varchar2(32000);
    BEGIN
    v_new := :P102_HDN_NEW;
    htp.p(v_new);
    IF v_new = 'N-Set' THEN
    v_SQL := 'select ' ||
    'APEX_ITEM.DISPLAY_AND_SAVE(10,c.sdescr) descr, ' ||
    'APEX_ITEM.DISPLAY_AND_SAVE(12,DECODE(ld.level,''All'', ''All Categories'',ld.level)) level, ' ||
    'apex_item.checkbox(1, ld.opt_in_auto_flag, decode(ld.opt_in_auto_flag,NULL,''disabled'',''Y'',''checked'')) auto_in, ' ||
    'apex_item.checkbox(2, ld.opt_in_manual_flag, decode(ld.opt_in_manual_flag,NULL,''disabled'',''Y'',''checked'')) manual_in, ' ||
    'apex_item.checkbox(3, ld.opt_out_auto_flag, decode(ld.opt_out_auto_flag,NULL,''disabled'',''Y'',''checked'')) auto_out, ' ||
    'apex_item.checkbox(4, ld.opt_out_manual_flag, decode(ld.opt_out_manual_flag,NULL,''disabled'',''Y'',''checked'')) manual_out, ' ||
    'DECODE(c.code, ''NMBR'', NULL,''Change to '' || DECODE(ld.level,''All'',''Categories'',''All Categories'')) link_change, ' ||
    'APEX_ITEM.DISPLAY_AND_SAVE(11,c.code) code ' ||
    'from ' ||
    'tbl1 c, ' ||
    'tbl2 ld ' ||
    'where c.code = ld.code ' ||
    'and c.type = ''TYPE1'' ' ||
    'and c.active = ''Y'' ' ||
    'order by c.sorting ';
    ELSIF v_new = 'Y-Set' THEN
    v_SQL := 'select ' ||
    'APEX_ITEM.DISPLAY_AND_SAVE(10,c.sdescr) descr, ' ||
    'APEX_ITEM.DISPLAY_AND_SAVE(12,''All Categories'') level, ' ||
    'apex_item.checkbox(1, c.option_1, decode(c.option_1,NULL,''disabled'',''Y'',''checked'')) auto_in, ' ||
    'apex_item.checkbox(2, c.option_3, decode(c.option_3,NULL,''disabled'',''Y'',''checked'')) manual_in, ' ||
    'apex_item.checkbox(3, c.option_2, decode(c.option_2,NULL,''disabled'',''Y'',''checked'')) auto_out, ' ||
    'apex_item.checkbox(4, c.option_4, decode(c.option_4,NULL,''disabled'',''Y'',''checked'')) manual_out, ' ||
    'DECODE(c.code, ''AAA'', NULL,''Options by AAA'') link_change, ' ||
    'APEX_ITEM.DISPLAY_AND_SAVE(11,c.code) code ' ||
    'from ' ||
    'tbl1 c ' ||
    'where 1 = 1 ' ||
    'and c.type = ''TYPE1'' ' ||
    'and c.active = ''Y'' ' ||
    'order by c.sorting ';
    END IF;
    RETURN v_SQL;
    END;
    If i put just this
    DECLARE
    v_new VARCHAR2(10);
    v_SQL varchar2(32000);
    BEGIN
    v_new := :P102_HDN_NEW;
    htp.p(v_new);
    v_SQL := 'select ' ||
    'APEX_ITEM.DISPLAY_AND_SAVE(10,c.sdescr) descr, ' ||
    'APEX_ITEM.DISPLAY_AND_SAVE(12,DECODE(ld.level,''All'', ''All Categories'',ld.level)) level, ' ||
    'apex_item.checkbox(1, ld.opt_in_auto_flag, decode(ld.opt_in_auto_flag,NULL,''disabled'',''Y'',''checked'')) auto_in, ' ||
    'apex_item.checkbox(2, ld.opt_in_manual_flag, decode(ld.opt_in_manual_flag,NULL,''disabled'',''Y'',''checked'')) manual_in, ' ||
    'apex_item.checkbox(3, ld.opt_out_auto_flag, decode(ld.opt_out_auto_flag,NULL,''disabled'',''Y'',''checked'')) auto_out, ' ||
    'apex_item.checkbox(4, ld.opt_out_manual_flag, decode(ld.opt_out_manual_flag,NULL,''disabled'',''Y'',''checked'')) manual_out, ' ||
    'DECODE(c.code, ''NMBR'', NULL,''Change to '' || DECODE(ld.level,''All'',''Categories'',''All Categories'')) link_change, ' ||
    'APEX_ITEM.DISPLAY_AND_SAVE(11,c.code) code ' ||
    'from ' ||
    'tbl1 c, ' ||
    'tbl2 ld ' ||
    'where c.code = ld.code ' ||
    'and c.type = ''TYPE1'' ' ||
    'and c.active = ''Y'' ' ||
    'order by c.sorting ';
    RETURN v_SQL;
    END;
    it works fune...

  • Issue with running PL/SQL function returning Sql query

    hi, I am trying to create a report region by using the option of PL/SQL function returning sql query.
    I notice that it's very slow for the report region page to show up. In my PL/SQL function body, there are only 3 steps, first update all the 10 rows of varchar2 fields to null,then insert values to those fields, then select all from the table to show report results. It takes more than 5 minitues for the page to load up, how ever, if i run those steps in SQL*Plus, it only takes a couple of seconds to finish. Any suggestions?
    Thanks,
    gina

    Sergio, the codes are as followed,
    Declare
    q varchar2(32767); -- query
    Begin
    q := 'select "ID",'||
    '"ENTRY NAME","TOTAL","#CM","%CM","#CA",'||
    '"%CA", from Info_table';
    update info_table
    set "TOTAL" = '',
    "#CM" = '',
    "%CM" = '',
    "#CA" ='',
    "%CA"=''
    where "ID"<=10;
    // set all data in column Total to null,there is only 10 rows in the table
    update info_Table set Total = vTotal,
    "#CM" = vCM
    (those variables hold user key-in Text filed value)
    where ID = 1;
    return q;
    End;

  • Multiple Select List looping thru PL/SQL function body returning SQL query

    Hi,
    I have a Multiple Select List. I want to loop through the values from the Select List and process them in a PL/SQL function body returning a SQL query. Currently, my code only returns the SQL SELECT results of one item in the select list. How do I change my code to make it return the results of all of the items in the select list? (I tested it and it is definitely picking up all the values in the select list).
    <b>
    DECLARE
    selected_items HTMLDB_APPLICATION_GLOBAL.VC_ARR2;
    s   VARCHAR2(20);
    q varchar2(32767);
    BEGIN
    selected_items := HTMLDB_UTIL.STRING_TO_TABLE(:P50_SELECTED_INSTRUMENTS);
    -- htp.p('COUNT: '||selected_items.count);
      FOR i in 1..selected_items.count LOOP
      s := TO_CHAR(selected_items(i));
    -- htp.p('First: '||s);
    -- htp.p('Second: '||:s);
    -- htp.p('Third: '||TO_CHAR(selected_items(i)));
      q:= 'SELECT  '||
    'SUBSTR(orig_geo_loc_sys,1,INSTR(orig_geo_loc_sys,''-'')-1) AS INSTRUMENT, '||
    'SUBSTR(orig_geo_loc_sys,INSTR(orig_geo_loc_sys,''-'')+1, LENGTH'||
    ' (orig_geo_loc_sys)) AS ORIG_LINENUM, '||
    'sum(orig_intrl) orig_intrl, '||
    'sum(orig_extrl) orig_extrl, '||
    'sum(recv_intrl) recv_intrl, '||
    'sum(recv_extrl) recv_extrl '||
    'FROM line_usage_sum_view '||
    'WHERE TO_CHAR(orig_geo_loc_sys) LIKE ''' || s ||'%'' '||
    --'WHERE TO_CHAR(orig_geo_loc_sys) LIKE ''2213003%'' '||
    'AND switch_id = ''' || :P1_SWITCH_ID || ''' ' ||
    'AND call_start_date > TO_DATE(''30-NOV-1999'') ' ||
    'AND call_clear_time > TO_DATE(''30-NOV-1999'') '||
    'AND '||
    :SORTFIELD||' BETWEEN '||
    'TO_DATE(:STARTDATE,''dd-MON-YYYY HH24:MI'') AND '||
    'TO_DATE(:STOPDATE, ''dd-MON-YYYY HH24:MI'') '||
    'GROUP BY GROUPING SETS (orig_geo_loc_sys)';
    -- htp.p('SQL query: '||q);
      RETURN q;
      END LOOP;
    END;</b>
    Thank you,
    Laura

    Laura,
    First, I would be careful of introducing SQL Injection possibilities. Any time I see
    'Select ... ' || :P123_FOO || ' ... '
    I worry about sql injection. In your case you are converting :P50_SELECTED_INSTRUMENTS into selected_items and then selected_items into s. So when I see
    'WHERE TO_CHAR(orig_geo_loc_sys) LIKE ''' || s ||'%'' '||
    I think, "I could use sql Injection and hack this."
    So, I would do some validation on :P50_SELECTED_INSTRUMENTS or some other method to avoid this.
    I'm not certain I understand your query. Do you really intend to allow the user to select the beginning of a string and then find all rows that start with that string? Or, do you just want to let them find when it matches the string. This is one way if you want to do matching:
    DECLARE
    selected_items HTMLDB_APPLICATION_GLOBAL.VC_ARR2;
    s VARCHAR2(32767);
    q varchar2(32767);
    BEGIN
    -- Change the : separate string to be comma separated with quoted strings
    s := '''' || replace(:P50_SELECTED_INSTRUMENTS, ',', ''',''')|| '''' ;
    -- htp.p('COUNT: '||selected_items.count);
    q:= 'SELECT '||
    'SUBSTR(orig_geo_loc_sys,1,INSTR(orig_geo_loc_sys,''-'')-1) AS INSTRUMENT, '||
    'SUBSTR(orig_geo_loc_sys,INSTR(orig_geo_loc_sys,''-'')+1, LENGTH'||
    ' (orig_geo_loc_sys)) AS ORIG_LINENUM, '||
    'sum(orig_intrl) orig_intrl, '||
    'sum(orig_extrl) orig_extrl, '||
    'sum(recv_intrl) recv_intrl, '||
    'sum(recv_extrl) recv_extrl '||
    'FROM line_usage_sum_view '||
    'WHERE TO_CHAR(orig_geo_loc_sys) in (' || s ||' ) '||
    --'WHERE TO_CHAR(orig_geo_loc_sys) LIKE ''2213003%'' '||
    'AND switch_id = ''' || :P1_SWITCH_ID || ''' ' ||
    'AND call_start_date > TO_DATE(''30-NOV-1999'') ' ||
    'AND call_clear_time > TO_DATE(''30-NOV-1999'') '||
    'AND '||
    :SORTFIELD||' BETWEEN '||
    'TO_DATE(:STARTDATE,''dd-MON-YYYY HH24:MI'') AND '||
    'TO_DATE(:STOPDATE, ''dd-MON-YYYY HH24:MI'') '||
    'GROUP BY GROUPING SETS (orig_geo_loc_sys)';
    -- htp.p('SQL query: '||q);
    RETURN q;
    END;
    If you want to do something more like you originally stated, try this:
    DECLARE
    selected_items HTMLDB_APPLICATION_GLOBAL.VC_ARR2;
    s VARCHAR2(20);
    q varchar2(32767);
    BEGIN
    selected_items := HTMLDB_UTIL.STRING_TO_TABLE(:P50_SELECTED_INSTRUMENTS);
    -- htp.p('COUNT: '||selected_items.count);
    q:= 'SELECT '||
    'SUBSTR(orig_geo_loc_sys,1,INSTR(orig_geo_loc_sys,''-'')-1) AS INSTRUMENT, '||
    'SUBSTR(orig_geo_loc_sys,INSTR(orig_geo_loc_sys,''-'')+1, LENGTH'||
    ' (orig_geo_loc_sys)) AS ORIG_LINENUM, '||
    'sum(orig_intrl) orig_intrl, '||
    'sum(orig_extrl) orig_extrl, '||
    'sum(recv_intrl) recv_intrl, '||
    'sum(recv_extrl) recv_extrl '||
    'FROM line_usage_sum_view '||
    'WHERE 1=1 ';
    FOR i in 1..selected_items.count LOOP
    s := TO_CHAR(selected_items(i));
    q := q || ' and TO_CHAR(orig_geo_loc_sys) LIKE '''|| s ||'%'' ' ;
    END LOOP;
    q := q || ||'%'' '||
    --'WHERE TO_CHAR(orig_geo_loc_sys) LIKE ''2213003%'' '||
    'AND switch_id = ''' || :P1_SWITCH_ID || ''' ' ||
    'AND call_start_date > TO_DATE(''30-NOV-1999'') ' ||
    'AND call_clear_time > TO_DATE(''30-NOV-1999'') '||
    'AND '||
    :SORTFIELD||' BETWEEN '||
    'TO_DATE(:STARTDATE,''dd-MON-YYYY HH24:MI'') AND '||
    'TO_DATE(:STOPDATE, ''dd-MON-YYYY HH24:MI'') '||
    'GROUP BY GROUPING SETS (orig_geo_loc_sys)';
    -- htp.p('SQL query: '||q);
    RETURN q;
    END;
    Hope this helps...
    Anton

Maybe you are looking for

  • Trying to add photo gallery to Dreamweaver page

    Hi, I would like to add a photo gallery in a style similar to this one:  http://www.pezziunici.com/html/prodotti.php?lng=eng&pro=310 with a thumbnail grid on the left and large photos with captions to the right - all on the same page. I see there is

  • Problems with Apache JServ

    I'm Working with the Apache Jserv module. When anyone access to any of my servlets, goes to any link, and then return back with the 'Back' button of the browser, the servlet reloads itself, there's no cache or something like that. It means doing the

  • IPod Screen Issue

    I recieved my iPod 3rd Gen. brand new for Christmas. I opened it on Tuesday, Jan. 6th, and set it up with iTunes. I also recieved an iTunes Gift Card, so I tried to download a video (with no success - it would download 270MB/1.18GB and stop). I have

  • Disk Space. Is there a solution for the "Other" in Storage

    This has been asked over and over again, yet I haven't found an answer to fix the issue. I have a 160 GB Drive, with 86GB under other. A Few months ago, the Other section was about 60GB, and I had photos for about 30GB. I deleted all my photos from m

  • Can I recover a kenote presentation that was on my Macboock air that was stolen ?

    My Macbook air was stolen 6 moth ago and I need a Keynote presentation that was in it. I rebooted when I notice it was lost. I never found it. Is it possible to recover the info. ?