当前位置:网站首页>Stored procedures and functions
Stored procedures and functions
2022-07-27 07:48:00 【Yun Wenwu】
stored procedure ( Behavior oriented )
stored procedure (Stored Procedure) In a large database system , A group of SQL Statements set (pl/sql Subroutines ), It's stored in a database , Once compiled, it's permanent , The user specifies the name of the stored procedure and gives the parameters ( If the stored procedure has parameters ) To execute it. .
Create stored procedure
One 、 Grammar format
create [or replace] procedure Stored procedure name [( Parameter list [in|out|in out] Parameter data type )]
{
is|as}
Declaration part ;
begin
pl/sql Code block ;
exception
Exception handling statements ;
end;
- or replace Indicates that if there is a stored procedure with the same name, it will overwrite , If not, create
- Note that the formal parameter data type cannot be given specific precision, that is, it cannot be given varchar2(21) After (21) part
Two 、 Parameterless stored procedure
create or replace procedure p1 is
begin
dbms_output.put_line('hello world');
end;
stay vs code The results after running in are as follows :
3、 ... and 、 Stored procedures with parameters
- in—— Enter the storage function from the outside , And cannot be changed by stored procedures ( In a stored procedure, it is similar to a constant being processed )
(1) Error model -1( Give... In the stored procedure in Type parameter assignment )
create or replace procedure p1(a IN NUMBER) is
begin
-- The statement is not allowed
a:=1;
-- Print a
dbms_output.put_line(a);
end;

(2) Correct demonstration
-- Receive an employee number , Output employee name and employee position
create OR REPLACE PROCEDURE p1(v_empno IN EMP.EMPNO%TYPE)
IS
-- Declare a variable
A EMP%ROWTYPE;
BEGIN
-- assignment
SELECT * INTO A FROM EMP WHERE EMPNO=v_empno;
-- Output
dbms_output.put_line(' Name of employee :'||a.ename||', Employee position :'||a.job);
END;

- out—— From stored procedure to external pl/sql Block , Can be changed by relevant contents in the stored procedure ( It can be seen as a variable thrown from a stored procedure ),out Variables must have corresponding variables externally
-- Throw the stored procedure variable value
create OR replace PROCEDURE p1(n OUT VARCHAR2) IS
a VARCHAR2(200):='hello world';
BEGIN
n:=a;
END;

- in out—— In and out , Both external program blocks and internal stored procedures can be changed
---- According to the department number entered by the user , Output department related information
create OR replace PROCEDURE p1(n IN OUT DEPT%ROWTYPE) IS
BEGIN
SELECT * INTO n FROM DEPT WHERE DEPTNO=n.DEPTNO;
END;
/
-- call
DECLARE
n DEPT%ROWTYPE;
BEGIN
n.DEPTNO:=& Please enter the department number ;
-- Note that the parameter here is n instead of n.deptno
p1(n);
dbms_output.put_line(n.DEPTNO||', '||n.dname||', '||n.loc);
END;

Calling stored procedure
One 、pl/sql Call in
begin
-- Stored procedure call with parameters
Stored procedure name ( Actual parameters 1, Actual parameters 2,..., Actual parameters n);
-- Stored procedure calls without parameters
Stored procedure name ;
Stored procedure name ();
end;
Two 、call call
The key codes are as follows :
--call There is a special point : Even if there is no parameter passing in the stored procedure, after the name of the stored procedure () Still cannot be omitted
call Stored procedure name ();
3、 ... and 、execute/exec call (cmd Window )
(1)win+R Open the command prompt to log in sqlplus
(2) open serveroutput service , The code is as follows :
set serveroutput on;

(3) Input exec/execute Stored procedure name ();
Delete stored procedure
drop procedure Stored procedure name ;
Other users use other stored procedures
(1) Log in to the stored procedure owner account , Grant user permission to use this process , The statement is as follows
grant execute on Stored procedure name to user name ;
- The user applies the stored procedure owner account when calling this function . Stored procedure name ; for example scott.p1
- Withdraw permission to use revoke…from… Sentence can be used
function ( Pay attention to the results )
Functions can be understood as special must have return Statement stored procedure
Create a function
One 、 Grammar format
create [or replace] function Function name ( Shape parameter [in|out|in out] Parameter data type ) return Return value data type
is
Declaration part ;
begin
pl/sql Code block ;
exception
exception handling ;
end;
Two 、 Parameterless function
create or replace function demo return varchar2
is
begin
return 'Hello World';
end;

3、 ... and 、 Functions with parameters ( Similar to stored procedures , I won't repeat , Relevant examples are given as follows )
- in
-- Output user input
create or replace function demo(a IN VARCHAR2) return varchar2
is
begin
return a;
end;

- out
-- Throw the value in the function (return It can also be regarded as a out Parameters )
create or replace function demo(a OUT NUMBER) return NUMBER
is
b NUMBER:=3;
begin
a:=b;
return a;
end;

- in out
-- Enter the employee number and return the employee name
create or replace function demo(a IN OUT EMP%ROWTYPE) return EMP%ROWTYPE
is
begin
SELECT * INTO a FROM EMP WHERE EMPNO=a.EMPNO;
return a;
end;
/
-- call
DECLARE
-- Carrying formal parameter variables
q EMP%ROWTYPE;
-- Carrying function variables
e EMP%ROWTYPE;
BEGIN
q.EMPNO:=& Please enter employee number ;
e:=DEMO(A => q /*OUT NUMBER*/);
dbms_output.put_line(q.ename);
END;

Call function
- sql Call in
select Function name ( Argument list ) from dual;
- pl/sql Call in
-- Because the function must have return Then we can treat the function as a value
declare
Carrying function variables return Return value data type ;
begin
Carrying function variables := Function name ( Argument list );
-- What's printed here is return Return value content
dbms_output.put_line( Carrying function variables );
end;
Delete function
drop function Function name ;
View all stored procedures and functions under the current user
SELECT * FROM USER_PROCEDURES;
边栏推荐
- Framework of electronic mass production project -- basic idea
- Shell condition test, judgment statement and operator of shell system learning
- C# 事件用法案例 订阅事件+=
- Do me a favor ~ don't pay attention, don't log in, a questionnaire in less than a minute
- 单片机多级菜单
- Demo submit a program and obtain ALV data of the program
- DEMO SUBMIT 某程序并获取该程序ALV数据
- 【万字长文】吃透负载均衡,和阿里大牛的技术面谈
- 什么是真正的HTAP?(一)背景篇
- The token verification of applet message push configuration failed. Please check and confirm
猜你喜欢

Cadence (XI) silk screen printing adjustment and subsequent matters

Am I delayed by the code... Unfortunately, I became a programmer

【已解决】新版Pycharm(2022)连接服务器进行上传文件报错“Command rsync is not found in PATH”,无法同步文件

Lua迭代器

小程序支付管理-新版支付对接流程

记录一个自己挖的坑~

Framework of electronic mass production project -- basic idea

Help send a recruitment, base all over the country. If you are interested, you can come and have a look

自动化测试的使用场景

JS regular expression implementation adds a comma to every three digits
随机推荐
「翻译」SAP变式物料的采购如何玩转?看看这篇你就明白了
Plato farm is expected to further expand its ecosystem through elephant swap
How to get DDL information of an object
存储过程与函数
Practical new drug R & D project management platform
小程序消息推送配置 Token校验失败,请检查确认
Primary key in MySQL secondary index - MySQL has a large number of same data paging query optimization
【已解决】单点登录成功SSO转发,转发URL中带参数导致报错There was an unexpected error (type=Internal Server Error, status=500)
【已解决】新版Pycharm(2022)连接服务器进行上传文件报错“Command rsync is not found in PATH”,无法同步文件
C语言:随机生成数+插入排序
Shell awk related exercises
yhb_ sysbench
Temperature and humidity measurement and display device based on Arduino
SQL labs SQL injection platform - level 1 less-1 get - error based - Single Quotes - string (get single quote character injection based on errors)
Demo submit a program and obtain ALV data of the program
【小程序】uniapp发行微信小程序上传失败Error: Error: {'errCode':-10008,'errMsg':'invalid ip...
[stonedb class] introductory lesson 1: popular science of database knowledge
Zero training platform course-1. SQL injection Foundation
shell循环练习
安装tensorflow