?
Solved

Parametric query in oracle

Posted on 2016-10-25
6
Medium Priority
?
81 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
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…
Suggested Courses

777 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