It’s hard to know why you’re getting that without seeing the whole script, but my guess is you’re trying to use it outside the current block. Here’s some
recommendations…
-
Make sure your variables are used within the master begin/end
-
Define your variables in your declare section
-
Put your select statements in begin/end clauses - that way you can add exceptions to them as needed and control your data better.
-
Use
*dbms_output.put_line *statements to see your output - this will show you the values of your variables along the way.
-
Include exception clauses in your blocks and at the end of your anonymous block to capture errors.
Also, know that unless your query your building returns only one row, then you’re going to run into an error of fetching too many rows, unless you handle
it - again, a good use of exceptions.
Here’s an example of the things I mentioned above…
declare
start_dt
date
:=
‘07-SEP-1981’;
end_dt
date
:=
‘09-SEP_1981’;
emp_no
number;
begin
dbms_output.put_line( 'start_dt:
'||start_dt);
begin
select
e.empno
into emp_no
from scott.emp
e
where
e.hiredate
between start_dt
and end_dt;
exception
when
no_data_found
then
dbms_output.put_line( ‘No
Data Found between dates’);
end;
dbms_output.put_line( 'emp_no:
'||emp_no);
exception
when
others
then
dbms_output.put_line( 'SQLERRM:
'||SQLERRM);
end;
From: Mark J [mailto:bounce-Mark_J@toadworld.com]
Sent: Wednesday, January 28, 2015 1:42 PM
To: toadoracle@toadworld.com
Subject: [Toad for Oracle - Discussion Forum] Define vs declare date varaible in Toad for Oracle 9.0.1.8 using sql
Define vs declare date varaible in Toad for Oracle 9.0.1.8 using sql
Thread created by Mark J
I am relatively new to Oracle - having used Sql server for a few years
I am trying to create a date varaible in a script that will use it in multiple statements later in the script.
This declare statement seems to work when I execute it as a script:
declare
start_dt date;
begin
select
to_date(‘01-Jun-2015’,‘DD-MON-YY’)
into start_dt from dual;
end;
but when I try to join to it later in the follwong where clause:
and mytable.My_START_DATE
between (start_dt)
and (‘30-Jun-2015’)
I get a message “ORA-00904: “START_DT”: invalid identifier”
Nay help would be appreciated.
Thanks
To reply, please reply-all to this email.
Stop receiving emails on this subject.
Or
Unsubscribe from Toad for Oracle - General notifications altogether.
Toad for Oracle - Discussion Forum
Flag
this post as spam/abuse.