简单的plsql语句块
- 1.helloworld
-- 1.在命令行窗口 这样才有输出结果
set serveroutput on
-- 简单的helloworld
declare
-- 声明的变量 类型 游标
begin
-- 程序的执行部分(类似Java中的main方法)
dbms_output.put_line('helloworld');
-- exception
-- 针对begin块中出现的异常,提供异常的处理机制
-- when then
-- when then
end;
- 2 .定义变量跟简单的sql语句
declare
-- 定义一个变量用v_ 开头
v_name varchar2(20);
begin
-- sql 语句赋值
select dept_name into v_name from sys_dept where dept_id = 100;
dbms_output.put_line(v_name);
end;
declare
-- 定义一个变量用v_ 开头
v_name varchar2(20);
v_dept_id number(20);
v_time date;
begin
-- sql 语句赋值 多个变量赋值
select dept_name,dept_id,create_time into v_name,v_dept_id,v_time from sys_dept where dept_id = 103;
dbms_output.put_line(v_name||','||v_dept_id||','||v_time);
end;
- 3.定义变量跟随表对应变量的数据类型
declare
-- 动态获取数据类型
v_name sys_dept.dept_name%type;
v_dept_id sys_dept.dept_id%type;
v_time sys_dept.create_time%type;
begin
-- sql 语句赋值
select dept_name,dept_id,create_time into v_name,v_dept_id,v_time from sys_dept where dept_id = 103;
dbms_output.put_line(v_name||','||v_dept_id||','||v_time);
end;
- 4.声明一个记录类型
declare
-- 声明一个记录类型 相当与java中的一个类的概念 记录类型_record
type dept_record is record(
v_name sys_dept.dept_name%type,
v_dept_id sys_dept.dept_id%type,
v_time sys_dept.create_time%type);
-- 定义一个记录类型的成员变量
v_dept dept_record;
begin
-- sql 语句赋值
select dept_name,dept_id,create_time into v_dept from sys_dept where dept_id = 103;
dbms_output.put_line(v_dept.v_name||','||v_dept.v_dept_id||','||v_dept.v_time);
end;
- 5.声明一个类型跟表的类型结构一样
declare
-- 声明一个类型跟表的结构类型一样 %rowtype
v_dept sys_dept%rowtype;
begin
-- sql 语句赋值
select * into v_dept from sys_dept where dept_id = 103;
dbms_output.put_line(v_dept.dept_name||','||v_dept.dept_id||','||v_dept.create_time);
end;
- 6.if流程控制语句块
declare
v_id sys_dept.dept_id%type;
begin
select dept_id into v_id from sys_dept where dept_id = 100;
-- 流程控制判断语句块 if then
if v_id >100
then
dbms_output.put_line('id大于100');
elsif v_id >50
then
dbms_output.put_line('id大于50小于等于100');
else
dbms_output.put_line('id小于等于50');
end if;
end;
- 7.流程循环语句
declare
v_i number(10) :=1;
begin
-- 简单的循环语句
loop
dbms_output.put_line(v_i);
v_i :=v_i +1;
exit when v_i >=100;
end loop;
end;
declare
v_i number(10) :=1;
begin
-- 简单的循环语句
while v_i <=100 loop
dbms_output.put_line(v_i);
v_i := v_i +1;
end loop;
end;
-
- 跳出循环语句的两个关键字
declare
begin
-- 简单的循环语句 跳出循环语句 exit
for i in 1..100 loop
if i= 50 then dbms_output.put_line('打印结束');
exit;
end if;
dbms_output.put_line(i);
end loop;
end;
declare
begin
-- 简单的循环语句 跳出循环语句 exit
for i in 1..100 loop
if i= 50 then goto lable;
exit;
end if;
dbms_output.put_line(i);
end loop;
<<lable>>
dbms_output.put_line('结束');
end;
- 9.定义一个简单的游标
declare
v_id sys_dept.dept_id%type;
v_name sys_dept.dept_name%type;
-- 定义一个游标
cursor dept_list is select dept_id,dept_name from sys_dept;
begin
-- 打开游标
open dept_list;
-- 提取游标
fetch dept_list into v_id,v_name;
while dept_list%found loop
dbms_output.put_line(v_id || v_name);
fetch dept_list into v_id,v_name;
end loop;
-- 关闭游标
close dept_list;
end;
declare
v_id sys_dept.dept_id%type;
v_name sys_dept.dept_name%type;
v_dept sys_dept%rowtype;
-- 定义一个游标
cursor dept_list is select * from sys_dept;
begin
-- 打开游标
open dept_list;
-- 提取游标
fetch dept_list into v_dept;
while dept_list%found loop
dbms_output.put_line(v_dept.dept_id || v_dept.dept_name);
fetch dept_list into v_dept;
end loop;
-- 关闭游标
close dept_list;
end;
declare
v_id sys_dept.dept_id%type;
v_name sys_dept.dept_name%type;
v_dept sys_dept%rowtype;
-- 定义一个游标
cursor dept_list is select * from sys_dept;
begin
-- 使用for来循环游标 更简单
for dept in dept_list loop
dbms_output.put_line(dept.dept_id || dept.dept_name);
end loop;
end;
- 10.异常处理
declare
v_id sys_dept.dept_id%type;
-- 定义非预定义异常
e_many_exception exception;
pragma exception_init(e_many_exception,-6512);
begin
select dept_id into v_id from sys_dept where dept_id> 100;
exception
when e_many_exception then dbms_output.put_line('有多条结果');
when others then dbms_output.put_line('报错了');
end;
declare
v_id number(10);
-- 手动抛出异常测试
e_test_exception exception;
-- pragma exception_init(e_many_exception,-6512);
begin
select dept_id into v_id from sys_dept where dept_id= 100;
if v_id >50 then
raise e_test_exception;
end if;
exception
when e_test_exception then dbms_output.put_line('测试异常');
when others then dbms_output.put_line('报错了');
end;
-
- 创建函数
-- 创建一个简单的函数返回helloworld
create or replace function hello_world
return varchar2
is
begin
return 'helloworld';
end;
-- 创建一个简单的函数返回带参数
create or replace function hello_world1(v_name varchar)
return varchar2
is
begin
return 'helloworld' || v_name;
end;
create or replace function get_date
return varchar2
is
-- 定义变量
v_date date;
begin
v_date :=sysdate;
return v_date;
end;
-- 带返回值类型的参数
create or replace function get_num(id number,id_id out number)
return varchar2
is
-- 定义变量
begin
id_id := id;
return id;
end;
- 12创建一个简单的存储过程
create or replace procedure test_procedure(dept_id number,temp_id out number)
is
cursor dept_cursor is select * from sys_dept;
begin
for dept in dept_cursor loop
if dept.dept_id = 100 then dbms_output.put_line('id是一百');
else dbms_output.put_line('其他');
end if;
end loop;
dbms_output.put_line(dept_id);
dbms_output.put_line(temp_id);
end;
- 13.创建一个简单的触发器
-- 创建一个简单的触发器
create or replace trigger update_emp_triger
after
update on sys_dept
for each row
begin
dbms_output.put_line('更新了');
end;