PL/SQL Script for dates

Hello
I am new to Oracle and PL/SQL Script. I would like to ask - is it possible to write a PL/SQL Script to compare two dates in Oracle.
It is a very basic example of the structure of the table.

table_1)
client_name varchar2 (30);
client_expire_date DATE;
status varchar2 (30);
);

settings)
time_to_run varchar2 (30);
);

I want to write a script that runs each for example 5 minutes (I want the script to take time to time_to_run rank, each time when it starts) and compare the client_expire_date with the sysdate. If the client_expire_date < sysdate scripts updates the State of the locked client.
Is it possible to do?

concerning
CREATE TABLE clients
( client_serial       INTEGER CONSTRAINT clients_pk PRIMARY KEY
, client_expire_date  DATE
, client_name         VARCHAR2(30) NOT NULL
, status              VARCHAR2(20) DEFAULT 'active' NOT NULL );

INSERT INTO clients VALUES (1, TO_DATE('2010-08-21 15','YYYY-MM-DD HH24'), 'Largo', 'active');
INSERT INTO clients VALUES (2, TO_DATE('2010-08-21 16','YYYY-MM-DD HH24'), 'Blofeld', 'active');
INSERT INTO clients VALUES (3, TO_DATE('2010-08-21 17','YYYY-MM-DD HH24'), 'Scaramanga', 'active');
INSERT INTO clients VALUES (4, TO_DATE('2010-08-21 18','YYYY-MM-DD HH24'), 'Drax', 'active');
INSERT INTO clients VALUES (5, TO_DATE('2010-08-21 19','YYYY-MM-DD HH24'), 'Goldfinger', 'active');

COMMIT;

BEGIN
    DBMS_SCHEDULER.CREATE_JOB
    ( job_name        => 'SET_STATUS'
    , job_type        => 'PLSQL_BLOCK'
    , job_action      => 'BEGIN ' ||
                               'UPDATE clients SET status = ''locked'' ' ||
                               'WHERE client_expire_date < SYSDATE AND status != ''locked''; ' ||
                         'END;'
    , repeat_interval => 'FREQ=MINUTELY;INTERVAL=5'
    , enabled         => TRUE
    , auto_drop       => FALSE
    , comments        => 'Demo job to update CLIENTS table every 5 minutes');
END;
/

SELECT log_date, status, run_duration
FROM   user_scheduler_job_run_details
WHERE  job_name = 'SET_STATUS';

LOG_DATE                                          STATUS                         RUN_DURATION
------------------------------------------------- ------------------------------ -------------------
21-AUG-10 16.53.35.671000 +01:00                  SUCCEEDED                      +000 00:00:00
21-AUG-10 16.58.35.609000 +01:00                  SUCCEEDED                      +000 00:00:00

SELECT * FROM clients;

CLIENT_SERIAL CLIENT_EXPIRE_DATE CLIENT_NAME      STATUS
------------- ------------------ ---------------- --------------------
            1 2010-08-21 15:00:0 Largo            locked
            2 2010-08-21 16:00:0 Blofeld          locked
            3 2010-08-21 17:00:0 Scaramanga       active
            4 2010-08-21 18:00:0 Drax             active
            5 2010-08-21 19:00:0 Goldfinger       active

select to_char(sysdate,'YYYY-MM-DD HH24:MI:SS') as time_now from dual;

TIME_NOW
-------------------
2010-08-21 17:02:17

(Scaramanga account has been locked by the next run at 17:03)

You can call a procedure under the action of work rather than including the command line.

To abandon the work,

CALL DBMS_SCHEDULER.DROP_JOB('SET_STATUS');

Tags: Database

Similar Questions

  • How to write a script for date get to the Clipboard

    Hi experts,

    How to write a script for date get to the Clipboard.

    the date format will be like this:

    05 - may

    respect of

    John

    Thanks guys, thanks Sanon

    I finally use the .bat doc

    like this:

    @@echo off
    for /f "delims =" % in (' wmic OS Get localdatetime ^ | find ".") "") Set "dt = %% a"
    the value "YYYY = % dt: ~ 0, 4%.
    the value "MM = % dt: ~ 4, 2%.

    If MM % is 01 set MM = January
    If % MM == 02 set MM = February
    If MM % is MM value = March 03
    If MM % is 04 MM value = April
    If MM % is 05 MM value = may
    If MM % is 06 MM value = June
    If MM % == 07 set MM = July
    If MM % is MM value = August 08
    If MM % is MM value = September 09
    If MM % is 10 MM value = October
    If MM % is 11A set MM = November
    If MM % is game MM 12 = December

    the value "DD = % dt: ~ 6, 2%.
    the value "HH = % dt: ~ 8, 2%.
    the value "Min = % dt: ~ 10, 2%.
    Set "s = % dt: ~ 12, 2%.

    Echo DD - MM HH % %% % Min | Clip

    It works

    respect of

    John

  • SQLdeveloper export to Excel - how to hide the SQL worksheet for data export

    Export to Excel - how to hide SQL worksheet for data export?

    Hello

    SQL Developer forum is here: SQL Developer

    Frank

  • Scripts for data cleansing Perf

    Hello

    We currently use BB 4.4 with the new features enabled PERF. Someone at - it scripts for cleaning or truncate the Perf Data collected? 4.5 is supposed to have the function programmed in. We run the BB server on unix.

    Perf Data under the $BBHOME / bbvar / perf

    The files are in plain text with lines like this:

    Here is a not so elegant way to truncate your files in perl. You will need to set the time as a unix timestamp.

    #Purge $BBPERF
    @files = "grep - Rl. $BBPERF / *';"

    $file (@files) {} foreach
    chomp ($file);
    Print "$file\n";
    @data ='cat $file | AWK ' {if (\$4 > = $startTime) {print \$0}} ";
    If (!) () open (FILE, "> $file"))) {print "cannot open $file: $!"} \n » ; print LOGFILE "cannot open $file: $! \n » ; next ;}
    foreach $line (@data) {print FILE $line ;}
    Close the FILE;
    }

  • Insert the SQL script for BLOBs

    Hello

    I managed to upload my word and pdf documents in the database using blob support APEX and have these columns in the MY_DOC table

    , VARCHAR2 (400) FILE NAME NOT NULL
    BLOB_CONTENT BLOB
    , VARCHAR2 (48) MIME_TYPE

    Now I want to migrate the data to another environment by using an insert SQL script, how to respond to the BLOB column in the insert script?

    Kind regards

    ADE

    You cannot migrate data blob with insertion of scripts. However, you can achieve this by using the datapump...

    Example:

    expdp sysadm/sysadm tables = dumpfile = dump.dmp logfile = expdp_dump.log patterns =
    Impdp sysadm/sysadm dumpfile = dump.dmp

    BR,

    Nico

  • I couln can't find the dbmshptab.sql script for

    I would likle to use Profiler hierarchical. In order to use it, I need to run below script. However, I couln t find anywhere I searched the table object DBMS_HPROF package, there are in db. You have an idea how I can run this script? In this case there is not in my computer can I download it anywhere?

    Note: I am using windows 7

    @$ORACLE_HOME/rdbms/admin/dbmshptab.sql
    select * from all_objects where object_name = 'DBMS_HPROF';
    
    SYS     DBMS_HPROF          10581          PACKAGE BODY     01/11/2007     01/11/2007     2007-11-01:21:00:04     VALID     N
    SYS     DBMS_HPROF          7480          PACKAGE     01/11/2007     01/11/2007     2007-11-01:20:55:24     VALID     N
    PUBLIC     DBMS_HPROF          7481          SYNONYM     01/11/2007     01/11/2007     2007-11-01:20:55:24     VALID     N
    
    
    select * from v$version;
    
    Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production
    PL/SQL Release 11.1.0.6.0 - Production
    "CORE     11.1.0.6.0     Production"
    TNS for 64-bit Windows: Version 11.1.0.6.0 - Production
    NLSRTL Version 11.1.0.6.0 - Production
    Thanks for your help.
    Rem
    Rem $Header: dbmshptab.sql 30-jul-2007.13:07:41 sylin Exp $
    Rem
    Rem dbmshptab.sql
    Rem
    Rem Copyright (c) 2005, 2007, Oracle. All rights reserved.
    Rem
    Rem    NAME
    Rem      dbmshptab.sql - dbms hierarchical profiler table creation
    Rem
    Rem    DESCRIPTION
    Rem       Create tables for the dbms hierarchical profiler
    Rem
    Rem    NOTES
    Rem      The following tables are required to collect data:
    Rem        dbmshp_runs
    Rem          information on hierarchical profiler runs
    Rem
    Rem        dbmshp_function_info -
    Rem          information on each function profiled
    Rem
    Rem        dbmshp_parent_child_info -
    Rem          parent-child level profiler information
    Rem
    Rem      The dbmshp_runnumber sequence is used for generating unique
    Rem      run numbers.
    Rem
    Rem      The tables and sequence can be created in the schema for each user
    Rem      who wants to gather profiler data. Alternately these tables can be
    Rem      created in a central schema. In the latter case the user creating
    Rem      these objects is responsible for granting appropriate privileges
    Rem      (insert,update on the tables and select on the sequence) to all
    Rem      users who want to store data in the tables. Appropriate synonyms
    Rem      must also be created so the tables are visible from other user
    Rem      schemas.
    Rem
    Rem      THIS SCRIPT DELETES ALL EXISTING DATA!
    Rem
    Rem    MODIFIED   (MM/DD/YY)
    Rem    sylin       07/30/07 - Modify foreign key constraints with on delete
    Rem                           cascade clause
    Rem    kmuthukk    06/13/06 - fix comments
    Rem    sylin       03/15/05 - Created
    Rem
    
    drop table dbmshp_runs                     cascade constraints;
    drop table dbmshp_function_info            cascade constraints;
    drop table dbmshp_parent_child_info        cascade constraints;
    
    drop sequence dbmshp_runnumber;
    
    create table dbmshp_runs
    (
      runid               number primary key,  -- unique run identifier,
      run_timestamp       timestamp,
      total_elapsed_time  integer,
      run_comment         varchar2(2047)    -- user provided comment for this run
    );
    
    comment on table dbmshp_runs is
            'Run-specific information for the hierarchical profiler';
    
    create table dbmshp_function_info
    (
      runid                  number references dbmshp_runs on delete cascade,
      symbolid               number,               -- unique internally generated
                                                   -- symbol id for a run
      owner                  varchar2(32),         -- user who started run
      module                 varchar2(32),         -- module name
      type                   varchar2(32),         -- module type
      function               varchar2(4000),       -- function name
      line#                  number,               -- line number where function
                                                   -- defined in the module.
      hash                   raw(32) DEFAULT NULL, -- hash code of the method.
      -- name space/language info (such as PL/SQL, SQL)
      namespace              varchar2(32) DEFAULT NULL,
      -- total elapsed time in this symbol (including descendats)
      subtree_elapsed_time   integer DEFAULT NULL,
      -- self elapsed time in this symbol (not including descendants)
      function_elapsed_time  integer DEFAULT NULL,
      -- number of total calls to this symbol
      calls                  integer DEFAULT NULL,
      --
      primary key (runid, symbolid)
    );
    
    comment on table dbmshp_function_info is
            'Information about each function in a run';
    
    create table dbmshp_parent_child_info
    (
      runid                  number,       -- unique (generated) run identifier
      parentsymid            number,       -- unique parent symbol id for a run
      childsymid             number,       -- unique child symbol id for a run
      -- total elapsed time in this symbol (including descendats)
      subtree_elapsed_time   integer DEFAULT NULL,
      -- self elapsed time in this symbol (not including descendants)
      function_elapsed_time  integer DEFAULT NULL,
      -- number of calls from the parent
      calls                  integer DEFAULT NULL,
      --
      foreign key (runid, childsymid)
        references dbmshp_function_info(runid, symbolid) on delete cascade,
      foreign key (runid, parentsymid)
        references dbmshp_function_info(runid, symbolid) on delete cascade
    );
    
    comment on table dbmshp_parent_child_info is
            'Parent-child information from a profiler runs';
    
    create sequence dbmshp_runnumber start with 1 nocache;
    

    See you soon,.
    Manik.

  • SQL query for data of the quarter... Help, please

    Dear Experts,

    Please help with this query...

    I have data as below:

    YearQuarterMRRMORR %
    -------------------
    20121ST QUARTER10
    Q230
    Q350
    Q460
    20131ST QUARTER20
    Q230

    Now I need to caluclate MORR % as column values below

    for 2012 T2 MRR % = (Q2MRR-Q1MRR) / (30-10) Q1 * 100 / 10 * 100

    for 2012, T3 MRR % = (Q4MRR-Q3MRR) / Q2 * 100

    for 2012 T4 MRR % = (Q1MRR-Q4MRR) / Q1 * 100 here q1 2013 and the 4th quarter of 2012...

    like this he wll go...

    Please help with me query calculate values in the column % MRR.

    Best regards

    ASP.

    Maybe NOT TESTED! No database at your fingertips

    Select the year, quarter, mrr,.

    ratio_to_report (q_mrr) on pct_mrr (partition by order of the year by quarter)

    (select the year, quarter, mrr,)

    MRR - lag (mrr, 1, 0) on q_mrr (partition by order of the year by quarter)

    t

    )

    Concerning

    Etbin

  • SQL query for date/time

    Hi all

    I have a table that contains data like this


    Person_name Time_spent Date/time
    5.5 ABC 16-seven.-10 12:00
    ABC 3 16 - seven.-10 05:30
    ABC 2.5 17-seven.-10 12:00
    ABC.5 16 - seven.-10 09:00

    What I want is to get data like this

    Person_name Time_spent Date/time
    ABC 9 16 - seven.-10
    ABC 2.5 17-seven.-10

    which application should I run?
    Thanx
    OMy

    a query like this...

    Select sum (time_spent), person_name, trunc (date_time) of
    Person_name group, trunc (date_time);

    HTH.

  • can the run/run .sql script in another application?

    Hello..

    I have a problem to run my .sql script (for example: @c:\Users\test.sql) in my netbeans application. There is no problem when I want to run/run my sql query (for example: "select * from...").

    But when I want to run my script, no presentation of the result. my script content my query to export to the csv file.

    script: @c:\Users\test.sql

    in the script:

    coil c:\Users\test.csv;

    ' Select ' ' ' | LOT_NUMBER | « «, » » || DEVICENUMBER | « «, » » || DEVICENAME | « «, » » || STEP_NAME | « «, » » || INSERT_DATE | « «, » » || STATUS | « «, » » || DISPOSEDBY | « «, » » || ACCEPTBY | « «, » » || AVAILABLE | « «, » » || GENDISPOSITION | » » »

    of RF_LOT_ON_HOLD_LIST;

    spool off;

    Thank you.

    bunch

    Yes, we know what talking about your question.

    a .sql script is just a plain text file containing everything you want.  The code in it can be a pure SQL statements, it may contain of the PL/SQL code, and it can also include SQL * more specific orders (keep in mind SQL * more is a tool, not a language, so that these commands are specific to this tool and other tools which can also interpret the same commands).

    Not quite sure how your question relates to this blog, as this blog is showing the code that gets the data in JSON format in a jqGrid (jQuery grid?) and then exports the data in an Excel file.

    What you're trying to do is to take a series of SQL * Plus and SQL statements in a text file and generate a CSV file from them.

    As already mentioned, SPOOL is a SQL * more order, specific to SQL * more command line tool.  The database itself has no idea what "spool" means, while passing to the database somehow does not work.  I also doubt that netbeans knows what that means, either.

    So, in short, how you try to do, not you cannot.

  • 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.

  • Access 2003-&gt; Oracle 10 g: fixed error of migration, SQL script

    I recently migrated an Access 2003 to SQL using Oracle SQL developer Oracle database, and the process went well, with the exception of some migration errors.

    The errors I had have largely brought on 3 main groups:

    (i) "doesn't have to convert' errors:
    Failed to convert view Q345_Delete_NR > filename. Q345 - Delete NR: unexpected node in the AST: EXEC_PARAM_LIST: column of the row-1-1

    (II) 'limitation of translation mistakes:
    Limitation of the translation "cannot convert to display" met on view filename. Q326 - update column of NA column_name line 0 0

    (III) "parse errors":
    oracle.dbtools.migration.parser.ext.ExtStringTemplate.setValue(ExtStringTemplate.java:106)

    I know that these can be fixed, but I'm unable to understand what they mean. I would be grateful for some advice on what this means and how do I address to them.

    Finally, I would like to watch the complete SQL script for the database migrated - please someone let me know how to consider this in a single sheet? I would like to create a stored procedure from this one.

    Thank you.

    Published by: 803242 on October 19, 2010 16:23

    As you have an Oracle database you currently have support for this database and this is quite enough to save a service request for the SQL Migration Workbench developer.
    Saving a service request will allow us to examine the migration of more detailed SQL Dev and then we can improve the product.

    (The stored procedures, you need to manually extract the instructions in the script generated) that you intend to do) and then put it in a procedure.

  • Need a sql script loader to load data into a table

    Hello

    IM new to Oracle... Learn some basic things... and now I want the steps to do to load the data from a table dump file...

    and the script for sql loader

    Thanks in advance

    Hello

    You can do all these steps for loading data...

    Step 1:

    Create a table in Toad to load your data...

    Step 2:

    Creating a data file... Create your data file with column headers...

    Step 3:

    Creating a control file... Create your control file to load the data from the table data file (there is a structure of control file, you can search through the net)

    Step 4:

    Move the data file and the control file in the path of the server...

    Step 5:

    Load the data into the staging table using sql loader.

    sqlldr control = data =

    connect as: username/password@instance.

  • Single SQL query for the analysis of the date of customs declaration under the table of Stock codes

    Dear all,


    Please tell us a single SQL query for the below,

    We have a Table of Stock as shown below,

    STOCK_TABLE

     

    ITEM_CODE

    (item code)

    BAT_NO

    (lot no.)

    TXN_CODE

    (transaction code)

    DOC_NO

    (number)

    BOE_DT

    (date of the customs declaration)

    I1

    B1

    I1

    I2

    I3

    B70

    I4

    B80

    I5

    B90

    T102

    1234

    JULY 2, 2015

    I6

    B100

    We have to find the date of customs declaration (i.e. the date when the items have come under this particular table) for items that are not attached to any document (that is, who have TXN_CODE, DOC_NO and BOE_DT fields with a NULL value).

    For each item in the table of actions, which is not attached to any document, the customs declaration date is calculated as follows.

    1. If (code section, lot number) combination is present under HISTORY_TABLE, the date of customs declaration will receive the UPDT_DT, the transaction code (TXN_CODE) is an IN or transactions (which can be analyzed from the TRANSACTIONS table).

    2. If (code section, lot number) combination is NOT currently at the HISTORY_TABLE (or) the transaction code respective to item - batch number combination code is an operation then customs declaration date will be the date of the document (DOC_DT) that we receive from one of the 3 tables IN_TABLE_HEAD that contains the element of that particular lot.

  • If the case 1 and case 2 fails, our customs declaration date will be the last date of document (DOC_DT) that we receive from one of the 3 tables IN_TABLE_HEAD containing that particular item and the BAT_NO in expected results will be that corresponding to this document, as appropriate, to another NULL.

  • If the case 1 or case 2 is successful, the value of the last field (in the output expected, shown further below) BATCH_YN will be 'Y', because it fits the lot. Otherwise it will be 'n'.
  • HISTORY_TABLE

     

    ITEM_CODE

    BAT_NO

    TXN_CODE

    DOC_NO

    UPDT_DT

    I1

    B1

    T1

    1234

    JANUARY 3, 2015

    I1

    B20

    T20

    4567

    MARCH 3, 2015

    I1

    B30

    T30

    7890

    FEBRUARY 5, 2015

    I2

    B40

    T20

    1234

    JANUARY 1, 2015

    TRANSACTION

     

    TXN_CODE

    TXN_TYPE

    T1

    IN

    T20

    OFF

    T30

    ALL THE

    T50

    IN

    T80

    IN

    T90

    IN

    T60

    ALL THE

    T70

    ALL THE

    T40

    ALL THE

    IN_TABLE_HEAD_1

     

    H1_SYS_ID

    (primary key)

    TXN_CODE

    DOC_NO

    DOC_DATE

    H1ID1

    T1

    1234

    JANUARY 1, 2015

    H1ID2

    T70

    1234

    FEBRUARY 1, 2015

    IN_TABLE_ITEM_1

     

    I1_SYS_ID

    H1_SYS_ID

    (foreign key referencing H1_SYS_ID in IN_TABLE_HEAD_1)

    ITEM_CODE

    I1ID1

    H1ID1

    I1

    I1ID2

    H1ID1

    I100

    I1ID3

    H1ID2

    I3

    IN_TABLE_BATCH_1

     

    B1_SYS_ID

    TXN_CODE                DOC_NO

    (now in IN_TABLE_HEAD_1)

    BAT_NO

    B1ID1

    T1

    1234

    B1 / can be empty

    B1ID2

    T70

    1234

    B70

    IN_TABLE_HEAD_2

     

    H2_SYS_ID

    (primary key)

    TXN_CODE

    DOC_NO

    DOC_DATE

    H2ID1

    T30

    4567

    FEBRUARY 3, 2015

    H2ID2

    T60

    1234

    JANUARY 3, 2015

    IN_TABLE_ITEM_2

     

    I2_SYS_ID

    H2_SYS_ID

    (foreign key referencing H2_SYS_ID in IN_TABLE_HEAD_2)

    ITEM_CODE

    I2ID1

    H2ID1

    I1

    I2ID2

    H2ID1

    I200

    I2ID3

    H2ID2

    I2

    IN_TABLE_BATCH_2

     

    B2_SYS_ID

    I2_SYS_ID

    (foreign key referencing I2_SYS_ID in IN_TABLE_ITEM_2)

    BAT_NO

    B2ID1

    I2ID1

    B30 / null

    B2ID2

    I2ID2

    B90

    B2ID2

    I2ID3

    B60

    IN_TABLE_HEAD_3

     

    H3_SYS_ID

    (primary key)

    TXN_CODE

    DOC_NO

    DOC_DATE

    H3ID1

    T50

    1234

    JANUARY 2, 2015

    H3ID2

    T80

    1234

    JANUARY 3, 2015

    H3ID3

    T90

    1234

    JANUARY 4, 2015

    H3ID4

    T40

    1234

    AUGUST 5, 2015

    IN_TABLE_ITEM_3

     

    I3_SYS_ID

    H3_SYS_ID

    (foreign key referencing H3_SYS_ID in IN_TABLE_HEAD_3)

    ITEM_CODE

    BAT_NO

    I3ID1

    H31D1

    I2

    B50

    I3ID2

    H3ID2

    I4

    B40

    I3ID3

    H3ID3

    I4

    I3ID4

    H3ID4

    I6

    There is no IN_TABLE_BATCH_3

    Please find below the expected results.

    OUTPUT

     

    ITEM_CODE

    BAT_NO

    TXN_CODE

    DOC_NO

    BOE_DT

    BATCH_YN

    I1

    B1

    T1

    1234

    JANUARY 3, 2015

    THERE

    I1

    B30

    T30

    7890

    FEBRUARY 5, 2015

    N

    I2

    B60

    T60

    1234

    JANUARY 3, 2015

    N

    I3

    B70

    T70

    1234

    FEBRUARY 1, 2015

    THERE

    I4

    T90

    1234

    JANUARY 4, 2015

    N

    I6

    T40

    1234

    AUGUST 5, 2015

    N

    Controls database to create the tables above and insert the records.

    CREATE TABLE stock_table()item_code VARCHAR2()80),bat_no VARCHAR2()80),txn_code VARCHAR2()80),

    doc_no VARCHAR2 (80), boe_dt DATE );

    INSERT EN stock_table

       VALUES ('I1', 'B1', '', '', '');

    INSERT EN stock_table

       VALUES ('I1', '', '', '', '');

    INSERT IN stock_table

       VALUES ('I2', '', '', '', '');

    INSERT EN stock_table

       VALUES ('I3', 'B70', '', '', '');

    INSERT EN stock_table

       VALUES ('I4', 'B80', '', '', '');

    INSERT EN stock_table

       VALUES ('I5', 'B90', 'T102', '1234', '02-JUL-2015');

    INSERT EN stock_table

       VALUES ('I6', 'B100', '', '', '');

    SELECT *

    FROM stock_table




     

    CREATE TABLE history_table()item_code VARCHAR2()80),bat_no VARCHAR2()80),txn_code VARCHAR2()80),

    doc_no VARCHAR2 (80), updt_dt DATE );

    INSERT IN history_table

       VALUES ('I1', 'B1', 'T1', '1234', '03-JAN-2015');

    INSERT IN history_table

       VALUES ('I1', 'B20', 'T20', '4567', '03-MAR-2015');

    INSERT IN history_table

       VALUES ('I1', 'B30', 'T30', '7890', '05-FEB-2015');

    INSERT IN history_table

       VALUES ('I2', 'B40', 'T20', '1234', '01-JAN-2015');

    SELECT *

    FROM history_table




     

    CREATE TABLE transaction1()txn_code VARCHAR()80),txn_type VARCHAR()80));


    INSERT INTO transaction1

       VALUES ('T1', 'IN');


    INSERT INTO transaction1

       VALUES ('T20', 'OUT');

    INSERT INTO transaction1

       VALUES ('T30', 'ALL');

    INSERT INTO transaction1

       VALUES ('T40', 'ALL');

    INSERT INTO transaction1

       VALUES ('T50', 'IN');

    INSERT INTO transaction1

       VALUES ('T60', 'ALL');

    INSERT INTO transaction1

       VALUES ('T70', 'ALL');

    INSERT INTO transaction1

       VALUES ('T80', 'IN');

    INSERT INTO transaction1

       VALUES ('T90', 'IN');

    SELECT *

    FROM transaction1




     

    CREATE TABLE in_table_head_1()h1_sys_id VARCHAR2()80) PRIMARY KEY,txn_code VARCHAR2()80),

    doc_no VARCHAR2 (80), doc_dt DATE );

    CREATE TABLE in_table_head_2()h2_sys_id VARCHAR2()80) PRIMARY KEY,txn_code VARCHAR2()80),

    doc_no VARCHAR2 (80), doc_dt DATE );

    CREATE TABLE in_table_head_3()h3_sys_id VARCHAR2()80) PRIMARY KEY,txn_code VARCHAR2()80),

    doc_no VARCHAR2 (80), doc_dt DATE );

     

    INSERT IN in_table_head_1

       VALUES ('H1ID1', 'T1', '1234', '01-JAN-2015');

    INSERT IN in_table_head_1

       VALUES ('H1ID2', 'T70', '1234', '01-FEB-2015');

    INSERT IN in_table_head_2

       VALUES ('H2ID1', 'T30', '4567', '03-FEB-2015');

    INSERT IN in_table_head_2

       VALUES ('H2ID2', 'T60', '1234', '03-JAN-2015');

    INSERT IN in_table_head_3

       VALUES ('H3ID1', 'T50', '1234', '02-JAN-2015');

    INSERT IN in_table_head_3

       VALUES ('H3ID2', 'T80', '1234', '03-JAN-2015');

    INSERT IN in_table_head_3

       VALUES ('H3ID3', 'T90', '1234', '05-JAN-2015');

    INSERT IN in_table_head_3

       VALUES ('H3ID4', 'T40', '1234', '05-AUG-2015');




     

    CREATE TABLE in_table_item_1()i1_sys_id VARCHAR2()80) PRIMARY KEY,

    h1_sys_id VARCHAR2 (80) REFERENCES in_table_head_1()h1_sys_id),item_code VARCHAR2()80));

    CREATE TABLE in_table_item_2()i2_sys_id VARCHAR2()80) PRIMARY KEY,

    h2_sys_id VARCHAR2 (80) REFERENCES in_table_head_2()h2_sys_id),item_code VARCHAR2()80));

    CREATE TABLE in_table_item_3(i3_sys_id VARCHAR2(80) PRIMARY KEY,

    h3_sys_id VARCHAR2 (80) REFERENCES in_table_head_3()h3_sys_id),item_code VARCHAR2()80),

    bat_no VARCHAR2 (80));

     

    INSERT IN in_table_item_1

       VALUES ('I1ID1', 'H1ID1', 'I1');

    INSERT IN in_table_item_1

       VALUES ('I1ID2', 'H1ID1', 'I100');

    INSERT IN in_table_item_1

       VALUES ('I1ID3', 'H1ID2', 'I3');

    INSERT IN in_table_item_2

       VALUES ('I2ID1', 'H2ID1', 'I1');

    INSERT IN in_table_item_2

       VALUES ('I2ID2', 'H2ID1', 'I200');

    INSERT IN in_table_item_2

       VALUES ('I2ID3', 'H2ID2', 'I2');

    INSERT IN in_table_item_3

       VALUES ('I3ID1', 'H3ID1', 'I2','B50');

    INSERT IN in_table_item_3

       VALUES ('I3ID2', 'H3ID2', 'I4','B40');

    INSERT IN in_table_item_3

       VALUES ('I3ID3', 'H3ID3', 'I4','');

    INSERT IN in_table_item_3

       VALUES ('I3ID4', 'H3ID4', 'I6','');

    SELECT *

    FROM in_table_item_1

    SELECT *

    FROM in_table_item_2

    SELECT *

    FROM in_table_item_3




     

    CREATE TABLE in_table_batch_1()b1_sys_id VARCHAR2()80) PRIMARY KEY,

    txn_code VARCHAR2 (80), doc_no VARCHAR2 (80), bat_no VARCHAR2 (80));

    CREATE TABLE in_table_batch_2()b2_sys_id VARCHAR2()80) PRIMARY KEY,

    i2_sys_id VARCHAR2 (80) REFERENCES in_table_item_2()i2_sys_id),bat_no VARCHAR2()80));

     

    INSERT IN in_table_batch_1

       VALUES ('B1ID1', 'T1', '1234', 'B1');

    INSERT IN in_table_batch_1

       VALUES ('B1ID2', 'T70', '1234', 'B70');

    INSERT IN in_table_batch_2

       VALUES ('B2ID1', 'I2ID1', 'B30');

    INSERT IN in_table_batch_2

       VALUES ('B2ID2', 'I2ID2', 'B90');

    INSERT IN in_table_batch_2

       VALUES ('B2ID3', 'I2ID3', 'B60');

    Please advise a solution for the same.

    Thank you and best regards,

    Séverine Suresh

    very forced (question subfactoring used to allow easy testing/verification - could work with these test data only)

    with

    case_1 as

    (select s.item_code,

    s.bat_no,

    h.txn_code,

    h.doc_no,

    h.updt_dt boe_dt,

    cases where s.bat_no = h.bat_no then 'Y' else ' n end batch_yn.

    cases where h.txn_code is not null

    and h.doc_no is not null

    and h.updt_dt is not null

    then 'case 1' '.

    end refers_to

    from (select item_code, bat_no, txn_code, doc_no, boe_dt

    of w_stock_table

    where bat_no is null

    or txn_code is null

    or doc_no is null

    or boe_dt is null

    ) s

    left outer join

    w_history_table h

    On s.item_code = h.item_code

    and s.bat_no = h.bat_no

    and exists (select null

    of w_transaction1

    where txn_code = nvl (s.txn_code, h.txn_code)

    and txn_type in ('IN', 'ALL')

    )

    ),

    case_2 as

    (select s.item_code,

    NVL (s.bat_no, h.bat_no) bat_no.

    NVL (s.txn_code, h.txn_code) txn_code.

    NVL (s.doc_no, h.doc_no) doc_no.

    NVL (s.boe_dt, h.updt_dt) updt_dt.

    cases where s.bat_no = h.bat_no then 'Y' else ' n end batch_yn.

    cases where h.txn_code is not null

    and h.doc_no is not null

    and h.updt_dt is not null

    then 'case 2'.

    end refers_to

    from (select item_code, bat_no, txn_code, doc_no, boe_dt

    of case_1

    where refers_to is null

    ) s

    left outer join

    w_history_table h

    On s.item_code = h.item_code

    and exists (select null

    of w_transaction1

    where txn_code = nvl (s.txn_code, h.txn_code)

    and txn_type in ('IN', 'ALL')

    )

    and not exists (select null

    of case_1

    where item_code = h.item_code

    and bat_no = h.bat_no

    and txn_code = h.txn_code

    and doc_no = h.doc_no

    and updt_dt = h.updt_dt

    )

    ),

    case_31 as

    (select s1.item_code,

    NVL (S1.bat_no, W1.bat_no) bat_no.

    NVL (S1.txn_code, W1.txn_code) txn_code.

    NVL (S1.doc_no, W1.doc_no) doc_no.

    NVL (S1.updt_dt, W1.doc_dt) updt_dt.

    cases where s1.bat_no = w1.bat_no then 'Y' else ' n end batch_yn.

    cases where w1.txn_code is not null

    and w1.doc_no is not null

    and w1.doc_dt is not null

    then "case 31'.

    end refers_to

    from (select item_code, bat_no, txn_code, doc_no, updt_dt, batch_yn, refers_to

    of case_2

    where refers_to is null

    ) s1

    left outer join

    (select i1.item_code, h1.txn_code, h1.doc_no, h1.doc_dt, b1.bat_no

    of w_in_table_item_1 i1

    inner join

    w_in_table_head_1 h1

    On i1.h1_sys_id = h1.h1_sys_id

    inner join

    w_in_table_batch_1 b1

    On h1.txn_code = b1.txn_code

    and h1.doc_no = b1.doc_no

    ) w1

    On s1.item_code = w1.item_code

    ),

    case_32 as

    (select s2.item_code,

    NVL (S2.bat_no, W2.bat_no) bat_no.

    NVL (S2.txn_code, W2.txn_code) txn_code.

    NVL (S2.doc_no, W2.doc_no) doc_no.

    NVL (S2.updt_dt, W2.doc_dt) updt_dt.

    cases where s2.bat_no = w2.bat_no then 'Y' else ' n end batch_yn.

    cases where w2.txn_code is not null

    and w2.doc_no is not null

    and w2.doc_dt is not null

    then "case 32'.

    end refers_to

    from (select item_code, bat_no, txn_code, doc_no, updt_dt, batch_yn, refers_to

    of case_2

    where refers_to is null

    ) s2

    left outer join

    (select i2.item_code, h2.txn_code, h2.doc_no, h2.doc_dt, b2.bat_no

    of w_in_table_item_2 i2

    inner join

    w_in_table_head_2 h2

    On i2.h2_sys_id = h2.h2_sys_id

    inner join

    w_in_table_batch_2 b2

    On i2.i2_sys_id = b2.i2_sys_id

    ) w2

    On s2.item_code = w2.item_code

    ),

    case_33 as

    (select s3.item_code,

    w3.bat_no,

    NVL (S3.txn_code, w3.txn_code) txn_code.

    NVL (S3.doc_no, w3.doc_no) doc_no.

    NVL (S3.updt_dt, w3.doc_dt) updt_dt.

    cases where s3.bat_no = w3.bat_no then 'Y' else ' n end batch_yn.

    cases where w3.txn_code is not null

    and w3.doc_no is not null

    and w3.doc_dt is not null

    then "case 33'.

    end refers_to

    from (select item_code, bat_no, txn_code, doc_no, updt_dt, batch_yn, refers_to

    of case_2

    where refers_to is null

    ) s3

    left outer join

    (select i3.item_code, h3.txn_code, h3.doc_no, h3.doc_dt, i3.bat_no

    of w_in_table_item_3 i3

    inner join

    w_in_table_head_3 h3

    On i3.h3_sys_id = h3.h3_sys_id

    ) w3

    On s3.item_code = w3.item_code

    )

    Select item_code, bat_no, txn_code, doc_no, boe_dt, batch_yn

    of case_1

    where refers_to is not null

    Union of all the

    Select item_code, bat_no, txn_code, doc_no, updt_dt, batch_yn

    of case_2

    where refers_to is not null

    Union of all the

    Select item_code, bat_no, txn_code, doc_no, updt_dt, batch_yn

    from (select item_code, bat_no, txn_code, doc_no, updt_dt, batch_yn,

    ROW_NUMBER() over (partition by item_code of updt_dt desc order) rn

    from (select item_code, bat_no, txn_code, doc_no, updt_dt, batch_yn

    of case_31

    where refers_to is not null

    Union of all the

    Select item_code, bat_no, txn_code, doc_no, updt_dt, batch_yn

    of case_32

    where refers_to is not null

    Union of all the

    Select item_code, bat_no, txn_code, doc_no, updt_dt, batch_yn

    of case_33

    where refers_to is not null

    )

    )

    where rn = 1

    ITEM_CODE BAT_NO TXN_CODE DOC_NO BOE_DT BATCH_YN
    I1 B1 T1 1234 JANUARY 3, 2015 THERE
    I1 B30 T30 7890 FEBRUARY 5, 2015 N
    I2 B60 T60 1234 JANUARY 3, 2015 N
    I3 B70 T70 1234 FEBRUARY 1, 2015 THERE
    I4 - T90 1234 JANUARY 5, 2015 N
    I6 - T40 1234 AUGUST 5, 2015 N

    Concerning

    Etbin

  • Where the SQL Scripts running after the installation of the ODAC for Oracle Client?

    I just installed ODAC for Oracle Client on a windows server that runs a database of sql server, allowing developers to connect to a web application to an Oracle/Unix database. The installation States to run SQL scripts, located in the $ORACLE_HOME\ASP.NET\SQL directory after installation. I don't know where to run them? On the ORACLE DB on the Unix server? If someone could let me know, I'd appreciate it. Thank you.

    You must run on the Oracle database server, but they are necessary only if you use Oracle for ASP.NET providers.

    If you just install the software, so you can use OLEDB with SQL Server linked server, for example, you don't need them. What do you need to run although if this is the case is the oramtsadmin.sql script that creates the objects used by the Oracle MTS Recovery Service (if you do not have these objects created on the database) that SQL Server uses a distributed transaction for Oracle connections I think.

    It will be useful,
    Greg

  • 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

Maybe you are looking for

  • I forgot my security answer

    Hello My account is *. Now, I forgot my security answer. I have send an email and you answered me. But I can't have more of your help to get new security answer, because he asked that I have the answer 2 answer to security question. Please, let me kn

  • HP COMPAQ 6200 PRO SFF PC: Bus PCI 0, device 31, function 3 DRIVER

    I HAVE A PC HP COMPAQ 6200 PRO SMALL FORM FACTOR I KEEP GETTING MESSAGE ' CAN FIND DRIVER FOR CONTROLLER of BUS SM THE DRIVER IS: PCI bus 0, device 31, function 3 WHERE CAN GET THIS DRIVER? Thank you

  • Pavilion p7-1000 bios reset

    I am trying to boot from a cd, but when I go to boot at startup options, it asks for a password. I removed the CMOS of the day battery overnight and that reset the clock, but he always asks for a password. I can't because of the password of the bios

  • Satellite Pro 6000 tweets in total at startup

    Im just of the trouble getting an old notebook running. The laptop is not even the bios post, I get a long beep followed a short, being 2 beeps in total. Doesnt post bios or windows, just get these 2 beeps and a black screen.Any suggestions or ideas

  • What memory can I add to Satellite P35-S6292?

    Hello! I have Toshiba Satellite P35-S6292 with 512 MB of memory (Samsung M4 70L6524BT0-CB3, PC2700, 166 MHz). I want lots of update of the memory modules (I want to buy 2 or 2 * 512 * 1024). My North Bridge chipset: ATI RS300 + South Bridge: ATI SB20