Help in wriring an sql

Hi gurus,
just wondering whether this sql works fine.
Insert into contact_history@demo (narrative) select substrb(narrative,1,4000) from CONTACT_HISTORY
where I in (select i from missing_ivalue)
where demo is a dblink for source and the target database and narrative is a column name which is varchar2(4000)
contact_history@demo is a target table.
i actually need to insert complete record which has missing i value with the substrb(narrative,1,4000)
any suggestion is highly apprciated
Thanks
Edited by: 790072 on 8/02/2011 15:54
Edited by: 790072 on 16/08/2011 19:56

Hi,
790072 wrote:
Insert into toccontact_history@demo (narrative) select substrb(narrative,1,4000) from TOCCONTACT_HISTORY
where I in (select i from missing_ivalue)
where demo is a dblink for source and the target database and narrative is a column name which is varchar2(4000)
toccontact_history@demo is a target table.As per your post,the above insert statement will absolutely work fine,unless if you have insert permission for toccontact_history table,to your present schema,where you are executing the insert statement.Also you need to have db link created to access the tables,created in other schema.
Check the db_link created by using the query
select * from user_db_linksIts always suggested to post a sample data to understand your requirement cleary and we can try to provide you a solution to your problem.
Hope this helps.
Regards,
Achyut

Similar Messages

  • Need help in rewriting a sql query

    Can any one please tell me if there is any utility that can help me correcting the sql I have I need to tune the query as its taking lot of time. I want to use some tool that will help me re-formating the query.
    Any help in this regard will be highly appreciated.

    If you think that Oracle SQL Tuning Tools like SQL Tuning Advisor and SQL Access Advisor are not helping.
    You might look into thrid party tools like Quest- SQL Navigator and TOAD.
    But I don't advise this based on the following:
    Re: Oracle Third Party Tools and Oracle Database
    Oracle have enough tools of its own to satisfy the various needs.
    Adith

  • Help needed in framing SQL query.

    Hi,
    I have table having following schema:
    PRODUCT_ID         INT
    DATE                   DATETIME
    ITEMS_SOLD         INT
    This table contains data for a week (sunday to saturday). I want to write an SQL query to get (filter out) PRODUCT_ID of products which has same no. of ITEMS_SOLD for all 7 days. Also for given PRODUCT_ID I need to find the longest period of successive days where the no. of ITEMS_SOLD is same for all days in this period. Eg.(PRODUCT_ID 23 was sold as following along the week in no. of units sold: 4,6,6,6,6,7,4 .So the longest period is *4 days* from Monday to Thursday.) The first condition is special case of second condition where no. of days is 7.
    Any help to get the SQL query will be appreciated.
    Thanks,
    Akshay.

    PRODUCT_ID      DATE           ITEMS_SOLD
    1          10/10/2011     3
    1          11/10/2011     3
    1          12/10/2011     3
    1          13/10/2011     3
    1           16/10/2011     5
    2          10/10/2011     4
    2           11/10/2011     4
    2          12/10/2011     4
    2          13/10/2011     4
    2           14/10/2011     4
    2          15/10/2011     4
    2          16/10/2011     4
    Output:
    PRODUCT_ID ITEMS_SOLD NO_OF_DAYS
    1          3                4     
    2          4               7
    Explanation of results:
    The table to be queried contains data for 1 week: from 10/10/2011(Sunday) to 16/10/2011(Saturday). Now, product with PRODUCT_ID '1' was sold on dates 10,11,12,13,16. Out of these 5 days 3 units were sold on 4 successive days (from 10-13). So output should be like :
    PRODUCT_ID ITEMS_SOLD NO_OF_DAYS
    1          3               4     
    as longest period of successive days is 4 where same no. of units were sold i.e 3 units (other period is of 1 day on 16th ).
    For PRODUCT_ID 2 we have only one period of 7 days where 4 units were sold each day. So we output :
    PRODUCT_ID ITEMS_SOLD NO_OF_DAYS
    2           4               7
    Other case where same PRODUCT_ID have different units sold on each day should be ignored.
    I hope that clarifies the problem more. Let me know in case I have missed out anything which should have been mentioned.
    -Akshay.

  • Can anybody help....SQL to display row as column and column as rows

    Can anybody help in writing a SQL to display row as column and column as rows?
    Thanks

    check this link:
    Re: Creating Views - from rows to a new column?

  • Plz help me to understand sql trace

    Hi,
    i need your help to understand the sql trace and where exactly i should concentrate,
    i am also going through docs,
    your help is appreciated,
    select count(rep)
    from
    (select /*+ leading(v1 v2) use_nl_with_index(v2) */ count(v1.val) rep from
      (select rn, val from (select rownum rn, val from (select /*+ no_merge
      no_parallel(t) no_parallel_index(t) dbms_stats cursor_sharing_exact
      use_weak_name_resl dynamic_sampling(0) no_monitoring */"FFPR_FFAMS_ID" val
      from sys.ora_temp_1_ds_108308 t where "FFPR_FFAMS_ID" is not null group by
      "FFPR_FFAMS_ID" having count("FFPR_FFAMS_ID") = 1)) where ora_hash(rn) <=
      273538662) v1, (select /*+ index(t2) */ "FFPR_FFAMS_ID" val from
      "DM_FM_REPORTING_OWNER"."FM_FP_PLAN_RESULTS" t2) v2 where v2.val = v1.val
      group by v1.val having count(v1.val) <= 2)
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.00       0.00          0          0          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch        1      0.00       0.29          0       8131          0           1
    total        3      0.00       0.30          0       8131          0           1
    Misses in library cache during parse: 1
    Optimizer mode: ALL_ROWS
    Parsing user id: 213     (recursive depth: 1)
    select /*+ no_parallel_index(t,"FFPR_FFAMS_FK_I") dbms_stats
      cursor_sharing_exact use_weak_name_resl dynamic_sampling(0) no_monitoring
      no_expand index(t,"FFPR_FFAMS_FK_I") */ count(*) as nrw,count(distinct
      sys_op_lbid(692895,'L',t.rowid)) as nlb,count(distinct "FFPR_FFAMS_ID") as
      ndk,sys_op_countchg(substrb(t.rowid,1,15),1) as clf
    from
    "DM_FM_REPORTING_OWNER"."FM_FP_PLAN_RESULTS" t where ("FFPR_FFAMS_ID" is not
      null) and (TBL$OR$IDX$PART$NUM("DM_FM_REPORTING_OWNER"."FM_FP_PLAN_RESULTS",
      0,4,0,"ROWID") = :objn)
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.00       0.00          0          0          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch        1      0.00       2.56          0        425          0           1
    total        3      0.00       2.56          0        425          0           1
    Misses in library cache during parse: 1
    Misses in library cache during execute: 1
    Optimizer mode: ALL_ROWS
    Parsing user id: 213     (recursive depth: 1)
    select /*+ no_parallel_index(t,"FFPR_FFAMS_FK_I") dbms_stats
      cursor_sharing_exact use_weak_name_resl dynamic_sampling(0) no_monitoring
      no_expand index(t,"FFPR_FFAMS_FK_I") */ count(*) as nrw,count(distinct
      sys_op_lbid(692895,'L',t.rowid)) as nlb,count(distinct "FFPR_FFAMS_ID") as
      ndk,sys_op_countchg(substrb(t.rowid,1,15),1) as clf
    from
    "DM_FM_REPORTING_OWNER"."FM_FP_PLAN_RESULTS" t where "FFPR_FFAMS_ID" is not
      null
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.00       0.00          0          0          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch        1      0.00       2.56          0        427          0           1
    total        3      0.00       2.56          0        427          0           1
    Misses in library cache during parse: 1
    Optimizer mode: ALL_ROWS
    Parsing user id: 213     (recursive depth: 1)
    select /*+ no_parallel_index(t,"FFPR_PK_I") dbms_stats cursor_sharing_exact
      use_weak_name_resl dynamic_sampling(0) no_monitoring no_expand index(t,
      "FFPR_PK_I") */ count(*) as nrw,count(distinct sys_op_lbid(692907,'L',
      t.rowid)) as nlb,null as ndk,sys_op_countchg(substrb(t.rowid,1,15),1) as
      clf
    from
    "DM_FM_REPORTING_OWNER"."FM_FP_PLAN_RESULTS" t where ("FFPR_ID" is not null
      or "FFPR_PLAN_NAME" is not null) and
      (TBL$OR$IDX$PART$NUM("DM_FM_REPORTING_OWNER"."FM_FP_PLAN_RESULTS",0,4,0,
      "ROWID") = :objn)
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.00       0.00          0          0          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch        1      0.00       1.51        847        857          0           1
    total        3      0.00       1.52        847        857          0           1
    Misses in library cache during parse: 1
    Misses in library cache during execute: 1
    Optimizer mode: ALL_ROWS
    Parsing user id: 213     (recursive depth: 1)
    select /*+ no_parallel_index(t,"FFPR_PK_I") dbms_stats cursor_sharing_exact
      use_weak_name_resl dynamic_sampling(0) no_monitoring no_expand index(t,
      "FFPR_PK_I") */ count(*) as nrw,count(distinct sys_op_lbid(692907,'L',
      t.rowid)) as nlb,null as ndk,sys_op_countchg(substrb(t.rowid,1,15),1) as
      clf
    from
    "DM_FM_REPORTING_OWNER"."FM_FP_PLAN_RESULTS" t where "FFPR_ID" is not null
      or "FFPR_PLAN_NAME" is not null
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.00       0.00          0          0          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch        1      0.00       1.26          0        850          0           1
    total        3      0.00       1.27          0        850          0           1
    Misses in library cache during parse: 1
    Optimizer mode: ALL_ROWS
    Parsing user id: 213     (recursive depth: 1)
    OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS
    call     count       cpu    elapsed       disk      query    current        rows
    Parse       13      0.00       0.03          0          0          0           0
    Execute     13      0.00       1.32         43        242         29           9
    Fetch        4      0.00       0.00          0          0          0           4
    total       30      0.00       1.36         43        242         29          13
    Misses in library cache during parse: 4
    OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
    call     count       cpu    elapsed       disk      query    current        rows
    Parse     2945      0.00       1.04          0          0          0           0
    Execute   3741      0.00       5.49       3137      12995       7050      203362
    Fetch     3088      0.00      38.42       3344      39486         27        9811
    total     9774      0.00      44.96       6481      52481       7077      213173
    Misses in library cache during parse: 284
    Misses in library cache during execute: 160
      144  user  SQL statements in session.
    2814  internal SQL statements in session.
    2958  SQL statements in session.Regards
    nic

    Hi,
    i did the homework, but some how i dont seem to understand what oracle is doing behind
    esp with hints like
    select /*+ leading(v1 v2) use_nl_with_index(v2) */ count(v1.val)
    /*+ no_parallel(t) no_parallel_index(t) dbms_stats
      cursor_sharing_exact use_weak_name_resl dynamic_sampling(0) no_monitoring
    substrb(dump(min("XXX"),16,0,32),1,120),
      substrb(dump(max("XXX"),16,0,32),1,120),i havent used this things any where in my program, but still they are used,
    so i got confused, can you throw some light on this issue,
    regards
    nic

  • Please sugges the link helpful for writing efficient sql queries

    Please suggest any good resource that is weblink which can help me in optimizing sql queries. especially while writing select statements improve the execution time of the sql query.
    Thanx in advance
    prasanth

    in general I found books from O'Reilly very helpful (not only for Oracle, but for Unix too).
    Moreover there is pretty good Oracle Documentation available.
    After all, it's not only about writing good queries, but also about setting up data-models, indexes, primary keys, etc.
    Look for a real slow computer, take a lot of data, then try writing your speedy queries. This is the school of hard knocks, on the long run it's the best training.

  • Help Needing in optimizing SQL

    I need help optimizing the following SQL.
    Following are the schema elements -
    cto_xref_job_comp - Contains the Job Data
    cto_mast_component - Contains component
    cto_mast_product - Contains product info
    cto_mast_model - Contains model info
    cto_xref_mod_prod - Contains model and product assoiciation
    cto_mast_model_scan - Contain the Scan order for each model family (the mod_id(should be renamed) refers to mod_fam_id on the cto_mast_model table).
    Here is what I am trying to achieve
    I want all the ATP components whose Travel card order (on cto_mast_model_scan > 0 ) and in addition I need the Phantoms that do not have any ATP components under them if they do not appear on ATP component list.
    SELECT f.travelcard_order, b.job_number,
    b.product_code,
    b.line_number,
                   b.component_type,
    b.component_item_number,
    b.parent_phantom,
    b.item_type,
    a.comp_id,
    a.comp_type_id,
    a.comp_desc_short,
    b.batch_id,
    b.quantity_per_unit,
    a.comp_notes,
                   b.COMPONENT_ITEM_DESCRPTION
    FROM      cto_xref_job_comp b,
                        cto_mast_component a,
                        cto_mast_product c,
    cto_xref_mod_prod d,
    cto_mast_model e,
                   cto_mast_model_scan f
    WHERE ( b.job_number = 'CTO2499814001' ) AND
    ( b.batch_id = 21 ) AND
    (b.item_type = 'ATP') AND
                   (b.parent_phantom = a.comp_desc_short ) and          
                   (b.product_code = c.prod_desc_short ) and
    (c.prod_id = d.prod_id ) and
    (d.mod_id = e.mod_id) and
    (e.mod_fam_id = f.mod_id) and
    (a.comp_type_id=f.comp_type_id) and
                                       (f.travelcard_order > 0)
    union
    SELECT distinct f.travelcard_order, b.job_number,
    b.product_code,
    b.line_number,
                   b.component_type,
    b.parent_phantom,
    b.item_type,
    a.comp_id,
    a.comp_type_id,
    a.comp_desc_short,
    b.batch_id,
    1,
    a.comp_notes,
    FROM      cto_xref_job_comp b,
                        cto_mast_component a,
                        cto_mast_product c,
    cto_xref_mod_prod d,
    cto_mast_model e,
                   cto_mast_model_scan f
    WHERE ( b.job_number = 'CTO2499814001' ) AND
    ( b.batch_id = 21 ) AND (b.item_type is null) and
                   (substr(b.parent_phantom,1,1)='C') and
                   (b.parent_phantom = a.comp_desc_short )
    and (b.parent_phantom not in (select parent_phantom from cto_xref_job_comp where job_number=b.job_number and batch_id=b.batch_id and item_type='ATP')) and           
                   (b.product_code = c.prod_desc_short ) and
    (c.prod_id = d.prod_id ) and
    (d.mod_id = e.mod_id) and
    (e.mod_fam_id = f.mod_id) and
    (a.comp_type_id=f.comp_type_id) and (f.travelcard_order > 0)

    Here is a small example.
    DECLARE
         my_table VARCHAR2(10) := 'DUAL';
         my_value INTEGER;
    BEGIN
         EXECUTE IMMEDIATE 'SELECT 1 FROM ' || my_table INTO my_value;
         DBMS_OUTPUT.PUT_LINE(my_value);
    END;
    you can use your cursor value in the place of my_table.
    Thanks,
    Karthick.
    http://www.karthickarp.blogspot.com/

  • Help me write a SQL query; urgent

    Hi, can somebody please help me write a SQL query.
    I have 3 tables each with the same column names (Col1, Col2, Col3). Col1 is PK with Unique Constraint.
    I wanted to add values of Col2 and Col3 (from all 3 tables) and put it in a separate table (i.e aggregated) of all values found in Col1.
    Does anybody help me please ?
    thanks alot.

    Please don't mark your question as urgent. You've been around here long enough that you should know that it will not get your question anwered any faster, and may just irritate people into not answering at all.
    I'm not sure exactly what you want.
    Are you saying you want t1.c2 + t1.c3 + t2.c2 + t2.c3+ t3.c2 + t3.c3 for the rows that have the same c1 in all three tables?
    If so, it would be like this, I think: insert into t4
    select t1.c1
    , t1.c2 + t1.c3 + t2.c2 + t2.c3+ t3.c2 + t3.c3
    from t1, t2, t3
    where t2.c1 = t1.c1
    and t3.c1 = t1.c1If that's not what you want, please clarify.
    And next time maybe you should post your SQL question in a SQL forum.

  • Help to write a sql

    Hi ,
    I need help in putting a sql together for the ones listed below:
    I have 2 queries and I need to put it in a format that I can use to run my sql .
    select count(TRANSACTION_SID) from
    (select EFFECTIVE_DATE, CREATION_DATE, ORG_SID, TRANSACTION_SID, DP_COUNT, DV_COUNT, TOTAL_SIZE,
    rank() over ( partition by transaction_sid order by effective_date desc) rank
    from Nightly_Inventory where effective_date<= to_date ('2008-12-03', 'yyyy/mm/dd') and org_sid='243')
    where rank=1 and transaction_sid NOT IN (select transaction_sid from NIGHTLY_DELETED where effective_date<= to_date ('2008-12-03', 'yyyy/mm/dd'))
    minus this:
    select count(TRANSACTION_SID) from
    NIGHTLY_DELETED where effective_date<= to_date ('2008-12-03', 'yyyy/mm/dd')
    and transaction_sid IN (select TRANSACTION_SID) from
    (select EFFECTIVE_DATE, CREATION_DATE, ORG_SID, TRANSACTION_SID, DP_COUNT, DV_COUNT, TOTAL_SIZE,
    rank() over ( partition by transaction_sid order by effective_date desc) rank
    from Nightly_Inventory where effective_date<= to_date ('2008-12-03', 'yyyy/mm/dd') and org_sid='243')
    where rank=1 and transaction_sid NOT IN (select transaction_sid from NIGHTLY_DELETED where effective_date<= to_date ('2008-12-03', 'yyyy/mm/dd')) )
    I would really really appreciate your help if I can get this working .
    thanks .

    Hi,
    Do you want a single query that shows you the difference?
    If so, you can do this:
    SELECT    (
              select count(TRANSACTION_SID) from
              (select EFFECTIVE_DATE, CREATION_ ... -- your first query
              select count(TRANSACTION_SID) from
              NIGHTLY_DELETED where ... -- your second query
    FROM      dual;There might be a much more efficient way, depending on what the two queries do.

  • Please help me convert the SQL to HQL

    select
    count(*) as col_0_0_
    from
    edu.hr_people_all hrpeopleal0_
    left outer join
    edu.conditions_journal conditions1_
    on hrpeopleal0_.people_id=conditions1_.people_id
    and conditions1_.personal_number like '%'
    where
    hrpeopleal0_.status=11

    Hibernate version: 3
    HQL as I 've done
    FROM PMSProjectsORO pjt LEFT JOIN PMSChecklistORO pcl
    ON pjt.projectId = pcl.projectId order by
    pjt.projectCode ascHQL is probably incorrect.
    Try this:
    FROM
    PMSProjectsORO pjt
    LEFT JOIN
    PMSChecklistORO pcl
    ORDER BY
    pjt.projectCode asc>
    Original SQL for MSQL5:
    FROM PMSProject LEFT JOIN PMSCheckList ON
    PMSProject.PROJECT_ID = PMSCheckList.PROJECT_ID order
    by PMSProject.PROJECT_CODE desc
    Name and version of the database you are using:
    MYSQL 5
    Please help me convert the sql to hql since what i
    've done is throwing error like unexpected token...
    Please reply ASAPLet me know if that's better.
    %

  • Help needed in writing SQL CASE or DECODE statement

    Hi experts,
    I need to write a SQL to select order_num, cntry_cde, prod_id and Qty by joining order_num on PROD_ORDER and PROD_ORDER_TXT.
    Here is my sample data
    PRODORDER_               
    order_num     cntry_cde     Prod_id     Qty
    100     US     A1     5
    101     US     A2     10
    102     AU     A3     4
    103     AU     A4     9
    104     IN     A5     3
    PRODORDER_TXT_               
    order_num     cntry_cde     Prod_id     
    100     US     A1     
    101     US     A2     
    102     NZ     A3     
    103     AU     A4     
    104          A5     
    Here is the requirement,
    1) If the cntry_cde in PROD_ORDER is same as cntry_cde in PROD_ORDER_TXT then select PROD_ORDER.cntry_cde (orders 100, 101, 103)
    2) If they are different, pick the country code from PROD_ORDER_TXT (order 102, AU <> NZ)
    3) If they are different and PROD_ORDER_TXT.cntry_cde is NULL, I cannot use it as cntry_cde in my report (order 104). It happenend just because of the bad data at source.
    I cannot avoid it. Then simply use the cntry_cde from PROD_ORDER
    Output expected
    100     US     A1     5
    101     US     A2     10
    102     NZ     A3     4 -- AU changed to NZ
    103     AU     A4     9
    104     IN     A5     3 -- IN retained as PROD_ORDER_TXT.cntry_cde is null
    sample table creation and insert statements are below
    create table prod_order
    (order_num number,
    cntry_cde CHAR(2),
    prod_id VARCHAR2(6),
    qty number)
    create table prod_order_txt
    (order_num number,
    cntry_cde CHAR(2),
    prod_id VARCHAR2(6))
    insert into prod_order values (100, 'US', 'A1',5);
    insert into prod_order values (101, 'US', 'A2',1);
    insert into prod_order values (102, 'AU', 'A3',4);
    insert into prod_order values (103, 'AU', 'A4',9);
    insert into prod_order values (104, 'IN', 'A5',3);
    insert into prod_order_txt values (100,'US','A1');
    insert into prod_order_txt values (101,'US','A2');
    insert into prod_order_txt values (102,'NZ','A3');
    insert into prod_order_txt values (103,'AU','A4');
    insert into prod_order_txt values (104,NULL,'A5');
    commit;
    Thanks for your help in advance
    Edited by: sarvan on Mar 28, 2012 1:39 PM

    Hello
    Thank you for posting all of the ddl and test data along with your expected output - very helpful!. One small point would be to remember to type {noformat}{noformat} before and after any section of code or data in your post so that formatting is retained.  Anyway, this should be a simple join and a combination of CASE and NVL...Select
    po.order_num,
    CASE
    WHEN po.cntry_cde != NVL(pot.cntry_cde,po.cntry_cde)
    THEN
    pot.cntry_cde
    ELSE
    po.cntry_cde
    END cntry_code,
    po.prod_id,
    po.qty
    FROM
    prod_order po
    JOIN
    prod_order_txt pot
    ON
    ( po.order_num = pot.order_num
    ORDER_NUM CN PROD_I QTY
    100 US A1 5
    101 US A2 1
    102 NZ A3 4
    103 AU A4 9
    104 IN A5 3
    5 rows selected.
    HTH
    David
    Edited by: Bravid on Mar 28, 2012 8:32 AM
    corrected !=                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Can someone help me correct this sql statement in a jsp page?

    ive been getting the java.sql.SQLException: Incorrect syntax error for one of my sql nested statements. i cant seem to find similar egs online, so reckon if anyone here could help, really appreciate it.
    as im putting the nested sql in jsp page, it has to be with lots of " " n crap. very confusing if there are nested.
    heres the sql statement without those "" that i want to use:
    select top 5 * from(
    select top+"'"+offset+"'"+" * from prod where cat=" +"'" cat "'"+"
    )order by prodID desc
    when i put this in my jsp pg, i had to add "" to become:
    String sql = "select top 5 * from("+"select top"+"'"+offset+"'"+" * from prod where cat=" +"'" +cat+ "'"+")order by prodID desc";cat=" +"'" cat "'"+")order by prodID desc";
    all those "" are confusing me to no end, so i cant figure out what should be the correct syntax. the error says the syntax error is near the offset.

    If offset is, say, 10, and cat is, say, "new", then it looks like you're going to produce the SQL:
    select top 5 * from(
      select top '10' * from prod where cat='new'
    )order by prodID descThat looks exactly like incorrect syntax to me... top almost certainly can't handle a string literal as its operand... you almost certainly would want "top 10" instead of "top '10'"...
    If you use PreparedStatement, you don't have to remember what you quote and what you don't and you can have your SQL in a single static final string to boot...

  • Need help in Report From SQL Query

    Hi All,
    I am facing a problem with a report. I need your help.
    I am creating a Report From SQL Query (Portal) with some arguments passed at runtime. I am able to view the output, if the query returns few rows ( arount 1000 rows). But for some inputs it needs to generate >15000 records, at this point the page is getting time out (i think!) and showing error page. I am able to execute query from the SQL Plus console ot using TOAD editor. Here the query is not taking more that 2 mins time to show the result.
    If i am executing from Portal i observed that, once i give the appropriate input and hit submit button a new oracle process is getting created for the query on UNIX (I am usign "TOP" command to check processes). The browser page will be shown error page after 5 minutes (i am assuming session time out!) , but on the backend the process will be executed for more than 30 mins.
    I tried also increase the page time out in httpd.conf, but no use.
    The data returned as a result of the query is sized more than 10 MB. Is caching this much data is possible by the browser page? is the returned data is creating any problem here.
    Please help me to find appropriate reasone for the failure?

    user602513 wrote:
    Hi All,
    I am facing a problem with a report. I need your help.
    I am creating a Report From SQL Query (Portal) with some arguments passed at runtime. I am able to view the output, if the query returns few rows ( arount 1000 rows). But for some inputs it needs to generate >15000 records, at this point the page is getting time out (i think!) and showing error page. I am able to execute query from the SQL Plus console ot using TOAD editor. Here the query is not taking more that 2 mins time to show the result.
    If i am executing from Portal i observed that, once i give the appropriate input and hit submit button a new oracle process is getting created for the query on UNIX (I am usign "TOP" command to check processes). The browser page will be shown error page after 5 minutes (i am assuming session time out!) , but on the backend the process will be executed for more than 30 mins.
    I tried also increase the page time out in httpd.conf, but no use.
    The data returned as a result of the query is sized more than 10 MB. Is caching this much data is possible by the browser page? is the returned data is creating any problem here.
    Please help me to find appropriate reasone for the failure?Do you get any errors or warnings or it is just the slow speed which is the issue?
    There could be a variety of reasons for the delayed processing of this report. That includes parameter settings for that page, cache settings, network configurations, etc.
    - explore best optimization for your query;
    - evaluate portal for best performance configuration; you may follow this note (Doc ID: *438794.1* ) for ideas;
    - third: for that particular page carrying that report, you can use caching wisely. browser cache is neither decent for large files, nor practical. instead, explore the page cache settings that portal provides.
    - also look for various log files (application.log and apache logs) if you are getting any warnings reflecting on some kind of processing halt.
    - and last but not the least: if you happen to bring up a portal report with more than 10000 rows for display then think about the usage of the report. Evaluate whether that report is good/useful for anything?
    HTH
    AMN

  • Need help to redesign legacy SQL Script

    Hello Experts,
    I have the below code which produces a CREATE TRIGGER statement. as of now this does for updating. I need to re-design this code to add for inserting and deleting as well. I just need help in the structuring wise. I can build the logic for inserting and updating inside. I want to know how i can continue to get for "inserting" and "deleting" as well.
    you will understand my question better if you go through main code, present output and required output format below.
    I know this is a bad design first of all. but the below code is a legacy one. so i cant change the entire structure of the code :-( all i can do is to continue designing it to add new changes. Hence sought help from you all.
    please help
    SQL CODE:
    WITH audit_tables
    AS
       (SELECT object_name,
               MIN (column_id) min_col,
               MAX (column_id) max_col
        FROM   user_objects o,
               user_tab_columns c
        WHERE  o.object_name = 'CHR_VAL_DESC_A_T'
        AND    o.object_name = c.table_name
        GROUP BY object_name
    SELECT txt
    FROM (
    SELECT ' CREATE OR REPLACE TRIGGER ' || REPLACE(object_name,'_A_T') || '_ADT_TRG' || CHR(13) ||
           '   AFTER INSERT ' || CHR(13) ||
           '   OR    UPDATE ' || CHR(13) ||
           '   OR    DELETE ' || CHR(13) ||
           '   ON ' || REPLACE(object_name,'_A_T','_T') || CHR(13) ||
           '   FOR EACH ROW ' || CHR(13) ||
           ' DECLARE ' || CHR(13) ||
           ' BEGIN ' || CHR(13) ||
           ' IF updating THEN ' || CHR(13) ||
           '   INSERT INTO ' || object_name || CHR(13) ||
           '   (' txt, object_name, 1 disp_order, 0 column_id
    FROM audit_tables
    UNION ALL
    SELECT txt, object_name, disp_order, column_id
    FROM (SELECT 
            CASE
              WHEN max_col = column_id THEN
                '    '||column_name
              ELSE
                '    '||column_name || ','
              END AS txt, object_name, 2 disp_order, column_id
          FROM  audit_tables t,
                user_tab_columns C
          WHERE c.table_name = t.object_name
          ORDER BY c.column_id ASC)
    UNION ALL
    SELECT '   )' || CHR(13) ||
           '   VALUES ' || CHR(13) ||
           '   (', object_name, 3 disp_order, 0
    FROM audit_tables t
    UNION ALL
    SELECT txt, object_name, disp_order, column_id
    FROM (SELECT
            CASE
              WHEN max_col = column_id THEN
                CASE
                  WHEN SUBSTR(column_name,1,2) = 'N_' THEN
                    '    :NEW.'||decode(substr(column_name,1,2),'N_',substr(column_name,3))||');'
                  WHEN SUBSTR(column_name,1,2) = 'O_' THEN
                    '    :OLD.'||decode(substr(column_name,1,2),'O_',substr(column_name,3))||');'
                  WHEN min_col = column_id THEN
                    '    1'
                  WHEN column_id = 2 THEN
                    '     ''I'''
                  WHEN column_id = 3 THEN
                    '    SYSDATE'
                  ELSE
                  '    :NEW.'||column_name||');'
                END
              ELSE
                CASE
                  WHEN SUBSTR(column_name,1,2) = 'N_' THEN
                    '    :NEW.'||decode(substr(column_name,1,2),'N_',substr(column_name,3))||','
                  WHEN SUBSTR(column_name,1,2) = 'O_' THEN
                    '    :OLD.'||decode(substr(column_name,1,2),'O_',substr(column_name,3))||','
                  WHEN min_col = column_id THEN
                    '    1'||','
                  WHEN column_id = 2 THEN
                    '    ''I'''||','
                  WHEN column_id = 3 THEN
                    '    SYSDATE' ||','
                  ELSE
                    '    :NEW.'||column_name||','
                   END
              END AS txt,object_name, 4 disp_order, column_id
          FROM audit_tables t,
               user_tab_columns c
          WHERE c.table_name = t.object_name
          ORDER BY c.column_id ASC)
    UNION ALL
    SELECT 'END '||REPLACE(object_name,'_A_T') || '_ADT_TRG;' || CHR(13),
           object_name, 5 disp_order, 0
    FROM    audit_tables)
    ORDER BY object_name, disp_order, column_id
    PRESENT OUTPUT:
    CREATE OR REPLACE TRIGGER CHR_VAL_DESC_ADT_TRG
       AFTER INSERT
       OR    UPDATE
       OR    DELETE
       ON CHR_VAL_DESC_T
       FOR EACH ROW
    DECLARE
    BEGIN
    IF updating THEN
       INSERT INTO CHR_VAL_DESC_A_T
        TXN_ID,                                  
        TXN_TYP,                                 
        ADT_DTTM,                                
        CHR_VAL_DESC_ID,                         
        CHR_VAL_ID,                              
        LANG_ID,                                 
        DESC_ID,                                 
        O_CHR_VAL_DESC,                          
        N_CHR_VAL_DESC,                          
        O_TRANS_STATE,                           
        N_TRANS_STATE,                           
        CRTD_BY,                                 
        CRTD_DTTM,                               
        O_UPD_BY,                                
        N_UPD_BY,                                
        O_UPD_DTTM,                              
        N_UPD_DTTM,                              
        O_LOCK_NUM,                              
        N_LOCK_NUM                               
       VALUES
        1,                                       
        'I',                                     
        SYSDATE,                                 
        :NEW.CHR_VAL_DESC_ID,                    
        :NEW.CHR_VAL_ID,                         
        :NEW.LANG_ID,                            
        :NEW.DESC_ID,                            
        :OLD.CHR_VAL_DESC,                       
        :NEW.CHR_VAL_DESC,                       
        :OLD.TRANS_STATE,                        
        :NEW.TRANS_STATE,                        
        :NEW.CRTD_BY,                            
        :NEW.CRTD_DTTM,                          
        :OLD.UPD_BY,                             
        :NEW.UPD_BY,                             
        :OLD.UPD_DTTM,                           
        :NEW.UPD_DTTM,                           
        :OLD.LOCK_NUM,                           
        :NEW.LOCK_NUM);                          
    END CHR_VAL_DESC_ADT_TRG;
    REQUIRED OUTPUT FORMAT:
    CREATE OR REPLACE TRIGGER TRIGGER_NAME
       AFTER INSERT
       OR    UPDATE
       OR    DELETE
       ON TABLE_NAME
       FOR EACH ROW
    DECLARE
    BEGIN
    IF updating THEN
       INSERT TABLE_NAME
        list of column names                               
       VALUES
    IF inserting THEN
       INSERT TABLE_NAME
        list of column names                               
       VALUES
    IF deleting THEN
       INSERT TABLE_NAME
        list of column names                               
       VALUES
    END TRIGGER_NAME;

    can anyone please help?
    i tried adding with inserting and updating also..but when i tried to add deleting part the final output not comes in proper structure.
    WITH audit_tables
    AS
       (SELECT object_name,
               MIN (column_id) min_col,
               MAX (column_id) max_col
        FROM   user_objects o,
               user_tab_columns c
        WHERE  o.object_name IN ('CHR_VAL_DESC_A_T', 'CHR_VAL_A_T')
        AND    o.object_name = c.table_name
        GROUP BY object_name
    SELECT txt
    FROM (
    SELECT ' CREATE OR REPLACE TRIGGER ' || REPLACE(object_name,'_A_T') || '_ADT_TRG' || CHR(13) ||
           '   AFTER INSERT ' || CHR(13) ||
           '   OR    UPDATE ' || CHR(13) ||
           '   OR    DELETE ' || CHR(13) ||
           '   ON ' || REPLACE(object_name,'_A_T','_T') || CHR(13) ||
           '   FOR EACH ROW ' || CHR(13) ||
           ' DECLARE ' || CHR(13) ||
           ' BEGIN ' || CHR(13) ||
           *' IF inserting THEN '* || CHR(13) ||
           '   INSERT INTO ' || object_name || CHR(13) ||
           '   (' txt, object_name, 1 disp_order, 0 column_id
    FROM audit_tables
    UNION ALL
    SELECT txt, object_name, disp_order, column_id
    FROM (SELECT 
            CASE
              WHEN max_col = column_id THEN
                '    '||column_name
              ELSE
                '    '||column_name || ','
              END AS txt, object_name, 2 disp_order, column_id
          FROM  audit_tables t,
                user_tab_columns C
          WHERE c.table_name = t.object_name
          ORDER BY c.column_id ASC)
    UNION ALL
    SELECT '   )' || CHR(13) ||
           '   VALUES ' || CHR(13) ||
           '   (', object_name, 3 disp_order, 0
    FROM audit_tables t
    UNION ALL
    SELECT txt, object_name, disp_order, column_id
    FROM (SELECT
            CASE
              WHEN max_col = column_id THEN
                CASE
                  WHEN SUBSTR(column_name,1,2) = 'N_' THEN
                    '    :NEW.'||decode(substr(column_name,1,2),'N_',substr(column_name,3))||');'
                  WHEN SUBSTR(column_name,1,2) = 'O_' THEN
                    '    NULL'||');'
                  WHEN min_col = column_id THEN
                    '    1'
                  WHEN column_id = 2 THEN
                    '     ''I'''
                  WHEN column_id = 3 THEN
                    '    SYSDATE'
                  ELSE
                  '    :NEW.'||column_name||');'
                END
              ELSE
                CASE
                  WHEN SUBSTR(column_name,1,2) = 'N_' THEN
                    '    :NEW.'||decode(substr(column_name,1,2),'N_',substr(column_name,3))||','
                  WHEN SUBSTR(column_name,1,2) = 'O_' THEN
                    '    NULL'||','
                  WHEN min_col = column_id THEN
                    '    1'||','
                  WHEN column_id = 2 THEN
                    '    ''I'''||','
                  WHEN column_id = 3 THEN
                    '    SYSDATE' ||','
                  ELSE
                    '    :NEW.'||column_name||','
                   END
              END AS txt,object_name, 4 disp_order, column_id
          FROM audit_tables t,
               user_tab_columns c
          WHERE c.table_name = t.object_name
          ORDER BY c.column_id ASC)
    UNION ALL
    SELECT txt, object_name, disp_order, column_id
    FROM(SELECT *' ELSIF updating THEN '* || CHR(13) ||
           '   INSERT INTO ' || object_name || CHR(13) ||
           '   (' txt, object_name, 5 disp_order, 0 column_id
    FROM audit_tables
    UNION ALL
    SELECT txt, object_name, disp_order, column_id
    FROM (SELECT 
            CASE
              WHEN max_col = column_id THEN
                '    '||column_name
              ELSE
                '    '||column_name || ','
              END AS txt, object_name, 6 disp_order, column_id
          FROM  audit_tables t,
                user_tab_columns C
          WHERE c.table_name = t.object_name
          ORDER BY c.column_id ASC)
    UNION ALL
    SELECT '   )' || CHR(13) ||
           '   VALUES ' || CHR(13) ||
           '   (', object_name, 7 disp_order, 0
    FROM audit_tables t
    UNION ALL
    SELECT txt, object_name, disp_order, column_id
    FROM (SELECT
            CASE
             WHEN max_col = column_id THEN
                CASE
                  WHEN SUBSTR(column_name,1,2) = 'N_' THEN
                    '    :NEW.'||decode(substr(column_name,1,2),'N_',substr(column_name,3))||');'
                  WHEN SUBSTR(column_name,1,2) = 'O_' THEN
                    '    :OLD.'||decode(substr(column_name,1,2),'O_',substr(column_name,3))||');'
                  WHEN min_col = column_id THEN
                    '    1'
                  WHEN column_id = 2 THEN
                    '     ''U'''
                  WHEN column_id = 3 THEN
                    '    SYSDATE'
                  ELSE
                  '    :NEW.'||column_name||');'
                END
              ELSE
                CASE
                  WHEN SUBSTR(column_name,1,2) = 'N_' THEN
                    '    :NEW.'||decode(substr(column_name,1,2),'N_',substr(column_name,3))||','
                  WHEN SUBSTR(column_name,1,2) = 'O_' THEN
                    '    :OLD.'||decode(substr(column_name,1,2),'O_',substr(column_name,3))||','
                  WHEN min_col = column_id THEN
                    '    1'||','
                  WHEN column_id = 2 THEN
                    '    ''U'''||','
                  WHEN column_id = 3 THEN
                    '    SYSDATE' ||','
                  ELSE
                    '    :NEW.'||column_name||','
                END
              END AS txt,object_name, 8 disp_order, column_id
          FROM audit_tables t,
               user_tab_columns c
          WHERE c.table_name = t.object_name
          ORDER BY c.column_id ASC)
    UNION ALL
    SELECT 'END IF;' || CHR(13) ||
           'END '||REPLACE(object_name,'_A_T') || '_ADT_TRG;' || CHR(13),
           object_name, 9 disp_order, 0
    FROM    audit_tables)
    ORDER BY object_name, disp_order, column_id)

  • Need Help With Basics of SQL

    Hey I'm trying to get a working understanding of some of the basics behind SQL, I've composed a few questions that I think may help me with this. Anyone that can help me with any of them will greatly help me thanks.
    1. How to create synonym for tables?
    2. How to describe the structure of tables?
    3. How to list the contents of tables?
    4. How to create a table named with the same structure as another table?
    5. How to copy rows with less than a certain criteria in value (e.g. Price<$5.00) into another table?
    6. How to change the data type to e.g. NUMBER(6)?
    7. How to add a new column named with data type e.g. VARCHAR2(10)?
    8. How to change a specific field within a table (e.g. For ORDER_NUMBER 12489, change the C_NUMBER to 315)?
    9. How to delete a specific row from a table?
    10. How to declare a column as the primary key of a table and call it e.g. PK_something?
    11. How to show certain columns when another column is less than a certain criteria in value (e.g. Price<$5.00)?
    12. How to show certain columns with another column having a certain item class e.g. HW or AP?
    13. How to list certain columns when another column e.g. price is between two values?
    14. How to list certain columns when another column e.g. price is negative?
    15. How to use the IN operator to find certain columns (e.g. first and last name of customers who are serviced by a certain ID)
    16. How to find certain columns when one of the columns begins with a particular letter (e.g. A)
    18. How to list the contents of the a table sorted in ascending order of item class and, within each item class, sorted in descending order of e.g. price?
    19. How to do a count of column in a table?
    20. How to sum a column and make rename is something?
    21. How to do a count of a column in a table (without repeats e.g. if a certain number repeats more than once than to only count it once)?
    22. How to use a subquery to find certain fields in columns when the another column’s fields values are greater than e.g. its average price?

    848290 wrote:
    Hey I'm trying to get a working understanding of some of the basics behind SQL, I've composed a few questions that I think may help me with this. Anyone that can help me with any of them will greatly help me thanks.To use the terminology you have in those questions, you must already have a basic understanding of SQL, so you have exposed yourself as not being the author of such questions.
    Please do not ask homework questions without having at least attempted to answer them yourself first and show where you're struggling.

Maybe you are looking for