2013-05-09 53 views
1

我在使用動態查詢在oracle上,當我傳遞字符串參數的情況下,其不工作。動態查詢,參數不工作在

for x in (
      SELECT DISTINCT column_id 
      FROM table 
      WHERE column_id in (in_column_ids)   
      /* WHERE column_id in (15,16,17) =>works */ 
      /* => in_column_ids is a varchar type which 
        holds comma separated value */ 
      and column_title=in_column_title /* works */ 
    ) 

在這裏,如果我直接在這個in_column_ids上保留值,查詢就可以工作。 但是,作爲參數傳遞的值似乎不適用於where in

有什麼想法?

回答

2

IMO,您必須使用regexp_substr分隔逗號分隔變量。您的查詢應該是這樣的:

for x in (
      SELECT DISTINCT column_id 
      FROM table 
      WHERE column_id in (
      SELECT DISTINCT regexp_substr(in_column_ids,'[^,]+', 1, LEVEL) FROM DUAL 
      CONNECT BY regexp_substr(in_column_ids, '[^,]+', 1, LEVEL) IS NOT NULL 
      )   
      /* WHERE column_id in (15,16,17) =>works */ 
      /* => in_column_ids is a varchar type which 
        holds comma separated value */ 
      and column_title=in_column_title /* works */ 
    ) 

退房SQLFIDDLE DEMO