代码改变世界

Oracle procedure存储过程/function函数

  Evan.Pei  阅读(371)  评论(0编辑  收藏  举报
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
--函数的创建
create function func1(dno number)
return NUMBER--必须带有返回值
is
  v_max number;--定义返回值
  begin
    select  max(sal) into v_max--赋值
    from emp where deptno= dno;
    RETURN v_max;--返回
  end;
--函数的调用,只能是表达式的一部分,不能单独调用
 select * from emp where sal= func1(10);
 -------------------------------------------------------------------------------
 --创建过程存在则替换,带输出参数
 create or replace procedure proc1(dno in number,maxsal out number)----------参数不能有(10)这样的长度控制
 is
  v_maxsal number;
 begin
  select max(sal) into v_maxsal
  from emp where deptno= dno;
  maxsal := v_maxsal;--给输出参数赋值
  dbms_output.put_line(v_maxsal);--打印
 end;
  
 --调用过程
 declare maxsal number;
 begin
  proc1(10,maxsal);
  dbms_output.put_line(maxsal);
 end;
--------------------------------------------------------------------------------
/*
 创建返回多行的存储过程
*/
create or REPLACE procedure proc_test
is
begin
   DECLARE cursor cc is SELECT * from emp;--这里定义一个游标集合变量cc
   begin
       
      for r in cc loop--对游标cc进行遍历
         dbms_output.put_line(r.ename || ':' || r.sal);
      end loop;--结束遍历
   end;
   end;
   ---调用
   begin
    proc_test();
   end;

  1.函数必须是和语句一起执行,不能被单独执行,必须有返回值。

      2存储过程 可以被单独执行,没有返回值。

下面是在PL/SQL中使用动态sql语句:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-------Oracle中 DML:insert,delete,update
-------TCL:COMMIT,ROLLBACK,SAVEPOINT ONT,ROLLBACK TO ONE;
-------DQL:SELECT
--PL/SQL 中使用本地动态sql
DECLARE
str VARCHAR2(200);
outid number;
begin
 --values(:1,:2)占位符,returning id into :3将id赋值给第三个参数
 str :='insert into test123 values(:1,:2) returning id into :3';
 execute immediate str --运行动态sql
 using 1,'php'--给占位符赋值 1 2
 RETURNING into outid;--3
 dbms_output.put_line(outid);
end;

  

 

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示