Solved

Parametric query in oracle

Posted on 2016-10-25
6
37 Views
Last Modified: 2016-10-30
Hi,


 I would like to parameterize a query in Oracle. I have setup as the following but that doesn't seem to be working.


DECLARE paramQtr varchar(6)
SET paramQtr := '2011Q1'


select S.dept_ID, S.dept_name, s.actualsales, s.quarter, s.expectedsales

from (select distinct f.*, t.quarter

from yearly_sales f left join smry_expectations t

on f.dept_id = t.dept_id where Quarter = :paramQtr) S

Please help. Thanks
0
Comment
Question by:LuckyLucks
  • 2
  • 2
  • 2
6 Comments
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 41858603
If you are using sqlplus or some tool that recognizes the syntax (use varchar2 not varchar):

VAR paramQtr varchar2(6)
exec :paramQtr := '2011Q1'


select S.dept_ID, S.dept_name, s.actualsales, s.quarter, s.expectedsales
from (select distinct f.*, t.quarter
from yearly_sales f left join smry_expectations t
on f.dept_id = t.dept_id where Quarter = :paramQtr) S
0
 
LVL 4

Expert Comment

by:Abhimanyu Suri
ID: 41858620
or, as alternate you can do

cat test.sql

define abc = &1;
define def = &2;

declare
numbero number := &abc;
cgara varchar2(20) := '&def';
cou number;
chara varchar2(200);
begin
select numbero,cgara into cou,chara from dual;
dbms_output.put_line(cou||','||chara);
end;
/

sql > test.sql 1 name
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 41858642
>>or, as alternate you can do

No need for most of that.  Also no need for the PL/SQL code block.

If you want to go that route just do the select:
select &1,'&2' from dual;
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 4

Expert Comment

by:Abhimanyu Suri
ID: 41859007
PL/SQL example is a mundane  block and primarily an example

I have personally found it very useful when arguments need to be passed on command line and from the top part of the question it appears user is trying to achieve the same

DECLARE paramQtr varchar(6)
SET paramQtr := '2011Q1'
0
 

Author Comment

by:LuckyLucks
ID: 41865873
thx all
0
 

Author Comment

by:LuckyLucks
ID: 41865874
thanks all
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
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 copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to recover a database from a user managed backup

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now