Hi amankhan,
Do you want to pass multiple parameters? is the number of parameters fixed or varying?
Main Topics
Browse All TopicsHi Guys,
I am writing a sql script for which i want to pass parameters and run that sql script. For ex:
select * from dept where deptno in (10,20,30,40);
i want to pass 10,20,30,40 as parameters to the sql script that is written. How can i write the query and pass the parameters to the above query.
Help Appreciated.
Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
The above solution will work but the user will have to enter the string as 10,20,40,40. If you want the user to enter 4 values with some prompt being shown to them , you can use the accept command as
accept dept1 prompt 'Enter Unit 1 : '
accept dept2 prompt 'Enter Unit 2 : '
accept dept3 prompt 'Enter Unit 3 : '
accept dept4 prompt 'Enter Unit 4 : '
accept dept5 prompt 'Enter Unit 5 : '
select *
from dept
where deptno = '&dept1'
UNION ALL
select *
from dept
where deptno = '&dept2'
UNION ALL
select *
from dept
where deptno = '&dept3'
UNION ALL
select *
from dept
where deptno = '&dept4'
UNION ALL
select *
from dept
where deptno = '&dept5';
Actually its very simple..
and you dont have to interactively pass the values.
Just say,
@test.sql parameter1, parameter2, parameter3.
You have to refer parameter1, parameter2, parameter3 as &1, &2, &3.
For example write the sql as following:
test.sql
select * from emp
where emp_id between &1 AND &3
and emp_id < &2;
Now to pass the parameters just do following
SQLPLUS> @test.sql 12345 1000 12500
Good luck !!
Rgds,
HR
Following covers a comprehensive discussion on the topic of passing parameters
http://www.oracle.com/tech
Rgds,
HR
Accept one parameter, and pass the full string :
SQL> select * from dept where deptno in (&1);
Enter value for 1: 10,20,30,40
old 1: select * from dept where deptno in (&1)
new 1: select * from dept where deptno in (10,20,30,40)
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL>
I am sure there are plenty of ways of passing parameters, apart from many that have already been discussed here.
If you want to use the parameters as input to 'IN' clause, then the solution given by actonwang would work very well.
It would be useful for all of us, if you can share how exactly you are using the parameters inside the script.
Rgds,
HR
Hi Guys,
Thanks for the help provided by all of you. For ex:- the parameter values are stored in a file. How can we pass the values of that file as parameters to the script.
My parameter values change and they are very huge in number. So am storing them in a file and want to pass the values from the file one by one.
Is there a way it be done. Help Appreciated
for ex:--
deptid.txt --- File name
10
20
30
40
50
60
70
80
90
100
110 --- are the values in the file that should be passed to the query. This list can vary from time to time.
You can use UTL_FILE.get_line function to read the data from file and pass it to the sqlscript
Prerequisite:
1)You must place your input file into one of the directories listed againset parameter UTL_FILE_DIR. You can see this parameter by issuing following command on sqlplus
select value from v$parameter where name = 'utl_file_dir';
declare
file_handle utl_file.file_type;
inputparameter varchar2(1000);
begin
file_hande := utl_file.fopen (path, filename, 'R'); -- Open the file
--here path is directory e.g /home/user, and filename is the name of
--file that contains parameters
WHILE TRUE
LOOP
utl_file.get_line (file_hande, inputparameter); -- read the first line
@test.sql inputparameter;
-- here instead of calling test.sql, you can write the select statement directly. e.g select * from my_table where id = inputparameter;
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND
UTL_FILE.fclose(file_handl
WHEN OTHERS
UTL_FILE.fclose(file_handl
END;
Rgds,
HR
hey harish,
thanks for the idea u have given. Its good to implement using utl_file i think.
Guys, i need some more help.
I am selecting one value using select by passing a parameter to that select statement which are stored in a file using utl_file package.
Now, I want to use the output of that query and use that value to update a column in the same table.
for ex:-
select comm from emp where empno = &empno1;
update emp
set comm = comm + 10
where empno = &empno1
Help Appreciated
Thanks
Your example is to simplistic, but I got your point.
You can capture the value in a variable and then use that variable to update the column.
Eg
--Store the value in a variable.
declare
v_comm varchar2(2000)
begin
Select comm into v_comm from emp where empno = &empno1;
--Now you can update the column in the table
update emp
set comm = v_comm
where emp_no = &emp_no;
End;
Rgds,
HR
Business Accounts
Answer for Membership
by: ploubierPosted on 2006-04-03 at 21:39:04ID: 16367637
select * from dept where deptno in (&1);