【发布时间】:2018-05-09 12:47:12
【问题描述】:
我在 .net 应用程序中有一些动态表单。根据表单的不同,插入/更新中的字段会有所不同。我正在尝试构建一个动态 sql 语句,但字符串可能超过 4000 个字符,这对于字符串文字不起作用,所以我正在尝试使用绑定变量。由于保存的字段是动态的,我不知道如何处理 USING 块。下面是我正在尝试做的一个简化版本。如果它有助于解释动态表单,您可以查看我询问的关于动态获取数据的旧问题。 collection of records to out sys_refcursor
附言。我知道我可以在每个占位符中插入空值,但这意味着当我向表中添加一个字段时,我必须更新该过程,这不是可行的方法。
procedure bindTest(oCur out sys_refcursor)
as
vFirst varchar2(50) := 'Joe';
vMiddle varchar2(50) := 'Vs'
vLast varchar2(50) := 'Volcano';
vVars varchar2(50) := 'vFirst, vLast';
vSql varchar2(1000) := '';
begin
-- This form does not use the middle name so there are only 2 bind vars.
-- The field exists in the table but not in the web form so it would not be passed to the procedure.
-- I've included it here to show data I want to ignore.
-- vVars includes the list of valid fields to save.
-- This would be the sql created by my script.
vSql := 'insert into tbl_users (firstName, lastName) values (:a, :b)';
-- depending on the form, vSql might look like
---- 'insert into tbl_users (firstName, middle, lastName) values (:a,:b,:c)'
---- 'insert into tbl_users (lastName) values (:a)'
---- etc
execute immediate vSql using {what goes here? or how do I handle this?};
-- I understand normally it would be `USING vFirst, vLast` but what about when it's dynamic?
open oCur for
select
ID
, firstName
, lastName
from
tbl_users
where
rownum = 1
order by
id desc;
end bindTest;
【问题讨论】:
-
您的
insert into tbl_users总是可以为所有列使用占位符,如果没有值,只需将NULL作为绑定参数传递 -
@KaushikNayak 我确定您在我添加编辑时发表了评论。请参阅 OP 中的
p.s.行。 -
“我正在尝试构建动态 sql 语句,但字符串可能超过 4000 个字符” - 为什么不使用
CLOB而不是VARCHAR2?另请阅读此oracle-base.com/articles/10g/dbms_assert_10gR2 -
@KaushikNayak 即使使用 varchar2(32000) 我仍然得到
string literal too long这是同样的问题在这里演示dba-oracle.com/t_execute_immediate_large_insert_string.htm -
如果您将它们作为字符串文字传递,那么您需要将每个单独的块限制为