Oracle PL/SQL/PL SQL/Console Prompt Read
Содержание
A single query can contain multiple runtime variables
CREATE TABLE EMP (EMPNO NUMBER(4) NOT NULL,
ENAME VARCHAR2(10),
JOB VARCHAR2(9),
MGR NUMBER(4),
HIREDATE DATE,
SAL NUMBER(7, 2),
COMM NUMBER(7, 2),
DEPTNO NUMBER(2));
INSERT INTO EMP VALUES (7369, "SMITH", "CLERK", 7902, TO_DATE("17-DEC-1980", "DD-MON-YYYY"), 800, NULL, 20);
INSERT INTO EMP VALUES (7499, "ALLEN", "SALESMAN", 7698, TO_DATE("20-FEB-1981", "DD-MON-YYYY"), 1600, 300, 30);
INSERT INTO EMP VALUES (7521, "WARD", "SALESMAN", 7698, TO_DATE("22-FEB-1981", "DD-MON-YYYY"), 1250, 500, 30);
INSERT INTO EMP VALUES (7566, "JONES", "MANAGER", 7839, TO_DATE("2-APR-1981", "DD-MON-YYYY"), 2975, NULL, 20);
INSERT INTO EMP VALUES (7654, "MARTIN", "SALESMAN", 7698,TO_DATE("28-SEP-1981", "DD-MON-YYYY"), 1250, 1400, 30);
INSERT INTO EMP VALUES (7698, "BLAKE", "MANAGER", 7839,TO_DATE("1-MAY-1981", "DD-MON-YYYY"), 2850, NULL, 30);
INSERT INTO EMP VALUES (7782, "CLARK", "MANAGER", 7839,TO_DATE("9-JUN-1981", "DD-MON-YYYY"), 2450, NULL, 10);
INSERT INTO EMP VALUES (7788, "SCOTT", "ANALYST", 7566,TO_DATE("09-DEC-1982", "DD-MON-YYYY"), 3000, NULL, 20);
INSERT INTO EMP VALUES (7839, "KING", "PRESIDENT", NULL,TO_DATE("17-NOV-1981", "DD-MON-YYYY"), 5000, NULL, 10);
INSERT INTO EMP VALUES (7844, "TURNER", "SALESMAN", 7698,TO_DATE("8-SEP-1981", "DD-MON-YYYY"), 1500, 0, 30);
INSERT INTO EMP VALUES (7876, "ADAMS", "CLERK", 7788,TO_DATE("12-JAN-1983", "DD-MON-YYYY"), 1100, NULL, 20);
INSERT INTO EMP VALUES (7900, "JAMES", "CLERK", 7698,TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 950, NULL, 30);
INSERT INTO EMP VALUES (7902, "FORD", "ANALYST", 7566,TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 3000, NULL, 20);
INSERT INTO EMP VALUES (7934, "MILLER", "CLERK", 7782,TO_DATE("23-JAN-1982", "DD-MON-YYYY"), 1300, NULL, 10);
-- A single query can contain multiple runtime variables. Consider the execution the following
-- statements:
SET VERIFY OFF
SELECT &empno, job
FROM emp
WHERE job ="&job";
drop table emp;
Dealing with repeating values: using double ampersands
CREATE TABLE EMP (EMPNO NUMBER(4) NOT NULL,
ENAME VARCHAR2(10),
JOB VARCHAR2(9),
MGR NUMBER(4),
HIREDATE DATE,
SAL NUMBER(7, 2),
COMM NUMBER(7, 2),
DEPTNO NUMBER(2));
INSERT INTO EMP VALUES (7369, "SMITH", "CLERK", 7902, TO_DATE("17-DEC-1980", "DD-MON-YYYY"), 800, NULL, 20);
INSERT INTO EMP VALUES (7499, "ALLEN", "SALESMAN", 7698, TO_DATE("20-FEB-1981", "DD-MON-YYYY"), 1600, 300, 30);
INSERT INTO EMP VALUES (7521, "WARD", "SALESMAN", 7698, TO_DATE("22-FEB-1981", "DD-MON-YYYY"), 1250, 500, 30);
INSERT INTO EMP VALUES (7566, "JONES", "MANAGER", 7839, TO_DATE("2-APR-1981", "DD-MON-YYYY"), 2975, NULL, 20);
INSERT INTO EMP VALUES (7654, "MARTIN", "SALESMAN", 7698,TO_DATE("28-SEP-1981", "DD-MON-YYYY"), 1250, 1400, 30);
INSERT INTO EMP VALUES (7698, "BLAKE", "MANAGER", 7839,TO_DATE("1-MAY-1981", "DD-MON-YYYY"), 2850, NULL, 30);
INSERT INTO EMP VALUES (7782, "CLARK", "MANAGER", 7839,TO_DATE("9-JUN-1981", "DD-MON-YYYY"), 2450, NULL, 10);
INSERT INTO EMP VALUES (7788, "SCOTT", "ANALYST", 7566,TO_DATE("09-DEC-1982", "DD-MON-YYYY"), 3000, NULL, 20);
INSERT INTO EMP VALUES (7839, "KING", "PRESIDENT", NULL,TO_DATE("17-NOV-1981", "DD-MON-YYYY"), 5000, NULL, 10);
INSERT INTO EMP VALUES (7844, "TURNER", "SALESMAN", 7698,TO_DATE("8-SEP-1981", "DD-MON-YYYY"), 1500, 0, 30);
INSERT INTO EMP VALUES (7876, "ADAMS", "CLERK", 7788,TO_DATE("12-JAN-1983", "DD-MON-YYYY"), 1100, NULL, 20);
INSERT INTO EMP VALUES (7900, "JAMES", "CLERK", 7698,TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 950, NULL, 30);
INSERT INTO EMP VALUES (7902, "FORD", "ANALYST", 7566,TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 3000, NULL, 20);
INSERT INTO EMP VALUES (7934, "MILLER", "CLERK", 7782,TO_DATE("23-JAN-1982", "DD-MON-YYYY"), 1300, NULL, 10);
CREATE TABLE DEPT (DEPTNO NUMBER(2),DNAME VARCHAR2(14),LOC VARCHAR2(13) );
INSERT INTO DEPT VALUES (10, "ACCOUNTING", "NEW YORK");
INSERT INTO DEPT VALUES (20, "RESEARCH", "DALLAS");
INSERT INTO DEPT VALUES (30, "SALES", "CHICAGO");
INSERT INTO DEPT VALUES (40, "OPERATIONS", "BOSTON");
-- Dealing with repeating values: using double ampersands (&&)
SELECT &Colx, &Coly
FROM Emp
WHERE &Colx > &cond
ORDER BY &Colx;
SELECT &&Colx, &Coly
FROM Emp
WHERE &&Colx > &cond
ORDER BY &Colx;
drop table dept;
drop table emp;
Read data from user and insert them to table
--Creating the employee table.
CREATE TABLE employee
(emp_id INTEGER,
emp_name VARCHAR2(32),
supervised_by INTEGER,
pay_rate NUMBER(9,2),
pay_type CHAR,
emp_dept_id INTEGER);
ALTER TABLE employee
ADD CONSTRAINT pk_emp primary key (emp_id);
insert into employee values (1,"Jack Chen",3,100.50, H, 3);
insert into employee values (2,"Jason Lee",1,300.50, H, 3);
insert into employee values (3,"Ala Wang",5,200.50, H, 3);
-- Creating the department table.
CREATE TABLE department
(dept_id INTEGER,
dept_name VARCHAR2(32));
ALTER TABLE department
ADD CONSTRAINT PRIMARY KEY (dept_id);
insert into department values (1,"Computer");
insert into department values (2,"Publish");
insert into department values (3,"Management");
-- Creating the emp_dept table.
CREATE TABLE emp_dept
(emp_id INTEGER,
dept_id INTEGER,
CONSTRAINT unq_1 unique (emp_id, dept_id));
insert into emp_dept values(1,3);
insert into emp_dept values(2,3);
insert into emp_dept values(3,3);
select * from employee;
select * from department;
select * from emp_dept;
-- Inserting records with PL/SQL code.
DECLARE
i_dept_id INTEGER,
i_dept_name,
BEGIN
INSERT into department values (&i_dept_id,"&dept_name");
END;
COMMIT;
DECLARE
i_id INTEGER;
e_id INTEGER;
i_name VARCHAR2(32);
i_super INTEGER;
i_rate NUMBER(9,2);
i_type CHAR;
i_emp_dept INTEGER;
e_emp_dept INTEGER;
BEGIN
e_id:=12;
e_emp_dept:=12;
INSERT into employee values (e_id, "&i_name",&i_super,&i_rate,"&i_type",e_emp_dept);
END;
/
select * from employee;
select * from department;
select * from emp_dept;
drop table employee;
drop table department;
drop table emp_dept;
Read user input
set echo on;
set verify off;
set define "&";
prompt "Input degree (numerically in degrees F)?:";
/
accept temp default "100";
/
declare
too_hot exception;
too_cold exception;
begin
case
when "&temp" < 90.00 then raise too_cold;
when "&temp" > 140.00 then raise too_hot;
else null;
end case;
dbms_output.put_line("The temperature is just right");
exception
when VALUE_ERROR then
dbms_output.put_line("Please enter a numeric temperature (like 100)");
when too_hot then
dbms_output.put_line("The porridge is way too hot...");
when too_cold then
dbms_output.put_line("The porridge is way too cold...");
end;
/