Solved

Parametric query in oracle

Posted on 2016-10-25
6
59 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
database upgrade 8 75
Oracle - SQL Parse String 5 34
How to Gracefuly recover in Racle stored procedure 1 30
Field name with special character (Ñ) in Oracle 11 50
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

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