当前位置:网站首页>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;
边栏推荐
- Menu bar + status bar + toolbar ==pyqt5
- brpc源码解析(一)—— rpc服务添加以及服务器启动主要过程
- 30套中国风PPT/创意PPT模板
- brpc源码解析(五)—— 基础类resource pool详解
- 已解决 Files‘ name is invalid or does not exist (1205)
- Filter过滤器解决request请求参数乱码的原理解析
- JVM performance tuning methods
- cookie and session
- 【MySQL 17】安装异常:Could not open file ‘/var/log/mysql/mysqld.log‘ for error logging: Permission denied
- Varest blueprint settings JSON
猜你喜欢
随机推荐
W5500多节点连接
Miidock Brief
Hardware peripherals =maixpy3
小微企业智能名片管理小程序
JS常用内置对象 数据类型的分类 传参 堆栈
城市雕塑典型作品信息管理系统(图片分享系统SSM)
Emmet syntax quick query syntax basic syntax part
菜单栏+状态栏+工具栏==PYQT5
Attendance system based on w5500
Information management system for typical works of urban sculpture (picture sharing system SSM)
SQL language (4)
Brpc source code analysis (VIII) -- detailed explanation of the basic class eventdispatcher
Signal and slot mechanism ==pyqt5
擎创科技加入龙蜥社区,共建智能运维平台新生态
[electronic device notes 5] diode parameters and selection
贪心问题01_活动安排代码分析
Greedy problem 01_ Activity arrangement code analysis
varest蓝图设置json
基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
Introduction to shortcut keys in debug chapter









