Read user input and use if to determine which table to print : Accept « SQL Plus « Oracle PL / SQL

Home
Oracle PL / SQL
1.Aggregate Functions
2.Analytical Functions
3.Char Functions
4.Constraints
5.Conversion Functions
6.Cursor
7.Data Type
8.Date Timezone
9.Hierarchical Query
10.Index
11.Insert Delete Update
12.Large Objects
13.Numeric Math Functions
14.Object Oriented Database
15.PL SQL
16.Regular Expressions
17.Report Column Page
18.Result Set
19.Select Query
20.Sequence
21.SQL Plus
22.Stored Procedure Function
23.Subquery
24.System Packages
25.System Tables Views
26.Table
27.Table Joins
28.Trigger
29.User Previliege
30.View
31.XML
Oracle PL / SQL » SQL Plus » Accept 




Read user input and use if to determine which table to print
   
SQL> CREATE TABLE employees
  2  employee_id          number(10)      not null,
  3    last_name            varchar2(50)      not null,
  4    email                varchar2(30),
  5    hire_date            date,
  6    job_id               varchar2(30),
  7    department_id        number(10),
  8    salary               number(6),
  9    manager_id           number(6)
 10  );

Table created.

SQL>
SQL>
SQL> insert into employeesemployee_id, last_name, email, hire_date, job_id, salary,department_id ,manager_id)
  2                values 1001'Lawson', 'la[email protected]', '01-JAN-2002','MGR', 30000,,1004);

row created.

SQL>
SQL> insert into employeesemployee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)
  2                values 1002'Wells', 'we[email protected]', '01-JAN-2002', 'DBA', 20000,21005 );

row created.

SQL>
SQL> insert into employeesemployee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)
  2                 values1003'Bliss', 'bl[email protected]', '01-JAN-2002', 'PROG', 24000,,1004);

row created.

SQL>
SQL> insert into employeesemployee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
  2                 values1004,  'Kyte', 'tk[email protected]', SYSDATE-3650'MGR',25000 ,41005);

row created.

SQL>
SQL> insert into employeesemployee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
  2                 values1005'Viper', 'sdillon@a .com', SYSDATE, 'PROG', 2000011006);

row created.

SQL>
SQL> insert into employeesemployee_id, last_name, email, hire_date, job_id, salary, department_id,manager_id)
  2                 values1006'Beck', 'cl[email protected]', SYSDATE, 'PROG', 200002null);

row created.

SQL>
SQL> insert into employeesemployee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
  2                 values1007'Java', 'ja[email protected]', SYSDATE, 'PROG', 2000031006);

row created.

SQL>
SQL> insert into employeesemployee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
  2                 values1008'Oracle', 'wv[email protected]', SYSDATE, 'DBA', 2000041006);

row created.

SQL>
SQL>
SQL> CREATE TABLE departments
  2  (department_id           number(10)            not null,
  3   department_name      varchar2(50)      not null,
  4   CONSTRAINT departments_pk PRIMARY KEY (department_id)
  5  );

Table created.

SQL>
SQL>
SQL>
SQL> insert into departments department_id, department_name )
  2                    values1,             'Data Group' );

row created.

SQL>
SQL> insert into departments department_id, department_name )
  2                    values2,             'Purchasing' );

row created.

SQL>
SQL> insert into departments department_id, department_name )
  2                    values3,             'Call Center' );

row created.

SQL>
SQL> insert into departments department_id, department_name )
  2                    values4,             'Communication' );

row created.

SQL>
SQL>
SQL>
SQL>
SQL> set echo off
SQL> set serveroutput on
SQL> set verify off
SQL> set define '&'
SQL>
SQL> prompt

SQL> prompt 'What table would you like to see?'
'What table would you like to see?'
SQL> accept tab prompt '(L)ocations, (D)epartments, or (E)mployees : '
(L)ocations, (D)epartments, or (E)mployees : prompt
SQL>
SQL> declare
  2    type refcur_t is ref cursor;
  3    refcur refcur_t;
  4
  5    type sample_rec_type is record (
  6      id          number,
  7      description varchar2(200));
  8    sample sample_rec_type;
  9
 10    selection varchar2(1:= upper(substr('&tab',1,1));
 11  begin
 12    if selection = 'D' then
 13      open refcur for
 14        select department_id, department_name
 15          from departments
 16         where rownum < 11
 17         order by 1;
 18      dbms_output.put_line('Sample DEPARTMENT data:');
 19
 20    elsif selection = 'E' then
 21      open refcur for
 22        select employee_id, last_name
 23          from employees
 24         where rownum < 11
 25          order by 1;
 26      dbms_output.put_line('Sample EMPLOYEE data:');
 27
 28    else
 29      dbms_output.put_line('Please enter ''D'', or ''E''.');
 30      return;
 31    end if;
 32
 33    fetch refcur into sample;
 34
 35    while refcur%FOUND loop
 36      dbms_output.put_line('#' || sample.id || ' i' || sample.description);
 37      fetch refcur into sample;
 38    end loop;
 39    close refcur;
 40  end;
 41  /
Please enter 'D', or 'E'.

PL/SQL procedure successfully completed.

SQL>
SQL> drop table departments cascade constraints;

Table dropped.

SQL> drop table employees cascade constraints;

Table dropped.

   
    
  














Related examples in the same category
1.ACCEPT variable value from user input
2.Use accept to get user input value and use them in a select statement
3.ACCEPT command creates a variable of datatype CHAR by default
4.Using the ACCEPT command
5.Accept input and pass into a query
6.Read value from prompt and output with pl/sql
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.