Question

How can you pass parameters to a sql script....

Asked by: amankhan

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

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2006-04-03 at 21:05:00ID21800441
Tags

sql

,

parameter

,

script

,

passing

Topic

Oracle Database

Participating Experts
5
Points
500
Comments
16

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

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.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

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.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

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.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Reports: Break on Deptno
    I am using reports ver 6i. From the emp relation when i build up a group above report, it shows department no and the its hild record. The problem is when the records increases the out put should like this.... For each deptno if it appears at the bottom, it's child moves to...
  2. Fetching max sal & deptno
    How do we fetch max sal & deptno for each deptno without using group by or rank/dense_rank with partition by.
  3. SQL query
    Hello experts, i have problem in SQL Query. i want to display for each deptt the following result Deptno No.Of Managers No of Clerks How do i write query for this.
  4. Group Not Working of Dept
    Hi, The result is right, not group by deptno or dname. SELECT e.ename,d.deptno,d.dname FROM emp e, dept d WHERE e.deptno = d.deptno; GROUP BY d.dname; Output ======================= ENAME DEPTNO DNAME ---------- ---------- -------------- KING 10 ACCOUNTING BLAKE...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

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.

Join the Community

Answers

 

by: ploubierPosted on 2006-04-03 at 21:39:04ID: 16367637

select * from dept where deptno in (&1);

 

by: riazpkPosted on 2006-04-04 at 00:48:50ID: 16368366

Hi amankhan,

Do you want to pass multiple parameters? is the number of parameters fixed or varying?

 

by: sapnamPosted on 2006-04-04 at 00:55:29ID: 16368398

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';

 

by: ploubierPosted on 2006-04-04 at 01:38:18ID: 16368665

Multiple parameters can be passed separately to the script with a single line of code :

select * from dept where deptno in (&1, &2, &3, &4);

 

by: Harish_RajaniPosted on 2006-04-04 at 05:10:39ID: 16369947

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

 

by: Harish_RajaniPosted on 2006-04-04 at 05:20:48ID: 16370016

Following covers a comprehensive discussion on the topic of passing parameters

http://www.oracle.com/technology/support/tech/sql_plus/htdocs/sub_var9.html

Rgds,
HR

 

by: amankhanPosted on 2006-04-04 at 06:45:12ID: 16370893

Hi Guys,

Thanks a lot for the assistace. My parameters are varying riaz. They are not fixed. In that case, How can we pass them.

Thanks

 

by: sapnamPosted on 2006-04-04 at 06:56:14ID: 16371047

IF you know the max number of parameters, you can provide for that many. User can enter the required ones and leave the rest blank. I tested that with the SQL I gave

 

by: ploubierPosted on 2006-04-04 at 06:58:09ID: 16371066

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>

 

by: actonwangPosted on 2006-04-04 at 08:35:21ID: 16372203

Instead of passing paramteres, the following script will do clearer and better job for you:


    set echo off
    set verify off

    accept values char prompt "Type in values as 10,20,30 : "
    select * from dept where deptno in (&values)
    /
   
    set verify on    

 

by: Harish_RajaniPosted on 2006-04-04 at 08:46:21ID: 16372315

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

 

by: amankhanPosted on 2006-04-05 at 11:29:47ID: 16384742

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.

 

by: ploubierPosted on 2006-04-05 at 22:46:44ID: 16389182

Which OS ?

 

by: Harish_RajaniPosted on 2006-04-06 at 02:56:57ID: 16390300

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_handle);
WHEN OTHERS

UTL_FILE.fclose(file_handle);

END;






Rgds,
HR

 

by: amankhanPosted on 2006-04-06 at 17:13:37ID: 16396912

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

 

by: Harish_RajaniPosted on 2006-04-13 at 00:30:05ID: 16443400

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

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...