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

x
?
Solved

Parametric query in oracle

Posted on 2016-10-25
6
Medium Priority
?
84 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
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 2000 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 5

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 77

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

636 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