当前位置:网站首页>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;
边栏推荐
- AI Challenger 2018 text mining competition related solutions and code summary
- What is regression testing? Talk about regression testing in the eyes of Ali Test Engineers
- [common error] UART cannot receive data error
- Function: find the approximate value of the limit of the ratio of the former term to the latter term of Fibonacci sequence. For example, when the error is 0.0001, the function value is 0.618056.
- For loop
- Wechat official account and synchronization assistant
- MySQL is installed as a Windows Service
- Software testers, how can you quickly improve your testing skills? Ten minutes to teach you
- A dichotomy of Valentine's Day
- Regular expression of shell script value
猜你喜欢
@EnableAsync @Async
The super fully automated test learning materials sorted out after a long talk with a Tencent eight year old test all night! (full of dry goods
Unity Shader入门精要读书笔记 第三章 Unity Shader基础
Reading notes on how programs run
On the day when 28K joined Huawei testing post, I cried: everything I have done in these five months is worth it
Sequence list and linked list
1-redis architecture design to use scenarios - four deployment and operation modes (Part 1)
[common error] UART cannot receive data error
A method to solve Bert long text matching
MySQL 8.0.12 error: error 2013 (HY000): lost connection to MySQL server during query
随机推荐
AI Challenger 2018 text mining competition related solutions and code summary
[CSDN Q & A] experience and suggestions
功能:求5行5列矩阵的主、副对角线上元素之和。注意, 两条对角线相交的元素只加一次。例如:主函数中给出的矩阵的两条对角线的和为45。
Unity Shader入门精要读书笔记 第三章 Unity Shader基础
1-redis architecture design to use scenarios - four deployment and operation modes (Part 1)
Qtcharts notes (V) scatter diagram qscatterseries
Release and visualization of related data
Global and Chinese markets for coronary artery disease treatment devices 2022-2028: Research Report on technology, participants, trends, market size and share
A dichotomy of Valentine's Day
OS interrupt mechanism and interrupt handler
@EnableAsync @Async
Global and Chinese market of melting furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
ESP Arduino playing with peripherals (V) basic concept of interrupt and timer interrupt
Pytest unit test framework: simple and easy to use parameterization and multiple operation modes
STM32 GPIO CSDN creative punch in
机器学习基础:用 Lasso 做特征选择
功能:将主函数中输入的字符串反序存放。例如:输入字符串“abcdefg”,则应输出“gfedcba”。
What is regression testing? Talk about regression testing in the eyes of Ali Test Engineers
ITK learning notes (VII) the position of ITK rotation direction remains unchanged
Global and Chinese market of underwater bags 2022-2028: Research Report on technology, participants, trends, market size and share