当前位置:网站首页>不得不会的Oracle数据库知识点(四)
不得不会的Oracle数据库知识点(四)
2022-07-04 00:33:00 【zhulin1028】
目录
一.使用PL/SQL
可用于创建存储过程,触发器,程序包,给SQL语句的执行添加程序逻辑。
支持SQL,在PL/SQL中可以使用:
数据操纵命令
事务控制命令
游标控制
SQL函数和SQL运算符
支持面向对象编程(OOP)
可移植性
更佳的性能,PL/SQL经过编译执行
分为三个部分:声明部分,可执行部分和异常处理部分
[declare
declarations]
begin
executable statements
[exception
handlers]
end;打开输出
set serverout on;二、游标管理
游标类型:隐式游标,显式游标,REF游标
REF游标用于处理运行时才能确定的动态SQL查询的结果
==========隐式游标==========
在PL/SQL中使用DML语句时自动创建隐式游标
隐式游标自动声明、打开和关闭,其名为SQL
隐式游标的属性:
%found SQL语句影响实质后返回true
%notfound SQL语句没有影响实质后返回true
%rowcount SQL语句影响的行数
%isopen 游标是否打开,始终为false
示例:
begin
update user_tbl set score=score+5;
if SQL%found then
dbms_output.put_line('数据被更改: '||SQL%rowcount);
elsif sql%notfound then
dbms_output.put_line('没有找到数据!');
end if;
if SQL%isopen then
dbms_output.put_line('Open');
else
dbms_output.put_line('Close');
end if;
end;==========显式游标==========
在PL/SQL的声明部分定义查询,该查询可以返回多行:
声明游标、打开游标、 从游标中取回数据、关闭游标
声明游标完成两个任务:
给游标命名
将一个查询与游标关联
cursor cursor_name is select statement;打开游标:
open cursor_name;取数据:
fetch cursor_name into record_list;关闭游标:
close cursor_name;显式游标的属性:
%found 执行最后一条fetch语句成功返回行时为true
%notfound 执行最后一条fetch语句未能返回行时为true
%rowcount 返回到目前为止游标提取的行数
%isopen 游标是否打开
示例:
带参的显式游标
declare
users user_tbl%rowtype;
cursor boys_cur(sexParam varchar2)
is select * from user_tbl where sex=sexParam;
begin
open boys_cur('&sex');
loop
fetch boys_cur into users;
exit when boys_cur%notfound;
dbms_output.put_line(users.user_name||' '||users.password);
dbms_output.put_line(boys_cur%rowcount);
end loop;
close boys_cur;
end;循环游标
declare
cursor user_cur is select * from user_tbl;
begin
for username in user_cur loop
dbms_output.put_line(username.user_name||' '||username.sex);
end loop;
end;REF游标:
REF游标和游标变量用于处理运行时动态执行的SQL查询
创建游标变量的步骤:
a、 声明REF游标类型
b、 声明REF游标类型的变量
声明类型的语法
Type ref_cursor_name is ref cursor [return return_type];打开游标变量的语法
Open cursor_name for select_statement;声明强类型的游标
declare
type ref_cur is ref cursor return user_tbl%rowtype;
users_cur ref_cur;声明弱类型的游标
declare
type ref_cur is ref cursor;
users_cur ref_cur;三、子程序
子程序分为:存储过程和函数,它是命名的PL/SQL块,编译并存储在数据库中。
子程序的各个部分:声明部分,可执行部分,异常处理部分。
过程----执行某些操作
函数----执行操作并返回值
==========存储过程==========
创建过程的语法:
create or replace procedure
proce_name (parameter_list)
is|as
local variable declaration
begin
executable statements
exception
exception_handlers
end proce_name;过程参数的三种模式:
In----用于接收调用的值,默认的参数模式
Out----用于向调用程序返回值
In out----用于接收调用程序的值,并向调用程序返回更新的值
执行过程的语法:
Execute proce_name(parameter_list);或
Declare
Variable var_list;
Begin
Proce_name(var_list);
End;将过程执行的权限授予其他用户:
Grant execute on proce_name to scott;
Grant execute on proce_name to public;删除存储过程:
Drop procedure proce_name;==========函数==========
创建函数的语法:
Create or replace function
Fun_name (parameter_list)
Return datatype is|as
Local declarations
Begin
Executable statements;
Return result;
Exception
Exce_handlers;
End;函数只能接收in参数,不能接受out或in out参数,形参不能是PL/SQL类型
函数的返回类型也必须是数据库类型
访问函数的方式:
a、 使用PL/SQL块
b、使用SQL语句
Select fun_name(parameter_list) from dual;边栏推荐
- Entropy and full connection layer
- [Mongodb] 2. Use mongodb --------- use compass
- (Video + graphics and text) introduction to machine learning series - Chapter 4 naive Bayes
- 【leetcode】300. Longest increasing subsequence (dynamic programming, dichotomy)
- Shell script three swordsman sed
- Kubedl hostnetwork: accelerating the efficiency of distributed training communication
- Double efficiency. Six easy-to-use pychar plug-ins are recommended
- No qualifying bean of type ‘com. netflix. discovery. AbstractDiscoveryClientOptionalArgs<?>‘ available
- AI Challenger 2018 text mining competition related solutions and code summary
- 2022 system integration project management engineer examination knowledge points: software development model
猜你喜欢

Idea a method for starting multiple instances of a service

Generic

It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something

2022 system integration project management engineer examination knowledge points: software development model

Analysis on the scale of China's smart health industry and prediction report on the investment trend of the 14th five year plan 2022-2028 Edition

(Introduction to database system | Wang Shan) Chapter V database integrity: Exercises
![[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections](/img/c6/3dc7d01600f6713afdbb4cf3df5238.jpg)
[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections

Vscode regular match replace console log(.*)

URL (data:image/png; Base64, ivborw0k... Use case

Investment demand and income forecast report of China's building ceramics industry, 2022-2028
随机推荐
(Introduction to database system | Wang Shan) Chapter V database integrity: Exercises
P1656 bombing Railway
ITK learning notes (VII) the position of ITK rotation direction remains unchanged
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
Pair
2022 system integration project management engineer examination knowledge points: software development model
Selenium library 4.5.0 keyword explanation (III)
What is regression testing? Talk about regression testing in the eyes of Ali Test Engineers
Smart fan system based on stm32f407
system. Exit (0) and system exit(1)
2022 Software Test Engineer skill list, please check
How to trade spot gold safely?
STM32 GPIO CSDN creative punch in
Gossip about redis source code 73
[BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
Suggestions for improving code quality
Global and Chinese market of melting furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
Axure resources and prototype tool Axure RP 9 download
Analysis: misunderstanding of choosing WMS warehouse management system
网上的低佣金链接安全吗?招商证券怎么开户?