当前位置:网站首页>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 .**
边栏推荐
- Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!
- 总结出现2xx、3xx、4xx、5xx状态码的原因
- NET中小型企业项目开发框架系列(一个)
- A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
- Granularity of blocking of concurrency control
- Summarize the reasons for 2XX, 3xx, 4xx, 5xx status codes
- 每日刷题记录 (十四)
- MATLAB | App Designer·我用MATLAB制作了一款LATEX公式实时编辑器
- 华为联机对战如何提升玩家匹配成功几率
- Kingbasees v8r3 cluster maintenance case -- online addition of standby database management node
猜你喜欢
Database tuning solution
Recovery technology with checkpoints
DBeaver同时执行多条insert into报错处理
深信服X计划-网络协议基础 DNS
总结出现2xx、3xx、4xx、5xx状态码的原因
资深电感厂家告诉你电感什么情况会有噪音电感噪音是比较常见的一种电感故障情况,如果使用的电感出现了噪音大家也不用着急,只需要准确查找分析出什么何原因,其实还是有具体的方法来解决的。作为一家拥有18年品牌
Server optimization of performance tuning methodology
华为快游戏调用登录接口失败,返回错误码 -1
Installation of VMware Workstation
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
随机推荐
A long's perception
Detailed explanation of memset() function usage
K210 learning notes (IV) k210 runs multiple models at the same time
The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
Cold violence -- another perspective of objective function setting
EL与JSTL注意事项汇总
K210学习笔记(四) K210同时运行多个模型
Defect detection - Halcon surface scratch detection
AD637使用筆記
[Yugong series] go teaching course in July 2022 004 go code Notes
如何开发引入小程序插件
Form artifact
PyGame practical project: write Snake games with 300 lines of code
How to add new fields to mongodb with code (all)
Overview of concurrency control
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
2.2.5 basic sentences of R language drawing
2.2.3 output of documents
Summarize the reasons for 2XX, 3xx, 4xx, 5xx status codes
The real situation of programmers