【发布时间】:2019-03-15 06:23:07
【问题描述】:
我有以下过程,根据输入参数更新客户端的信息。
-- Update client
create or replace procedure p_update_client (i_client_id in number, i_iin in number default null, i_full_name in varchar default null)
as
query_str varchar(200);
no_id_provided exception;
all_null_values exception;
begin
-- Handle input parameters
if i_client_id is null then
raise no_id_provided;
end if;
if i_iin is null and i_full_name is null then
raise all_null_values;
end if;
-- Base query string.
query_str := 'update t_client set';
-- Form SQL depending on the input parameters.
if i_iin is not null then
query_str := query_str || ' iin = :param1';
end if;
if i_full_name is not null then
query_str := query_str || ' full_name = :param2';
end if;
-- Add necessary where clause to identify record.
query_str := query_str || ' where client_id = :param3;';
-- Execute query.
execute immediate query_str using i_iin, i_full_name, i_client_id;
exception
when no_id_provided then
raise_application_error(-20100, 'Client_id value must not be null.');
when all_null_values then
raise_application_error(-20101, 'To update record, input parameters must not be null.');
when others then
rollback;
end p_update_client;
因此,过程的逻辑如下:如果传递的参数具有非空值,那么我动态更新我的 SQL 并使用execute immidiate 执行它。
只要两个参数都具有非空值,此方法就可以正常工作。如果其中一个参数为空,则query_str 将抛出 SQL 错误ORA-01006: bind variable does not exist,因为在query_str 中指定的参数数量与在using 子句中指定的数量不同。
处理这种情况的更好方法是什么,也许是某种命名参数,但据我所知,execute emmidiate 不提供。
有任何想法吗?
【问题讨论】:
标签: sql oracle plsql oracle11g dynamic-sql