- 不同数据库对SQL语言的扩展:
创新互联建站是一家专注于网站建设、网站制作与策划设计,卢氏网站建设哪家好?创新互联建站做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:卢氏等地区。卢氏做网站价格咨询:13518219792
注意:当使用DBMS_OUPUT包输出信息时,需要设置SQL*Plus环境serveroutput的值为ON。(使用 /后,键入回车键执行PL/SQL程序)
SQL> begin dbms_output.put_line('hello everyone!');
end;
/
PL/SQL procedure successfully completed
SQL> set serveroutput on
SQL> begin dbms_output.put_line('hello everyone!');
end;
/
hello everyone!
PL/SQL procedure successfully completed
2、使用SQL Window窗口:在PLSQL Developer工具中,使用菜单栏中的New ->SQL Window打开命令行:
begin dbms_output.put_line('hello everyone!');
end;
标识符 | 命令规则 | 例子 |
---|---|---|
程序变量 | v_name | v_sal |
程序常量 | c_name | c_pi |
游标变量 | name_curror | emp_curror |
异常标识 | e_name | e_integrity_error |
记录类型 | name_record | emp_record |
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 BOOLEAN DEFAULT TRUE;
BEGIN
SELECT ename, sal, hiredate INTO v_name, v_sal, v_hiredate FROM emp WHERE empno = 7369;
--计算所得税
v_tax_sal := v_sal * c_tax_rate;
--打印输出
DBMS_OUTPUT.put_line(v_name ||'的工资是:' || v_sal ||', 雇员日期是:' || v_hiredate || ',所得税是:' || v_tax_sal);
F v_valid THEN
DBMS_OUTPUT.put_line('已核实');
END IF;
END;
DECLARE
v_name varchar2(10);
v_sal number(7,2);
BEGIN
SELECT ename, sal INTO v_name, v_sal FROM emp WHERE empno = 7788;
--打印姓名和薪水
DBMS_OUTPUT.PUT_LINE(v_name || '的工资是:' || v_sal);
END;
DECLARE
--定义引用型变量
v_name emp.ename%TYPE;
v_sal emp.sal%TYPE;
BEGIN
--将7788的姓名和工资赋值给定义的引用型变量
SELECT ename, sal INTO v_name, v_sal FROM emp WHERE empno = 7788;
--打印姓名和薪水
DBMS_OUTPUT.put_line(v_name || '的工资是:' || v_sal);
END;
DECLARE
--定义记录型变量
emp_record emp%ROWTYPE;
BEGIN
--将7788的员工信息赋值给了记录型变量emp_record
SELECT * INTO emp_record FROM emp WHERE empno = 7788;
--打印姓名和薪水
DBMS_OUTPUT.PUT_LINE(emp_record.ename || '的工资是:' || emp_record.sal);
END;
运算符 | 意义 |
---|---|
+ | 加号 |
- | 减号 |
* | 乘号 |
/ | 除号 |
** | 乘方 |
BEGIN
dbms_output.put_line(10 + 2);
dbms_output.put_line(10 - 2);
dbms_output.put_line(10 * 2);
dbms_output.put_line(10 / 2);
dbms_output.put_line(10 ** 2);
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;
运算符 | 意义 |
---|---|
IS NULL | 是空值 |
BETWEEN...AND | 介于两者之间 |
IN | 等于列表中的某个值 |
示例:
DECLARE
v_num1 NUMBER(2) := &n1;
BEGIN
IF(v_num1 BETWEEN 5 AND 10) THEN
DBMS_OUTPUT.put_line('num1 介于5 到10之间');
ELSE
DBMS_OUTPUT.put_line('num1 不在5到10之间');
END IF;
IF(v_num1 IN(3, 8, 10)) THEN
DBMS_OUTPUT.put_line('num1 等于3, 8 , 10中的一个值');
ELSE
DBMS_OUTPUT.put_line('num1 不等于3, 8 , 10中的一个值');
END IF;
IF(v_num1 IS NULL) THEN
DBMS_OUTPUT.put_line('num1 为空');
ELSE
DBMS_OUTPUT.put_line('num1 不为空');
END IF;
END;
逻辑符 | 意义 |
---|---|
AND | 逻辑与 |
NOT | 逻辑或 |
OR | 取反,如IS NOT NULL, NOT IN |
示例:
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('b_v1取反为TRUE');
END IF;
END;
- 附加说明:PL/SQL流程控制语句分类:
- CASE语句中使用单一选择符进行等值比较的语法格式:
CASE selector
WHEN expression1 THEN sequence_of_statements1;
WHEN expression2 THEN sequence_of_statements2;
......
WHEN expressionN THEN sequence_of_statementsN;
[ELSE sequence_of_statements;]
END CASE;
DECLARE
v_grade CHAR(1) := '&n';
BEGIN
CASE v_grade
WHEN 'A' THEN
DBMS_OUTPUT.put_line('优秀');
WHEN 'B' THEN
DBMS_OUTPUT.put_line('中等');
WHEN 'C' THEN
Dbms_Output.put_line('一般');
ELSE
DBMS_OUTPUT.put_line('输入有误');
END CASE;
END;
- CASE语句中使用多种条件比较的语法格式:
CASE
WHEN condition1 THEN sequence_of_statements1;
WHEN condition2 THEN sequence_of_statements2;
......
WHEN conditionN THEN sequence_of_statementsN;
[ELSE sequence_of_statements;]
END CASE;
DECLARE
v_sal emp01.sal%TYPE;
v_empno emp01.empno%TYPE := &no;
BEGIN
SELECT sal INTO v_sal FROM emp01 WHERE empno = v_empno;
CASE
WHEN v_sal < 1500 THEN
UPDATE emp01 SET comm = nvl(comm, 0) + 100 where empno = v_empno;
WHEN v_sal < 2500 THEN
UPDATE emp01 SET comm = nvl(comm, 0) + 80 where empno = v_empno;
WHEN v_sal < 5000 THEN
UPDATE emp01 SET comm = nvl(comm, 0) + 50 where empno = v_empno;
COMMIT;
END CASE;
END;
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 LOOP;
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 counter in [REVERSE] lower_bound ..upper_bound LOOP
statement1; statement2;
......
END LOOP;
BEGIN
FOR i IN 1..10 LOOP
Dbms_Output.put_line(i);
END LOOP;
END;
DECLARE
v_result INT;
BEGIN
<>
FOR i IN 1..5 LOOP
<>
FOR j IN 1 .. 5 LOOP
v_result := i;
EXIT outter WHEN i = 4;
END LOOP inner;
DBMS_OUTPUT.put_line('内:' || v_result);
END LOOP outter;
DBMS_OUTPUT.put_line('外:' || v_result);
END;
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;
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;
DECLARE
v_cnt INT := 1;
BEGIN
LOOP
DBMS_OUTPUT.put_line(v_cnt);
IF v_cnt = 10 THEN
GOTO end_loop;
END IF;
v_cnt := v_cnt + 1;
END LOOP;
<>
DBMS_OUTPUT.put_line('循环结束');
END;
BEGIN
SELECT ename, sal INTO v_name, v_sal FROM emp01 WHERE empno = &no;
IF v_sal < 3000 THEN
UPDATE emp01 SET comm = nvl(comm, 0) + sal * 0.2 WHERE ename = v_name;
COMMIT;
DBMS_OUTPUT.put_line(v_name || '的奖金更新了');
ELSE
NULL;
END IF;
END;