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

边栏推荐
- ICMP introduction
- Net small and medium-sized enterprise project development framework series (one)
- Robot framework setting variables
- Codeforces 12D ball tree array simulation 3 sorting elements
- AD637使用筆記
- Official clarification statement of Jihu company
- Analysis and test of ModbusRTU communication protocol
- 初级软件测试必问面试题
- The real situation of programmers
- Reptile practice
猜你喜欢

每日刷题记录 (十四)

Bitbucket installation configuration

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

MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server

How can Huawei online match improve the success rate of player matching

多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争

EBS Oracle 11g cloning steps (single node)

MMAP learning

1.2 download and installation of the help software rstudio

华为游戏多媒体服务调用屏蔽指定玩家语音方法,返回错误码3010
随机推荐
Huawei game multimedia service calls the method of shielding the voice of the specified player, and the error code 3010 is returned
Chap2 steps into the palace of R language
如何组织一场实战攻防演练
总结出现2xx、3xx、4xx、5xx状态码的原因
Huawei fast game failed to call the login interface, and returned error code -1
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
如何向mongoDB中添加新的字段附代码(全)
华为快游戏调用登录接口失败,返回错误码 -1
Deeply convinced plan X - network protocol basic DNS
Concurrency control of performance tuning methodology
怎么利用Tensorflow2进行猫狗分类识别
DBeaver同时执行多条insert into报错处理
Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!
PIP install beatifulsoup4 installation failed
Tips for using SecureCRT
Lightweight dynamic monitorable thread pool based on configuration center - dynamictp
MMAP learning
Exercise 1 simple training of R language drawing
Reptile practice
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