1定义一个简单的plsql
需要先设置 set serveroutput on 否则运行完没有输出结果
结构如下
--declare
-- 声明变量,类型,游标
begin
-- 程序的执行部分(类似java的main()方法)
dbms_output.put_line('helloworld');
--exception
-- 针对 begin块中出现的异常,提供处理机制
-- when ...then...
-- when ...then...
end;
2 使用plsql 查存100 号员工的工资
1 指定数据类型
declare
-- 声明变量
v_sal number(20);
v_email varchar2(20);
begin
-- sql 操作 把值放到 v_sal ,v_email中
select salary,email into v_sal,v_email from employees where employee_id = 100;
-- 打印
dbms_output.putline(v_sal||','||v_email);
end;
2 动态获取表字段的数据类型
declare
-- 声明变量
v_sal employee.salary%type;
v_email employee.email%type;
begin
-- sql 操作 把值放到 v_sal ,v_email中
select salary,email into v_sal,v_email from employees where employee_id = 100;
-- 打印
dbms_output.putline(v_sal||','||v_email);
end;
3 记录类型
declare
-- 声明一个记录类型
type emp_record is record(
v_sal employee.salary%type,
v_email employee.email%type
)
--定义一个记录类型的实例
v_emp_record emp_record;
begin
-- sql 操作 把值放到 实例 v_emp_record 中
select salary,email into v_emp_record from employees where employee_id = 100;
-- 打印
dbms_output.putline(v_emp_record.v_sal||','||v_emp_record.v_email);
end;
4 plsql 流程控制
1
if<布尔表达式> then
pl/sql 和 sql 语句;
end if;
2
if<布尔表达式> then
pl/sql 和 sql 语句;
else
其他语句;
end if;
3
if<布尔表达式> then
pl/sql 和 sql 语句;
elsif<其他布尔表达式> then
其他语句;
elsif<其他布尔表达式> then
其他语句;
end if;
4 查询 150号员工的工资,若工资大于或等于10000 则打印salary>=10000;若在5000到10000之间,则打印5000<=salary<10000;否则打印sarlary <5000;
declare
v_sal employees.sarlary%type;
begin
select salary into v_sal form employees where employee_id = 150;
if v_sal>= 10000 then dbms_output.put_line('salary >=10000');
elseif v_sal >=5000 then dbms_output.put_line('5000<=salary<10000');
else dbms_output.put_line('salary<5000');
end if;
end;
5 定义变量记录查询的数据 查询 150号员工的工资,若工资大于或等于10000 则打印salary>=10000;若在5000到10000之间,则打印5000<=salary<10000;否则打印sarlary <5000;
declare
v_sal employees.sarlary%type;
v_temp varchar2(10);
begin
select salary into v_sal form employees where employee_id = 150;
if v_sal>= 10000 then v_temp :='salary >=10000';
elseif v_sal >=5000 then v_temp :='5000<=salary<10000';
else v_temp :='salary<5000';
end if;
dbms_output.put_line(v_sal||','||v_temp );
end;
5游标
1 打印80部门的所有员工的工资
declare
--定义变量 工资 v_sal
v_sal employees.salary%type;
--定义游标
cursor emp_sal_cursor is select salary form employees where department_id = 80;
begin
--打开游标
open emp_sal_cursor ;
--提取游标
fetch emp_sal_cursor into v_sal;
while emp_sal_cursor%found loop
dbms_output.put_line('salary:'||v_sal);
fetch emp_sal_cursor into v_sal;
end loop;
--关闭游标
close emp_sal_cursor;
end;
2 打印80部门的所有员工的工资
declare
--定义变量 工资 v_sal
v_sal employees.salary%type;
--定义游标
cursor emp_sal_cursor is selct salary form employees where department_id = 80;
begin
for c in emp_sal_cursor loop
dbms_output.put_line('salary:'||v_sal);
end loop;
end;
6 函数
1 -- 存函数
create or replace function func_name(dept_id number,salary number)
return number
is
-- 函数使用过程中,需要声明的变量,记录类型,cursor
begin
exception
end;
2 -- 存函数helloworld 返回一个字符串"helloworld"。
create or replace function helloworld
return varchar2
is
-- 函数使用过程中,需要声明的变量,记录类型,cursor
begin
return 'helloworld';
end;
3 --函数的掉用
select helloworld from dual
4 -- 存函数helloworld1 返回一个字符串"helloworld"。
create or replace function helloworld1(v_logo varchar2)
return varchar2
is
-- 函数使用过程中,需要声明的变量,记录类型,cursor
begin
return 'helloworld'||v_logo;
end;
5 --带参数的函数调用
select helloworld1('aaaa') form dual
6 -- 定义一个函数: 获取给定部门的工资总和,要求:部门号定义为参数,工资总和定义为返回值。
create or replace function get_sal(dept_id number)
return number
is
v_sumsal number(10);
cursor salary_cursor is select salary form employees where department_id = dept_id;
begin
for c in salary_cursor loop
v_sumsal := v_sumsal + c.salary;
end loop;
return v_sumsal;
end;
7 -- 定义一个函数: 获取给定部门的工资总和 和 该部门的员工总数(定义为out 类型的参数)
-- 要求:部门号定义为参数,工资总和定义为返回值。
create or replace function get_sal(dept_id number,total_count out number)
return number
is
v_sumsal number(10) :=0;
cursor salary_cursor is select salary form employees where department_id = dept_id;
begin
total_count = 0;
for c in salary_cursor loop
v_sumsal := v_sumsal + c.salary;
total_count : = total_count + 1;
end loop;
return v_sumsal;
end;
7 存储过程
-- 定义一个存储过程:获取给定部门的工资总和(通过out参数), 要求:部门号和工资总额定义为参数;
create or replace procedure get_sal(dept_id number,sumsal out number)
is
cursor salary_cursor is select salary from employees where department_id = dept_id;
begin
sumsal :=0;
for c in salary_cursor loop
sumsal :=sumsal +c.salary ;
end loop;
dbms_output.put_line('salary : '||sumsal );
end;