how to pass input parameter as a string separated by comma or list for IN clause in DB2

半腔热情 提交于 2019-12-10 22:56:50

问题


I am new in stored procedure ,i have one doubt,I need to pass input parameter as a string separated by comma or list for IN clause in DB2. please see the below sample procedure

 CREATE PROCEDURE  TEST_SP(IN  @listofUsername)
  SPECIFIC TEST_SP DYNAMIC RESULT SETS 1
P1:BEGIN
  DECLARE CURSOR1 CURSOR WITH RETURN FOR  
  SELECT  F_NAME FROM TEST  WHERE USER_NAME IN (@listofusername);
    }
    OPEN CURSOR1;  
END P1 

Please guide me, 1 how to write the above stored procedure 2 Is it possible to pass any kind of list as a input parameter ,if yes , which one will give better performance.


回答1:


You can create an array datatype, and define your parameter as that datatype.

CREATE OR REPLACE TYPE UserList as char(10) ARRAY[100]

I picked char(10) because that's what they are on my system, IBM i. Use whatever is appropriate for yours, and an array size large enough for whatever use you may have.

Use this as the type for your procedure parameter.




回答2:


Something like this:

Select f_name from test1 
where CONCAT(',',@listofusername,',')  LIKE CONCAT('%,',username,',%')


来源:https://stackoverflow.com/questions/17784348/how-to-pass-input-parameter-as-a-string-separated-by-comma-or-list-for-in-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!