High # fetches for a select query

Following is the tkprof output. Can anybody tell why the fetches are too high and what should be done? Thanks.
SELECT COUNT (*)
FROM
ACA_PI_ALLOC_CVE A WHERE A.PRGRM_INDEX = :B2 AND A.ALCTN_CODE = :B1 AND
TRUNC (SYSDATE) BETWEEN TRUNC (A.FROM_DATE) AND TRUNC (A.TO_DATE) AND
A.STATUS_CID = 2 AND A.OPRTNL_FLAG = 'A' AND A.FISCAL_MNTH =
FN_FISCAL_MONTH (SYSDATE)
call count cpu elapsed disk query current rows
Parse 6494 0.32 0.31 0 0 0 0
Execute 18760 1.69 1.67 0 0 0 0
Fetch 18760 604.39 593.07 801 22993642 0 18760
total 44014 606.40 595.05 801 22993642 0 18760
Misses in library cache during parse: 1
Misses in library cache during execute: 2
Optimizer mode: ALL_ROWS
Parsing user id: 277 (recursive depth: 1)
Rows Row Source Operation
5 SORT AGGREGATE (cr=2242 pr=17 pw=0 time=302663 us)
5 TABLE ACCESS BY INDEX ROWID ACA_PI_ALLOC_CVE (cr=2242 pr=17 pw=0 time=302541 us)
3265 INDEX RANGE SCAN TUNE_ACA_PI_ALLOC_CVE_01 (cr=30 pr=0 pw=0 time=26431 us)(object id 1331914)

FeNiCrC_Neil wrote:
Thanks Enrique. Is there a way to decrease the elapsed time for the fetches?I think that Mark pointed you in the right direction. Reposting what you previously posted:
SELECT
  COUNT (*)
FROM
  ACA_PI_ALLOC_CVE A
WHERE
  A.PRGRM_INDEX = :B2
  AND A.ALCTN_CODE = :B1
  AND TRUNC (SYSDATE) BETWEEN TRUNC (A.FROM_DATE) AND TRUNC (A.TO_DATE)
  AND A.STATUS_CID = 2
  AND A.OPRTNL_FLAG = 'A'
  AND A.FISCAL_MNTH = FN_FISCAL_MONTH (SYSDATE);
call     count    cpu elapsed disk    query current  rows
Parse     6494   0.32    0.31    0        0       0     0
Execute  18760   1.69    1.67    0        0       0     0
Fetch    18760 604.39  593.07  801 22993642       0 18760
total    44014 606.40  595.05  801 22993642       0 18760
Misses in library cache during parse: 1
Misses in library cache during execute: 2
Optimizer mode: ALL_ROWS
Parsing user id: 277 (recursive depth: 1)
Rows Row Source Operation
   5 SORT AGGREGATE (cr=2242 pr=17 pw=0 time=302663 us)
   5  TABLE ACCESS BY INDEX ROWID ACA_PI_ALLOC_CVE (cr=2242 pr=17 pw=0 time=302541 us)
3265   INDEX RANGE SCAN TUNE_ACA_PI_ALLOC_CVE_01 (cr=30 pr=0 pw=0 time=26431 us)(object id 1331914)The query, which is executed 18,760 times, is performing 22,993,642 logical IOs and is experiencing CPU limitations (606.40 CPU seconds, about 1/3 of a second per execution) due to the number of logical IOs. The execution plan that you posted is for a single execution, using a very unselective index. In this case, the unselective index returned 3,265 rows, of which only 5 survived the other restrictions specified by the WHERE clause.
Charles Hooper
IT Manager/Oracle DBA
K&M Machine-Fabricating, Inc.

Similar Messages

  • Need help in optimisation for a select query on a large table

    Hi Gurus
    Please help in optimising the code. It takes 1 hr for 3-4000 records. Its very slow.
    My Select is reading from a table which contains 10 Million records.
    I am writing the select on large table and Retrieving the values from large tables by comparing my table which has 3-4 k records.
    I am pasting the code. please help
    Data: wa_i_tab1 type tys_tg_1 .
    DATA: i_tab TYPE STANDARD TABLE OF tys_tg_1.
    Data : wa_result_pkg type tys_tg_1,
    wa_result_pkg1 type tys_tg_1.
    SELECT /BIC/ZSETLRUN AGREEMENT /BIC/ZREB_SDAT /BIC/ZLITEM1 from
    /BIC/PZREB_SDAT *******************THIS TABLE CONTAINS 10 MILLION RECORDS
    into CORRESPONDING FIELDS OF table i_tab
    FOR ALL ENTRIES IN RESULT_PACKAGE***************CONTAINS 3000-4000 RECORDS
    where
    /bic/ZREB_SDAT = RESULT_PACKAGE-/BIC/ZREB_SDAT
    AND
    AGREEMENT = RESULT_PACKAGE-AGREEMENT
    AND /BIC/ZLITEM1 = RESULT_PACKAGE-/BIC/ZLITEM1.
    sort RESULT_PACKAGE by AGREEMENT /BIC/ZREB_SDAT /BIC/ZLITEM1.
    sort i_tab by AGREEMENT /BIC/ZREB_SDAT /BIC/ZLITEM1.
    loop at RESULT_PACKAGE into wa_result_pkg.
    read TABLE i_tab INTO wa_i_tab1 with key
    /BIC/ZREB_SDAT =
    wa_result_pkg-/BIC/ZREB_SDAT
    AGREEMENT = wa_result_pkg-AGREEMENT
    /BIC/ZLITEM1 = wa_result_pkg-/BIC/ZLITEM1.
    IF SY-SUBRC = 0.
    move wa_i_tab1-/BIC/ZSETLRUN to
    wa_result_pkg-/BIC/ZSETLRUN.
    wa_result_pkg1-/BIC/ZSETLRUN = wa_result_pkg-/BIC/ZSETLRUN.
    modify RESULT_PACKAGE from wa_result_pkg1
    TRANSPORTING /BIC/ZSETLRUN.
    ENDIF.
    CLEAR: wa_i_tab1,wa_result_pkg1,wa_result_pkg.
    endloop.

    Hi,
    1) RESULT_PACKAGE internal table contains any duplicate records or not bassed on the where condotion like below
    2) Remove the into CORRESPONDING FIELDS OF table instead of that into table use.
    refer the below code is
    RESULT_PACKAGE1[] = RESULT_PACKAGE[].
    sort RESULT_PACKAGE1 by /BIC/ZREB_SDAT AGREEMENT /BIC/ZLITEM1.
    delete adjustant duplicate form RESULT_PACKAGE1 comparing /BIC/ZREB_SDAT AGREEMENT /BIC/ZLITEM1.
    SELECT /BIC/ZSETLRUN AGREEMENT /BIC/ZREB_SDAT /BIC/ZLITEM1
    from /BIC/PZREB_SDAT
    into table i_tab
    FOR ALL ENTRIES IN RESULT_PACKAGE1
    where
    /bic/ZREB_SDAT = RESULT_PACKAGE1-/BIC/ZREB_SDAT
    AND
    AGREEMENT = RESULT_PACKAGE1-AGREEMENT
    AND /BIC/ZLITEM1 = RESULT_PACKAGE1-/BIC/ZLITEM1.
    and one more thing your getting 10 million records so use package size in you select query.
    Refer the following link also For All Entry for 1 Million Records
    Regards,
    Dhina..
    Edited by: Dhina DMD on Sep 15, 2011 7:17 AM

  • I need to know the proper syntax for my SELECT query, please.

    Hello All,
    Quick one for you:
    Let's say that I have several columns in a table with names such as subject_1, subject_2, subject_3, etc. The table's name is subject_names.
    The number in each of the three column name examples is also a value passed along a query string, the user can select choices, 1, 2 or 3. That query string's variable is $qs.
    So, what I want is a SELECT query that uses the query string value as follows (KEEP IN MIND, I know this is not the proper syntax):
    "SELECT subject_[$qs]
    FROM subject_names";
    I have tried all sorts of cominations of quotes (single and double), dots, brackets, braces and parenthesis. I just want to know how to include such a variable within this code.
    Any and all help is sincerely appreciated!
    Cheers,
    wordman

    Well, I did give you the syntax though.
    $query = 'SELECT ' . $qs . ' FROM tbl_name';
    I put spaces between the periods this time to make it more clear.
    If you put the actual word 'subject' in there and just want your form to name it's options as the numbers available you could do this:
    $query = 'SELECT subject_' . $qs . ' FROM tbl_name';
    In PHP you can use either single or double quotes around your query string, I always just use single quotes. I see a lot of other use double quotes.
    Double quotes would look like:
    $query = "SELECT subject_' . $qs . ' FROM tbl_name";
    Or when using double quotes you can actually just place the variable right in the string without having to concatenate multiple strings like above.
    Since you mentioned that you are good with passing variables I probably don't have to mention that you need to set the value attribute of your option tags (if you are using a select) to the value you want them to pass.
    Ex:
    <select name="choices">
         <option value="1">1</option>
         <option value="2">2</option>
         <option value="3">3</option>
    </select>
    If you have that part all figured out then you can use the syntax above for your query string.
    Good luck.

  • Dump for a select query

    Hi Gurus,
    I have a select query as follows:
        SELECT
                   vbap~vbeln
                   vbap~posnr
                   vbap~matnr
                   vbap~arktx
                   vbap~pstyv
                   vbap~abgru
                   vbap~prodh
                   vbap~netwr
                   vbap~werks
                   vbap~kwmeng
                   vbap~prctr
                   vbap~ps_psp_pnr
                               FROM vbap
                INTO TABLE i_item_vbap
                FOR ALL ENTRIES IN i_reg_vbak
                WHERE
                vbap~vbeln EQ i_reg_vbak-vbeln AND
                vbap~matnr IN s_matnr          AND
                vbap~werks IN s_werks          .
    However, when I execute the program, I get the dump saying as:
    I get this exception "DBIF_RSQL_INVALID_RSQL", assigned to CLASS  "CX_SY_OPEN_SQL_DB"
    Please help me in understanding this.
    Also, there are only 85 entries in i_reg_vbak, plus not many values are there for s_matnr and s_werks.
    I tried executing this query w/o for all entries, but still it gave a dump.
    Waiting for your reply, thanks!

    hello
    SELECT
    vbap~vbeln
    vbap~posnr
    vbap~matnr
    vbap~arktx
    vbap~pstyv
    vbap~abgru
    vbap~prodh
    vbap~netwr
    vbap~werks
    vbap~kwmeng
    vbap~prctr
    vbap~ps_psp_pnr
    from vbap into corresponding fields of table it_vbap.
    if u use any  fields of vbap on selection scrren then mention as
    eg: where vbeln in s_vbeln etc.
    with regards,
    sumanth reddy

  • How can replace IF condition for a Select query in my reports?

    IF s_bukrs-LOW = '4312' OR s_bukrs-LOW = '4313' OR s_bukrs-LOW = '4349'  .
    ELSEIF s_bukrs-LOW = '4310' OR s_bukrs-LOW = '4311' OR s_bukrs-LOW = '4587'.
    ENDIF.
    What if I want to use select query in place of IF condition, in my report for a Z -table in which I have made entries of ZZUSEREXIT-my progam name ,VAR1-4310,VAR2-4311,VAR3-4312,VAR4-4313,VAR5-4349,VAR6-4587?

    HI
    U can do this by two ways-
    (1) Using Two SELECT statementsie. one for IF statements and second for ELSEIF statements.
    Like--
        SELECT < field name>,
    WHERE s_bukrs-LOW = '4312' OR s_bukrs-LOW = '4313' OR s_bukrs-LOW = '4349' 
    SELECT < field name>,
    WHERE s_bukrs-LOW = '4310' OR s_bukrs-LOW = '4311' OR s_bukrs-LOW = '4587'.
    (2) U can do this in using IF with select statements.
    This will help u

  • "System Resource Exceeded" for simple select query in Access 2013

    Using Access 2013 32-bit on a Windows Server 2008 R2 Enterprise. This computer has
    8 GB of RAM.
    I am getting:
    "System Resource Exceeded"  errors in two different databases
    for simple queries like:
    SELECT FROM .... GROUP BY ...
    UPDATE... SET ... WHERE ...
    I compacted the databases several times, no result. One database size is approx 1 GB, the other one is approx. 600 MB.
    I didn't have any problems in Office 2010
    so I had to revert to this version.
    Please advise.
    Regards,
    M.R.

    Hi Greg. I too am running Access on an RDP server. Checking Task Manager, I can see many copies of MSACCESS running in the process list, from all users on the server. We typically have 40-60 users on that server. I am only changing the Processor Affinity
    for MY copy, and only when I run into this problem. Restarting Access daily, I always get back to multi-processor mode soon thereafter.
    As this problem only seems to happen on very large Access table updates, and as there are only three of us performing those kind of updates, we have good control on who might want to change the affinity setting to solve this problem. However, I
    understand that in other environments this might not be a good solution. In my case, we have 16 processors on the server, so I always take #1, my co-worker here in the US always takes #2, etc. This works for us, and I am only describing it here in case it
    works for someone else.
    The big question in my mind is what multi-threading methods are employed by Microsoft for Access that would cause this problem for very large datasets. Processing time for an update query on, say, 2 million records is massively improved by going down
    to 1 processor. The problem is easily reproduced, and so far I have not seen it in Excel even when working with very large worksheets. Also have not seen it in MS SQL. It is just happening in Access.

  • Short Dump for Dynamic Select Query

    Hello all,
    I get a short dump for my dynamic select query at the end of the code. The error is "The types of operands "dbtab" and "itab" cannot be converted into one another."
    My code looks like below.
    FORM get_ccnum_2  USING    p_tabname TYPE dd03l-tabname.
    DATA: p_table(30)  TYPE c.
      FIELD-SYMBOLS:  <dyn_wa>,
                                   <t> TYPE table.
      DATA: it_fldcat    TYPE lvc_t_fcat.
      TYPE-POOLS : abap.
      DATA: it_details   TYPE abap_compdescr_tab,
            wa_details   TYPE abap_compdescr.
      DATA: ref_descr    TYPE REF TO cl_abap_structdescr.
      DATA: new_table    TYPE REF TO data,
            new_line     TYPE REF TO data,
            wa_it_fldcat TYPE lvc_s_fcat.
    p_table = p_tabname.
      ref_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
      it_details[] = ref_descr->components[].
      LOOP AT it_details INTO wa_details.
        CLEAR wa_it_fldcat.
        wa_it_fldcat-fieldname = wa_details-name .
        wa_it_fldcat-datatype  = wa_details-type_kind.
        wa_it_fldcat-intlen    = wa_details-length.
        wa_it_fldcat-decimals  = wa_details-decimals.
        APPEND wa_it_fldcat TO it_fldcat .
      ENDLOOP.
      CALL METHOD cl_alv_table_create=>create_dynamic_table
        EXPORTING
          it_fieldcatalog = it_fldcat
        IMPORTING
          ep_table        = new_table.
    ASSIGN new_table->* TO <t>.
    CREATE DATA new_line LIKE LINE OF <t>.
      ASSIGN new_line->* TO <dyn_wa>.
    wa_cond = 'CCNUM <> '' '' '.
    APPEND wa_cond TO tab_cond.
          SELECT * INTO TABLE <t>
                   FROM     (p_table)
                   WHERE    (tab_cond)
                   ORDER BY (tab_ord).
    ENDFORM.                    " GET_CCNUM_2

    Hi,
    I tried to execute the code using table BSEGC and it gave a short dump..
    the actual exception that shows in ST22 IS ..UNICODE_TYPES_NOT_CONVERTIBLE..
    I think there is something wrong in the internal table creation..
    Instead of using the method cl_alv_table_create=>create_dynamic_table to create the dynamic table I used the following and it worked..
    CREATE DATA new_table TYPE TABLE OF (p_table).
    * Comment begin  " Naren
    *  ref_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
    *  it_details[] = ref_descr->components[].
    *  LOOP AT it_details INTO wa_details.
    *    CLEAR wa_it_fldcat.
    *    wa_it_fldcat-fieldname = wa_details-name .
    *    wa_it_fldcat-datatype  = wa_details-type_kind.
    *    wa_it_fldcat-intlen    = wa_details-length.
    *    wa_it_fldcat-decimals  = wa_details-decimals.
    *    APPEND wa_it_fldcat TO it_fldcat .
    *  ENDLOOP.
    *  CALL METHOD cl_alv_table_create=>create_dynamic_table
    *    EXPORTING
    *      it_fieldcatalog = it_fldcat
    *    IMPORTING
    *      ep_table        = new_table.
    * Comment End.  " Naren
    CREATE DATA new_table TYPE TABLE OF (p_table).   " New code by naren
    Please Try this..
    Thanks
    Naren

  • Need help for SQL SELECT query to fetch XML records from Oracle tables having CLOB field

    Hello,
    I have a scenario wherein i need to fetch records from several oracle tables having CLOB fields(which is holding XML) and then merge them logically to form a hierarchy XML. All these tables are related with PK-FK relationship. This XML hierarchy is having 'OP' as top-most root node and ‘DE’ as it’s bottom-most node with One-To-Many relationship. Hence, Each OP can have multiple GM, Each GM can have multiple DM and so on.
    Table structures are mentioned below:
    OP:
    Name                             Null                    Type        
    OP_NBR                    NOT NULL      NUMBER(4)    (Primary Key)
    OP_DESC                                        VARCHAR2(50)
    OP_PAYLOD_XML                           CLOB       
    GM:
    Name                          Null                   Type        
    GM_NBR                  NOT NULL       NUMBER(4)    (Primary Key)
    GM_DESC                                       VARCHAR2(40)
    OP_NBR               NOT NULL          NUMBER(4)    (Foreign Key)
    GM_PAYLOD_XML                          CLOB   
    DM:
    Name                          Null                    Type        
    DM_NBR                  NOT NULL         NUMBER(4)    (Primary Key)
    DM_DESC                                         VARCHAR2(40)
    GM_NBR                  NOT NULL         NUMBER(4)    (Foreign Key)
    DM_PAYLOD_XML                            CLOB       
    DE:
    Name                          Null                    Type        
    DE_NBR                     NOT NULL           NUMBER(4)    (Primary Key)
    DE_DESC                   NOT NULL           VARCHAR2(40)
    DM_NBR                    NOT NULL           NUMBER(4)    (Foreign Key)
    DE_PAYLOD_XML                                CLOB    
    +++++++++++++++++++++++++++++++++++++++++++++++++++++
    SELECT
    j.op_nbr||'||'||j.op_desc||'||'||j.op_paylod_xml AS op_paylod_xml,
    i.gm_nbr||'||'||i.gm_desc||'||'||i.gm_paylod_xml AS gm_paylod_xml,
    h.dm_nbr||'||'||h.dm_desc||'||'||h.dm_paylod_xml AS dm_paylod_xml,
    g.de_nbr||'||'||g.de_desc||'||'||g.de_paylod_xml AS de_paylod_xml,
    FROM
    DE g, DM h, GM i, OP j
    WHERE
    h.dm_nbr = g.dm_nbr(+) and
    i.gm_nbr = h.gm_nbr(+) and
    j.op_nbr = i.op_nbr(+)
    +++++++++++++++++++++++++++++++++++++++++++++++++++++
    I am using above SQL select statement for fetching the XML records and this gives me all related xmls for each entity in a single record(OP, GM, DM. DE). Output of this SQL query is as below:
    Current O/P:
    <resultSet>
         <Record1>
              <OP_PAYLOD_XML1>
              <GM_PAYLOD_XML1>
              <DM_PAYLOD_XML1>
              <DE_PAYLOD_XML1>
         </Record1>
         <Record2>
              <OP_PAYLOD_XML2>
              <GM_PAYLOD_XML2>
              <DM_PAYLOD_XML2>
              <DE_PAYLOD_XML2>
         </Record2>
         <RecordN>
              <OP_PAYLOD_XMLN>
              <GM_PAYLOD_XMLN>
              <DM_PAYLOD_XMLN>
              <DE_PAYLOD_XMLN>
         </RecordN>
    </resultSet>
    Now i want to change my SQL query so that i get following output structure:
    <resultSet>
         <Record>
              <OP_PAYLOD_XML1>
              <GM_PAYLOD_XML1>
              <GM_PAYLOD_XML2> .......
              <GM_PAYLOD_XMLN>
              <DM_PAYLOD_XML1>
              <DM_PAYLOD_XML2> .......
              <DM_PAYLOD_XMLN>
              <DE_PAYLOD_XML1>
              <DE_PAYLOD_XML2> .......
              <DE_PAYLOD_XMLN>
         </Record>
         <Record>
              <OP_PAYLOD_XML2>
              <GM_PAYLOD_XML1'>
              <GM_PAYLOD_XML2'> .......
              <GM_PAYLOD_XMLN'>
              <DM_PAYLOD_XML1'>
              <DM_PAYLOD_XML2'> .......
              <DM_PAYLOD_XMLN'>
              <DE_PAYLOD_XML1'>
              <DE_PAYLOD_XML2'> .......
              <DE_PAYLOD_XMLN'>
         </Record>
    <resultSet>
    Appreciate your help in this regard!

    Hi,
    A few questions :
    How's your first query supposed to give you an XML output like you show ?
    Is there something you're not telling us?
    What's the content of, for example, <OP_PAYLOD_XML1> ?
    I don't think it's a good idea to embed the node level in the tag name, it would make much sense to expose that as an attribute.
    What's the db version BTW?

  • How to reduce logical count and scan count for a select query

    hi,
    I have two tables one is master and other is history. i need to combine this two tables into one temporary table.
    I am using the below query to create temp table.
    Select * into temporders
    from
    (select * from orders
    union
    select * from ordershistory) b
    where updateon= (select max(updateon)from (select updateon,name,units,subunits from orders
    union
    select updateon,name,units,subunits from ordershistory) a
    where updateon <='11/08/2008 11:18 AM' and a.name=b.name and a.units=b.units and a.subunits=b.subunits group by name,units,subunits)
    order by report,subunitsorder
    the statistics for this query:
    SQL Server parse and compile time:
    CPU time = 47 ms, elapsed time = 62 ms.
    Table 'Worktable'. Scan count 556, logical reads 1569, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    Table 'ORDERSHISTORY'. Scan count 116, logical reads 339, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    Table 'ORDERS'. Scan count 116, logical reads 285, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    SQL Server Execution Times:
    CPU time = 32 ms, elapsed time = 63 ms.
    (115 row(s) affected)
    you see logical reads and scan count for worktable(temporary) is quite high.
    So anyone can give a solution for reduce the scan count and logical reads.
    NOTE: name,units, subunits,updateon columns have primarykey

    SQL ServerAm i reading it properly? :(
    This is Oracle Forum And not the SQL Server.
    Regards.
    Satyaki De.

  • Dynamic fetch for dynamic sql query

    Hi All,
    I want to dynamically create a query and fetch it into a dynamic cursor record...The structure of the record should be the same as that of the query...
    Can you suggest any method to do that.
    eg
    OPEN <dynamic cur> FOR <dynamic stmt>;
    LOOP
    FETCH <dynamic cur> INTO <dynamic rec>;
    END LOOP;
    CLOSE <dynamic cur>;
    How do I declare the dynamic rec here?I would not know the columns in the query beforehand either.
    Thanks,
    Merz
    Edited by: merz on Sep 12, 2011 5:02 PM
    Edited by: merz on Sep 12, 2011 5:03 PM

    merz wrote:
    Hi All,
    I want to dynamically create a query and fetch it into a dynamic cursor record...The structure of the record should be the same as that of the query...
    Can you suggest any method to do that.And how are you expecting to write an application around a dynamic query and dynamic structure that you don't know?
    Saying that, as others have said, there is the DBMS_SQL package which can be used for certain valid reasons such as the following example....
    As sys user:
    CREATE OR REPLACE DIRECTORY TEST_DIR AS '\tmp\myfiles'
    GRANT READ, WRITE ON DIRECTORY TEST_DIR TO myuser
    /As myuser:
    CREATE OR REPLACE PROCEDURE run_query(p_sql IN VARCHAR2
                                         ,p_dir IN VARCHAR2
                                         ,p_header_file IN VARCHAR2
                                         ,p_data_file IN VARCHAR2 := NULL) IS
      v_finaltxt  VARCHAR2(4000);
      v_v_val     VARCHAR2(4000);
      v_n_val     NUMBER;
      v_d_val     DATE;
      v_ret       NUMBER;
      c           NUMBER;
      d           NUMBER;
      col_cnt     INTEGER;
      f           BOOLEAN;
      rec_tab     DBMS_SQL.DESC_TAB;
      col_num     NUMBER;
      v_fh        UTL_FILE.FILE_TYPE;
      v_samefile  BOOLEAN := (NVL(p_data_file,p_header_file) = p_header_file);
    BEGIN
      c := DBMS_SQL.OPEN_CURSOR;
      DBMS_SQL.PARSE(c, p_sql, DBMS_SQL.NATIVE);
      d := DBMS_SQL.EXECUTE(c);
      DBMS_SQL.DESCRIBE_COLUMNS(c, col_cnt, rec_tab);
      FOR j in 1..col_cnt
      LOOP
        CASE rec_tab(j).col_type
          WHEN 1 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000);
          WHEN 2 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_n_val);
          WHEN 12 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_d_val);
        ELSE
          DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000);
        END CASE;
      END LOOP;
      -- This part outputs the HEADER
      v_fh := UTL_FILE.FOPEN(upper(p_dir),p_header_file,'w',32767);
      FOR j in 1..col_cnt
      LOOP
        v_finaltxt := ltrim(v_finaltxt||','||lower(rec_tab(j).col_name),',');
      END LOOP;
      --  DBMS_OUTPUT.PUT_LINE(v_finaltxt);
      UTL_FILE.PUT_LINE(v_fh, v_finaltxt);
      IF NOT v_samefile THEN
        UTL_FILE.FCLOSE(v_fh);
      END IF;
      -- This part outputs the DATA
      IF NOT v_samefile THEN
        v_fh := UTL_FILE.FOPEN(upper(p_dir),p_data_file,'w',32767);
      END IF;
      LOOP
        v_ret := DBMS_SQL.FETCH_ROWS(c);
        EXIT WHEN v_ret = 0;
        v_finaltxt := NULL;
        FOR j in 1..col_cnt
        LOOP
          CASE rec_tab(j).col_type
            WHEN 1 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_v_val);
                        v_finaltxt := ltrim(v_finaltxt||',"'||v_v_val||'"',',');
            WHEN 2 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_n_val);
                        v_finaltxt := ltrim(v_finaltxt||','||v_n_val,',');
            WHEN 12 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_d_val);
                        v_finaltxt := ltrim(v_finaltxt||','||to_char(v_d_val,'DD/MM/YYYY HH24:MI:SS'),',');
          ELSE
            v_finaltxt := ltrim(v_finaltxt||',"'||v_v_val||'"',',');
          END CASE;
        END LOOP;
      --  DBMS_OUTPUT.PUT_LINE(v_finaltxt);
        UTL_FILE.PUT_LINE(v_fh, v_finaltxt);
      END LOOP;
      UTL_FILE.FCLOSE(v_fh);
      DBMS_SQL.CLOSE_CURSOR(c);
    END;This allows for the header row and the data to be written to seperate files if required.
    e.g.
    SQL> exec run_query('select * from emp','TEST_DIR','output.txt');
    PL/SQL procedure successfully completed.Output.txt file contains:
    empno,ename,job,mgr,hiredate,sal,comm,deptno
    7369,"SMITH","CLERK",7902,17/12/1980 00:00:00,800,,20
    7499,"ALLEN","SALESMAN",7698,20/02/1981 00:00:00,1600,300,30
    7521,"WARD","SALESMAN",7698,22/02/1981 00:00:00,1250,500,30
    7566,"JONES","MANAGER",7839,02/04/1981 00:00:00,2975,,20
    7654,"MARTIN","SALESMAN",7698,28/09/1981 00:00:00,1250,1400,30
    7698,"BLAKE","MANAGER",7839,01/05/1981 00:00:00,2850,,30
    7782,"CLARK","MANAGER",7839,09/06/1981 00:00:00,2450,,10
    7788,"SCOTT","ANALYST",7566,19/04/1987 00:00:00,3000,,20
    7839,"KING","PRESIDENT",,17/11/1981 00:00:00,5000,,10
    7844,"TURNER","SALESMAN",7698,08/09/1981 00:00:00,1500,0,30
    7876,"ADAMS","CLERK",7788,23/05/1987 00:00:00,1100,,20
    7900,"JAMES","CLERK",7698,03/12/1981 00:00:00,950,,30
    7902,"FORD","ANALYST",7566,03/12/1981 00:00:00,3000,,20
    7934,"MILLER","CLERK",7782,23/01/1982 00:00:00,1300,,10The procedure allows for the header and data to go to seperate files if required. Just specifying the "header" filename will put the header and data in the one file.
    Adapt to output different datatypes and styles are required.

  • Data fetching problem in select query

    HI Expert,
                   Pls check the below code as data is not fetching due to BGMKOBJ-OBJNR(22)  not equal to EQKT-EQUNR(18).
    select  EQUNR
            EQKTX
           from EQKT into corresponding fields of table IT_EQKT
           from EQKT into table IT_EQKT
             into table IT_EQKT
             from EQKT
           for all entries in IT_BGMKOBJ
           for all entries in IT_ITOB
             where EQUNR = IT_ITOB-OBJNR
            and spras eq 'EN'.
    Pls help me with code.
    Thanks & Regards,
    Singha

    Create a new internal table with the field EQUNR with BGMKOBJ-OBJNR values and do for all entries on this table.There is no alternative on this.
    e.g,
    types: begin of t_equnr,
    equnr type EQKT-EQUNR,
    end of t_equnr.
    data : it_equnr type table of t_equnr,
             wa_equnr type t_equnr.
    loop at IT_BGMKOBJ into wa_BGMKOBJ .
    wa_equnr-equnr  =  wa_BGMKOBJ-OBJNR.
    append wa_equnr to it_equnr.
    endloop.
    select EQUNR
    EQKTX
    from EQKT into corresponding fields of table IT_EQKT
    from EQKT into table IT_EQKT
    into table IT_EQKT
    from EQKT
    for all entries in IT_BGMKOBJ
    for all entries in it_equnr
    where EQUNR = it_equnr-EQUNR

  • Concurrent access issue with JPA  for a Select query?

    Hi All,
    I have been trying to understand why this code,
    public <T> List<T> findManyNativeSql(String queryString, Class<T> resultClass)
                Query aQuery = getEntityManager().createNativeQuery(queryString,resultClass); // Throwing the following exceptionis causing this exception.
    <openjpa-1.1.0-r422266:657916 fatal general error> org.apache.openjpa.persistence.PersistenceException: Multiple concurrent th
    reads attempted to access a single broker. By default brokers are not thread safe; if you require and/or intend a broker to be
    accessed by more than one thread, set the openjpa.Multithreaded property to true to override the default behavior.
            at org.apache.openjpa.kernel.BrokerImpl.endOperation(BrokerImpl.java:1789)
            at org.apache.openjpa.kernel.BrokerImpl.isActive(BrokerImpl.java:1737)
            at org.apache.openjpa.kernel.DelegatingBroker.isActive(DelegatingBroker.java:428)
            at org.apache.openjpa.persistence.EntityManagerImpl.isActive(EntityManagerImpl.java:606)
            at org.apache.openjpa.persistence.PersistenceExceptions$2.translate(PersistenceExceptions.java:66)
            at org.apache.openjpa.kernel.DelegatingBroker.translate(DelegatingBroker.java:102)
            at org.apache.openjpa.kernel.DelegatingBroker.newQuery(DelegatingBroker.java:1227)I have tried looking at the query which gets printed in the logs when the exception is thrown
    [[ACTIVE] ExecuteThread: '32' for queue: 'weblogic.kernel.Default (self-tuning)'] ERROR jpa
    - ID: 133  queryString= select * from Details where cust_name='SETH'Any suggestions on the following would be very helpful
    Also, AppServer: WL10.3 is being used.
    VR

    I'm not sure what a Broker is in OpenJPA so you may want to post in an OpenJPA forum. I would suspect though that a broker is underneath the EntityManager, and it might suggest that this EntityManager instance is being shared among threads. Verify that the EntityManager returned is not being used in multiple threads; if it is used in multiple threads concurrently, this needs to be changed to obtian a new one and release it when done as they are not thread safe. You might also try using EclipseLink as the JPA provider to see if you get a different error message that might point out the problem.
    Best Regards,
    Chris

  • Displaying different no. of  records for a select Query for the same dbuser

    Hi,
    While querying from my machine after connecting to custom schema it is displaying 3000 rows.
    When the same query was executed from different machine it is displaying just 31 records.
    1. Connect as xxyy user and executed the below command
    select count(*) from xxyy. XXYY_APAR_SUPP_CUST_MAST_V.
    output is showing some 3000 records
    2. Connect as xxto user and executed the below commands for a specific user and specific machine
    select count(*) from xxyy. XXYY_APAR_SUPP_CUST_MAST_V.
    output is showing just 31 records
    Please help...!!

    Hi,
    I hope you would have resolved the problem, if not, next step could be to check the TNS on both machines because now i suspect that your TNS on other machine points to some idfferent database.
    What do you mean by following
    2. Connect as xxto user and executed the below commands for a specific user and specific machineSo you connect with different users? If yes, then from both machines, if you connect with the same user, is result same?
    When the same query was executed from different machine it is displaying just 31 records.Please paste the TNs entry for the database from both of your client machines from where you run the query.
    Salman

  • Fetching values using select query

    Hi i want to get the details of belnr and bukrs using the where codn fields bukrs,anlkl,brdatu,ktansw,budat,WBS element.
    1)The value of bukrs is entered in the selection screen
    2)the value of asset class is optional and present in the anka
    3)brdatu is already fetched and placed in a table.field brdatu is present in table j_ittaxdep
    4)ktansw is entered in the selection screen and present in table j_ittaxdep
    5)budat is fetched and placed in a table.
    6)wbs element already fetched and  is present in prps table.
    Can anyone please tell how to code.actually the values of bukrs,anlkl ktansw, are present in structure anlav,brdatu is present in structure rbada and budat is present in structure anek.By using this structure is there way to get the belnr and bukrs value.

    Hi,
    Hema,
    things are not so flate as you are thinking.
    first u understand for which application u are using these Queries.Ask ur functional guy to confirm the usage tables.Then use thsee tables to get the data into u r internal tables and for these internal tables value u have to fetch belnr bukrs fronm bseg or FI table .
    so first confirm the tables the plan how to get data.
    if u need my help the write me in brief?
    Regards
    Ricky

  • Simple SELECT query to generate  INSERT for a table

    Hi,
    I am looking for a SELECT query which generates INSERT statements for a table.Please guide.
    Thanks
    PG.

    Like this?
    SQL> SELECT * from kons;
         COL1      COL2
            1         1
            2         2
    SQL> SELECT 'INSERT INTO kons VALUES('||col1||','||col2||');' statement FROM kons;
    STATEMENT
    INSERT INTO kons VALUES(1,1);
    INSERT INTO kons VALUES(2,2);
    SQL> If you have character and / or date columns you will have to change my example,
    but you might get the idea from it.
    You can spool the result to a file.
    Regards,
    Guido
    Edit: ';' added to end of statement...

Maybe you are looking for

  • Convert pdf to word text searchable

    I need to convert a pdf file into a word document that is text searchable.  how do I do that and how long does it take?

  • JOptionPane blocking problem

    I have the following code in my 1.4.1 applet: int response = JOptionPane.showConfirmDialog(NewJabber.mainFrame,"Receive message from unregistered user: "+jid.getName()+" over the "+JID.SHOWNAMES[jid.getHostType()]+" transport. Accept and add user to

  • WiFi Connection Problems WPA-Enterprise

    I am having an issue connecting to our WiFi at work using my Lumia 900.  I can connect to the network, but then it disconnects after about 15 seconds.  My Home and other Wifi networks do not exhibit this problem. Any Ideas?

  • HT4993 how can i check if my iphone 4 is still under warranty ?

    how can i check if my iphone 4 is still under warranty ?

  • Org Hierarchy Query

    Hii Gurus, Our client have the follwing requirement. They need to see the parent node of the 0ORGUNIT in a separate column. We do not know what it will take to accomplish this (enhancing 0ORGUNIT doesn't seem to be an option at this point), Can u giv