Home » SQL & PL/SQL » SQL & PL/SQL » IF-then instruction
IF-then instruction [message #313108] Thu, 10 April 2008 14:20 Go to next message
rogmichal
Messages: 2
Registered: December 2007
Junior Member
Hello,

I've got a problem with such a condition:
I need to obtain results of the first select when I put 1 in the input and results of the second select when I put anything else.

So where is the problem?

VAR digit VARCHAR2(8);
accept digit 
begin 
if digit = 1 then 
select * from accounts
else select * from alerts;
end;
/

Thanks a lot for a tip.
Re: IF-then instruction [message #313110 is a reply to message #313108] Thu, 10 April 2008 14:32 Go to previous messageGo to next message
Michel Cadot
Messages: 64103
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
You have to read PL/SQL User's Guide and Reference before being able to do this.
You are too far from the basic knowledge for us to be able to help you without feeding you.

Regards
Michel
Re: IF-then instruction [message #313135 is a reply to message #313110] Thu, 10 April 2008 17:05 Go to previous messageGo to next message
Barbara Boehmer
Messages: 8625
Registered: November 2002
Location: California, USA
Senior Member
You can do this without any pl/sql or if .. then:

VARIABLE digit NUMBER
VARIABLE dept_or_emp VARCHAR2(30)
ACCEPT digit PROMPT 'Enter 1 for dept or 2 for emp: '
COLUMN which_table NEW_VALUE dept_or_emp
SELECT DECODE (&&digit, 1, 'dept', 'emp') which_table FROM DUAL
/
SELECT * FROM &dept_or_emp
/

-- sample executions:
SCOTT@orcl_11g> 
SCOTT@orcl_11g> 
SCOTT@orcl_11g> VARIABLE digit NUMBER
SCOTT@orcl_11g> VARIABLE dept_or_emp VARCHAR2(30)
SCOTT@orcl_11g> ACCEPT digit PROMPT 'Enter 1 for dept or 2 for emp:  '
Enter 1 for dept or 2 for emp:  1
SCOTT@orcl_11g> COLUMN which_table NEW_VALUE dept_or_emp
SCOTT@orcl_11g> SELECT DECODE (&&digit, 1, 'dept', 'emp') which_table FROM DUAL
  2  /
old   1: SELECT DECODE (&&digit, 1, 'dept', 'emp') which_table FROM DUAL
new   1: SELECT DECODE (1, 1, 'dept', 'emp') which_table FROM DUAL

WHIC
----
dept

SCOTT@orcl_11g> SELECT * FROM &dept_or_emp
  2  /
old   1: SELECT * FROM &dept_or_emp
new   1: SELECT * FROM dept

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

SCOTT@orcl_11g> 
SCOTT@orcl_11g> ACCEPT digit PROMPT 'Enter 1 for dept or 2 for emp:  '
Enter 1 for dept or 2 for emp:  2
SCOTT@orcl_11g> COLUMN which_table NEW_VALUE dept_or_emp
SCOTT@orcl_11g> SELECT DECODE (&&digit, 1, 'dept', 'emp') which_table FROM DUAL
  2  /
old   1: SELECT DECODE (&&digit, 1, 'dept', 'emp') which_table FROM DUAL
new   1: SELECT DECODE (2, 1, 'dept', 'emp') which_table FROM DUAL

WHI
---
emp

SCOTT@orcl_11g> SELECT * FROM &dept_or_emp
  2  /
old   1: SELECT * FROM &dept_or_emp
new   1: SELECT * FROM emp

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

14 rows selected.

SCOTT@orcl_11g> 

Re: IF-then instruction [message #313136 is a reply to message #313108] Thu, 10 April 2008 17:24 Go to previous message
BlackSwan
Messages: 25036
Registered: January 2009
Location: SoCal
Senior Member
>You can do this without any pl/sql or if .. then:
But I suspect that using SQL only was NOT this homework assignment.
Previous Topic: Dynamically obtain updated column values PL/SQL triggers
Next Topic: Create Inline Function
Goto Forum:
  


Current Time: Sun Dec 04 02:33:03 CST 2016

Total time taken to generate the page: 0.17246 seconds