这里只介绍动态SQL的使用。关于动态SQL语句的语法,参见:http://blog.csdn.net/chiclewu/article/details/16097133 1.什么是时候需要使用动态SQL? SQL文本在编译时是未知的。 例如,SELECT语句包含的标识符(如表名)在编译时是未知的,或者WHERE子句的条件
这里只介绍动态SQL的使用。关于动态SQL语句的语法,参见:http://blog.csdn.net/chiclewu/article/details/16097133
1.什么是时候需要使用动态SQL?
SQL文本在编译时是未知的。静态SQL不支持例如,SELECT语句包含的标识符(如表名)在编译时是未知的,或者WHERE子句的条件数量在编译时是未知。
例如,在PL/SQL中用静态SQL只能执行查询以及DML语句。如果想要执行DDL语句,只能使用动态SQL。
当让使用静态SQL,也有它的好处:
编译成功验证了静态SQL语句引用有效的数据库对象和访问这些对象的权限
编译成功创建了模式对象的依赖关系2.EXECUTE IMMEDIATE语句
EXECUTE IMMEDIATE语句的意思是使用本地动态SQL处理大多数动态SQL语句。
如果动态SQL语句是自包含的(也就是说,它的绑定参数没有占位符,并且结果不可能返回错误),则EXECUTE IMMEDIATE语句不需要子句。
如果动态SQL语句包行占位符绑定参数,每个占位符在EXECUTE IMMEDIATE语句的子句中必须有一个相应的绑定参数,具体如下:
如果动态SQL语句是一个最多只能返回一行的SELECT语句,OUT绑定参数放置在INTO子句,IN绑定参数放置在USING子句。如果动态SQL语句是一个可以返回多行的SELECT语句,OUT绑定参数放置在BULK COLLECT INTO子句,IN绑定参数放置在USING子句。如果动态SQL语句是一个除了SELECT以外的其他DML语句,且没有RETURNING INTO子句,所有的绑定参数放置在USING子句中。如果动态SQL还语句一个匿名PL/SQL块或CALL语句,把所有的绑定参数放置在USING子句中。如果动态SQL语句调用一个子程序,请确保:
每个对应子程序参数占位符的绑定参数与子程序参数具有相同的参数模式和兼容的数据类型。
绑定参数不要有SQL不支持的数据类型(例如,布尔类型,关联数组,以及用户自定的记录类型)
USING子句不能包含NULL字面量。如果想要在USING子句中使用NULL值,可以使用位初始化的变量或者函数显示将NULL转换成一个有类型的值。
2.1动态SQL语句是一个最多只能返回一行的SELECT语句
使用动态SQL语句返回单列,查询SCOTT的薪水:
declare
v_sql_text varchar2(1000);
v_sal number;
v_ename emp.ename%type := 'SCOTT';
begin
v_sql_text := 'select e.sal from emp e where e.ename = :ename';
execute immediate v_sql_text
into v_sal
using v_ename;
dbms_output.put_line(v_ename || ':' || v_sal);
end;
使用动态SQL返回一条记录,查询SCOTT的基本信息:
declare
v_sql_text varchar2(1000);
v_ename emp.ename%type := 'SCOTT';
vrt_emp emp%rowtype;
begin
v_sql_text := 'select * from emp e where e.ename = :ename';
execute immediate v_sql_text
into vrt_emp
using v_ename;
dbms_output.put_line(v_ename || '的基本信息:');
dbms_output.put_line('工号:' || vrt_emp.empno);
dbms_output.put_line('工资:' || vrt_emp.sal);
dbms_output.put_line('入职日期:' || vrt_emp.hiredate);
end;
2.2动态SQL语句是一个可以返回多行的SELECT语句
2.2.1只有一个占位符
使用动态SQL语句返回多行记录,查询30部门的员工基本信息:
declare
v_sql_text varchar2(1000);
v_deptno emp.deptno%type := 30;
type nt_emp is table of emp%rowtype;
vnt_emp nt_emp;
begin
v_sql_text := 'select * from emp e where e.deptno = :deptno';
execute immediate v_sql_text bulk collect
into vnt_emp
using v_deptno;
for i in 1 .. vnt_emp.count loop
dbms_output.put_line(vnt_emp(i).ename || '的基本信息:');
dbms_output.put_line('工号:' || vnt_emp(i).empno);
dbms_output.put_line('工资:' || vnt_emp(i).sal);
dbms_output.put_line('入职日期:' || vnt_emp(i).hiredate);
dbms_output.put_line('');
end loop;
end
2.2.2多个占位符
查询20部门工资大于2000的员工基本信息:
declare
v_sql_text varchar2(1000);
v_deptno emp.deptno%type := 20;
v_sal number := 2000;
type nt_emp is table of emp%rowtype;
vnt_emp nt_emp;
begin
v_sql_text := 'select * from emp e where e.sal>:sal and e.deptno = :deptno';
execute immediate v_sql_text bulk collect
into vnt_emp
using v_sal, v_deptno; --注意绑定多个变量时,绑定变量只与占位符位置有关,与占位符名称无关,
for i in 1 .. vnt_emp.count loop
dbms_output.put_line(vnt_emp(i).ename || '的基本信息:');
dbms_output.put_line('工号:' || vnt_emp(i).empno);
dbms_output.put_line('工资:' || vnt_emp(i).sal);
dbms_output.put_line('入职日期:' || vnt_emp(i).hiredate);
dbms_output.put_line('');
end loop;
注意:对于SQL文本,占位符名称是没有意义的,绑定变量与占位符名称无关,只与占位符的配置有关。即使有多个相同名称占位符,也需要每个占位符对应一个绑定变量。对于PL/SQL块,占位符名称是有意义的,相同名称的占位符,只需要第一个占位符绑定变量。
2.3动态SQL语句是一个带有RETURNING子句的DML语句
KING的工资增长20%,返回增长后的工资:
eclare
v_sql_text varchar2(1000);
v_sal number;
v_ename emp.ename%type := 'KING';
begin
v_sql_text := 'update emp e set e.sal= e.sal*1.2 where e.ename = :ename returning e.sal into :sal';
execute immediate v_sql_text
using v_ename
returning into v_sal;
dbms_output.put_line(v_ename || ':' || v_sal);
end;
注意:只有当v_sql_text语句有returning into子句时,动态SQL语句才能使用returning into子句。
2.4给占位符传递NULL值
2.4.1通过未初始化变量传递NULL值
declare
v_sql_text varchar2(1000);
v_deptno emp.ename%type := 'ALLEN';
v_comm emp.comm%type;
begin
v_sql_text := 'update emp e set e.comm = :comm where e.ename =:ename';
execute immediate v_sql_text
using v_comm, v_deptno;
end;
2.4.2通过函数将NULL值显式的转换成一个有类型的值
declare
v_sql_text varchar2(1000);
v_deptno emp.ename%type := 'ALLEN';
begin
v_sql_text := 'update emp e set e.comm = :comm where e.ename =:ename';
execute immediate v_sql_text
using to_number(null), v_deptno;
end;
3.OPEN FOR语句
PL/SQL引入OPEN FOR语句实际上并不是为了支持本地动态SQL,而是为了支持游标变量。现在它以一种极其优雅的方式实现了多行的动态查询。
使用OPEN FOR语句来关联动态SQL语句的游标变量,在OPEN FOR语句的USING子句中,指定动态SQL语句每个占位符的绑定参数。
使用FETCH语句获取运行时结果集。使用CLOSE语句关闭游标变量使用OPEN FOR语句查询出10部门的员工的基本信息:
declare
type rc_emp is ref cursor;
vrc_emp rc_emp;
v_sql_text varchar2(1000);
v_deptno emp.deptno%type := 10;
vrt_emp emp%rowtype;
begin
v_sql_text := 'select * from emp e where e.deptno=:deptno';
open vrc_emp for v_sql_text
using v_deptno;
loop
exit when vrc_emp%notfound;
fetch vrc_emp
into vrt_emp;
dbms_output.put_line(vrt_emp.ename || '的基本信息:');
dbms_output.put_line('工号:' || vrt_emp.empno);
dbms_output.put_line('工资:' || vrt_emp.sal);
dbms_output.put_line('入职日期:' || vrt_emp.hiredate);
dbms_output.put_line('');
end loop;
close vrc_emp;
end;
4.重复的占位符名称
如果在动态SQL语句重复占位符名称,要知道占位符关联绑定参数的方式依赖于动态语句的类型。
如果执行的是一个动态SQL字符串,则必须为每一个占位符提供一个绑定参数,即使这些占位符是重复的。如果执行的是一个动态PL/SQL块,则必须为每一个唯一占位符提供一个绑定参数,即重复的占位符只需要提供一个绑定参数。4.1重复占位符的动态SQL字符串
declare
v_sql_text varchar2(1000);
v_sal emp.sal%type := 4000;
v_comm emp.comm%type;
v_ename emp.ename%type := 'SCOTT';
begin
v_sql_text := 'update emp e set e.sal=:sal , e.comm = :sal*0.1 where e.ename =:ename returning e.comm into :comm ';
execute immediate v_sql_text
using v_sal, v_sal, in v_ename
returning into v_comm;
dbms_output.put_line(v_ename || '分红:' || v_comm);
end;
4.2重复占位符的动态PL/SQL块
declare
v_sql_text varchar2(1000);
v_sal number;
v_ename emp.ename%type := 'KING';
begin
v_sql_text := ' begin select e.sal,e.ename into :sal,:ename from emp e where e.ename =:ename; end;';
execute immediate v_sql_text
using out v_sal, in out v_ename;
dbms_output.put_line(v_ename || ':' || v_sal);
end;

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft