plsql教学_plsql范本学习
plsql教学由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“plsql范本学习”。
1、一个简单的程序 set serveroutput on;&符号的作用:提示用户输入 begin
dbms_output.put_line('HelloWorld!');end;/--执行语句
2、一个简单的程序块 declare
v_name varchar2(20);begin
v_name:='i am ynp';
dbms_output.put_line(v_name);end;
3、一个完整的简单程序 declare
v_num number := 0;begin
v_num :=2/v_num;
dbms_output.put_line(v_num);exception
when others then
dbms_output.put_line('error');end;
----变量声明的规则
1.变量名不能够使用保留字,如from、select等 2.第一个字符必须是字母 3.变量名最多包含30个字符 4.不要与数据库的表或者列同名 5.每一行只能声明一个变量
------
4、变量声明尽量以v_ 开头
5、常用变量类型
binary_integer :整数,主要用来计数而不是用来表示字段类型 number :数字类型
char :定长字符串类型
v_empnos(-1):=12;
v_empnos(2):=111;
dbms_output.put_line(v_empnos(-1));end;
说明:table变量类型命名规则:type(自定义变量)-table(table类型变量)-emp(表emp)-empno(empno字段); 下标值可以为负值;
2、Record变量类型(类似于java中的类)declare type type_record_dept is record(deptno dept.deptno%type, dname dept.dname%type, loc dept.loc%type);
v_temp type_record_dept;begin
v_temp.deptno:=50;
v_temp.loc:='bj';
v_temp.dname:='aaa';
dbms_output.put_line(v_temp.deptno||' '||v_temp.dname);end;
但上述dept表变动时,此程序也得手动做相应改动,可以用下述方法自动改动:
使用%rowtype声明record变量 declare
v_temp dept%rowtype;begin
v_temp.deptno:=50;
v_temp.loc:='bj';
v_temp.dname:='aaa';
dbms_output.put_line(v_temp.deptno||' '||v_temp.dname);end;
errcode := SQLCODE;--出错代码 errmsg := SQLERRM
--出错信息
-----------------------------pl/sql中的sql语句----------------------
1、select语句
有且只有一条返回值,且必须加into 例子: declare
else
dbms_output.put_line('middle');
end if;--注意有这条语句 end;
5、while语句 declare
k binary_integer:=1;begin
while(k
dbms_output.put_line(k);
k:=k+1;
end loop;end;
6、do..while语句 declare
k binary_integer:=1;begin
loop
dbms_output.put_line(k);
k:=k+1;
exit when(k>=11);
end loop;end;
7、for循环 declare
k binary_integer:=1;begin
for k in 1..10 loop
dbms_output.put_line(k);
end loop;end;
declare
k binary_integer:=1;begin
for k in reverse 1..10 loop--加上reverse为逆序打印
dbms_output.put_line(k);
end loop;end;
close c;end;
4、for循环(循环时最简单)declare
cursor c is
select * from emp;begin
for v_emp in c loop
--不用定义v_emp,不用打开关闭游标了
dbms_output.put_line(v_emp.ename);
end loop;end;
5、带参数的游标 declare
cursor c(v_deptno emp.deptno%type,v_job emp.job%type)is
select * from emp where deptno = v_deptno and job= v_job;begin
for v_emp in c(30,'CLERK')loop
dbms_output.put_line(v_emp.ename);
end loop;end;
6、可更新的游标
游标一般是作为记录集读取数据用的,但有时候用游标修改记录,这就是可更新游标; declare
cursor c is
select * from emp2 for update;begin
for v_emp in c loop
if(v_emp.sal
update emp2 set sal =sal+1 where current of c;--修改定位到的当前记录,注意形式
elsif(v_emp.sal>=2000)then
delete from emp2 where current of c;
end if;
end loop;
commit;--提交 end;
-------------------------存储过程--------------------
1、把过程的declare变成 create or Replace produce p is 就行。
end if;
v_d := v_d+1;end;
---> 调试时:
可以在命令窗口调试,出错时 用show errors 显示出错信息; 可以在plDv中调试;
---> 运行时:
可以在命令窗口运行: declare
v_a number:=3;
v_b number:=4;
v_c number;
v_d number:=5;begin
p(v_a,v_b,v_c,v_d);
dbms_output.put_line(v_c);
dbms_output.put_line(v_d);end;可以在plDv中调试;
------------------函数-------------------
1、它有返回值
create or replace function tax_tag(sal number)return number--计算税率 is begin
if(sal > 1000)then
return 0.1;
elsif(sal>=2000)then
return 0.15;
else
return 0.2;
end if;end;
select ename, tax_tag(sal)from emp ;--直接用函数tax_tag
------------------------触发器(trigger)------------------------------1.create table emp2_log(insert into article values(10,'蚂蚁是护士',9,1,3);
----------》存储过程
create or replace procedure p(v_pid article.pid%type,v_level binary_integer)is
cursor c is select * from article where pid = v_pid;
v_perStr varchar2(2000):=' ';begin
for i in 1..v_level loop
v_perStr := v_perStr||'***';
end loop;
for v_article in c loop
dbms_output.put_line(v_perStr||v_article.cont);
if(v_article.isleaf = 0)then
p(v_article.id,v_level + 1);
end if;
end loop;end;
----最简单的块set serveroutput onbegindbms_output.put_line('Hello,World');end;----有定义和执行部分的块----把用户的编号也显示出来declare----定义变量v_ename var......
基本数据类型变量 1.基本数据类型Number 数字型Int 整数型Pls_integer 整数型,产生溢出时出现错误Binary_integer 整数型,表示带符号的整数Char 定长字符型,最大255个字符 Varc......
使用PL/SQL Developer远程连接Oracle数据库,本地机器上已经安装了Oracle数据库只要1.配置tnsnames.ora(我的安装在D:\oracle\product\10.2.0\db_1\network\ADMIN)2.登录p......
解决win7 x64系统下PL/SQL无法连接64位Oracle数据库的方法#问题描述:确定安装好Oracle 11g 64位客户端、PL/SQL developer(不区分32/64)后,打开PL/SQL,无法选择数据库实例,如下图:#......
使用PLSQL 创建Oracle数据库Users用户并设置权限1、首先在 PLQL Developer 中打开数据库。使用 "sys" 用户,输入数据库名称,输入该数据库 ”sys” 用户的密码,在 ”Connect a......
