Oracle中动态SQL详解(EXECUTE IMMEDIATE)
2018-03-06 14:12
1381 查看
Oracle中动态SQL详解(EXECUTE IMMEDIATE)
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语句执行后返回的参数列表。
3.下面列举一个实例:
设数据库的emp表,其数据为如下:
要求:
1.创建该表并输入相应的数据。
2.根据特定ID可以查询到其姓名和薪水的信息。
3.根据大于特定的薪水的查询相应的员工信息。
4. 对id为100和101的员工的薪水加薪10%。
根据前面的要求,可以分别创建三个过程(均使用动态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语句执行后返回的参数列表。
3.下面列举一个实例:
设数据库的emp表,其数据为如下:
ID | NAME | SALARY |
100 | Jacky | 5600 |
101 | Rose | 3000 |
102 | John | 4500 |
1.创建该表并输入相应的数据。
2.根据特定ID可以查询到其姓名和薪水的信息。
3.根据大于特定的薪水的查询相应的员工信息。
4. 对id为100和101的员工的薪水加薪10%。
根据前面的要求,可以分别创建三个过程(均使用动态SQL)来实现:
过程一:
create or replace procedure create_table as begin execute immediate 'create table emp(id number,name varchar2(10),salary number )'; --动态SQL为DDL语句 insert into emp values (100,'jacky',5600); insert into emp values (101,'rose',3000); insert into emp values (102,'john',4500); end create_table;
过程二:
create or replace procedure find_info(p_id number) as v_name varchar2(10); v_salary number; begin execute immediate 'select name,salary from emp where id=:1' using p_id returning into v_name,v_salary; --动态SQL为查询语句 dbms_output.put_line(v_name ||'的收入为:'||to_char(v_salary)); exception when others then dbms_output.put_line('找不到相应数据'); end find_info;
过程三:
create or replace procedure find_emp(p_salary number) as r_emp emp%rowtype; type c_type is ref cursor; c1 c_type; begin open c1 for 'select * from emp where salary >:1' using p_salary; //动态SQL语句使用了占位符“:1“,其实它相当于函数的形式参数,使用”:“作为前缀,然后使用using语句将p_id在运行时刻将:1给替换掉,这里p_id相当于函数里的实参。 loop fetch c1 into r_emp; exit when c1%notfound; dbms_output.put_line('薪水大于‘||to_char(p_salary)||’的员工为:‘); dbms_output.put_line('ID为'to_char(r_emp)||' 其姓名为:'||r_emp.name); end loop; close c1; end create_table;
过程四:
declare type num_list is varray(20) of number; v_id num_list :=num_list(100,101); begin ... for i in v_id.first .. v_id.last loop ... execute immediate 'update emp set =salary*1.2 where id=:1 ' using v_id(i); end loop; end;
相关文章推荐
- oracle中动态SQL详解
- oracle中动态SQL详解
- Oracle中动态SQL详解
- oracle中动态SQL详解
- Oracle中动态SQL详解
- Oracle中动态SQL详解
- Oracle中动态SQL详解
- Oracle中动态SQL详解
- Oracle中动态SQL详解
- Oracle学习教程:动态SQL与游标详解
- Oracle学习教程:动态SQL与游标详解
- (转)Oracle中动态SQL详解
- Oracle中动态SQL详解(EXECUTE IMMEDIATE)
- Oracle中动态SQL详解
- Oracle中动态SQL详解(EXECUTE IMMEDIATE)
- Oracle中动态SQL详解(EXECUTE IMMEDIATE)
- Oracle中动态SQL详解
- Oracle学习教程:动态SQL与游标详解
- 基于Oracle的高性能动态SQL程序开发
- oracle pl/sql 入门+ 数组使用+游标+动态SQL