oracle存储过程Word文档格式.docx

上传人:b****6 文档编号:21644291 上传时间:2023-01-31 格式:DOCX 页数:17 大小:141.39KB
下载 相关 举报
oracle存储过程Word文档格式.docx_第1页
第1页 / 共17页
oracle存储过程Word文档格式.docx_第2页
第2页 / 共17页
oracle存储过程Word文档格式.docx_第3页
第3页 / 共17页
oracle存储过程Word文档格式.docx_第4页
第4页 / 共17页
oracle存储过程Word文档格式.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

oracle存储过程Word文档格式.docx

《oracle存储过程Word文档格式.docx》由会员分享,可在线阅读,更多相关《oracle存储过程Word文档格式.docx(17页珍藏版)》请在冰豆网上搜索。

oracle存储过程Word文档格式.docx

HelloWorld

PL/SQL过程已成功完成。

变量声明

1declare

2v_namevarchar2(20);

3begin

4v_name:

='

myname'

;

5dbms_output.put_line('

6*end;

常用变量类型

使用%type

v_empno2将随emp表中的empno的变化

Table变量:

Record变量类型

使用%rowtype声明record变量

适应表结构的变化

语句块的组成

2v_numnumber:

=0;

--声明类型为number的变量v_num,并赋值0

4v_num:

=2/v_num;

--相当于java中的v_num=2/v_num

5dbms_output.put_line(v_num);

6exception--异常

7whenothersthen

8dbms_output.put_line('

error'

9*end;

error

“--”开头为PL-SQL的单行注释

在PL-SQL语句中有且只有一条记录。

SQL语句的运用

select

2v_enameemp.ename%type;

3v_salemp.sal%type;

4begin

5selectename,salintov_ename,v_salfromempwhereempno=7369;

6dbms_output.put_line(v_ename||'

'

||v_sal);

7*end;

SMITH800

into为:

将select到的数据ename,sal写入到v_ename,v_sal中。

createtable

begin

2executeimmediate'

createtableT(nnnvarchar2(20)default'

'

aaa'

)'

3end;

4/

查看结果:

select*fromtab;

TNAMETABTYPECLUSTERID

-----------------------------------------------

DEPTTABLE

EMPTABLE

BONUSTABLE

SALGRADETABLE

BIN$VP4S+C3rRkWg+GPMXARASw==$0TABLE

DEPT1TABLE

TTABLE

已选择7行。

判断、选择、循环

判断

2v_salemp.sal%type;

4selectsalintov_salfromempwhereempno=7369;

5if(v_sal<

1200)then

6dbms_output.put_line('

low'

7elsif(v_sal<

2000)then

middle'

9else

10dbms_output.put_line('

high'

11endif;

12*end;

low

循环

简单循环

相当于java中的do循环

2ibinary_integer:

=1;

4loop

5dbms_output.put_line(i);

6i:

=i+1;

7exitwhen(i>

=11);

8endloop;

1

2

3

4

5

6

7

8

9

10

while

2jbinary_integer:

4whilej<

11loop

5dbms_output.put_line(j);

6j:

=j+1;

7endloop;

8*end;

for

2forkin1..10loop

3dbms_output.put_line(k);

endloop;

4forkinreverse1..10loop

5dbms_output.put_line(k);

7/

123456789101

Exception

too_many_rows

2v_tempnumber(4);

4selectempnointov_tempfromempwheredeptno=10;

5exception

6whentoo_many_rowsthen

7dbms_output.put_line('

太多记录了'

8whenothersthen

9dbms_output.put_line('

10*end;

太多记录了

no_data_found

4selectempnointov_tempfromempwhereempno=2222;

6whenno_data_foundthen

没数据'

没数据

记录错误

记录用表

设置表为递增记录

尝试删除

游标cursor

游标的定义

简单循环输出

2cursorcis--游标的定义

3select*fromemp;

4v_empc%rowtype;

5begin

6openc;

7loop

8fetchcintov_emp;

9exitwhen(c%notfound);

--注意顺序

10dbms_output.put_line(v_emp.ename);

11endloop;

12closec;

13*end;

SMITH

ALLEN

WARD

JONES

MARTIN

BLAKE

CLARK

SCOTT

KING

TURNER

ADAMS

JAMES

FORD

MILLER

错误的输出做法

2cursorcis

3select*fromemp;

4v_empc%rowtype;

9dbms_output.put_line(v_emp.ename);

10exitwhen(c%notfound);

12closec;

14/

while循环输出

for循环输出(最佳方式)

带参数的游标

可更新的游标(了解)

存储过程produce

创建存储过程

执行存储过程

方式一:

execp;

方式二:

p;

end;

带参数的存储过程

createorreplaceprocedurep(v_ainnumber,v_bnumber,v_retoutnumber,v_tempinoutnumber)

is

if(v_a>

v_b)then

v_ret:

=v_a;

elsev_ret:

=v_b;

endif;

v_temp:

=v_temp+1;

调用上述过程

编译错误

oracle不直接提示错误,可通过showerror显示错误:

删除存储过程

函数

创建函数

函数调用

触发器

使用到的表emp2_log

创建触发器

测试

更新数据

查看emp2_log表

删除触发器

触发器的副作用(很少用)

以下这种情况直接update是不行的。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1