当前位置:网站首页>Introduction to pl/sql, very detailed notes
Introduction to pl/sql, very detailed notes
2022-07-25 11:52:00 【Whew, fly over】
PL/SQL introduction , Very detailed notes
-- PL/SQL brief introduction :
1.PL/SQl It's process language PL And structured languages SQL Combined programming language
2.PL/SQL The engine resides in Oracle Server
3. The engine receives PL/SQL Block and compile it
-- PL/SQL block :
It's the composition PL/SQL The basic unit of composition , Combine logically related declarations and statements
PL/SQL It's divided into three parts , Declaration part , The executable part , And exception handling
-- PL/SQL grammar :
[declare
Declaration part ]
[begin
The executable part
]
[exception
Exception handling part
end
]
/* PL/SQL Is a strongly typed programming language , All variables must be declared before they can be used , Variables are required in declare Partial declaration , There are the following provisions for variable names : 1. Variable names can be alphanumeric underscores $# Other components 2. All variable names should start with a letter , It can't be Oracle Keywords in 3. The length of the variable can only be 30 Characters -- In order to improve the PL/SQL Readability , All variables use "v_ Variable name " Define */
declare
v_name varchar2(20) :=' Who am I ?';
begin
-- Output operation
dbms_output.put_line('pl/sql The output operation of ' || v_name); -- PL/SQL Use... In string || Connect
end;
-- Use into Assign values to variables
declare
v_name varchar2(20);
begin
-- adopt SQL Statement and into Keyword assigns a value to a variable
select uname into v_name from user_book where userid=9999;
dbms_output.put_line(v_name); -- Print
end;
-- %type The type of the specified field
declare
v_name user_book.uname%type;
v_id user_book.userid%type;
begin
-- Assign values to fields
select user_book.uname,user_book.userid into v_name,v_id
from user_book
where user_book.userid=9999;
-- Output to see the results
dbms_output.put_line(v_id || v_name);
end;
-- %rowtype Data representing the entire row in the table
declare
v_row user_book%rowtype;
begin
-- assignment
select * into v_row from user_book where user_book.userid=9999;
-- Output the data in the row : Note that at this time, all fields in the row must be written into the output statement manually , Otherwise, an error will be reported
dbms_output.put_line(v_row.uname||v_row.userid||v_row.urole );
end;
-- Conditional statements
/* 1. if else sentence : if Conditions 1 then Meet the conditions 1 The statement executed when else Conditions 2 then Meet the conditions 2 The statement executed when */
/* 2. case when sentence : case when Conditions 1 then Meet the conditions 1 Code executed at when Conditions 2 then Meet the conditions 2 Code executed at when Conditions 3 then Meet the conditions 3 Code executed at else Code executed when none of the above conditions are met end case; */
-- loop
/* -- 1.exit when Exit loop : loop Statement block executed in a loop ; exit when Cycle end condition ; Step statement ; end loop; */
declare
v_i int := 0; -- Declare variables
begin
loop
dbms_output.put_line(v_i); -- Cyclic block : Print v_i
exit when v_i >= 10; -- The end condition
v_i := v_i+1; -- Step statement
end loop;
end;
/* -- 2.while loop : while Cycle end condition loop The loop body ; Step statement ; end loop; */
declare
v_i int := 0; -- Declare variables
begin
while exit when v_i >= 10; -- The end condition
loop
dbms_output.put_line(v_i); -- Cyclic block : Print v_i
v_i := v_i+1; -- Step statement
end loop;
end;
/* -- 3.for in loop : for Circular index variable in [recerse] -- reverse: reverse Lower limit of cycle area .. Loop area online loop Loop statement block ; end loop ; */
declare
begin
for i in 1..10 -- from 1 To 10 positive sequence
loop
dbms_output.put_line(i); -- Print i
end loop;
end;
-- exception handling
/* 1. Definition : Errors that occur when running exceptions are called exceptions 2. characteristic : When something goes wrong , Statement will stop executing , The transfer of control to PL/SQL The exception handling part of the block 3. Three types of exceptions : Predefined exceptions : When PL/SQL Procedural violation Oracle Implicitly raised when rules or system limits are exceeded ; Users do not need to define ; Non predefined exception : When PL/SQL Procedural violation Oracle Caused when rules or system limits are exceeded ; Users need to define ; User defined exception : The user is required to define , Explicitly raise ; */
-- Syntax of exception handlers :
begin
General code block ;
exception
when Abnormal conditions 1 Satisfy then Abnormal conditions 1 Code to execute when satisfied
when Abnormal conditions 2 Satisfy then Abnormal conditions 2 Code to execute when satisfied
when others then Code executed when none of them are satisfied
end;
-- Common system exceptions :
-- Excessive data : to_many_rows
-- The query data does not exist : no_data_found
declare
v_error exception;
v_name user_book.uname%type;
begin
select uname into v_name from user_book where user_book.userid=100000; -- At this time userid It doesn't exist
end;
-- User defined exception
-- -----------------------------------
declare
v_error exception;
v_name user_book.uname%type;
begin
select uname into v_name from user_book where user_book.userid=100000;
if v_name = ' Zhang San ' then -- If the condition is met, return v_error
raise v_error;
end if;
exception
when v_error then -- Triggered when this exception occurs
dbms_output.put_line(' It was Zhang San , Throw an exception to you ');
end;
边栏推荐
猜你喜欢

What is the global event bus?

Management of software defects

Functions in JS

varest蓝图设置json

SQL injection less23 (filter comment)

The principle analysis of filter to solve the request parameter garbled code

小微企业智能名片管理小程序

W5500 is in TCP_ In server mode, you cannot Ping or communicate in the switch / router network.

Teach you how to configure S2E to UDP working mode through MCU

Leetcode sword finger offer 27. image of binary tree
随机推荐
JS中的函数
微星主板前面板耳机插孔无声音输出问题【已解决】
基于W5500实现的考勤系统
[MySQL 17] installation exception: could not open file '/var/log/mysql/mysqld log‘ for error logging: Permission denied
已解决The JSP specification requires that an attribute name is preceded by whitespace
brpc源码解析(四)—— Bthread机制
Web APIs(获取元素 事件基础 操作元素)
Emmet syntax quick query syntax basic syntax part
SQL language (I)
Hacker introductory tutorial (very detailed) from zero basic introduction to proficiency, it is enough to read this one.
Teach you how to configure S2E as the working mode of TCP client through MCU
brpc源码解析(一)—— rpc服务添加以及服务器启动主要过程
Management of software defects
【电子器件笔记5】二极管参数和选型
基于MATLAB的常见线性调制方法
Similarity matrix, diagonalization condition
第一个C语言程序(从Hello World开始)
JS运算符
软件测试阶段的风险
toString()与new String()用法区别