Solved

If then in sqlplus

Posted on 2006-06-30
9
4,200 Views
Last Modified: 2011-10-03
Currently, I have the following code in SQL script runs in sqlplus:

DEFINE REGION         = &&1
EXECUTE MY_PROC


I like to add IF THEN condition.  Please remember this is not pl/sql.  How to do?  Thanks.

DEFINE REGION         = &&1

IF REGION ='USA' THEN
EXECUTE MY_PROC
END IF




0
Comment
Question by:ewang1205
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 14

Expert Comment

by:sathyagiri
ID: 17021569
If it was a function you could use some thing like

select decode('&&REGION','USA',MY_FUNC) from dual;

0
 
LVL 14

Assisted Solution

by:sathyagiri
sathyagiri earned 100 total points
ID: 17021631
You could probably create a wrapper function that will call your stored proc and then use the above select statement

create or replace my_func returns number
is
begin
my_proc;
return null;
end;
/

Then use
select decode('&REGION','USA',MY_FUNC) from dual;
0
 
LVL 19

Expert Comment

by:actonwang
ID: 17022139
It is simple. you don't need to any wrapper function or extra step. sql*plus wil be enough for you to do it.
0
 
LVL 19

Assisted Solution

by:actonwang
actonwang earned 100 total points
ID: 17022142
the following scriplet will do the logic for you


////////////////////////////////////////
DEFINE REGION=&&1

set feedback off echo off verify off

SPOOL  exec.sql

SELECT 'EXEC MY_PROC'
FROM DUAL
WHERE '&REGION' = 'USA';

SPOOL OFF

@exec.sql
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 16

Assisted Solution

by:MohanKNair
MohanKNair earned 300 total points
ID: 17025517
SQL> EGION varchar2(100);

SQL>  :REGION := 'USA'; end;
/

SQL> begin
IF :REGION ='USA' THEN
MY_PROC;
END IF;
/
0
 

Author Comment

by:ewang1205
ID: 17027485

actonwang :  I tried the following and returns EXEC MY_PROC as value, it don't EXECUTE MY_PROC.  

SELECT 'EXEC MY_PROC' FROM DUAL WHERE 1=1;


MohanKNair :  I don't want to use BEGIN and END in the SQL
0
 

Author Comment

by:ewang1205
ID: 17027641
I found a solution.  I change the MY_PROC to accept in parameter value and use if/then checking the REGION value then run like the following:

EXECUTE MY_PROCE(REGION)
0
 
LVL 19

Expert Comment

by:actonwang
ID: 17033450
>>actonwang :  I tried the following and returns EXEC MY_PROC as value, it don't EXECUTE MY_PROC.  

SELECT 'EXEC MY_PROC' FROM DUAL WHERE 1=1;

you'd have to use my whoe script, not just that ONE sql query:

///////////////
DEFINE REGION=&&1

set feedback off echo off verify off

SPOOL  exec.sql

SELECT 'EXEC MY_PROC'
FROM DUAL
WHERE '&REGION' = 'USA';

SPOOL OFF
0
 
LVL 16

Accepted Solution

by:
MohanKNair earned 300 total points
ID: 17034780
ewang1205,

sql*plus is not procedural and it is better to code if..then..else clauses within pl/sql.

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

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Stay Alert! 13 63
null value 15 67
sql for Oracle views 8 38
Maintaining Oracle Managed Accounts 2 21
Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

760 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now