Solved

SQL Query

Posted on 2007-03-23
4
1,294 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 76

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

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now