当前位置:网站首页>Oracle database knowledge points (IV)
Oracle database knowledge points (IV)
2022-07-04 00:41:00 【zhulin1028】
Catalog
One . Use PL/SQL
Can be used to create stored procedures , trigger , Package , to SQL Add program logic to the execution of the statement .
Support SQL, stay PL/SQL Can be used in :
Data manipulation command
Transaction control command
slider control
SQL Functions and SQL Operator
Support for object-oriented programming (OOP)
Portability
Better performance ,PL/SQL After compiling and executing
It's divided into three parts : Declaration part , Executable part and exception handling part
[declare
declarations]
begin
executable statements
[exception
handlers]
end;
Open output
set serverout on;
Two 、 Cursor management
cursor type : Implicit cursors , Explicit cursors ,REF The cursor
REF Cursors are used to handle dynamic objects that can only be determined at run time SQL Result of query
========== Implicit cursors ==========
stay PL/SQL Use in DML Statement automatically creates an implicit cursor
Implicit cursors automatically declare 、 Open and close , Its name is SQL
Properties of implicit cursors :
%found SQL Statement affects the substance and returns true
%notfound SQL The statement returns... After it has no effect on the substance true
%rowcount SQL The number of lines affected by the statement
%isopen Whether the cursor is open , Always be false
Example :
begin
update user_tbl set score=score+5;
if SQL%found then
dbms_output.put_line(' The data has been changed : '||SQL%rowcount);
elsif sql%notfound then
dbms_output.put_line(' No data found !');
end if;
if SQL%isopen then
dbms_output.put_line('Open');
else
dbms_output.put_line('Close');
end if;
end;
========== Explicit cursors ==========
stay PL/SQL The declaration section of defines the query , The query can return multiple rows :
declare cursor 、 Open cursor 、 Retrieve data from a cursor 、 Close cursor
Declare that the cursor performs two tasks :
Name the cursor
Associate a query with a cursor
cursor cursor_name is select statement;
Open cursor :
open cursor_name;
Take the data :
fetch cursor_name into record_list;
Close cursor :
close cursor_name;
Explicit cursor properties :
%found Carry out the last one fetch When the statement returns a row successfully, it is true
%notfound Carry out the last one fetch Statement fails to return a row true
%rowcount Returns the number of rows fetched by the cursor so far
%isopen Whether the cursor is open
Example :
An explicit cursor with parameters
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;
Loop cursor
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 The cursor :
REF Cursors and cursor variables are used to handle dynamically executed at run time SQL Inquire about
To create a cursor variable :
a、 Statement REF cursor type
b、 Statement REF Variable of cursor type
Syntax for declaring types
Type ref_cursor_name is ref cursor [return return_type];
Syntax for opening cursor variables
Open cursor_name for select_statement;
Declare strongly typed cursors
declare
type ref_cur is ref cursor return user_tbl%rowtype;
users_cur ref_cur;
Declare weakly typed cursors
declare
type ref_cur is ref cursor;
users_cur ref_cur;
3、 ... and 、 Subroutines
Subroutines are divided into : Stored procedures and functions , It is named PL/SQL block , Compile and store in the database .
Each part of the subroutine : Declaration part , The executable part , Exception handling part .
The process ---- Do something
function ---- Perform the operation and return the value
========== stored procedure ==========
The syntax of the creation process :
create or replace procedure
proce_name (parameter_list)
is|as
local variable declaration
begin
executable statements
exception
exception_handlers
end proce_name;
Three modes of process parameters :
In---- The value used to receive the call , Default parameter mode
Out---- Used to return a value... To the caller
In out---- Used to receive the value of the calling program , And return the updated value to the caller
The syntax of the execution process :
Execute proce_name(parameter_list);
or
Declare
Variable var_list;
Begin
Proce_name(var_list);
End;
Grant process execution permission to other users :
Grant execute on proce_name to scott;
Grant execute on proce_name to public;
Delete stored procedure :
Drop procedure proce_name;
========== function ==========
The syntax for creating functions :
Create or replace function
Fun_name (parameter_list)
Return datatype is|as
Local declarations
Begin
Executable statements;
Return result;
Exception
Exce_handlers;
End;
Function can only receive in Parameters , Can't accept out or in out Parameters , Formal parameters cannot be PL/SQL type
The return type of a function must also be a database type
How to access functions :
a、 Use PL/SQL block
b、 Use SQL sentence
Select fun_name(parameter_list) from dual;
边栏推荐
- Five high-frequency questions were selected from the 200 questions raised by 3000 test engineers
- What is the GPM scheduler for go?
- It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction
- Is the securities account opened by Caicai for individuals safe? Is there a routine
- leetcode 121 Best Time to Buy and Sell Stock 买卖股票的最佳时机(简单)
- Similarities and differences of text similarity between Jaccard and cosine
- Optimization of for loop
- (Video + graphics and text) introduction to machine learning series - Chapter 4 naive Bayes
- Global and Chinese markets for coronary artery disease treatment devices 2022-2028: Research Report on technology, participants, trends, market size and share
- [leetcode] interview question 17.08 Circus tower
猜你喜欢
Release and visualization of related data
It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction
(Introduction to database system | Wang Shan) Chapter V database integrity: Exercises
Unity elementary case notes of angry birds Siki college 1-6
@EnableAsync @Async
功能:编写函数fun求s=1^k+2^k +3^k + ......+N^k的值, (1的K次方到N的K次方的累加和)。
Mobile asynchronous sending SMS verification code solution -efficiency+redis
Regular expressions and text processors for shell programming
Unity Shader入门精要读书笔记 第三章 Unity Shader基础
OS interrupt mechanism and interrupt handler
随机推荐
How to use AHAS to ensure the stability of Web services?
GUI 应用:socket 网络聊天室
How to set the response description information when the response parameter in swagger is Boolean or integer
Understanding of Radix
[cloud native topic -48]:kubesphere cloud Governance - operation - overview of multi tenant concept
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
[PHP basics] session basic knowledge, application case code and attack and defense
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
The difference between objects and objects
NLP pre training technology development
12. Go implementation of integer to Roman numeral and leetcode
From functional testing to automated testing, how did I successfully transform my salary to 15K +?
Future源码一观-JUC系列
MySQL winter vacation self-study 2022 12 (2)
功能:求5行5列矩阵的主、副对角线上元素之和。注意, 两条对角线相交的元素只加一次。例如:主函数中给出的矩阵的两条对角线的和为45。
Arc 135 supplementary report
Data storage - interview questions
Global and Chinese markets for blood and liquid heating devices 2022-2028: Research Report on technology, participants, trends, market size and share
Software testers, how can you quickly improve your testing skills? Ten minutes to teach you
Makefile judge custom variables