?
Solved

SQL Query

Posted on 2007-03-23
4
Medium Priority
?
1,300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 22

Accepted Solution

by:
Ivo Stoykov earned 320 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 180 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
Suggested Courses

752 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