Solved

SQL Query

Posted on 2007-03-23
4
1,297 Views
Last Modified: 2008-02-01
Can you please tell me what's wrong with this SQL? TIA.
 
    accept startDate prompt "Input start date [eg. 5/Mar/2007]:  "
    accept endDate prompt "Input end date [eg. 18/Mar/2007]:  "
    Declare
      stDt DATE := TO_DATE('&startDate', 'DD-MON-RR');
      enDt DATE := TO_DATE('&endDate', 'DD-MON-RR');
    Begin
    if (stDt < enDt) then
      select count(*) from TBORDER_ACTION where action_type = 'CH' and ctdb_cre_datetime > stDt  and ctdb_cre_datetime < enDt;
    end if;
    End;
/

The select statement is executed whether I enter 5-Mar-2007 as the start date and 22-Mar-2007 as the end date, or the other way around?

Cheers!
V.
0
Comment
Question by:Nakuru1234
4 Comments
 
LVL 22

Accepted Solution

by:
Ivo Stoykov earned 80 total points
ID: 18779712
Hello Nakuru1234

you must delcare anothe variable where to store count i.e.

Declare
      stDt DATE := TO_DATE('&startDate', 'DD-MON-RR');
      enDt DATE := TO_DATE('&endDate', 'DD-MON-RR');
      nums integer;
    Begin
...
and select should be
select count(*) into nums from TBORDER_ACTION where action_type = 'CH' and ctdb_cre_datetime > stDt  and ctdb_cre_datetime < enDt;

and better use between
select count(*) into nums from TBORDER_ACTION where action_type = 'CH' and ctdb_cre_datetime between stDt  and enDt;

HTH

!i!

0
 
LVL 14

Assisted Solution

by:GGuzdziol
GGuzdziol earned 45 total points
ID: 18779749
there should be INTO clause in select statement hence it's inside PL/SQL block.

    accept startDate prompt "Input start date [eg. 5/Mar/2007]:  "
    accept endDate prompt "Input end date [eg. 18/Mar/2007]:  "
    Declare
      stDt DATE := TO_DATE('&startDate', 'DD-MON-RR');
      enDt DATE := TO_DATE('&endDate', 'DD-MON-RR');
      result NUMBER := -1;
    Begin
    if (stDt < enDt) then
      select count(*) into result from TBORDER_ACTION where action_type = 'CH' and ctdb_cre_datetime > stDt  and ctdb_cre_datetime < enDt;
    end if;
    dbms_output.put_line(to_char(result));
    End;
    /
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 18781411
I agree that the INTO is missing but if I understand what you're saying:  the 'if' statement isn't working?

If this is what you asking:  What version of Oracle are you on? please include all 4 numbers.  ex./ 10.2.0.3

The 'if' statement works for me with 10.2.0.3.
0
 

Author Comment

by:Nakuru1234
ID: 18781911
Thank you for the help.

Cheers!
V.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question