pl/sql是什么,简单点,我的理解就是处理oracle里面的数据集的一种过程化语言!
写点pl/sql的基础,跟别的语言差不多,特别之处看注释,代码格式方面参照下面例子的风格:
运算符方面:
- 赋值: :=
- 算术: + 、-、 *、 / 、 **(乘幂)
- 逻辑运算: and、or、 not
- 比较(非空值): =、 !=、 <、 >、<= 、>= 、in、 between
- 比较(对空值处理): is null 、 is not null
- 字符串: like、 ||
条件转移逻辑方面:
--if语句例子:
IF salary BETWEEN 1000 AND 40000
THEN
bonus := 1300;
ELSIF salary>40000 AND salary <= 1000000 —-不是elseif 噢
THEN
bonus := 1000;
ELSE
bonus := 0;
END IF;
--case语句例子:
CASE
WHEN salary BETWEEN 10000 AND 40000 THEN bonus := 1500;
WHEN salary > 40000 AND salary <= 100000 THEN bonus := 1000;
ELSE bonus := 0;
END CASE;
--case表达式例子:
gender_name :=
CASE gender_code
WHEN ‘m’ THEN ‘male’
WHEN ‘f’ THEN ‘female’
ELSE ‘unkonw’
END;
--for循环例子:
BEGIN
FOR month_num IN 1..6
LOOP
dbms_output.putline(month_num);
END LOOP;
END;BEGIN
FOR month_num IN REVERSE 1..6 ---递减
LOOP
dbms_output.putline(month_num);
END LOOP;
END;
--Loop循环(推荐)
LOOP
statements
EXIT WHEN condition –当条件为真的时候退出无限循环
END LOOP;
--while循环(不推荐)
WHILE condition
LOOP
statements
END LOOP;
--while循环(不推荐)
WHILE condition
LOOP
statements
END LOOP;
游标操作方面:
declare
v_ename emp.ename%type;
v_sal emp.sal%type;
cursor c_cursor is
select ename,sal from emp where rownum<10;begin
open c_cursor;
fetch c_cursor into v_ename,v_sal;
while c_cursor%found
loop
dbms_output.put_line(v_ename || ‘sal is ‘||v_sal);
fetch c_cursor into v_ename, v_sal;
end loop;
close c_cursor;
end;
–取整行
declare
v_emp_record emp%rowtype;
cursor c_cursor is
select * from emp where rownum<10;begin
open c_cursor;
fetch c_cursor into v_emp_record ;
while c_cursor%found
loop
dbms_output.put_line(v_emp_record.ename || ‘sal is ‘||v_emp_record.sal);
fetch c_cursor into v_emp_record ;
end loop;
close c_cursor;
end;
–参数化游标
declare
v_emp_record emp%rowtype;
cursor c_cursor(p_deptno emp.deptno%type) is
select * from emp where rownum
begin
open c_cursor(5);
fetch c_cursor into v_emp_record ;
while c_cursor%found
loop
dbms_output.put_line(v_emp_record.ename || ‘sal is ‘||v_emp_record.sal);
fetch c_cursor into v_emp_record ;
end loop;
close c_cursor;
end;