当前位置:网站首页>Pl/sql basic syntax
Pl/sql basic syntax
2022-07-05 22:01:00 【The sea of waves】
1. Process oriented language
Print hello word
declare
-- The explanatory part ( Variable , Cursor or exception )
begin
-- Procedural body
dbms_output.put_line("Hello Word");
end;
/ -- Exit the compilation environment , And execute this program
-- Turn on the output switch
set serveroutput on
/ -- perform
------ Output results -------
Hello World
-- Look at the structure of the package
desc dbms_output
Official grammar document
database-pl-sql-language-reference.pdf
Package related documentation
database-pl-sql-packages-and-types-reference.pdf
Define basic variables
type :char,varchar2,date,number,boolean,long
give an example : var1 char(15);
married boolean := true;
psal number(7,2);
Referential variables
pename emp.ename%type ; --pename The variable type will change with emp In the table ename The type of field changes
Be similar to pename varchar2(64);
Recordable variables
Be careful : Represents a line of records , There are different types in a row of records
emp_rec emp%rowtype;
select * into emp_rec from emp where empno= 7839;
if Use of statements
The first type
IF Conditions THEN sentence 1;
sentence 2;
END IF;
The second type
IF Conditions THEN sentence 1;
ELSE sentence 2;
END IF;
The third type
IF Conditions THEN sentence ;
ELSIF sentence THEN sentence ;
ELSE sentence ;
END IF;
Enter a number from the keyboard
Loop statement
The first type
While total <= 25000 Loop
.....
total := total+salary;
END LOOP;
The second type
Loop
EXIT[when Conditions ];
......
End loop;
The third type
FOR I IN 1...3 LOOp
Statement sequence ;
END LOOP;
cursor ( amount to Java Result set Resultset)
Properties of the cursor
%fund %notfund Judge whether the cursor gets a record
%isopen: Determine whether the cursor is on
%rowcount Number of rows affected
CURSOR Light label [( Parameter name data type [, Parameter name data type ]…)] IS SELECT sentence ;
for example cursor c1 is select ename from emp;
Limit on the number of cursors
By default ,oracle The database can only be in the same session , open 300 Cursors
Query parameters
Modify the number of cursors
alter system set open_cursors=400 scope=both;
Open the cursor :
open c1; Open the cursor to execute the query
Close the cursor :
close c1; Close the cursor to free resources
Take the value of a row of cursors :
fetch c1 into pename; Take a line into the variable
fetch The role of :
1) Returns the record that the current pointer points to
2) Point the pointer to the next record
The use of the cursor
1. Define a cursor cursor cname is select …
- Define a variable for the cursor pename emp.ename%type ; ( Reference type variable )
- Open the cursor open cname
- Take a record fetch cname intopename
- Close the cursor close cname
set serveroutput ondeclare-- Define a cursor cursor cemp is select ename,sal from emp;-- Define a cursor with parameters cursor cemp(dno number) is select ename from emp where deptno = dno;-- Define relative variables for the cursor pename emp.ename%type;psal emp.sal%type;begin -- Open cursor open cemp; loop -- Take a record fectch cemp into pename,psal; exit when cemp%notfound; -- Print dbms_output.putline(pename || ' New water volume '|| psal); end loop; -- Close the cursor close cemp; end; /
Be careful : If PL/SQL The data is modified in , Remember that commit ;
about Oracle , The default transaction isolation level is read commiteed ;
abnormal
Throw exceptions
declare The explanatory part ( Variable description 、 The cursor States 、 Exceptions )begin Statement sequence exception Exception handling statements end;/ give an example :exception when no_data_fund then dbms_output.put_line(" The employee was not found "); when others then dbms_output.put_line(' Other exceptions ');end;/
Common abnormal
no_data_fund No data found
too_many_rows Returns multiple lines of data
zero_divide : 0 Can't do divisor
value_error: Arithmetic or conversion error , For example, a variable of string type is converted into a numeric variable .
Custom exception
When an exception is thrown and the cursor is not closed normally ,Oracle It will start automatically pmon process (Process Monitor) Will automatically recycle garbage process , It's kind of like Java Medium GC
Last , Welcome to pay attention to my wechat , What do you like , Collection , Forwarding is my greatest encouragement .**
边栏推荐
- matlab绘制hsv色轮图
- EBS Oracle 11g cloning steps (single node)
- 【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
- 854. 相似度为 K 的字符串 BFS
- How to use tensorflow2 for cat and dog classification and recognition
- EL与JSTL注意事项汇总
- Blocking of concurrency control
- NET中小型企业项目开发框架系列(一个)
- Microservice link risk analysis
- 854. String BFS with similarity K
猜你喜欢
深信服X计划-网络协议基础 DNS
Defect detection - Halcon surface scratch detection
AD637使用筆記
华为快游戏调用登录接口失败,返回错误码 -1
MATLAB | App Designer·我用MATLAB制作了一款LATEX公式实时编辑器
装饰器学习01
Bitbucket installation configuration
AD637 usage notes
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
Yolov5 training custom data set (pycharm ultra detailed version)
随机推荐
大约SQL现场“这包括”与“包括在”字符串的写法
Database recovery strategy
How to view Apache log4j 2 remote code execution vulnerability?
Pointer parameter passing vs reference parameter passing vs value parameter passing
Interprocess communication in the "Chris Richardson microservice series" microservice architecture
HDU 4391 Paint The Wall 段树(水
让开发效率提升的跨端方案
1.2 download and installation of the help software rstudio
Livelocks and deadlocks of concurrency control
poj 3237 Tree(樹鏈拆分)
AD637使用笔记
EL与JSTL注意事项汇总
Oracle检查点队列–实例崩溃恢复原理剖析
Granularity of blocking of concurrency control
Advantages of robot framework
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
如何开发引入小程序插件
Analysis and test of ModbusRTU communication protocol
Chap2 steps into the palace of R language
Shell script, awk condition judgment and logic comparison &||