Solved

Parametric query in oracle

Posted on 2016-10-25
6
62 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 77

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 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
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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to free up undo space? 3 50
exp/imp 25 87
Problem with duplicate records in Oracle query 16 40
Oracle DBLINKS From 11g to 8i 3 48
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
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…
This video shows how to recover a database from a user managed backup

828 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