Home » SQL & PL/SQL » SQL & PL/SQL » Want to use date variable value in select query (11g)
Want to use date variable value in select query [message #605763] Tue, 14 January 2014 07:44 Go to next message
cplusplus1
Messages: 58
Registered: October 2012
Location: usa
Member
I want to run a select query passing date value via declared variable.

I have created the below query and decalrade date var and trying to use.


declare v_Date1 date := to_date('01-JAN-2013', 'DD-Mon-YYYY');

begin

select 'MPEMP' as metric_code, sum(metric_amt) as METRIC_AMT from FACT_PIL_MET
INNER JOIN DT_METR
ON FACT_PIL_MET.METRIC_ID = DT_METR.METRIC_ID
INNER JOIN DT_CALENDAR_DAY
ON FACT_PIL_MET.METRIC_DAY_ID = DT_CALENDAR_DAY.CALENDAR_DAY_ID
where DT_METR.metric_code = 'MPEMP' and DT_CALENDAR_DAY.CALENDAR_DAY = v_Date1;

end;


I need to run almost 39 queries using same variable date value, for that reason trying to declare var and use it with in select queries with union all.


Thank you very much for the helpful info.
Re: Want to use date variable value in select query [message #605768 is a reply to message #605763] Tue, 14 January 2014 07:55 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
You've got some code that looks like it ought to work, so what exactly is the problem?
Re: Want to use date variable value in select query [message #605769 is a reply to message #605768] Tue, 14 January 2014 07:58 Go to previous messageGo to next message
cplusplus1
Messages: 58
Registered: October 2012
Location: usa
Member
When i run the complete query in the sql developer, i am getting following error:
Error starting at line 3 in command:
Error report:
ORA-06550: line 3, column 1:
PLS-00428: an INTO clause is expected in this SELECT statement
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
Re: Want to use date variable value in select query [message #605771 is a reply to message #605769] Tue, 14 January 2014 08:09 Go to previous messageGo to next message
joy_division
Messages: 4963
Registered: February 2005
Location: East Coast USA
Senior Member
Yes, you need an INTO clause when you use PL/SQL (as opposed to using aliases for the columns).
Just check the documentation.

I, like cookiemonster, didn't even notice the BEGIN and END as at least I was focused on the DATEs, which are ok by the way.

[Updated on: Tue, 14 January 2014 08:10]

Report message to a moderator

Re: Want to use date variable value in select query [message #605773 is a reply to message #605771] Tue, 14 January 2014 08:19 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
are you trying to use a variable in a sqlplus script?
Re: Want to use date variable value in select query [message #605779 is a reply to message #605773] Tue, 14 January 2014 08:43 Go to previous messageGo to next message
cplusplus1
Messages: 58
Registered: October 2012
Location: usa
Member
Yes, trying to use a date based variable within sql select query, this is my first time trying to use. What is the proper way to run the query getting variable date value in where condition.

Thanks a lot for the helpful info.
Re: Want to use date variable value in select query [message #605780 is a reply to message #605779] Tue, 14 January 2014 08:47 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/
Re: Want to use date variable value in select query [message #605786 is a reply to message #605779] Tue, 14 January 2014 09:44 Go to previous message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
cplusplus1 wrote on Tue, 14 January 2014 14:43
Yes, trying to use a date based variable within sql select query, this is my first time trying to use. What is the proper way to run the query getting variable date value in where condition.

Thanks a lot for the helpful info.


you realise that doesn't answer my question?

You can run sql queries from various different tools/languages, how you use variables in the query depends on the tool/language being used. Your example above is PL/SQL, if that's what you want then the variable is correct but you need to tell it what to do with the result.
Previous Topic: ORA-01481: invalid number format model
Next Topic: SQL to generate sequential numbers for each repeating row and reset upon new key
Goto Forum:
  


Current Time: Thu Apr 18 23:15:12 CDT 2024