db2 for i:在in子句中的sql过程中传递包含逗号分隔字符串的varchar

k4ymrczo  于 2023-03-02  发布在  DB2
关注(0)|答案(3)|浏览(211)

我有一个字符串包含逗号分隔的字符串在java..我正在传递到一个sql过程正在调用java...这里是java字符串的示例:

String codeString = "'232/232','34fd/34//'";

i的sql db2中的代码:

create procedure history (in id varchar (3), in code varchar (2000))
   ......
   ......
   begin
   insert into table1
   select date_from, date_to, code
   from table2 
   where table2.serial= id
   and table2.status not in (code); 
   end

此sql过程正在table1.code中插入相同的字符串,但未排除table2.status在in子句中。
正在table 1中插入值。代码为'232/232','34 fd/34//'(包括所有单引号和逗号)

omtl5h9j

omtl5h9j1#

你在这里说什么:

and table2.status not in (code);

就等于说:

and table2.status <> code;

你不是在说

and table2.status not in ('232/232','34fd/34//');

原因是它是针对整个code检查status,而不是针对code的某个部分。code中的逗号分隔列表不会被解析。事实上,不会解析任何变量。它们是完整的。如果您希望in predicate 中有多个值,则需要多个文字值、多个变量或它们的某种组合。
它适用于insert,因为它只是将code的整个值插入到table1的列中,不需要解析。
另一个选择,因为你是在一个过程中,是把逗号分隔的列表自己解析成一个字符串数组(这里不会给你展示),然后使用一个集合派生表把数组转换成一个表,你可以在in predicate 中使用,如下所示:

status not in (select s.string from UNNEST(stringArray) as s(string))
6za6bjd0

6za6bjd02#

尝试类似这样的事情:

create procedure history (in id varchar (3), in code varchar (2000))

begin
DECLARE stmttxt varchar(2000);
SET stmttxt = 'insert into table1 select date_from, date_to, cast(''' concat replace(code, '''', '''''') concat ''' as varchar(2000)) from table2 where table2.serial= ' concat id concat ' and table2.status not in (' concat code concat ')'; 
EXECUTE IMMEDIATE stmttxt;
end;
jexiocij

jexiocij3#

用于i的DB2 V7.4

对于SQL:

DECLARE @myVariable varchar(200);
SET @myVariable myVariable = '1,2,3';

IN (SELECT element
          FROM TABLE (
              SYSTOOLS.SPLIT(@myVariable , ',')
            ))

对于RPG:

dcl-s myVariable varchar(200);
myVariable = '1,2,3';

IN (SELECT element
          FROM TABLE (
              SYSTOOLS.SPLIT(:myVariable, ',')
            ))

相关问题