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

时间:2018-05-12 19:37:04

标签: sql db2 procedure db2-400

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

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

sql db2 for i中的代码:

    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中插入相同的字符串,但它不排除in子句中的table2.status。

在table1.code中插入的值是' 232/232'' 34fd / 34 //' (包括所有单引号和逗号)

2 个答案:

答案 0 :(得分:3)

你在这说什么:

and table2.status not in (code);

与说法相同:

and table2.status <> code;

说:

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

原因是它正在针对整个status检查code,而不是code的某些部分。不解析code中逗号分隔的列表。实际上没有解析变量。他们是完整的。如果您想在in谓词中使用多个值,则需要多个文字值,多个变量或其中某些组合。

它适用于插入,因为它只是将code的整个值插入到table1的列中。无需解析。

另一个选项,因为你在一个过程中是自己解析逗号分隔列表到一个字符串数组(这里不会向你展示),然后使用集合派生表将数组转换为一个表您可以在in谓词中使用,如下所示:

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

答案 1 :(得分:0)

尝试这样的事情:

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;