-----------------------------------------------------------------------------------
备份和恢复数据库略过,在后面讲解应用场景207---231,越过。。。。。
数据库网络配置略过,其在实际的应用中不是很普遍,所以,此处先不进行详细的讲解,如果后期有时间会进行整理。
应用场景32----244越过。。。。。。。。
-----------------------------------------------------------------------------------
-------------------PL/SQL语言基础------------------------
基础知识点:
PL/SQL字符集
PL/SQL示例程序
声明变量与常量
----------------------------------------------------------
应用场景2245 :使用赋值语句
SET ServerOutput ON;
DECLARE
BookName VARCHAR2(50);
BEGIN
BookName := ' Oracle 11g学习笔记';
dbms_output.put_line(BookName);
END;
/
应用场景246 :使用条件语句
SET ServerOutput ON;
DECLARE
Num INTEGER := -11;
BEGIN
IF Num < 0 THEN
dbms_output.put_line('负数');
ELSIF Num >0 THEN
dbms_output.put_line('正数');
ELSE
dbms_output.put_line('0');
END IF;
END;
/
应用场景247:分支语句CASE
SET ServerOutput ON;
DECLARE
varDAY INTEGER := 3;
Result VARCHAR2(20);
BEGIN
Result := CASE varDAY
WHEN 1 THEN '星期一'
WHEN 2 THEN '星期二'
WHEN 3 THEN '星期三'
WHEN 4 THEN '星期四'
WHEN 5 THEN '星期五'
WHEN 6 THEN '星期六'
WHEN 7 THEN '星期七'
ELSE '数据越界'
END;
dbms_output.put_line(Result);
END;
/
应用场景248 :循环语句LOOP...EXIT...END
SET ServerOutput ON;
DECLARE
v_Num INTEGER := 1;
v_Sum INTEGER := 0;
BEGIN
LOOP
v_Sum := v_Sum + v_Num;
dbms_output.put_line(v_Num);
IF v_Num = 4 THEN
EXIT;
END IF;
dbms_output.put_line(' + ');
v_Num := v_Num + 1;
END LOOP;
dbms_output.put_line(' = ');
dbms_output.put_line(v_Sum);
END;
/
应用场景249 :循环语句LOOP...EXIT WHEN ..END
SET ServerOutput ON;
DECLARE
v_Num INTEGER := 1;
v_Sum INTEGER := 0;
BEGIN
LOOP
v_Sum := v_Sum + v_Num;
dbms_output.put_line(v_Num);
EXIT WHEN v_Num = 4;
dbms_output.put_line(' + ');
v_Num := v_Num + 1;
END LOOP;
dbms_output.put_line(' = ');
dbms_output.put_line(v_Sum);
END;
/
应用场景250 :循环语句WHILE ....LOOP...END LOOP
SET ServerOutput ON;
DECLARE
v_Num INTEGER := 1;
v_Sum INTEGER := 0;
BEGIN
WHILE v_Num <= 4
LOOP
v_Sum := v_Sum + v_Num;
dbms_output.put_line(v_Num);
IF v_Num < 4 THEN
dbms_output.put_line(' + ');
END IF;
v_Num := v_Num + 1;
END LOOP;
dbms_output.put_line(' = ');
dbms_output.put_line(v_Sum);
END;
/
应用场景251 :循环语句FOR...IN...LOOP...END LOOP
SET ServerOutput ON;
DECLARE
v_Num INTEGER;
v_Sum INTEGER := 0;
BEGIN
FOR v_Num IN 1..4
LOOP
v_Sum := v_Sum + v_Num;
dbms_output.put_line(v_Num);
IF v_Num < 4 THEN
dbms_output.put_line(' + ');
END IF;
END LOOP;
dbms_output.put_line(' = ');
dbms_output.put_line(v_Sum);
END;
/
应用场景252 :异常处理
SET SERVEROUTPUT ON;
DECLARE
x NUMBER;
BEGIN
x:= 'abc';--向NUMBER类型的变量X中赋值字符串,导致异常
EXCEPTION
WHEN VALUE_ERROR THEN
DBMS_OUTPUT.PUT_LINE('数据类型错误');
END;
/
SET SERVEROUTPUT ON;
DECLARE
var_DepName VARCHAR(40);
BEGIN
SELECT Dep_Name INTO var_DepName
FROM HRMAN.Departments WHERE Dep_id > 1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('没有数据');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('返回多行匹配的数据');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('错误情况不明');
END;
/
应用场景253 :数值型函数
1.ABS函数
SET ServerOutput ON;
BEGIN
dbms_output.put_line(ABS(-4));
END;
/
2.CEIL函数
SET ServerOutput ON;
BEGIN
dbms_output.put_line(CEIL(116.34));
dbms_output.put_line(CEIL(-112.74));
dbms_output.put_line(CEIL(0));
END;
/
3.FLOOR函数
SET ServerOutput ON;
BEGIN
dbms_output.put_line(FLOOR(116.34));
dbms_output.put_line(FLOOR(-112.74));
dbms_output.put_line(FLOOR(0));
END;
/
4.POWER函数
SET ServerOutput ON;
BEGIN
dbms_output.put_line(POWER(15, 4));
END;
/
5.ROUND函数
SET ServerOutput ON;
BEGIN
dbms_output.put_line(ROUND(123.456, 2));
dbms_output.put_line(ROUND(123.456, 1));
dbms_output.put_line(ROUND(123.456, 0));
dbms_output.put_line(ROUND(123.456, -1));
dbms_output.put_line(ROUND(123.456, -2));
dbms_output.put_line(ROUND(123.456, -3));
END;
/
应用场景254 :字符型函数
SET ServerOutput ON;
BEGIN
dbms_output.put_line(ASCII('ABC'));
END;
/
COL DEP_NAME FORMAT A20
SELECT Dep_name, LENGTH(Dep_name) FROM HRMAN.Departments;
SET ServerOutput ON;
BEGIN
dbms_output.put_line(UPPER('abc'));
END;
/
应用场景255 :日期型函数
sysdate函数:
SET ServerOutput ON;
BEGIN
dbms_output.put_line(SYSDATE);
END;
/
TO_CHAR函数
SET ServerOutput ON;
BEGIN
dbms_output.put_line(TO_CHAR(SYSDATE));
END;
/
last_day函数
SET ServerOutput ON;
BEGIN
dbms_output.put_line(LAST_DAY(SYSDATE));
END;
/
TO_DATE函数
SET ServerOutput ON;
BEGIN
dbms_output.put_line(TO_DATE('2010-2-5', 'yyyy-mm-dd'));
END;
/
months_between函数:
SET ServerOutput ON;
DECLARE
date1 VARCHAR2(20) := '2010-06-05';
date2 VARCHAR2(20) := '2010-10-05';
BEGIN
dbms_output.put_line(MONTHS_BETWEEN(TO_DATE(date2,'yyyy-mm-dd'), TO_DATE(date1, 'yyyy-mm-
dd')));
END;
/
round函数:
SET ServerOutput ON;
BEGIN
dbms_output.put_line(TO_CHAR(ROUND (SYSDATE, 'MM')));
END;
应用场景256 :统计函数
count函数
SELECT COUNT(Dep_id) FROM HRMAN.Departments;
MAX函数
SELECT MAX(Dep_id) FROM HRMAN.Departments;
MIN函数
SELECT MIN(Dep_id) FROM HRMAN.Departments;
版权声明:欢迎转载,希望在你转载的同时,添加原文地址,谢谢配合