Execute immediate

本文详细介绍了在Oracle PL/SQL中使用DynamicSQL时,如何解决无法直接使用isnotnull操作符的问题,并通过实例演示了替代方法。

     最近写一个Procedure,想用Dynamic SQL语句来根据传进的参数来选择不同的表。

     其中遇到一个问题是无法在 Dynamic SQL 字符串中加入 is null 或者 is not null 这样的字符。

     查了下oracle10 的reference ,说是不支持这样的输入,

The EXECUTE IMMEDIATE statement prepares (parses) and immediately executes a
dynamic SQL statement or an anonymous PL/SQL block.
The main argument to EXECUTE IMMEDIATE is the string containing the SQL
statement to execute. You can build up the string using concatenation, or use a
predefined string.
Except for multi-row queries, the dynamic string can contain any SQL statement
(without the final semicolon) or any PL/SQL block (with the final semicolon). The
string can also contain placeholders, arbitrary names preceded by a colon, for bind
arguments. In this case, you specify which PL/SQL variables correspond to the
placeholders with the INTO, USING, and RETURNING INTO clauses.
You can only use placeholders in places where you can substitute variables in the SQL
statement, such as conditional tests in WHERE clauses. You cannot use placeholders for
the names of schema objects. For the right way, see "Passing Schema Object Names As
Parameters" on page 7-9.
Used only for single-row queries, the INTO clause specifies the variables or record into
which column values are retrieved. For each value retrieved by the query, there must
be a corresponding, type-compatible variable or field in the INTO clause.
Used only for DML statements that have a RETURNING clause (without a BULK
COLLECT clause), the RETURNING INTO clause specifies the variables into which
column values are returned. For each value returned by the DML statement, there
must be a corresponding, type-compatible variable in the RETURNING INTO clause.
You can place all bind arguments in the USING clause. The default parameter mode is
IN. For DML statements that have a RETURNING clause, you can place OUT arguments
in the RETURNING INTO clause without specifying the parameter mode. If you use

both the USING clause and the RETURNING INTO clause, the USING clause can contain
only IN arguments.
At run time, bind arguments replace corresponding placeholders in the dynamic
string. Every placeholder must be associated with a bind argument in the USING
clause and/or RETURNING INTO clause. You can use numeric, character, and string
literals as bind arguments, but you cannot use Boolean literals (TRUE, FALSE, and
NULL). To pass nulls to the dynamic string, you must use a workaround. See "Passing
Nulls to Dynamic SQL" on page 7-10.
Dynamic SQL supports all the SQL datatypes. For example, define variables and bind
arguments can be collections, LOBs, instances of an object type, and refs.
As a rule, dynamic SQL does not support PL/SQL-specific types. For example, define
variables and bind arguments cannot be Booleans or associative arrays. The only
exception is that a PL/SQL record can appear in the INTO clause.
You can execute a dynamic SQL statement repeatedly using new values for the bind
arguments. However, you incur some overhead because EXECUTE IMMEDIATE
re-prepares the dynamic string before every execution.

 

如果要判断一个条件是否为空,资料上举了个例子

The literal NULL is not allowed in the USING clause. To work around this restriction,
replace the keyword NULL with an uninitialized variable:
DECLARE
a_null CHAR(1); -- set to NULL automatically at run time
BEGIN
EXECUTE IMMEDIATE 'UPDATE emp SET comm = :x' USING a_null;
END;
/

 

但是这种情况无法解决我想要解决的问题:

 

select measurementspec from spc_tb_offline_hist

where value is not null

and updatetimestamp>'20100601 000000';

 

这样的语句中包括有 is not null 这样的东西,编译时会出错,我没法找到解决的办法。希望能有高人知道怎么解决这个问题。

 

 

Reference Source: Oracle PLSQL User's Guide and Reference.pdf

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值