help on a pl sql script

Hi all!

I'm new to pl sql.

Can sb help me on how to do this:

I have a table where I want to find the max (value) per month for the cod of the CCA.

So that's how my table is:

InstanceId date value acc_cod

20/02/2013 1, 2

21/02/2013 2, 3 has

3                     23/02/2013     6                  b

16/09/2014 4, 12 b

Oh... I did not notice that... Try the below

SELECT instanceid,

acc_cod,

DATE_,

value

Of

(SELECT instanceid,
acc_cod,

DATE_,

value,

Max (InstanceId) OVER (PARTITION BY acc_cod, TO_CHAR (date_, 'MONYYYY')) mx_val

FROM table_name)

WHERE mx_val = instanceid;

(GOLD)

SELECT instanceid,

acc_cod,

DATE_,

value

Of

(SELECT instanceid,

DATE_,

acc_cod,

value,

ROW_NUMBER() over (PARTITION BY acc_cod, TO_CHAR(date_,'MONYYYY') ORDER BY DESC instanceid) rn

FROM table_name)

WHERE rn = 1;

Tags: Database

Similar Questions

  • help with pl SQL script

    Hello everyone.

    Can sb help me create the following script, based on three tables:

    ID col1
    314value2

    ID ID1 col2
    3141somevalue4
    3142somevalue5
    3143somevalue6
    ID
    ID1
    ID2
    COD
    Qty.

    31411LTR1031412KG2031421LTR40

    The result should be like this:

    id id1
    ID2
    cod_1
    qty_1
    COD2
    qty_2
    col1
    col2
    31411LTR10KG20value2somevalue4
    31421LTR40value2somevalue5

    Post edited by: 933651

    Hello

    Here is my view on this problem. May need some adjustments in the join conditions when the data model and the data is more clear

    create table t1 (
      id number,
      col1 varchar2(10)
    );
    insert into t1 values (314, 'somevalue2');
    create table t2 (
      id number,
      id1 number,
      col2 varchar2(10)
    );
    insert into t2 values (314, 1, 'somevalue4');
    insert into t2 values (314, 2, 'somevalue5');
    insert into t2 values (314, 3, 'somevalue6');
    create table t3(
        id  number,
        id1 number,
        id2 number,
        cod varchar2(3),
        qty number
    );
    insert into t3 values (314, 1, 1, 'LTR', 10);
    insert into t3 values (314, 1, 2, 'KG', 20);
    insert into t3 values (314, 2, 1, 'LTR', 40);
    
    with ltr as (
      select id, id1, id2, cod, qty  from
      (
        select id, id1, id2, cod as code, cod, qty from t3
      )
      pivot
      (
        sum(qty) for code in ('LTR' as qty)
      )
      where qty is not null
    ), kg as (
      select id, id1, id2, cod, qty  from
      (
        select id, id1, id2, cod as code, cod, qty from t3
      )
      pivot
      (
        sum(qty) for code in ('KG' as qty)
      )
      where qty is not null
    )
    select
      t2.id,
      t2.id1,
      ltr.id2,
      ltr.cod,
      ltr.qty,
      kg.cod,
      kg.qty,
      t1.col1,
      t2.col2
    from t2 join ltr on (
      t2.id = ltr.id and t2.id1 = ltr.id1
    ) full join kg on (
      t2.id = kg.id and t2.id1 = kg.id1
    ) left join t1 on (
      t1.id = t2.id
    )
    order by id, id1
    ;
    
    drop table t1 purge;
    drop table t2 purge;
    drop table t3 purge;
    
    table T1 created.
    1 rows inserted.
    table T2 created.
    1 rows inserted.
    1 rows inserted.
    1 rows inserted.
    table T3 created.
    1 rows inserted.
    1 rows inserted.
    1 rows inserted.
    
            ID        ID1        ID2 COD        QTY COD        QTY COL1       COL2
    ---------- ---------- ---------- --- ---------- --- ---------- ---------- ----------
           314          1          1 LTR         10 KG          20 somevalue2 somevalue4
           314          2          1 LTR         40                somevalue2 somevalue5
    
    table T1 dropped.
    table T2 dropped.
    table T3 dropped.
    
  • Update SQL script

    Hi friends,

    I started this thread yesterday, but for some reason that I'm not able to answer/continue on this old thread. Here are the details from the old thread

    I need help with a sql script to update sid_no_new column with values based on sid_no for about 6000 + records.

    Here is an example below...

    SQL > select * from test1;

    SID_NO SID_NO_NEW UNIT_NO

    --------------------         --------------------           --------------------

    2000                                                          unit1

    2000                                                          unit3

    2000                                                          unit4

    2002                                                          unit16

    4500                                                          unit22

    In the example above, there are 3 sid_no consecutive '2000'. Sid_no_new must be 2000_1, 2000_2, 2000_3, respectively.

    It should be like:

    SQL > select * from test1;

    SID_NO SID_NO_NEW UNIT_NO

    --------------------      --------------------           --------------------

    2000 2000_1 unit1

    2000 2000_2 unit3

    unit4 2000_3 2000

    2002 2002 unit16

    4500 4500 unit22

    I ran the script (thanks to the experts who have contributed to) below, it works very well for the above documents, but I get ORA-30926 error when I run on the table with 6000 records...

    Fusion IN test1 tgt USING

    (SELECT sid_no,

    sid_no_new,

    unit_no,

    Count (sid_no) over (partition BY sid_no) cnt,

    ROW_NUMBER() over (partition BY order of sid_no to unit_no) rn

    OF test1

    ) CBC ON(tgt.sid_no=src.sid_no AND tgt.unit_no=src.unit_no)

    WHEN matched THEN

    UPDATE SET Sid_no_new = CASE WHEN cnt > 1 THEN sid_no |' _'. RN ELSE sid_no END;

    ERROR on line 1:

    ORA-30926: failed to get a stable set of rows in the source tables

    Please give your suggestions... Thank you very much

    In fact, this could be done easier:

    SQL > select *.

    test1 2

    3.

    SID_ SID_NO UNIT_N

    ---- ------ ------

    2000 2000_1 unit1

    2000 2000_2 unit3

    unit4 2000_3 2000

    2002 2002 unit16

    4500 4500 unit22

    SQL > select *.

    2 of test4

    3.

    UID_NO TYP RULE_DUE

    ---------- --- ----------

    RAF 2000 90

    3140 TH 100

    6712 JUL 21

    233 12 H6

    SQL > fusion

    2 in d of test4

    3 use)

    4 with as t4)

    5. Select uid_no,

    6                               type,

    7                               rule_due,

    8 rowid RID,

      9                               uid_no || '_' || ROW_NUMBER() over (partition by uid_no by 1 order) sid_no_new

    10 of test4

    11                      )

    12. Select t1.sid_no_new,

    T4.type 13,

    T4.rule_due 14,

    15 cases

    16 when t1.sid_no = t1.sid_no_new then t4.rid

    17 when t4.sid_no_new = t1.sid_no_new then t4.rid

    18 end rid

    19 of t4,

    Test1 20 t1

    21 where t1.sid_no = t4.uid_no

    22          ) s

    23 on)

    24 d.rowid = s.rid

    25       )

    26 when matched

    27 then

    Update 28

    29 set d.uid_no = s.sid_no_new

    30 when no match

    31 then

    32 insert

    33 values)

    s.sid_no_new 34,

    s.type 35,

    36 s.rule_due

    37                )

    38.

    3 lines merged.

    SQL > select *.

    2 of test4

    3.

    UID_NO TYP RULE_DUE

    ---------- --- ----------

    2000_1 RAF 90

    3140 TH 100

    6712 JUL 21

    233 12 H6

    2000_3 RAF 90

    2000_2 RAF 90

    6 selected lines.

    SQL >

    SY.

  • Running SQL scripts on different databases

    Hello!

    At my work we have many databases on a server. I have to make a request so that users can run the application on a selected database SQL scripts.

    So my question may be separated into 2 sections:

    1 - is possible to let users SQL scripts run directly from the application?

    I think a database to create a page that displays the list of all the SQL scripts, that we want to use, and when you click on a specific script name in the list, it asks something like "Where the database you want to run the script?", and then, as confirmed by the user, it would show results running the script on a different page.

    is 2 - possible to let users choose what database to run these scripts?

    Any information would be helpful, I'm at the beginning of the project and am not sure at all about how to proceed, or if it is at all possible.

    Thank you!

    Fallen_Kaede wrote:

    Thanks for the quick response. I confirm that we are talking about databases on a server.

    The request is to "debug" to other databases. For example, be run scripts to the locks on the list, to show the different indexes on a table, etc. to help diagnose problems.

    It looks like the 'users' are DBA who should use OEM/Grid Control to do so.

    1 - is possible to let users SQL scripts run directly from the application?

    Lol the only way to run scripts in the APEX is the use of the SQL Scripts for SQL workshop component.

    The key word in your question is 'application '. SQL * more and Developer SQL are applications that allow users to run SQL scripts on the desktop. Scripts SQL is an APEX integrated application that allows developers to run SQL scripts on the web. However, there is no documented way supported for end users to run SQL scripts in a custom of APEX application that you created. Therefore, rethink your approach. Rather than create an application APEX runs scripts, you must convert the scripts in PL/SQL, APEX packages, pages, components and applications leverage features of the APEX and reports, graphics and interactivity (e.g. exploration links; timed refreshes etc.). This will give you a user experience improved, beyond what is possible using static SQL scripts.

    is 2 - possible to let users choose what database to run these scripts?

    The APEX applications would exist on an instance of the APEX in a database and access other databases using the database links (which can have a performance impact). It would be possible to create a LOV based on the view of the USER_DB_LINKS dictionary and use in a selection set to a global page list to provide a control to set the current context of the database. Region data sources will have to be generated dynamically in the packages of PL/SQL use the links from relevant database based on this value LOV.

  • Where is CMCLEAN. SQL script now?

    Hello

    I think I should find the cmclean.sql script in the following note:

    Simultaneous treatment of - CMCLEAN. SQL - Script Non-destructive to clean the simultaneous Tables Manager [ID 134007.1]


    But when I searched Doc ID 134007.1 I was surprised to see that the Doc ID is present but cmclean.sql script is not available here as it was:


    Now the title of the Doc has also changed to:

    Simultaneous - treatment simultaneous Recovery Manager Troubleshooting Wizard (Doc ID 134007.1)

    Can you please let me know what happened to the original note and help me get to the script cmclean.sql on MOS.

    Thank you

    Armande

    Florent,

    cmclean. SQL script is so more exist and you should use the "Competitor Assistant Manager of recovery".

    Thank you

    Hussein

  • Backingup database using SQL script

    Hello

    Let me start by saying I am new to Oracle and I'm learning how to do a project where I have to write a SQL script that allows you to back up all data files (i.e. control, redo log and data files).  Assuming that all the files are stored in a folder, the source locations and the destination of these files must be provided as 'substitution variables.  The script must implement the following tasks (directions)


    1. Log in as the SYS user with SYSDBA role
    2. Closing the database
    3. Copy the files to the location of the source database and store it in the destination location
    4. Restart the database
    5. Log in the user SCOTT

    In addition, the script must use the "HOST" command to publish the operating system command to perform the copy task.


    * Since this is just a learning project and not a real scenario, I can't run my script in SQLplus to verify if it is correct. That's why I ask for professional advice. I am currently working with Oracle 11 g Enterprise running on a client Windows XP OS.


    Here's what I decided, it's a cold backup, please let me know if what I do is correct so far, and if not can you please steer me in the right direction.


    connect SYS / < password > have SYSDBA

    Shutdown;

    Copy of HOST of & source_file to & destination_file;       -It is the line that confuses me.

    connect SYS / < password > have SYSDBA

    start-up;

    Connect scott/tiger


    Thanks in advance for your time and input.

    I currently have the free version of 11g Oracle Enterprise downloaded on my computer, running on VB with the client Windows XP OS. But being new to this that I find myself afraid of bad script execution that can break my DB. I know that I can just reload and try again, but I try to approach the question as carefully as possible. Although the error codes help me understand what I am doing wrong much of the time.

    Break things (and their fixation) is how to learn.  Because you are running in VBox, simply take a snapshot of vbox system before testing.  So if it is irrevocably broken, you just restore the snapshot system.

    But give what you're trying to do, what do you think might happen this may break your db?  As far as the db is concerned, everything you do connect, stop and commissioning.  Nothing can go off of the truth of that.

  • SQL script problem

    Hi all

    I have a sql script which is given below.

    Select "alter system kill session ' |" SID | «, » || SERIES # session $ v where MACHINE = "Skykollap27";

    Out: -.

    "ALTERSYSTEMKILLSESSION" | SID | «, » || SERIAL NO.

    -----------------------------------------------------------------------------------------------------------

    alter system kill session 57.25

    But it should be

    alter system kill session '57.25';

    How to do this. Help, please.

    Thanks in advance

    its solved...

    Thank you

  • Help with anonymus pl/sql layout using javascript

    Hi people.

    I want to display records on a region (using apex 4.2), this anonymous pl/sql script below shows 3 records in database, but the layout only shows me the first album with its data, the other 2 files, only show me the labels.
    I checked this same query creating a report and it shows the 3 files correctly.
    I guess I'm doing something wrong with this javascript routine or certainly miss something,
    in this link you can see the layout I get. (http://sdrv.ms/Xrv0J8).

    Another is the little help I need to display in a line for each record. in fact, I'm getting a line for each label and a line for each data record. I have read and learned something is/div, but I don't know how to change it to get the desired results, any suggestion is welcome.

    Thanks in advance for any help.



    I am completely new in apex and java script, but I have several years of experience in pl/sql.

    -------


    The Script:

    Begin
    Declare
    Anyth cursor is
    Select the item,
    To_char (delivery, ' month DD, YYYY hh24:mi:ss ") delivery.
    Current_date
    Of pending_items;
    Begin
    For a loop Anyth
    Begin
    Sys.Htp.P ("< script type =" text/javascript"> '");
    Sys.Htp.P ('function cdtd() {'});
    Sys.Htp.P ("var Christmas = new Date (" ' | ') ") a.Delivery | '")');
    Sys.Htp.P ("var now = new Date();'");
    Sys.Htp.P ("var timeDiff = xmas.getTime () - now.getTime ();'");
    Sys.Htp.P (' if (timeDiff < = 0) {'});
    Sys.Htp.P ('clearTimeout() (timer)');
    -Sys.Htp.P ("document.write ("of the text here.")");
    Sys.Htp.P (' / / execute all necessary code to the completion of the countdown here ');
    Sys.Htp.P ('} ');
    Sys.Htp.P (' var seconds = Math.floor(timeDiff / 1000);) ") ;
    Sys.Htp.P ("var minutes = Math.floor(seconds / 60) ;");
    Sys.Htp.P (' varhour = Math.floor(minutes / 60);) ") ;
    Sys.Htp.P ("days var Math.floor(hours / 24) = ;"); ")
    Sys.Htp.P (' hours = 24 ;'); %)
    Sys.Htp.P (' minutes % = 60 ;');)
    Sys.Htp.P (' seconds % = 60 ;');)
    Sys.Htp.P (' document.getElementById("daysBox").innerHTML = days ;');)
    Sys.Htp.P (' document.getElementById("hoursBox").innerHTML = hours;) ") ;
    Sys.Htp.P (' document.getElementById("minsBox").innerHTML = minutes ;');)
    Sys.Htp.P (' ;'); seconds = document.getElementById("secsBox").innerHTML)
    Sys.Htp.P (' timer var = setTimeout ("cdtd ()", 1000);');
    Sys.Htp.P ('} ');
    Sys.Htp.P ("< /script >");
    Sys.Htp.P ("days");
    Sys.Htp.P ("< div id ="daysBox"> < / div > '");
    Sys.Htp.P ("hours");
    Sys.Htp.P ("< div id ="hoursBox"> < / div > '");
    Sys.Htp.P ('minute');
    Sys.Htp.P ("< div id ="minsBox"> < / div > '");
    Sys.Htp.P ("seconds");
    Sys.Htp.P ("< div id ="secsBox"> < / div > '");
    Sys.Htp.P ("< script type =" text/javascript"> '");
    Sys.Htp.P ('cdtd() < /script > ');
    End;
    End loop;
    End;
    End;

    For now, let's focus on delivery dates and the javascript.
    Why you would put your javascript in htp.p called in a plsql region is beyond me. When you edit the page there is a region of javascript where you can put global variables and functions, a great spot for it then and much easier to manage than that.
    Now, it is also clear that your query on pending items will return multiple records. Thus, using a report would serve you well in this case. However, using code like this:

    document.getElementById("daysBox").innerHTML = days;
    document.getElementById("hoursBox").innerHTML = hours;
    document.getElementById("minsBox").innerHTML = minutes;
    document.getElementById("secsBox").innerHTML = seconds;
    

    you won't have to. getElementById is supposed to return an element identified by a code. If you have a report that contains multiple lines and elements on each line with the same ID, you're doing something wrong. You should be the date of delivery by rank, and I suppose that you put your code with htp.p because you don't know how to deal with this and passing the procedure of javascript.
    But would it not more oppurtune for you simply create a report with a sql source when you calculate each part of the date and then refresh this region with a certain interval. If you would be refreshed every 5 minutes, not who is more fast enough to keep track of things? Remember, refreshing a region will execute the SQL again.
    It is not that you cannot make a report multi-record with a countdown by rank, but are you sufficiently comfortable with javascript and jquery to encode and maintain that against leverage plsql and dynamic actions (there is even an action timer plugin supplied by oracle dynamic)?

  • SQL script error

    Hi all

    I create a sql script. I am getting an error on an IF clause and I can not find the reason for this is the script:

    command prompt
    prompt QUIN CANVI FLIGHT REALITZAR TIPUS?
    prompt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    command prompt
    prompt CANVIAR has RO (PULSA 1)
    prompt CANVIAR has RW (PULSA 2)
    command prompt
    Guest Introdueix el canvi tipus a realitzar: & & tipus
    variable number of tipus_canvi;
    variable tipus_antic char (10);
    Start
    tipus_canvi: = & tipus.
    If type = 1 then tipus_antic: = "ONLINE";
    ElseIf type = 2 then tipus_antic: = 'READ ONLY ';
    of another raise_application_error (-20200, 'Not valor of United Nations are valid per aquesta operacio Aquest.');
    end if;
    end;
    /

    And this is the error:

    ElseIf (type = 2) then tipus_antic: = 'READ ONLY ';
    *
    ERROR on line 4:
    ORA-06550: line 4, column 25:
    PLS-00103: encountered the symbol "THEN" when expecting one of the following conditions:
    := . ( % ;
    ORA-06550: line 5, column 6:
    PLS-00103: encountered the symbol "ELSE" when expecting one of the following conditions:
    (begin case declare end exit for goto if loop mod null
    pragma raise return select update when all with
    < an ID > < a double quote delimited id
    ORA-06550: line 7, column 1:
    PLS-00103: encountered the symbol "END".

    Please, can you help me?
    Concerning
    dbajug

    Hello

    Your if syntax is incorrect.

    Please find below syntax

    IF condition THEN
             {...statements...}
    ELSIF condition THEN
             {...statements...}
    ELSE
             {...statements...}
    END IF;
    

    It's ELSIF not ELSEIF.
    Twinkle

  • How to assign the variable in the SQL script?

    I have the below running to generate the XML from the file.
    set long 100
    set pages 0
    set trimspool on
    set serveroutput on
    set echo off
    set terminal off
    variable out CLOB
    begin
            pack.proc('&&1','&&2',:out);
    end;
    /
    spool /dir/loc/file1.xml
    select :out from dual;
    spool off
    I'm queue in the order of the coil, the output to a file named file1.xml. I invoke this SQL script from a shell script, I will pass variable association 3rd since the shell script which I want to use in the file ' & & 3'.xml. Help me set this variable to the DIGITAL field in this SQL script and how can I add to the command of the coil?
    set long 100
    set pages 0
    set trimspool on
    set serveroutput on
    set echo off
    set terminal off
    variable out CLOB
    define var1='&1' -- to allow for more readable code
    define var2='&2'
    define var3 ='&3'
    begin
            pack.proc('&var1','&var2',:out);
    end;
    /
    spool /dir/loc/file&var3..xml -- note the substition variable has  . appended
    print out
    spool off
    

    ------------
    Sybrand Bakker
    Senior Oracle DBA

  • Output is not as expected (getting wrapped) for a simple sql script

    Hello

    I try to run a simple sql script with a few queries including a semi colon output (;) when text file separated that later - I can open in excel.

    My SQL file contains in part the code - below

    SELECT
    "PORTFOLIO" | « ; » ||
    "CONTRACT # | « ; » ||
    "CONS_INV #" | « ; » ||
    "CUSTOMER #" | « ; » ||
    "CLIENT_NAME | « ; » ||
    'MESSAGE ' | « ; » ||
    "REQUEST_ID" | « ; » ||
    "BILL_TO_COUNTRY" | « ; » ||
    "BILL_TO_ADDRESS1" | « ; » ||
    "BILL_TO_ADDRESS2" | « ; » ||
    "BILL_TO_ADDRESS3" | « ; » ||
    "BILL_TO_ADDRESS4" | « ; » ||
    "BILL_TO_CITY" | « ; » ||
    "BILL_TO_POSTAL_CODE" | « ; » ||
    "BILL_TO_STATE" | « ; » ||
    "BILL_TO_PROVINCE" | « ; » ||
    "BILL_TO_COUNTY" | « ; » ||
    "SHIP_TO_COUNTRY | « ; » ||
    "SHIP_TO_ADDRESS1" | « ; » ||
    "SHIP_TO_ADDRESS2" | « ; » ||
    "SHIP_TO_ADDRESS3" | « ; » ||
    "SHIP_TO_ADDRESS4" | « ; » ||
    "SHIP_TO_CITY" | « ; » ||
    "SHIP_TO_POSTAL_CODE" | « ; » ||
    "SHIP_TO_STATE | « ; » ||
    "SHIP_TO_PROVINCE" | « ; » ||
    "SHIP_TO_COUNTY" | « ; » ||
    "INVOICE_DATE | « ; » ||
    "INVOICE_CREATION_DATE" | « ; » ||
    "PARTIALLY_DROPPED" | « ; » ||
    "AMOUNT".
    OF the double
    /
    SELECT "DOUBLE
    /
    SELECT "error Code" | « ; » ||' Error message "OF the DOUBLE
    /

    SELECT lookup_code. « ; » || Description
    OF fnd_lookup_values
    WHERE lookup_type = 'CUST_ERROR. '
    /

    The issue I'm facing is that the output of the competitive program file comes out bit so enter each line only 132 characters in a line and therefore gets pushed in the next row in excel.

    Output-

    PORTFOLIO; CONTRACT #; CONS_INV #; CUSTOMER #; CLIENT_NAME; MESSAGE; REQUEST_ID; BILL_TO_COUNTRY; BILL_TO_ADDRESS1; BILL_TO_ADDRESS2; BILL_TO_A
    DDRESS3; BILL_TO_ADDRESS4; BILL_TO_CITY; BILL_TO_POSTAL_CODE; BILL_TO_STATE; BILL_TO_PROVINCE; BILL_TO_COUNTY; SHIP_TO_COUNTRY; SHIP_TO_ADDR
    ESS1; SHIP_TO_ADDRESS2; SHIP_TO_ADDRESS3; SHIP_TO_ADDRESS4; SHIP_TO_CITY; SHIP_TO_POSTAL_CODE; SHIP_TO_STATE; SHIP_TO_PROVINCE; SHIP_TO_COUN
    TY; INVOICE_DATE; INVOICE_CREATION_DATE; PARTIALLY_DROPPED; AMOUNT


    Please advice on all of the commands that I can use in the script sql in order to expand the output page or any other workaround solutions!
    Please answer as soon as possible if you can help me!

    Thank you!

    SQL * MORE?

    SQL> set lin 1000
    SQL> spool your_file_out.csv
    SQL> your_query
    SQL> spool off
    

    Kind regards
    Malakshinov Sayan

  • question about pl/sql script

    Hi all
    I need to manage other tables that will contain as follows:
    whenever the user query it will be an insert in this table called mng_rishui_session that contain
    track:
    NUM of user's session id,
    Date and time of execution of the request,
    type of query
    every 15 minutes, I need to delete the data of the user of some the various tables according to the type of the query.
    the tables that contain the data:
    mng_rishui_session / / DESC
    session_id_num NOT NULL VARCHAR2 (30)
    type_query not null number (2)
    date of time_query
    I want to write a pl/sql script like this:
     
    declare cursor c1 as 
    select session_id_num,type_query 
    from mng_rishui_session 
    where time_query (/*here i need to do the condition that 15 minutes has passed from the time query 
     i don't know how to do this */
    begin 
    for e in c1 loop 
    
    case e.type_query 
    when 1 
    delete query1_table where session_id_num :=e.session_id_num;
    when 3 
    delete query3_table where session_id_num :=e.session_id_num;
    end
    end loop;
    end;
    issues related to the:
    1. how to check if 15 minutes where the past
    2. it is good to do with loop for writing valid deletion in the loop
    or do I have to do it another way to write this.
    Thanks in advance
    Naama

    Hi, Naama,

    Naama wrote:
    ... now my script looks like this:
    mng_session. SQL

    
    declare cursor c1 as
    select session_id_num,type_query
    from mng_rishui_session
    where  (sysdate - t_query) * 24 * 60 >= 15;  
    
    begin
    for e in c1 loop
    case e.type_query
    when 1 then
    delete query1_table where session_id_num = e.session_id_num;
    when 3 then
    delete query3_table where session_id_num  =e.session_id_num;
    end case;
    end loop;
    end;
    

    first of all, I want to know where I'm going to do the validation, if I need to do.

    In the PL/SQL code (so we won't do it in case of error) but after the LOOP is finished.
    In other words, assuming you are using a LOOP. It will be much more effective if you just do a DELETE statement on each table. Think about it: If your mng_rishui_session has 1,000 rows where t_query (or time_query, if the column is always so called) is fortunate to have one of the type_queries you want at least 15 minutes, and 100 of these lines, then you must fetch the 1,000 rows (if not all of the table) , and separate 100 DELETE queries. It is more effective to extract 100 lines (assuming that there is an index composed of type_query and t_query) and to only 2 DELETE statements. See below.

    Second:
    I run this script through linux system, in order to head to sleep.
    My problem is that I am trying to append the output of script;
    but it won't let me

    People on this site will try to help you. Why not try to help them do it?
    Do not say "he won't let me": show all that you know the problem. What exactly have you tried? That's happened? If you got an error, display the full error message, including the line number. If you got the wrong results, describe what happened.

    My goal is:

    a script running the sql scripy called:
    CS-mng-session:

    #!bin/bash
    sqlplus user/passwd@dbname @mng_session /*mng_session is the script were i declare the cursor etc.*/
    in the command line i want to write :
    (sleep 600; sh cs-mng-session)& 
    

    the problem is that if I do not exit. He recorded in sql and the script does not return to the command line.
    How do I overcome this problem?

    Make an EXIT.

    Try something like this:

    DECLARE
         cutoff_date     DATE     := SYSDATE - (15 / (24 * 60));
    BEGIN
         DELETE     query1_table
         WHERE     session_id_num  IN (
                             SELECT  session_id_num
                             FROM     mng_rishui_session
                             WHERE     type_query     = 1
                             AND     t_query          <= cutoff_date
                           );
    
         DELETE     query3_table
         WHERE     session_id_num  IN (
                             SELECT  session_id_num
                             FROM     mng_rishui_session
                             WHERE     type_query     = 3
                             AND     t_query          <= cutoff_date
                           );
    
         COMMIT;          -- If desired
    END;
    /
    
    EXIT;
    

    Is there a reason why query1_table should be different from query3_table? Why can't you have just a query_table, with a column of type_query?

    What happens if there is an error? You won't want to know? You must add an EXCEPTION section to write the details about the error in a log table and someone send an email on this subject.

    Published by: Frank Kulash, August 31, 2010 09:07
    Added / issue;

  • Save sql scripts

    Hi all

    Please help me to set the default directory to save the sql script files.

    Thank you!

    I guess you're asking about SQL * more. This tool saves files in the current directory.

    Therefore, specify the full path or enter SQL * Plus from the directory desired. I'm not aware of any way to change the current directory to in SQL * more.

  • No active SQL script editor

    I downloaded oracle xe and I tried to enter the page of sql script in sql and the place where you enter sqls was not editable. All of the suggestions.

    Also I tried to enter the page of commands sql queries and the query works fine with a comment statement, but if
    I comment with '-' or ' / *' and then it gives an error of invalid request.

    Any help will be appreciated.

    Yes if you want to use the script editor (or IE... hmmmm) or upgrade.

    New features. Better security. Special limited time offer-press until exhaustion of stocks last ;)

  • SQL script to check the status of the last command

    Hi all

    I need to make a SQL script and call it from Unix (bash) shell.


    =========================
    sqlplus vikas/vikas < < END
    coil /oracle/vikas/output.txt
    order 1
    command 2
    ...
    ....
    command N
    spool off;
    commit;
    END
    =========================


    It works perfectly, but my problem is that if a command fails, the other controls in suite works. However, I need to keep a check when a broken command that you exit the script.

    On unix, we have an option of echo $? to check the status of the previous command, but how to accomplish this SQL.

    Pls help!
    __________________
    == > VIKAS < ==

    =========================
    sqlplus vikas/vikas<>
    coil /oracle/vikas/output.txt
    WHENEVER SQLERROR EXIT; -Add this
    order 1
    command 2
    ...
    ....
    command N
    spool off;
    commit;
    END
    =========================

Maybe you are looking for