Oracle:通过存储过程

时间:2016-11-20 13:47:34

标签: sql oracle stored-procedures oracle11g

如果我在下面的查询中执行,它会给我4条正确的记录。

select SUBSCRIBER_NUM, SUBSCRIBER_STATUS, P_ID
from C_S_FORWARD_INFO
where SUBSCRIBER_NUM IN ('0', '07', '070', '0705', '07052', '070526', '0705262', '07052620') and
      SCP_VER = 1

但是当我在下面执行时,它给了我0条记录。实际上我必须动态传递查询中“IN”部分的值。

我在下面尝试过(VAR_CALLING_NUM = 07052620):

while var1<=len LOOP 
temp1 := SUBSTR(VAR_CALLING_NUM, 1, var1); 
temp1 := concat('''',temp1); 
temp1 := concat(temp1,''''); 
temp6 := temp6 || temp1 || ',' ; 
var1:=var1+1; 
END LOOP; 
temp6 := SUBSTR(temp6, 1,length(temp6)-1);

select SUBSCRIBER_NUM, SUBSCRIBER_STATUS, P_ID from C_S_FORWARD_INFO where SUBSCRIBER_NUM IN ( temp6 ) and SCP_VER = 1 order by length(subscriber_num) desc; 

为什么这会给我带来0条记录。我做错了什么,在SUBSCRIBER_NUM IN ( temp6 )

等查询中传递temp6

1 个答案:

答案 0 :(得分:2)

您的字符串看起来像"'0', '07', . . ."。这是一个单一的字符串。它不会被in解释为多个值。

您将需要使用动态SQL将其作为列表放入。这需要构建SQL,然后使用execute immediate