Sql query to_date function

hi all,
I am working on a project where some sql scripts are already been developed by
someone else.
In the code i found
Select col_name
from table_name
where txn_dt between to_date(to_date('05-dec-2007') - 6) and to_date('05-dec-2007');
I am wondering whats the usage of 2 to_date function in the where clause.
This can be achieved using this as well:
Select col_name
from table_name
where txn_dt between to_date('05-dec-2007') - 6 and to_date('05-dec-2007');
Can anybody tell me this diff if there is any?
Thanks
Shane

to_date(to_date('05-dec-2007') - 6)This is an error. Not a syntax error, but a runtime error waiting to happen.
The inner to_date is fine (except it should have a format mask) but the outer to_date is expecting a character type and it's getting a date. So it has to be converted.
All this depends on the nls settings which could change.
Ideally, the code should read:
Select col_name
from table_name
where txn_dt between to_date('05-dec-2007','dd-mon-yyyy') - 6 and to_date('05-dec-2007','dd-mon-yyyy');
(on second thought...if the nls settings match this date format, it should convert without error in both to_date functions, but still...why do it?)
Edited by: SomeoneElse on Dec 12, 2008 9:12 AM

Similar Messages

  • SQL query or function...

    Hi everyone,
      Please help me to clarify this question
    I have a "patient" table and a "address" table.And each patient has differnt types of addresses
    like PMI,Mailing and default address i need to pick patient address in the order of preference
    (PMI,Mailing ,Default)
    I have two options to get the address of a patient.Either write a SQL query to get the address of a
    patient(according to preference)
    or write a function which checks
    if there is PMI,Mailing and default
      take PMI
    or
    If there is mailing and Default
    take mailing
    or
    take default.
    which one is the best method to get the address of a patient?  SQL query or function..
    Definitely everyone will say that it should be direct SQL query instead of multiple function calls.
    But in my organization there are many developers which will get "patient address" in their application
    or SQL code or PL/SQL code or from some
    'X' Business intelligence tool.
    At this scenario is it best to have a function which every one will call and get a standardized output
    for every one.
    Question:
    In my scenario which one is the best to have "SQL query or function"?
    if i choose "function" i have to sacrifice the performance
    if i choose "SQL query" i am not sure whether each and every developer querying the same way as it would be
    Sorry if it is a dumb question to ask.
    Thanks in advance
    phani

    question for hoek: what if i have to choose between SQL query and function? (suppose they dont accept to create a view)If views are not accepted (which is very strange imo), but you do need some 'standard object' which everybody can use, then you'd choose to use a function instead of everybody having 'their own query'.
    It's a trade-off between maintaining standards and performance (calling PL/SQL from SQL is usually to be avoided due to context-switching, the other way around is OK).
    One advantage of a view: no hassle with parameters you have to enter but never will use, you just pick the columns you need and there you go.
    Other one: it's straightforward, simple and clear.
    It's easier to maintain.
    But it all depends on what your function will return.
    Perhaps a REF CURSOR?
    Your question reminds me of this interesting discussion:
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:672724700346558185
    Edited by: hoek on Jul 1, 2009 4:39 PM tyop's

  • Pl/sql function body returning SQL query - Print function

    Hello all,
    I have pl/sql function body returning SQL query for my reports for my new project that I am developing. We dont have any BI tool or anything for APEX so we use Oracle reports to get the same reports to be printed in PDF format. I had been using SQL function for Reports all these days and grabbing the data using SQL query was easy in Oracle reports. But this time we had atleast 8 fields in search criteria and hence I thouhgt PL/sql function body returning SQL query could be something easy to handle that scenario. We have 11 such reports in our project. Now when we tried to use the same PL/sql function to oracle reports , I was told by one of our Oracle reports expert, that we have to write it into functions and use it in SQL query to get the Reports in Oracle reports. Is there any Easy way to convert the same Pl/SQL function or get a PDF format of the same report in APEX without going thru the much pains of rewriting the whole SQL Query.
    thank you
    Devisri

    Hi,
    give this a go.
    I can't test it as I don't have the tables in my schema.
    create or replace package MK_TEST_PF is
    -- Author  : MK
    -- Created : 21/06/2010 16:30:19
    -- Purpose : FOR LUCY_DISCOVER
    -- Public type declarations
    /*     -- just guess the table row types.....
         -- otherwise it won't compile
         type test_rec is record
              (INV REP.inv%type
              ,cNUMBER REP.cNUMBER%type
              ,OPENDATE REP.OPENDATE%type
              ,TARGETDATE REP.ESTCOMPLETE%type
              ,DATECLOSED REP.COMPLETED%type
              ,STATUS REP.STATUS%type
              ,cCODE REP.cCODE%type
              ,line varchar2(4000)
              ,SIGc varchar2(4000)
              ,CLASS REP.CLASS%type
              ,SUMM REP.SUMM%type
              ,AREA REP.AREA%type
         type test_rec is record
              (INV varchar2(4000)
              ,cNUMBER varchar2(4000)
              ,OPENDATE varchar2(4000)
              ,TARGETDATE varchar2(4000)
              ,DATECLOSED varchar2(4000)
              ,STATUS varchar2(4000)
              ,cCODE varchar2(4000)
              ,line varchar2(4000)
              ,SIGc varchar2(4000)
              ,CLASS varchar2(4000)
              ,SUMM varchar2(4000)
              ,AREA varchar2(4000)
         type test_tab is table of test_rec;
    -- Public constant declarations
    -- Public variable declarations
    -- Public function and procedure declarations
    end MK_TEST_PF;
    create or replace package body MK_TEST_PF is
    -- Private type declarations
    -- Private constant declarations
    -- Private variable declarations
    -- Function and procedure implementations
         function get_query_f
              (p_inv VARCHAR2 := UPPER(v('P44_INV'))
              ,p_reg VARCHAR2 := UPPER(v('P44_CLASS'))
              ,p_proarea VARCHAR2 := UPPER(v('P44_PROGRAM_AREA'))
              ,p_disp VARCHAR2 := UPPER(v('P44_DISPOSITION'))
              ,p_coding VARCHAR2 := UPPER(v('P44_CODING'))
              ,p_status VARCHAR2 := UPPER(v('P44_STATUS'))
              ,p_SIG VARCHAR2 := UPPER(v('P44_SIG_c'))
              ,p_inc_sum VARCHAR2 := UPPER(v('P44_INCLUDE_SUMM_FIELD'))
              ,p_word VARCHAR2 := UPPER(v('P44_WORD_IN_SUMM'))
              ,p_timeframe VARCHAR2 := UPPER(v('P44_TIME_FRAME'))
              ,p_rec VARCHAR2 := UPPER(v('P44_RECORD_KEEPING'))
              ,p_WORD_IN_SUMM VARCHAR2 := UPPER(v('P44_WORD_IN_SUMM'))
              ,p_ON_AFTER VARCHAR2 := UPPER(v('P44_ON_AFTER'))
              ,p_ON_BEFORE VARCHAR2 := UPPER(v('P44_ON_BEFORE'))
              return varchar2
         is
              v_sql VARCHAR2(5000);
         --     v_inv VARCHAR2(100);
         --     v_reg VARCHAR2(100);
         --     v_proarea VARCHAR2(100);
         --     v_status VARCHAR2(100);
         --     v_SIG VARCHAR2(100);
         --     v_disp VARCHAR2(100);
         --     v_coding VARCHAR2(100);
         --     v_inc_sum VARCHAR2(4);
         --     v_word VARCHAR2(4000);
              v_wildcard VARCHAR2(2000);
         --     v_timeframe VARCHAR2(100);
         --     v_rec VARCHAR2(5);
              v_record VARCHAR2(5);
              v_open VARCHAR2(100);
              v_closed VARCHAR2(100);
              v_PEND VARCHAR2(100);
              v_refSIG VARCHAR2(100);
              v_refreg VARCHAR2(100);
              v_refother VARCHAR2(100);
              v_y varchar2(100);
         BEGIN
              --v_inv := UPPER(v('P44_INV')) ;
              v_record := 'R%';
              v_wildcard := '%';
              v_open := 'OPEN';
              v_closed := 'CLOSED';
              v_PEND := 'PEND';
              v_refSIG := 'REF - SIG';
              v_refreg := 'REF - CLASS';
              v_refother := 'REF - OTHER';
              v_y := 'Y';
              v_sql := 'SELECT REP.INV as INV, REP.cNUMBER as cNUMBER, REP.OPENDATE as OPENDATE,
              REP.ESTCOMPLETE as TARGETDATE, REP.COMPLETED as DATECLOSED, REP.STATUS as STATUS,
              REP.cCODE as cCODE, apex_item.checkbox(1,REP.line,null,'''||v_y||''') line , apex_item.checkbox(1,REP.SIG,null,'''||v_y||''') SIGc ,
              REP.CLASS as CLASS, REP.SUMM as SUMM, REP.AREA as AREA from REP where 1=1';
              IF p_rec is not null then
                   IF p_rec = 'E' then
                        v_sql := v_sql|| ' and upper(REP.cnumber) not like '''||v_record||'''';
                   ELSIF p_rec = 'D' then
                        v_sql := v_sql|| ' and upper(REP.cnumber) like '''||v_record||'''';
                   ELSIF p_rec = 'I' then
                        v_sql := v_sql|| ' and REP.cnumber = REP.cnumber ';
                   end if ;
              end if ;
              IF upper(p_status) not like '%NULL%' then
                   IF upper(p_status) like '%OPEN%' then
                   v_sql := v_sql||' AND upper(REP.status) like '''||v_open||'''';
                   ELSIF upper(p_status) like '%CLOSED%' then
                   v_sql := v_sql||' AND upper(REP.status) like '''||v_closed||'''';
                   ELSIF upper(v_PEND) like '%PEND%' then
                   v_sql := v_sql||' AND upper(REP.status) like '''||v_PEND||'''';
                   ELSIF upper(v_refSIG) like '%REF - SIG%' then
                   v_sql := v_sql||' AND upper(REP.status) like '''||v_refSIG||'''';
                   ELSIF upper(v_refreg) like '%REF - CLASS%' then
                   v_sql := v_sql||' AND upper(REP.status) like '''||v_refreg||'''';
                   ELSIF upper(v_refother) like '%REF - OTHER%' then
                   v_sql := v_sql||' AND upper(REP.status) like '''||v_refother||'''';
                   END IF ;
              END IF ;
              IF p_inv = 'NULL' THEN
                   v_sql := v_sql||' AND instr(upper(REP.INV),'''||p_inv||''') > 0';
              END IF ;
              IF p_reg = 'NULL' THEN
                   v_sql := v_sql||' AND instr(upper(REP.CLASS),'''||p_reg||''') > 0';
              END IF ;
              IF p_proarea = 'NULL' THEN
                   v_sql := v_sql||' AND instr(upper(REP.AREA),'''||p_proarea||''') > 0';
              END IF ;
              IF p_disp = 'NULL' THEN
                   v_sql := v_sql||' AND instr(upper(REP.disposition),'''||p_disp||''') > 0';
              END IF ;
              IF p_coding = 'NULL' THEN
                   v_sql := v_sql||' AND instr(upper(REP.ccode),'''||p_coding||''') > 0';
              END IF ;
              IF p_SIG = ' ' THEN
                   v_sql := v_sql||' AND instr(upper(REP.SIG),'''||p_SIG||''') > 0';
              END IF ;
              IF p_word is not null then
                   v_sql := v_sql|| ' and
                   instr(upper(REP.SUMM),
                   upper(nvl('''||p_WORD_IN_SUMM||''',REP.SUMM))) > 0';
              end if ;
              If p_timeframe is not null then
                   if upper(p_timeframe) = 'OPEN' then
                   v_sql := v_sql|| ' and to_date(REP.opendate) between to_date ('''||p_ON_AFTER||''') and to_date('''||p_ON_BEFORE||''')';
                   elsif upper(p_timeframe) = 'CLOSED' then
                   v_sql := v_sql|| ' and to_date(REP.completed) between to_date ('''||p_ON_AFTER||''') and to_date('''||p_ON_BEFORE||''')';
                   elsif upper(p_timeframe) = 'EST' then
                   v_sql := v_sql|| ' and to_date(REP.estcomplete) between to_date ('''||p_ON_AFTER||''') and to_date('''||p_ON_BEFORE||''')';
                   end if;
              end if;
              v_sql := v_sql ||' order by REP.INV ';
              return v_sql;
         end get_query_f;
         function test_pf
              (p_inv VARCHAR2 := UPPER(v('P44_INV'))
              ,p_reg VARCHAR2 := UPPER(v('P44_CLASS'))
              ,p_proarea VARCHAR2 := UPPER(v('P44_PROGRAM_AREA'))
              ,p_disp VARCHAR2 := UPPER(v('P44_DISPOSITION'))
              ,p_coding VARCHAR2 := UPPER(v('P44_CODING'))
              ,p_status VARCHAR2 := UPPER(v('P44_STATUS'))
              ,p_SIG VARCHAR2 := UPPER(v('P44_SIG_c'))
              ,p_inc_sum VARCHAR2 := UPPER(v('P44_INCLUDE_SUMM_FIELD'))
              ,p_word VARCHAR2 := UPPER(v('P44_WORD_IN_SUMM'))
              ,p_timeframe VARCHAR2 := UPPER(v('P44_TIME_FRAME'))
              ,p_rec VARCHAR2 := UPPER(v('P44_RECORD_KEEPING'))
              ,p_WORD_IN_SUMM VARCHAR2 := UPPER(v('P44_WORD_IN_SUMM'))
              ,p_ON_AFTER VARCHAR2 := UPPER(v('P44_ON_AFTER'))
              ,p_ON_BEFORE VARCHAR2 := UPPER(v('P44_ON_BEFORE'))
              RETURN test_tab PIPELINED
         is
              type test_c is ref cursor;
              v_row test_tab;
              v_sql varchar2(4000);
              v_cursor test_c;
         begin
              v_sql := get_query_f
                   (p_inv
                   ,p_reg
                   ,p_proarea
                   ,p_disp
                   ,p_coding
                   ,p_status
                   ,p_SIG
                   ,p_inc_sum
                   ,p_word
                   ,p_timeframe
                   ,p_rec
                   ,p_WORD_IN_SUMM
                   ,p_ON_AFTER
                   ,p_ON_BEFORE
              open v_cursor for v_sql;
              fetch v_cursor bulk collect into v_row;
              close v_cursor;
              for i in 1 .. v_row.count loop
                   pipe row (v_row(i));
              end loop;
              return;
         end test_pf;
    end MK_TEST_PF;
    /Regards
    Michael

  • Comma delimited in Sql query decode function errors out

    Hi All,
    DB: 11.2.0.3.0
    I am using the below query to generate the comma delimited output in a spool file but it errors out with the message below:
    SQL> set lines 100 pages 50
    SQL> col "USER_CONCURRENT_QUEUE_NAME" format a40;
    SQL> set head off
    SQL> spool /home/xyz/cmrequests.csv
    SQL> SELECT
    2 a.USER_CONCURRENT_QUEUE_NAME || ','
    3 || a.MAX_PROCESSES || ','
    4 || sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'Q',1,0),0)) Pending_Standby ||','
    5 ||sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'I',1,0),0)) Pending_Normal ||','
    6 ||sum(decode(b.PHASE_CODE,'R',decode(b.STATUS_CODE,'R',1,0),0)) Running_Normal
    7 from FND_CONCURRENT_QUEUES_VL a, FND_CONCURRENT_WORKER_REQUESTS b
    where a.concurrent_queue_id = b.concurrent_queue_id AND b.Requested_Start_Date <= SYSDATE
    8 9 GROUP BY a.USER_CONCURRENT_QUEUE_NAME,a.MAX_PROCESSES;
    || sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'Q',1,0),0)) Pending_Standby ||','
    ERROR at line 4:
    ORA-00923: FROM keyword not found where expected
    SQL> spool off;
    SQL>
    Expected output in the spool /home/xyz/cmrequests.csv
    Standard Manager,10,0,1,0
    Thanks for your time!
    Regards,

    Get to work immediately on marking your previous questions ANSWERED if they have been!
    >
    I am using the below query to generate the comma delimited output in a spool file but it errors out with the message below:
    SQL> set lines 100 pages 50
    SQL> col "USER_CONCURRENT_QUEUE_NAME" format a40;
    SQL> set head off
    SQL> spool /home/xyz/cmrequests.csv
    SQL> SELECT
    2 a.USER_CONCURRENT_QUEUE_NAME || ','
    3 || a.MAX_PROCESSES || ','
    4 || sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'Q',1,0),0)) Pending_Standby ||','
    5 ||sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'I',1,0),0)) Pending_Normal ||','
    6 ||sum(decode(b.PHASE_CODE,'R',decode(b.STATUS_CODE,'R',1,0),0)) Running_Normal
    7 from FND_CONCURRENT_QUEUES_VL a, FND_CONCURRENT_WORKER_REQUESTS b
    where a.concurrent_queue_id = b.concurrent_queue_id AND b.Requested_Start_Date <= SYSDATE
    8 9 GROUP BY a.USER_CONCURRENT_QUEUE_NAME,a.MAX_PROCESSES;
    || sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'Q',1,0),0)) Pending_Standby ||','
    >
    Well if you want to spool query results to a file the first thing you need to do is write a query that actually works.
    Why do you think a query like this is valid?
    SELECT 'this, is, my, giant, string, of, columns, with, commas, in, between, each, word'
    GROUP BY this, is, my, giant, stringYou only have one column in the result set but you are trying to group by three columns and none of them are even in the result set.
    What's up with that?
    You can only group by columns that are actually IN the result set.

  • SQL Developer: to_date function..

    Hi..
    Can anyone tell me what is wrong with the following:
    select to_date('02-Jun-2008 12:10:00', 'DD-MON-RR:HH24:MI:SS') - 2 "Subtract 2 days" from dual;
    I get the following result:
    31-MAY-08
    I thought the format mask supplied would generate the result in the requested format with HH:MI:SS but, as you can see...it doesn't. The nls_date_format is DD-MON-RR, but that shoudn't matter as I have supplied an alternate mask to use (should it..?).
    It's just bugging me..using SQL Developer Version 1.5.5
    Thanks,
    Rory

    Hi Roy,
    I'm sorry, but You're wrong.
    The format mask you provided is only to transform a text into date format. Not for displaying a date. So the default format mask used to display the date is the one defined in NLD_DATE_FORMAT.
    If you want display date in 'DD-MON-RR:HH24:MI:SS' format you should either modify the NLS_DATE_FORMAT or use a TO_CHAR in your select ... from dual;
    Pierre.

  • SQL query, Decode function or Case

    Hi,
    I am writing a procedure, receiving IN parameter p_user_type_cd
    where p_user_type possibles values are = 'STUDENT','STAFF','PROVIDER'
    if p_user_type_cd = 'STUDENT' , I would like to select WHERE user_type_cd = 'STUDENT'
    if p_user_type_cd = 'ALL' , then I would like to select all records. i.e I like to select 'STUDENT','STAFF','PROVIDER'.
    SELECT user_type_cd, page_name_cd, access_mode, home_page
    FROM oua_user_page_access
    WHERE user_type_cd = p_user_type_cd
    ORDER BY user_type_cd,home_page;
    Will you pl. modify this query to meet the condition.
    Thanks,
    Murali

    Hi murali,
    I got your situation and its easy to write a decode for the following
    WHERE user_type_cd = p_user_type_cdWHERE user_type_cd = decode(:p_user_type_cd, 'STUDENT','STUDENT','STAFF','STAFF','PROVIDER','PROVIDER',
    p_user_type_cd)
    Here you write the above and you will get the data.
    Hope this helps you,
    Best Wishes,
    Kranthi.

  • Reg : SQL query or Function -

    Hi Experts,
    I'm in a fix between 2 approaches.
    Scenario :
    There are 2 tables say LIT (contains Literature docs) and DIAG (contains Diagams). There's some concept called 'Base number' and 'Revision' for both the tables.
    Ex - If Literature (or Diagram) is 1 then this is the 'Base number' and further revisions to this forms - 1A, 1B, 1C...
    Similarly, 2 and 2A, 2B, 2C...
    Inside a package I need to find out for each Base Lit and Base Diagram, their latest revisions.
    Approach [1] :
    Create 2 functions (1 for Lit and other for Diag) where we'll take input as a Base No and fetch its latest.
    Approach [2] :
    Create 2 queries, we'll use SUBSTR for all lits, find the Base no and use analytic funtions like ROW_NUMBER() and find the latest revision.
    Which of the 2 will be more efficient?
    In which 'context switch' will be an overhead or any other concerns?
    Help much appreciated.
    Thanks,
    Ranit
    Oracle version : Oracle Database 10g Enterprise Edition Release *10.2.0.5.0* - 64bi

    ranit B wrote:
    Hi Experts,
    I'm in a fix between 2 approaches.
    Scenario :
    There are 2 tables say LIT (contains Literature docs) and DIAG (contains Diagams). There's some concept called 'Base number' and 'Revision' for both the tables.
    Ex - If Literature (or Diagram) is 1 then this is the 'Base number' and further revisions to this forms - 1A, 1B, 1C...
    Similarly, 2 and 2A, 2B, 2C...
    Inside a package I need to find out for each Base Lit and Base Diagram, their latest revisions.
    Approach [1] :
    Create 2 functions (1 for Lit and other for Diag) where we'll take input as a Base No and fetch its latest.
    Approach [2] :
    Create 2 queries, we'll use SUBSTR for all lits, find the Base no and use analytic funtions like ROW_NUMBER() and find the latest revision.
    Oracle version : Oracle Database 10g Enterprise Edition Release *10.2.0.5.0* - 64biSince others have already expressed opinions about Context Switches, and I favor them, so I would not comment on it. And I will suggest to opt Approach 2.
    Because, you would be able to harness the powers of FBI over the Columns storing versions and better the performance.
    Another approach, I feel, worth trying is to have an MView, that separates the Base number and Max Version Number of the corresponding base number, which would be refreshed on Commit/some other way suitable for your situation.

  • Getting run time value on form field by sql query and function

    Hi All,
    we have below requirement -
    create a form with fields as below -
    KRA_Type Grade Weightage Start Date End date
    Functinal E2 30 1-Jan-2010 1-jan-2011
    Organizational E2 70 1-jan-2010 1-jan-2011
    lov of kra_type has 2 values (Functinal, Organizational). requirement is to validate Weightage field such that for same grade :- Fuctional Weightage + Organizational Weightage = 100.
    please suggest on how to approch this.
    Thanks,
    Sam D

    this is not related to FORMS at all......
    1) create a database function which take grade parameter and return the total weightage.
    2) use it on when-validate-item trigger to validate
    create or replace function get_grade_weightage(p_grade varchar2)
    return number
    is
    v_grade number;
    begin
      select sum(Weightage)
       into v_weightage
      from mytable
    where grade = p_grade;
    exception when no_data_found then
       return 0;
    end;
    select get_grade_weightage(:grade)  into v_weigtage from dual;
    if :current_weigth < v_weightage then
        -- do something
    else
      --do something
    end if;Hope it helps
    plz mark the answer as correct or helpful if it is

  • 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

  • Using 'Function Returning SQL Query' with Flash charts

    I have created a pl/sql function that returns a SQL query as a varchar2 of this form:
    select null link
    <x value> value
    <Series1 y value> Series 1 Label
    <Series2 y value> Series 2 Label
    <Series3 y value> Series 3 Label
    from tablea a
    join tableb b
    on a.col = b.col
    order by <x value>
    If I now call the function from a Flash Chart Series SQL box with the Query Source Type set to 'Function Returning SQL Query' like this:
    return functionname(to_date('30-sep-2010', 'dd-mon-yyyy'))
    it parses correctly and the page is saved; however, when I run the page I don't get any output - nor any error messages or other indication of a problem.
    Now, if I call the function in a SQL client, capture the SQL query output using dbms_output and paste that into the Flash Chart Series SQL box - changing the Query Source Type to SQL Query - and save the page it works fine when I run it and returns a multi-series flash chart.
    Can anyone suggest either;
    1. What have I might have missed or done wrong?
    2. Any way to usefully diagnose the problem...
    I have tried using the Apex debugger - which is very nice, by the way - but it doesn't provide any info on what my problem might be. I even tried writing my own debug messages from my function using the apex_debug_message package - got nothing...
    Thanks,
    Eric

    Hi Eric,
    Try expressing the source as this:
    begin
       return functionname(to_date('30-sep-2010', 'dd-mon-yyyy'));
    end;That works fine for me, and if I take out the begin-end and the trailing semicolon from the return statement I get the same behavior as you.
    It does mention in the help for the source (only during the wizard though) that this source type has to be expressed that way, but I agree it would be helpful if the tool would validate for this format when 'Function Returning SQL Query' is used or give some sort of indication of the trouble. Anyway, this should get you going again.
    Hope this helps,
    John
    If you find this information useful, please remember to mark the post "helpful" or "correct" so that others may benefit as well.

  • [SQL Server]'TO_DATE' is not a recognized built-in function name

    Running 11g dg4msq and getting the error :
    SQL> select * from all_users@dg4msql;
    select * from all_users@dg4msql
    ERROR at line 1:
    ORA-00604: error occurred at recursive SQL level 1
    ORA-28500: connection from ORACLE to a non-Oracle system returned this message:
    [Oracle][ODBC SQL Server Driver][SQL Server]'TO_DATE' is not a recognized
    built-in function name. {10196,NativeErr = 195}

    Hi,
    This problem happened on Windows platforms after upgrading the RDBMS used to access the gateway from 10.2.0.3 to 10.2.0.4 patch 22.
    The solution is to install and apply Oracle Database 10.2.0.4 Patch 29 (Windows Bundle Patch 29) or later to the RDBMS that is running the query and connecting to the Gateway.
    This is detailed in the following note available on My Oracle Support -
    Query Using DG4MSQL Returns Error After Upgrade of RDBMS From 10.2.0.3 to 10.2.0.4 (Doc ID 1078940.1)
    Regards,
    Mike

  • Calling the function from SQL query

    Hi,
    I am trying to run the below statement,
    Select to_number(apps.pay_balance_pkg.get_value( 326, :paa.assignment_action_id,to_date ('31032011','ddmmyyyy'))) from dual;
    getting an error as :
    ORA-14552 cannot perform a DDL, commit or rollback inside a query or DML
    ORA - 06512 at apps.pay_balance_pkg , line 4526.
    How can I execute this funciton "apps.pay_balance_pkg.get_value" from sql query?
    Thanks in advance.

    user1175432 wrote:
    Hi,
    I am trying to run the below statement,
    Select to_number(apps.pay_balance_pkg.get_value( 326, :paa.assignment_action_id,to_date ('31032011','ddmmyyyy'))) from dual;
    getting an error as :
    ORA-14552 cannot perform a DDL, commit or rollback inside a query or DML
    ORA - 06512 at apps.pay_balance_pkg , line 4526.
    How can I execute this funciton "apps.pay_balance_pkg.get_value" from sql query?
    Thanks in advance.If the function is performing DDL, commit or rollback inside it then you will not be able to call it from an SQL statement.
    Either change the function so it doesn't perform DDL, commit or rollback, or use a different means to obtain the information you want (assuming you can't change the function)

  • Report- Pl/sql function returning sql query parsing page items as text?

    Hi Team,
    I am facing a strange issue .
    I have four page items namely
    1)JOB_CODE
    2)MIN_EXP
    3) MAX_EXP
    4) SOURCES1
    I have a report of the type "Pl/sql function returning sql query"
    declare
    v_sql varchar2(4000);
    begin
    if (:JOB_CODE IS NOT NULL and :MIN_EXP IS NOT NULL and :MAX_EXP IS NOT NULL and :SOURCES1 IS NOT NULL) then
    v_sql:= 'select v_candidate_id, v_fname,v_current_employer,v_Experience_years from candidature where V_REQUIREMENT = :JOB_CODE and v_experience_years >= :MIN_EXP and v_experience_years <= :MAX_EXP and source like ' || '''' || '%'|| ':SOURCES1' || '%' || '''';
    elsif (:JOB_CODE IS NULL and :MIN_EXP IS NOT NULL and :MAX_EXP IS NOT NULL and :SOURCES1 IS NOT NULL) then
    v_sql := 'select v_candidate_id, v_fname,v_current_employer,v_Experience_years from candidature where v_experience_years >= :MIN_EXP and v_experience_years <= :MAX_EXP and source like ' || '''' || '%'|| ':SOURCES1' || '%' || '''';
    elsif (:MIN_EXP IS NULL and :JOB_CODE IS NOT NULL and :MAX_EXP IS NOT NULL and :SOURCES1 IS NOT NULL) then
    v_sql := 'select v_candidate_id, v_fname,v_current_employer,v_Experience_years from candidature where v_experience_years <= :MAX_EXP and V_REQUIREMENT = :JOB_CODE and source like ' || '''' || '%'|| ':SOURCES1' || '%' || '''';
    elsif (:MAX_EXP is null and :JOB_CODE IS NOT NULL and :MIN_EXP IS NOT NULL and :SOURCES1 IS NOT NULL) then
    v_sql := 'select v_candidate_id, v_fname,v_current_employer,v_Experience_years from candidature where V_REQUIREMENT = :JOB_CODE and v_experience_years >= :MIN_EXP and source like ' || '''' || '%'|| ':SOURCES1' || '%' || '''';
    end if;
    insert into query_list values (v_sql);
    insert into debug values (:JOB_CODE , :MIN_EXP , :MAX_EXP , :SOURCES1);
    return v_sql;
    end;
    Please not that I am insertin the query into a table called Query_list and the page item values into the table called Debug thru the pl/sql function which returns teh query.
    Now I select the data from the debug tables.
    select unique(query) from query_list;
    select v_candidate_id, v_fname,v_current_employer,v_Experience_years from candidature where V_REQUIREMENT = :JOB_CODE and v_experience_years >= :MIN_EXP and v_experience_years <= :MAX_EXP and source like '%:SOURCES1%'
    select * from debug;
    JOBCODE     MINEX     MAXEX     SOURCE
    21     1     10     donkeyHire
    And if I run the query in sql I get some records returned
    select v_candidate_id, v_fname,v_current_employer,v_Experience_years from candidature where V_REQUIREMENT = 21 and v_experience_years >= 1 and v_experience_years <= and source like 'donkeyHire'
    V_CANDIDATE_ID     V_FNAME     V_CURRENT_EMPLOYER     V_EXPERIENCE_YEARS
    2     Vengu     Andale Tech     4
    But the record does not show up in the report!
    does this type of report parse page items as text?
    Why is it so?
    Waiting for an early reply.
    Thanks,
    venkat

    Venkat - You don't want to put ':SOURCES1' in quotes like that.
    Scott

  • PL/SQL function body returning SQL Query Problem

    I have wandered around the forums and found quite a bit of helpful information that has gotten me to the point I am now at. Unfortuntely, PL/SQL is not my strongest point and I am getting an error when I attempt to run my report.
    This is what I have for my package:
    CREATE OR REPLACE PACKAGE LIB2.report_query
    is
    function create_report2(v1 IN varchar2) RETURN VARCHAR2;
    end report_query;
    CREATE OR REPLACE PACKAGE BODY LIB2.report_query
    as
    function create_report2(v1 in varchar2) return varchar2
    is
    l_vc_format HTMLDB_APPLICATION_GLOBAL.VC_ARR2 := HTMLDB_UTIL.STRING_TO_TABLE(v1);
    l_format varchar2(255) := HTMLDB_UTIL.TABLE_TO_STRING(l_vc_format);
    q1 varchar2(32767) := ' ';
    begin
    q1 :=
    'select b.BOOK_ID, bk.book_id bkid, bkk.book_id bkkid, b.TITLE, b.SUBTITLE, b.SERIES, b.VOLUME, b.ISBN, f.FORMAT_RET, b.DESCRIPTION, .PUBLISHED,'
    ||'b.PURCHASED_FROM, b.COMMENTS, b.WEBSITE, c.LENGTH_MINS, stragg(p.last_name || '', '' || p.first_name) Author, '
    ||'stragg(p.person_id) person_id, '
    ||'pb.name PUBLISHER, decode(b.abridged, ''Y'',''Abridged'',''N'',''Unabridged'') Abridged,'
    ||'(nvl(d.disk01,0)+nvl(d.disk02,0)+nvl(d.disk03,0)+nvl(d.disk04,0)+nvl(d.disk05,0)+nvl(d.disk06,0)+nvl(d.disk07,0)+nvl(d.disk08,0)+'
    ||'nvl(d.disk09,0)+nvl(d.disk10,0)+nvl(d.disk11,0)+nvl(d.disk12,0)+nvl(d.disk13,0)+nvl(d.disk14,0)+nvl(d.disk15,0)+nvl(d.disk16,0)+nvl(d.disk17,0)+'
    ||'nvl(d.disk18,0)+nvl(d.disk19,0)+nvl(d.disk20,0)+nvl(d.disk21,0)+nvl(d.disk22,0)+nvl(d.disk23,0)+nvl(d.disk24,0)+nvl(d.disk25,0)+nvl(d.disk26,0)+'
    ||'nvl(d.disk27,0)+nvl(d.disk28,0)+nvl(d.disk29,0)+nvl(d.disk30,0)+nvl(d.disk31,0)+nvl(d.disk32,0)+nvl(d.disk33,0)+nvl(d.disk34,0)+nvl(d.disk35,0)+'
    ||'nvl(d.disk36,0)+nvl(d.disk37,0)+nvl(d.disk38,0)+nvl(d.disk39,0)+nvl(d.disk40,0)+nvl(d.disk41,0)+nvl(d.disk42,0)+nvl(d.disk43,0)+nvl(d.disk44,0)+'
    ||'nvl(d.disk45,0)+nvl(d.disk46,0)+nvl(d.disk47,0)+nvl(d.disk48,0)+nvl(d.disk49,0)+nvl(d.disk50,0)) total_tracks'
    ||'from book b, '
    ||'book bk,'
    ||'book bkk,'
    ||'person p,'
    ||'lkup_book_author la,'
    ||'lkup_book_publisher lp,'
    ||'lkup_book_format lkf,'
    ||'format f,'
    ||'publisher pb,'
    ||'conversion_info_audio c,'
    ||'lkup_book_disk_info d'
    ||'where b.book_id = la.book_id'
    ||'and b.book_id = bk.book_id'
    ||'and b.book_id = bkk.book_id'
    ||'and p.person_id = la.author_id'
    ||'and b.book_id = lp.book_id'
    ||'and b.book_id = c.book_id'
    ||'and b.book_id = d.book_id'
    ||'and b.book_id = lkf.book_id'
    ||'and lkf.format_id = f.format_id'
    ||'and pb.publisher_id(+) = lp.publisher_id'
    ||'and b.wishlist = ''N'''
    ||'and (upper(b.book_id) like ''%'' || upper(:P40_SEARCH) || ''%'''
    ||'or upper(b.title) like ''%'' || upper(:P40_SEARCH) || ''%'''
    ||'or upper(b.subtitle) like ''%'' || upper(:P40_SEARCH) || ''%'''
    ||'or upper(b.series) like ''%'' || upper(:P40_SEARCH) || ''%'''
    ||'or upper(b.volume) like ''%'' || upper(:P40_SEARCH) || ''%'''
    ||'or upper(b.isbn) like ''%'' || upper(:P40_SEARCH) || ''%'''
    ||'or upper(b.format) like ''%'' || upper(:P40_SEARCH) || ''%'''
    ||'or upper(b.description) like ''%'' || upper(:P40_SEARCH) || ''%'''
    ||'or upper(b.published) like ''%'' || upper(:P40_SEARCH) || ''%'''
    ||'or upper(b.purchased_from) like ''%'' || upper(:P40_SEARCH) || ''%'''
    ||'or upper(b.comments) like ''%'' || upper(:P40_SEARCH) || ''%'''
    ||'or upper(b.website) like ''%'' || upper(:P40_SEARCH) || ''%'''
    ||'or upper(p.last_name) like ''%'' || upper(:P40_SEARCH) || ''%'''
    ||'or upper(p.first_name) like ''%'' || upper(:P40_SEARCH) || ''%'''
    ||'or upper(pb.name) like ''%'' || upper(:P40_SEARCH) || ''%'''
    ||'or upper(:P40_SEARCH) is null)'
    ||'and ((upper(b.title) like ''%'' || upper(:P40_TITLE) || ''%'' or upper(:P40_TITLE) is null))'
    ||'and ((upper(b.series) like ''%'' || upper(:P40_SERIES) || ''%'' or upper(:P40_SERIES) is null))'
    ||'and ((upper(p.last_name) like ''%'' || upper(:P40_LASTNAME) || ''%'' or upper(:P40_LASTNAME) is null))'
    ||'and ((upper(p.first_name) like ''%'' || upper(:P40_FIRSTNAME) || ''%'' or upper(:P40_FIRSTNAME) is null))'
    ||'and ((upper(f.format_ret) in (upper(l_vc_format)) or upper(:P40_FORMAT) is null))'
    ||'group by b.BOOK_ID, bk.book_id, bkk.book_id, b.TITLE, b.SUBTITLE, b.SERIES, b.VOLUME, b.ISBN, f.FORMAT_ret, b.DESCRIPTION, '
    ||'b.PUBLISHED, b.PURCHASED_FROM, b.COMMENTS, b.WEBSITE, c.LENGTH_MINS, pb.name, b.abridged, '
    ||'(nvl(d.disk01,0)+nvl(d.disk02,0)+nvl(d.disk03,0)+nvl(d.disk04,0)+nvl(d.disk05,0)+nvl(d.disk06,0)+nvl(d.disk07,0)+nvl(d.disk08,0)+'
    ||'nvl(d.disk09,0)+nvl(d.disk10,0)+nvl(d.disk11,0)+nvl(d.disk12,0)+nvl(d.disk13,0)+nvl(d.disk14,0)+nvl(d.disk15,0)+nvl(d.disk16,0)+nvl(d.disk17,0)+'
    ||'nvl(d.disk18,0)+nvl(d.disk19,0)+nvl(d.disk20,0)+nvl(d.disk21,0)+nvl(d.disk22,0)+nvl(d.disk23,0)+nvl(d.disk24,0)+nvl(d.disk25,0)+nvl(d.disk26,0)+'
    ||'nvl(d.disk27,0)+nvl(d.disk28,0)+nvl(d.disk29,0)+nvl(d.disk30,0)+nvl(d.disk31,0)+nvl(d.disk32,0)+nvl(d.disk33,0)+nvl(d.disk34,0)+nvl(d.disk35,0)+'
    ||'nvl(d.disk36,0)+nvl(d.disk37,0)+nvl(d.disk38,0)+nvl(d.disk39,0)+nvl(d.disk40,0)+nvl(d.disk41,0)+nvl(d.disk42,0)+nvl(d.disk43,0)+nvl(d.disk44,0)+'
    ||'nvl(d.disk45,0)+nvl(d.disk46,0)+nvl(d.disk47,0)+nvl(d.disk48,0)+nvl(d.disk49,0)+nvl(d.disk50,0))';
    RETURN q1;
    EXCEPTION
    WHEN OTHERS THEN
    RETURN q1;
    end create_report2;
    end;
    And here is what I have for my Region Source on my report:
    return lib2.report_query.create_report2(v('P40_FORMAT'));
    Here is my error when I run the page:
    failed to parse SQL query:
    ORA-00936: missing expression
    I have tried the region source line in many variations, this is just my latest one. None of them have worked. I am quite obviously missing something quite important and probably extremely silly. Any ideas?
    Thanks!
    Chrissy

    Chrissy,
    This is what the package returns as a query:
    select b.BOOK_ID, bk.book_id bkid, bkk.book_id bkkid,
    b.TITLE, b.SUBTITLE, b.SERIES, b.VOLUME, b.ISBN, f.FORMAT_RET,
    b.DESCRIPTION, .PUBLISHED,b.PURCHASED_FROM, b.COMMENTS, b.WEBSITE,
    c.LENGTH_MINS, stragg(p.last_name || ', ' || p.first_name) Author,
    stragg(p.person_id) person_id, pb.name PUBLISHER, decode(b.abridged,
    'Y','Abridged','N','Unabridged') Abridged,
    (nvl(d.disk01,0)+nvl(d.disk02,0)+nvl(d.disk03,0)+nvl(d.disk04,0)+nvl(d.disk05,0)
    nvl(d.disk06,0)nvl(d.disk07,0)+nvl(d.disk08,0)+nvl(d.disk09,0)+nvl(d.disk10,0)
    nvl(d.disk11,0)nvl(d.disk12,0)+nvl(d.disk13,0)+nvl(d.disk14,0)+nvl(d.disk15,0)
    nvl(d.disk16,0)nvl(d.disk17,0)+nvl(d.disk18,0)+nvl(d.disk19,0)+nvl(d.disk20,0)
    nvl(d.disk21,0)nvl(d.disk22,0)+nvl(d.disk23,0)+nvl(d.disk24,0)+nvl(d.disk25,0)
    nvl(d.disk26,0)nvl(d.disk27,0)+nvl(d.disk28,0)+nvl(d.disk29,0)+nvl(d.disk30,0)
    nvl(d.disk31,0)nvl(d.disk32,0)+nvl(d.disk33,0)+nvl(d.disk34,0)+nvl(d.disk35,0)
    nvl(d.disk36,0)nvl(d.disk37,0)+nvl(d.disk38,0)+nvl(d.disk39,0)+nvl(d.disk40,0)
    nvl(d.disk41,0)nvl(d.disk42,0)+nvl(d.disk43,0)+nvl(d.disk44,0)+nvl(d.disk45,0)
    nvl(d.disk46,0)nvl(d.disk47,0)+nvl(d.disk48,0)+nvl(d.disk49,0)+nvl(d.disk50,0)) total_tracksfrom book b,
    book bk,book bkk,person p,lkup_book_author la,lkup_book_publisher lp,
    lkup_book_format lkf,format f,publisher pb,conversion_info_audio c,
    lkup_book_disk_info dwhere b.book_id = la.book_idand b.book_id = bk.book_idand b.book_id = bkk.book_idand p.person_id = la.author_idand
    b.book_id = lp.book_idand b.book_id = c.book_idand b.book_id = d.book_idand
    b.book_id = lkf.book_idand lkf.format_id = f.format_idand pb.publisher_id(+) = lp.publisher_idand
    b.wishlist = 'N'and (upper(b.book_id) like '%' || upper(:P40_SEARCH) || '%'
    or upper(b.title) like '%' || upper(:P40_SEARCH) || '%'or upper(b.subtitle) like '%' ||
    upper(:P40_SEARCH) || '%'or upper(b.series) like '%' || upper(:P40_SEARCH) || '%'or
    upper(b.volume) like '%' || upper(:P40_SEARCH) || '%'or upper(b.isbn) like '%' ||
    upper(:P40_SEARCH) || '%'or upper(b.format) like '%' || upper(:P40_SEARCH) || '%'or upper(b.description) like '%' || upper(:P40_SEARCH) || '%'or upper(b.published)
    like '%' || upper(:P40_SEARCH) || '%'or upper(b.purchased_from) like '%' ||
    upper(:P40_SEARCH) || '%'or upper(b.comments) like '%' || upper(:P40_SEARCH)
    || '%'or upper(b.website) like '%' || upper(:P40_SEARCH) || '%'or
    upper(p.last_name) like '%' || upper(:P40_SEARCH) || '%'or upper(p.first_name)
    like '%' || upper(:P40_SEARCH) || '%'or upper(pb.name) like '%' ||
    upper(:P40_SEARCH) || '%'or upper(:P40_SEARCH) is null)and
    ((upper(b.title) like '%' || upper(:P40_TITLE) || '%' or
    upper(:P40_TITLE) is null))and ((upper(b.series) like '%' ||
    upper(:P40_SERIES) || '%' or upper(:P40_SERIES) is null))and
    ((upper(p.last_name) like '%' || upper(:P40_LASTNAME) || '%' or
    upper(:P40_LASTNAME) is null))and ((upper(p.first_name) like '%' ||
    upper(:P40_FIRSTNAME) || '%' or upper(:P40_FIRSTNAME) is null))and
    ((upper(f.format_ret) in (upper(l_vc_format)) or upper(:P40_FORMAT) is null))
    group by b.BOOK_ID, bk.book_id, bkk.book_id, b.TITLE, b.SUBTITLE, b.SERIES, b.VOLUME,
    b.ISBN, f.FORMAT_ret, b.DESCRIPTION, b.PUBLISHED, b.PURCHASED_FROM, b.COMMENTS, b.WEBSITE,
    c.LENGTH_MINS, pb.name, b.abridged, (nvl(d.disk01,0)+nvl(d.disk02,0)+nvl(d.disk03,0)
    nvl(d.disk04,0)nvl(d.disk05,0)+nvl(d.disk06,0)+nvl(d.disk07,0)+nvl(d.disk08,0)
    nvl(d.disk09,0)nvl(d.disk10,0)+nvl(d.disk11,0)+nvl(d.disk12,0)+nvl(d.disk13,0)
    nvl(d.disk14,0)nvl(d.disk15,0)+nvl(d.disk16,0)+nvl(d.disk17,0)+nvl(d.disk18,0)
    nvl(d.disk19,0)nvl(d.disk20,0)+nvl(d.disk21,0)+nvl(d.disk22,0)+nvl(d.disk23,0)
    nvl(d.disk24,0)nvl(d.disk25,0)+nvl(d.disk26,0)+nvl(d.disk27,0)+nvl(d.disk28,0)
    nvl(d.disk29,0)nvl(d.disk30,0)+nvl(d.disk31,0)+nvl(d.disk32,0)+nvl(d.disk33,0)
    nvl(d.disk34,0)nvl(d.disk35,0)+nvl(d.disk36,0)+nvl(d.disk37,0)+nvl(d.disk38,0)
    nvl(d.disk39,0)nvl(d.disk40,0)+nvl(d.disk41,0)+nvl(d.disk42,0)+nvl(d.disk43,0)
    nvl(d.disk44,0)nvl(d.disk45,0)+nvl(d.disk46,0)+nvl(d.disk47,0)+nvl(d.disk48,0)
    nvl(d.disk49,0)nvl(d.disk50,0))
    This query will never work. I marked only couple of errors you have there. Actually,
    I meant "formating" errors rather than "syntactical" errors. You are missing spaces
    all over the place. This is why I suggested to use a CLOB column in a test table
    to return the query for debugging purposes - this is how I do it at least, when I need
    to escape and concatenate a lot of code.
    If you are referencing item values from a user session in a function or a procedure,
    then you need to use the v('ITEM') syntax instead of :ITEM syntax. However, if the
    output of this procedure will be used as a function returning SQL query, you will
    be fine with :ITEM.
    Denes Kubicek
    http://deneskubicek.blogspot.com/
    http://htmldb.oracle.com/pls/otn/f?p=31517:1
    -------------------------------------------------------------------

  • Converting SQL Report Region to PLSQL Function Body Returning SQL Query

    All:
    I want to convert my SQL Report Region to a PLSQL-generated SQL Report Region so that I can eliminate where clauses dynamically and speed up my app, and also so that I can provide additional sorting options.
    <br><br>
    My problem is that I have lots of embedded single quotes that already are coded as 3 single quotes in the SQL Report Region. I am not sure at all how to code them within the PLSQL.
    <br><br>
    As example, here is one column from my query:
    <br><br>
    select decode(nvl(g.date_sub_1,g.date_rec_1), null, decode(g.article, 0, 'E', 1, 'U'), '< a href="javascript:unElevate( ' ' ' || g.grid || ' ' ',' ' ' || g.natca || ' ' ')">' || decode(g.article, 0, 'E', 1, 'U') || '< /a>') "Reverse" from g_table g
    <br><br>
    (Note that I added spaces within the code so it would display properly in the browser.)
    <br><br>
    To clarify, that's a double quote before javascript, and then 3 single quotes before the first concatenation group, then 3 single quotes after the second concatenation group, then 3 single quotes before the third, 3 single quotes after the fourth followed by an end-paren followed by a double quote etc.
    <br><br>
    My question is, what do I do with these triple-single quotes within PLSQL? Probably a no-brainer for the experienced folks, but I am thinking like a mobius strip at this point.
    <br><br>
    Bill
    Message was edited by:
    [email protected]
    Message was edited by:
    [email protected]
    Message was edited by:
    [email protected]

    Scott,
    I think that feature was made for my situation! I keep getting
    Function returning SQL query: 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.
    (ORA-20001: Unable to bind :43 verify length of item is 30 bytes or less. Use v() syntax to reference items longer than 30 bytes. ORA-01006: bind variable does not exist)
    I did chop the query up into little bits, i.e.
    p_sql := p_sql || q'! ... !';
    p_sql := p_sql || q'! ... !';
    p_sql := p_sql || q'! ... !';
    return p_sql;
    But I'm not sure what I'm supposed to do here.
    Thank you.
    Bill

Maybe you are looking for