当前位置:网站首页>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 .**

边栏推荐
- POJ 3237 tree (tree chain splitting)
- ICMP 介绍
- Matlab | app designer · I used Matlab to make a real-time editor of latex formula
- Codeforces 12D Ball 树形阵列模拟3排序元素
- Code bug correction, char is converted to int high-order symbol extension, resulting in changes in positivity and negativity and values. Int num = (int) (unsigned int) a, which will occur in older com
- 2.2.3 output of documents
- Huawei cloud modelarts text classification - takeout comments
- AD637 usage notes
- Concurrency control of performance tuning methodology
- Type of fault
猜你喜欢

database mirroring

Shell script, awk uses if, for process control

An exception occurred in Huawei game multimedia calling the room switching method internal system error Reason:90000017

资深电感厂家告诉你电感什么情况会有噪音电感噪音是比较常见的一种电感故障情况,如果使用的电感出现了噪音大家也不用着急,只需要准确查找分析出什么何原因,其实还是有具体的方法来解决的。作为一家拥有18年品牌

MMAP learning

Bitbucket installation configuration

2.2.3 output of documents

Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~

Overview of concurrency control

Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
随机推荐
Type of fault
Granularity of blocking of concurrency control
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
Recovery technology with checkpoints
oracle 控制文件的多路复用
AD637使用笔记
装饰器学习01
Official clarification statement of Jihu company
Oracle检查点队列–实例崩溃恢复原理剖析
让开发效率提升的跨端方案
如何向mongoDB中添加新的字段附代码(全)
MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server
华为联机对战如何提升玩家匹配成功几率
The American Championship is about to start. Are you ready?
阿龙的感悟
Advantages and disadvantages of the "Chris Richardson microservice series" microservice architecture
QML reported an error expected token ";", expected a qualified name ID
Implementation technology of recovery
Daily question brushing record (XIV)
Summary of El and JSTL precautions