0

I just switched from SQL Server to Oracle and I'm confused about how to get results when I'm using a variable who's value isn't set based on a result from sql query. What on earth is wrong with this?

variable firstDate date; 
variable secondDate date;
exec firstDate := '03/01/2019';
exec secondDate := '03/31/2019';

select 
   c.xent_id  
FROM 
   lic c 
WHERE 
   C.EXPR_DTE >= TO_DATE(firstDate,'MM/DD/YYYY') 
   AND C.EXPR_DTE <= TO_DATE(secondDate,'MM/DD/YYYY') 
   AND c.clnt_cde = 8801

1 Answer 1

1

You cannot define a DATE bind variable in SQL* Plus / SQL developer. It maybe defined as a VARCHAR2 and assigned a string and converted where needed. Also, note that you are missing colons at appropriate places before the bind variables. Check the query to see how it's used. You may put the same expressions in your where clause of the actual query.

variable firstDate VARCHAR2; 
variable secondDate VARCHAR2;
exec :firstDate := '03/01/2019';
exec :secondDate := '03/31/2019';


select TO_DATE(:firstDate,'MM/DD/YYYY') dt1 , TO_DATE(:secondDate,'MM/DD/YYYY')
 as dt2 from dual;

DT1      DT2     
-------- --------
01-03-19 31-03-19
Sign up to request clarification or add additional context in comments.

6 Comments

I need to use those variables in the following statement: select c.xent_id FROM lic c WHERE C.EXPR_DTE >= TO_DATE(firstDate,'MM/DD/YYYY') AND C.EXPR_DTE <= TO_DATE(secondDate,'MM/DD/YYYY') AND c.clnt_cde = 8801 ;
@Clos : Go ahead and use it , what's stopping you?
when i try and use it i get an error report: Error report - ORA-06550: line 2, column 5: PLS-00428: an INTO clause is expected in this SELECT statement ORA-06550: line 5, column 76: PL/SQL: ORA-00904: "DT2": invalid identifier ORA-06550: line 5, column 5: PL/SQL: SQL Statement ignored 06550. 00000 - "line %s, column %s:\n%s" *Cause: Usually a PL/SQL compilation error.
@Clos : You didn't tell us that you are using that SQL statement in a procedure / PL/SQL block. I answered it in SQL context and if you want to use the same in a PL/SQL scope you need a variable to hold the selected column, i.e. xent_id
like i mentioned above...I'm very new to Oracle, I've been reading, but not finding what I need. I have been able to declare variables but not use them in my where statement. If you look at the very top where I posted, that is what I need to do, I just don't know how to do it correctly. I cannot make a stored procedure or a function (long story) I need to be able to execute the script and get the results.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.