Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Query

Posted on 2007-03-23
4
Medium Priority
?
1,301 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
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 explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

618 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