Oracle Database --- PLSQL

SET SERVEROUTPUT ON
BEGIN
--打印输出
DBMS_OUTPUT.PUT_LINE('hello everyone!');
END;

The DECLARE
v_name VARCHAR2 (10);
v_sal NUMBER (7,2);
v_hiredate DATE;
c_tax_rate CONSTANT NUMBER (3,2-): = 0.02;
v_tax_sal NUMBER (7,2);
v_valid the DEFAULT TRUE BOOLEAN;
the BEGIN
the SELECT ename, SAL, HireDate
v_name the INTO, v_sal, v_hiredate
the FROM EMP
the WHERE EMPNO = 7369;
- calculating tax
v_tax_sal: * = v_sal c_tax_rate;
- printout
DBMS_OUTPUT.PUT_LINE (v_name || 'wages:' || v_sal || 'employee date : '|| v_hiredate ||' tax is: '|| v_tax_sal);
the IF THEN v_valid
the DBMS_OUTPUT.PUT_LINE (' verified ');
the END the IF;
the END;

- a reference variable
the DECLARE
v_name emp.ename the TYPE%;
v_sal emp.sal the TYPE%;
the BEGIN
the SELECT ename, SAL
the INTO v_name, v_sal
the FROM EMP
the WHERE EMPNO = 7788;
- printout
DBMS_OUTPUT.PUT_LINE (v_name || 'wages They are: '|| v_sal);
the END;

- record variable
the DECLARE
emp_record EMP% ROWTYPE;
the BEGIN
the SELECT * the INTO emp_record the FROM EMP the WHERE EMPNO = 7788;
- printout
DBMS_OUTPUT.PUT_LINE (emp_record.ename || 'wages:' || emp_record.sal);
the END ;

--算术运算符
DECLARE
v_num1 NUMBER(3):=10;
v_num2 NUMBER(3):=2;
BEGIN
DBMS_OUTPUT.PUT_LINE(v_num1+v_num2);
DBMS_OUTPUT.PUT_LINE(v_num1-v_num2);
DBMS_OUTPUT.PUT_LINE(v_num1*v_num2);
DBMS_OUTPUT.PUT_LINE(v_num1/v_num2);
DBMS_OUTPUT.PUT_LINE(v_num1**v_num2);
END;

--关系运算符
DECLARE
v_num1 NUMBER(2) := &n1;
v_num2 NUMBER(2) := &n2;
BEGIN
IF (v_num1 = v_num2) THEN
DBMS_OUTPUT.PUT_LINE('num1等于num2');
ELSIF(v_num1 < v_num2) THEN
DBMS_OUTPUT.PUT_LINE('num1小于num2');
ELSIF(v_num1 > v_num2) THEN
DBMS_OUTPUT.PUT_LINE('num1大于num2');
END IF;

IF (v_num1 <> v_num2) THEN
DBMS_OUTPUT.PUT_LINE('num1不等于num2');
END IF;
END;

- comparison operator
the DECLARE
- & substitution variable is N1, when executing the program prompts for a value
v_num1 NUMBER (2): & = N1;
the BEGIN
the IF (v_num1 the BETWEEN. 5 the AND 10) THEN
the DBMS_OUTPUT.PUT_LINE ( 'num1 5 to 10 between ');
the ELSE
the DBMS_OUTPUT.PUT_LINE (' not between num1. 5 to 10 ');
the END the IF;

the IF (v_num1 the iN (3,8,10)) THEN
the DBMS_OUTPUT.PUT_LINE (' num1 in equal 3,8,10 a value ');
the ELSE
the DBMS_OUTPUT.PUT_LINE (' a value num1 is not equal 3,8,10 ');
the END the IF;

the IF (v_num1 the iS NULL) THEN
the DBMS_OUTPUT.PUT_LINE (' empty num1 ');
the ELSE
the DBMS_OUTPUT .PUT_LINE ( 'num1 not empty');
the END the IF;
the END;

--逻辑运算符
DECLARE
v_b1 BOOLEAN := &n1;
v_b2 BOOLEAN := &n2;
BEGIN
IF(v_b1 AND v_b2)THEN
DBMS_OUTPUT.PUT_LINE('AND--TRUE');
END IF;

IF(v_b1 OR v_b2)THEN
DBMS_OUTPUT.PUT_LINE('OR--TRUE');
END IF;

IF(NOT v_b1)THEN
DBMS_OUTPUT.PUT_LINE('v_b1取反为TRUE');
END IF;
END;

- Enter the number of employees to determine the salaries, wages and employee name display wages of less than 3000.
- simple IF statement
the DECLARE
v_name emp.ename the TYPE%;
v_sal emp.sal the TYPE%;
the BEGIN
the SELECT ename, SAL
the INTO v_name, v_sal
the FROM EMP
the WHERE & EMPNO = NO;
IF v_sal <3000 THEN
the DBMS_OUTPUT.PUT_LINE (v_name || ' wages: '|| v_sal);
the END the IF;
the END;


SELECT * FROM emp;

- Enter the number of employees to determine the salaries, wages less than the wages of 3000 rose 200, and displays the name of the employee a raise, other employees show employee names and salaries.
- Double branch statement
the DECLARE
v_name empnew.ename the TYPE%;
v_sal empnew.sal the TYPE%;
v_empno% empnew.empno the TYPE: & = NO;
the BEGIN
the SELECT ename, SAL
the INTO v_name, v_sal
the FROM empnew
the WHERE EMPNO = v_empno;
the IF v_sal < THEN 3000
the UPDATE SET empnew = SAL + SAL 200 is WHERE EMPNO = v_empno;
a COMMIT;
dbms_output.put_line (v_name || 'a raise');
the ELSE
dbms_output.put_line (v_name || 'wages:' || v_sal);
the IF the END;
the END;

SELECT * from empnew;


- Enter the number of employees to determine wages, salaries less than 2000, show low-income, wages of less than 6000, show the middle-income, high-income other display.
- Multiple branch statement
the DECLARE
v_name empnew.ename the TYPE%;
v_sal empnew.sal the TYPE%;

the BEGIN
the SELECT ename, SAL
the INTO v_name, v_sal
the FROM empnew
the WHERE & EMPNO = NO;
the IF v_sal <2000 THEN
the DBMS_OUTPUT.PUT_LINE (v_name || 'of wage is: '|| v_sal ||' low-income ');
ELSIF v_sal <6000 THEN
DBMS_OUTPUT.PUT_LINE (v_name ||' salary is: '|| v_sal ||' middle-income ');
ELSE
DBMS_OUTPUT.PUT_LINE (v_name || 'wages:' || v_sal || 'high-income');
the END the IF;
the END;

/ *
- Enter the number of employees to determine the salaries, wages and employee name display wages of less than 3000.
- simple IF statement
the DECLARE
v_name emp.ename the TYPE%;
v_sal emp.sal the TYPE%;
the BEGIN
the SELECT ename, SAL
the INTO v_name, v_sal
the FROM EMP
the WHERE & EMPNO = NO;
IF v_sal <3000 THEN
the DBMS_OUTPUT.PUT_LINE (v_name || ' wages: '|| v_sal);
the END the IF;
the END;


SELECT * FROM emp;

- Enter the number of employees to determine the salaries, wages less than the wages of 3000 rose 200, and displays the name of the employee a raise, other employees show employee names and salaries.
- Double branch statement
the DECLARE
v_name empnew.ename the TYPE%;
v_sal empnew.sal the TYPE%;
v_empno% empnew.empno the TYPE: & = NO;
the BEGIN
the SELECT ename, SAL
the INTO v_name, v_sal
the FROM empnew
the WHERE EMPNO = v_empno;
the IF v_sal < THEN 3000
the UPDATE SET empnew = SAL + SAL 200 is WHERE EMPNO = v_empno;
a COMMIT;
dbms_output.put_line (v_name || 'a raise');
the ELSE
dbms_output.put_line (v_name || 'wages:' || v_sal);
the IF the END;
the END;

SELECT * from empnew;


- Enter the number of employees to determine wages, salaries less than 2000, show low-income, wages of less than 6000, show the middle-income, high-income other display.
- Multiple branch statement
the DECLARE
v_name empnew.ename the TYPE%;
v_sal empnew.sal the TYPE%;

the BEGIN
the SELECT ename, SAL
the INTO v_name, v_sal
the FROM empnew
the WHERE & EMPNO = NO;
the IF v_sal <2000 THEN
the DBMS_OUTPUT.PUT_LINE (v_name || 'of wage is: '|| v_sal ||' low-income ');
ELSIF v_sal <6000 THEN
DBMS_OUTPUT.PUT_LINE (v_name ||' salary is: '|| v_sal ||' middle-income ');
ELSE
DBMS_OUTPUT.PUT_LINE (v_name || 'wages:' || v_sal || 'high-income');
the END the IF;
the END;
* /

- input into the stage level, which level is determined belongs, and prints
--CASE equivalence comparison
the DECLARE
v_grade char (. 1): = 'NO &';
the BEGIN
the CASE v_grade
the WHEN 'A' THEN
the DBMS_OUTPUT.PUT_LINE ( 'excellent');
the WHEN 'B' THEN
the DBMS_OUTPUT.PUT_LINE ( 'medium');
the WHEN 'C' THEN
the DBMS_OUTPUT.PUT_LINE ( 'general');
the ELSE
the DBMS_OUTPUT.PUT_LINE ( 'mistyped');
the END the CASE;
the END;

- enter the employee number, obtaining salaries, salary is determined, if salary is less than 1500, plus 100 grants, if salary is less than 2500, plus 80 grants, if less than 5000 wages, benefits plus 50. A
--CASE non-equivalence comparison
the DECLARE
v_sal empnew the TYPE% .sal;
v_empno% empnew.empno the TYPE: & = NO;
the BEGIN
the SELECT SAL
the INTO v_sal
the FROM empnew
the WHERE & EMPNO = NO;
the CASE
the WHEN v_sal <1500 THEN
the UPDATE SET empnew COMM = NVL (COMM, 0) = +100 v_empno the WHERE EMPNO ;
the WHEN v_sal <2500 THEN
the UPDATE SET empnew COMM = NVL (COMM, 0) = +80 v_empno the WHERE EMPNO;
the WHEN v_sal <5000 THEN
the UPDATE SET empnew COMM = NVL (COMM, 0) = +50 v_empno the WHERE EMPNO;
--commit ;
the END the CASE;
the END;

select * from empnew;

--基本循环
DECLARE
v_cnt INT :=1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE(v_cnt);
EXIT WHEN v_cnt = 10;
v_cnt := v_cnt+1;
END LOOP;
END;

--while循环
DECLARE
v_cnt INT :=1;
BEGIN
WHILE v_cnt<=10 LOOP
DBMS_OUTPUT.PUT_LINE(v_cnt);
v_cnt := v_cnt+1;
END LOOP;
END;

--for循环
BEGIN
FOR i IN REVERSE 1..10 LOOP
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
END;

--嵌套循环
DECLARE
v_result INT;
BEGIN
<<outter>>
FOR i IN 1..5 LOOP
<<inter>>
FOR j IN 1..5 LOOP
v_result:=i;
EXIT outter WHEN i=4;
END LOOP inter;
DBMS_OUTPUT.PUT_LINE('内'||v_result);
END LOOP outter;
DBMS_OUTPUT.PUT_LINE('外'||v_result);
END;

--CONTINUE
DECLARE
v_cnt INT :=0;
BEGIN
LOOP
v_cnt := v_cnt+1;
CONTINUE WHEN v_cnt = 5;
DBMS_OUTPUT.PUT_LINE(v_cnt);
EXIT WHEN v_cnt = 10;
END LOOP;
END;

--GOTO语句
DECLARE
v_cnt INT := 1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE(v_cnt);
IF v_cnt=10 THEN
--EXIT;
GOTO end_loop;
END IF;
v_cnt := v_cnt + 1;
END LOOP;
<<end_loop>>
DBMS_OUTPUT.PUT_LINE('循环结束');
END;

--NULL语句
DECLARE
v_sal empnew.sal%TYPE;
v_name empnew.ename%TYPE;
BEGIN
SELECT ename,sal
INTO v_name,v_sal
FROM empnew
WHERE empno = &no;
IF v_sal<3000 THEN
UPDATE empnew set comm = nvl(comm,0)+sal*0.2 WHERE ename=v_name;
COMMIT;
DBMS_OUTPUT.PUT_LINE(v_name||'的奖金更新了');
ELSE
NULL;
END IF;
END;

select * from empnew;

 

Guess you like

Origin www.cnblogs.com/xiaomifeng1010/p/11111978.html