当前位置: 首页 > news >正文

Oracle动态SQL和静态SQL比较

1.静态SQLSQL与动态SQL
Oracle编译PL/SQL程序块分为两个种:其一为前期联编(early binding),即SQL语句在程序编译期间就已经确定,大多数的编译情况属于这种类型;另外一种是后期联编(late binding),即SQL语句只有在运行阶段才能建立,例如当查询条件为用户输入时,那么Oracle的SQL引擎就无法在编译期对该程序语句进行确定,只能在用户输入一定的查询条件后才能提交给SQL引擎进行处理。通常,静态SQL采用前一种编译方式,而动态SQL采用后一种编译方式。

2动态SQL程序开发
理解了动态SQL编译的原理,也就掌握了其基本的开发思想。动态SQL既然是一种”不确定”的SQL,那其执行就有其相应的特点。Oracle中提供了Execute immediate语句来执行动态SQL,语法如下:

Excute immediate 动态SQL语句 using 绑定参数列表 returning into 输出参数列表;

对这一语句作如下说明:
1)动态SQL是指DDL和不确定的DML(即带参数的DML)
2)绑定参数列表为输入参数列表,即其类型为in类型,在运行时刻与动态SQL语句中的参数(实际上占位符,可以理解为函数里面的形式参数)进行绑定。
3)输出参数列表为动态SQL语句执行后返回的参数列表。
4)由于动态SQL是在运行时刻进行确定的,所以相对于静态而言,其更多的会损失一些系统性能来换取其灵活性。

 为了更好的说明其开发的过程,下面列举一个实例:
设数据库的他表,其数据为如下:

ID

NAME

SAL

10

scott

3000

20

tom

5000

30

jerry

4500

要求:
1.创建该表并输入相应的数据。
2.根据特定ID可以查询到其姓名和薪水的信息。
3.根据大于特定的薪水的查询相应的员工信息。
根据前面的要求,可以分别创建三个过程(均使用动态SQL)来实现:

过程一:(创建表并插入数据)

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
18 : 16 : 27  SCOTT@ prod>create  or  replace  procedure p1  as
18 : 16 : 36    2   flag number; 
18 : 16 : 36    3   begin
18 : 16 : 36    4   select count(*)  into  flag  from  all_tables  where  table_name= 'T1' ;
18 : 16 : 36    5   if  (flag= 0 then 
18 : 16 : 36    6   execute immediate  'create table t1(id number,name varchar2(10),sal number)' ;
18 : 16 : 36    7   else
18 : 16 : 36    8   insert  into  t1 values ( 10 , 'scott' , 3000 );
18 : 16 : 36    9   insert  into  t1 values ( 20 , 'tom' , 5000 );
18 : 16 : 37   10   insert  into  t1 values ( 30 , 'jerry' , 4500 );
18 : 16 : 37   11   end  if ;
18 : 16 : 37   12   end p1;
18 : 16 : 38   13   /
Procedure created.
Elapsed:  00 : 00 : 00.20
18 : 16 : 40  SCOTT@ prod>exec p1;
PL/SQL procedure successfully completed.
Elapsed:  00 : 00 : 00.18
18 : 16 : 47  SCOTT@ prod>select *  from  t1;
         ID NAME              SAL
---------- ---------- ----------
         10  scott             3000
         20  tom               5000
         30  jerry             4500
Elapsed:  00 : 00 : 00.01
18 : 16 : 52  SCOTT@ prod>


过程二:(按id查询用户信息)

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
18 : 40 : 24  SCOTT@ prod>create  or  replace  procedure p2 (p_id number)  as
18 : 40 : 26    2    v_name varchar2( 10 );
18 : 40 : 26    3    v_sal number;
18 : 40 : 26    4    begin
18 : 40 : 26    5    execute immediate  'select name,sal  from t1  where id=:1'  into  v_name,v_sal using p_id;
18 : 40 : 26    6    dbms_output.put_line(v_name || ' Salary is: ' ||to_char(v_sal));
18 : 40 : 26    7     exception
18 : 40 : 26    8    when others  then
18 : 40 : 26    9    dbms_output.put_line( 'No Data Found' );
18 : 40 : 26   10    end p2;
18 : 40 : 26   11    /
Procedure created.
Elapsed:  00 : 00 : 00.07
18 : 40 : 27  SCOTT@ prod>exec p2( 10 );
scott Salary is:  3000
PL/SQL procedure successfully completed.
Elapsed:  00 : 00 : 00.01
18 : 40 : 32  SCOTT@ prod>exec p2( 20 );
tom Salary is:  5000
PL/SQL procedure successfully completed.
Elapsed:  00 : 00 : 00.02
18 : 40 : 40  SCOTT@ prod>exec p2( 30 );
jerry Salary is:  4500
PL/SQL procedure successfully completed.
Elapsed:  00 : 00 : 00.02
18 : 40 : 45  SCOTT@ prod>


过程三:(查询薪水大于某个值的员工)

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
18 : 48 : 59  SCOTT@ prod>create  or  replace  procedure p3(p_sal number)  as
18 : 50 : 55    2   r_t1 t1%rowtype;
18 : 50 : 55    3   type c_type is ref cursor;
18 : 50 : 56    4   c1 c_type;
18 : 50 : 56    5   begin
18 : 50 : 56    6   open c1  for  '
18 : 50 : 56    7   select *  from  t1
18 : 50 : 56    8   where  sal >: 1 '
18 : 50 : 56    9   using p_sal;
18 : 50 : 56   10   loop
18 : 50 : 56   11   fetch c1  into  r_t1;
18 : 50 : 56   12   exit when c1%notfound;
18 : 50 : 56   13   dbms_output.put_line( 'Salary higher ' ||to_char(p_sal)|| ' Name is:' );
18 : 50 : 56   14   dbms_output.put_line( 'ID is '  ||to_char(r_t1.id)|| ' Name is: ' ||r_t1.name);
18 : 50 : 56   15   end loop;
18 : 50 : 56   16   close c1;
18 : 50 : 56   17   end p3;
18 : 50 : 57   18   /
Procedure created.
Elapsed:  00 : 00 : 00.12
18 : 50 : 58  SCOTT@ prod>exec p3( 2000 );
Salary higher  2000  Name is:
ID is  10  Name is: scott
Salary higher  2000  Name is:
ID is  20  Name is: tom
Salary higher  2000  Name is:
ID is  30  Name is: jerry
PL/SQL procedure successfully completed.
Elapsed:  00 : 00 : 00.02
18 : 51 : 15  SCOTT@ prod>


注意:在过程二中的动态SQL语句使用了占位符“:1“,其实它相当于函数的形式参数,使用”:“作为前缀,然后使用using语句将p_id在运行时刻将:1给替换掉,这里p_id相当于函数里的实参。另外过程三中打开的游标为动态游标,它也属于动态SQL的范畴,其整个编译和开发的过程与execute immediate执行的过程很类似。










本文转自 客居天涯 51CTO博客,原文链接:http://blog.51cto.com/tiany/1422210,如需转载请自行联系原作者

相关文章:

  • Android学习笔记--Content Provider 1
  • SpringMVC 参数注入
  • 【开源】简单4步搞定QQ登录,无需什么代码功底【无语言界限】下 ~ Net程序员的福利...
  • IIS6.0 + openssl执行版 + Windows2003 -- 移植篇
  • 【基础】ARP协议-交换机工作原理-及广播风暴问题分析
  • 【原创】机器学习之PageRank算法应用与C#实现(2)球队排名应用与C#代码
  • 算法之【大整数乘法】
  • 编程语言影响人的思维(2
  • 使用Rancher-Gen动态更新配置文件
  • 限制Apache日志access.log文件大小
  • ntop安装过程
  • ln命令解析_学习笔记
  • 利用软硬件逻辑等价原理模拟VMware硬件环境
  • rsync+inotify实时数据同步
  • Oracle Voyager Worm 一段匿名块SQL蠕虫
  • Debian下无root权限使用Python访问Oracle
  • Effective Java 笔记(一)
  • ES6 学习笔记(一)let,const和解构赋值
  • Laravel Telescope:优雅的应用调试工具
  • Linux gpio口使用方法
  • Redis在Web项目中的应用与实践
  • vue2.0一起在懵逼的海洋里越陷越深(四)
  • 开源地图数据可视化库——mapnik
  • 使用Gradle第一次构建Java程序
  • 数组大概知多少
  • 算法系列——算法入门之递归分而治之思想的实现
  • 阿里云移动端播放器高级功能介绍
  • 回归生活:清理微信公众号
  • #if 1...#endif
  • ()、[]、{}、(())、[[]]命令替换
  • (4)事件处理——(2)在页面加载的时候执行任务(Performing tasks on page load)...
  • (Redis使用系列) Springboot 实现Redis消息的订阅与分布 四
  • (十二)python网络爬虫(理论+实战)——实战:使用BeautfulSoup解析baidu热搜新闻数据
  • (一)SpringBoot3---尚硅谷总结
  • (原创) cocos2dx使用Curl连接网络(客户端)
  • (转)c++ std::pair 与 std::make
  • (转)http协议
  • (转)我也是一只IT小小鸟
  • .net core 源码_ASP.NET Core之Identity源码学习
  • .NET 中什么样的类是可使用 await 异步等待的?
  • .net快速开发框架源码分享
  • .NET下的多线程编程—1-线程机制概述
  • .Net中间语言BeforeFieldInit
  • .pub是什么文件_Rust 模块和文件 - 「译」
  • /etc/skel 目录作用
  • @JsonFormat与@DateTimeFormat注解的使用
  • @Tag和@Operation标签失效问题。SpringDoc 2.2.0(OpenApi 3)和Spring Boot 3.1.1集成
  • [2019.3.5]BZOJ1934 [Shoi2007]Vote 善意的投票
  • [20190416]完善shared latch测试脚本2.txt
  • [22]. 括号生成
  • [asp.net core]project.json(2)
  • [autojs]autojs开关按钮的简单使用
  • [BUUCTF]-PWN:wustctf2020_number_game解析(补码,整数漏洞)
  • [C++] 如何使用Visual Studio 2022 + QT6创建桌面应用
  • [Godot] 3D拾取