当前位置:网站首页>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 .**
边栏推荐
- Analyse des risques liés aux liaisons de microservices
- PIP install beatifulsoup4 installation failed
- About the writing method of SQL field "this includes" and "included in" strings
- Decorator learning 01
- Drawing HSV color wheel with MATLAB
- POJ 3237 tree (tree chain splitting)
- 初级软件测试必问面试题
- Server optimization of performance tuning methodology
- Web3为互联网带来了哪些改变?
- Poj 3237 Tree (Tree Chain Split)
猜你喜欢
Create a virtual machine on VMware (system not installed)
Recovery technology with checkpoints
总结出现2xx、3xx、4xx、5xx状态码的原因
1.2 download and installation of the help software rstudio
Serializability of concurrent scheduling
Advantages and disadvantages of the "Chris Richardson microservice series" microservice architecture
[Yugong series] go teaching course in July 2022 004 go code Notes
华为云ModelArts文本分类–外卖评论
Meituan dynamic thread pool practice ideas, open source
Exercise 1 simple training of R language drawing
随机推荐
让开发效率提升的跨端方案
华为云ModelArts文本分类–外卖评论
Poj3414广泛搜索
AD637使用笔记
Recovery technology with checkpoints
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
Learning of mall permission module
Codeforces 12D ball tree array simulation 3 sorting elements
regular expression
Serializability of concurrent scheduling
A long's perception
Detailed explanation of memset() function usage
NET中小型企业项目开发框架系列(一个)
Analyse des risques liés aux liaisons de microservices
Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
HDU 4391 Paint The Wall 段树(水
Form artifact
2.2.3 output of documents
Shell script, awk uses if, for process control