在PLSQL中使用逗号分隔的IN参数进行查询

时间:2015-10-14 13:32:00

标签: oracle plsql oracle11g

我在Oracle 11g中编写了以下内容来分隔IN参数(I_PRODUCT)并进行查询。当我给出一个参数作为i_PRODUCT时,它会填充一些结果。当我插入多个逗号分隔的参数时,我希望逐个查询并填充整个结果,但它不起作用。

int index = 0;
Rectange[] rect;

void setup () {
  size (1200, 700);
  background (255, 255, 255);
  smooth();
  rect = new Rectangle [count]; //creates a grid of possible rectangles, based on the dimensions of the screen
  for (int i = 0; i<= (count-1); i++) {
    fill (255, 255, 255);
    ypos = ypos + heigh;
    if (ypos >= 700) {
      ypos = 0;
      xpos = xpos + wid;
    }
    rect[i] = new Rectangle(xpos, ypos, wid, heigh, redPressed, greenPressed, yellowPressed, bluePressed, blackPressed);
  }
  int now = millis();
  frameRate(1);
}

void draw() {

  if (keyPressed) { //detects if key is pressed and colors the current rectangle that way
    if (key == 'r' ||key == 'R') {
      fill (255, 0, 0);
    }
    if (key == 'g' ||key == 'G') {
      fill (0, 255, 0);
    }
    if (key == 'y' || key == 'Y') {
      fill (255, 255, 0);
    }
    if (key == 'b' || key == 'B') {
      fill (0, 0, 255);
    }
    if (key == 'k' || key == 'K') {
      fill (0, 0, 0);
    }
  }
  stroke (0, 0, 0);
  rect (rect[index].xpos, rect[index].ypos, rect[index].xdim, rect[index].ydim);  
  index++;
  if (index >= rect.length) {
    noLoop();
  }
}

3 个答案:

答案 0 :(得分:1)

Table1.PRODUCT IN ( select regexp_substr(I_PRODUCT,'[^,]+', 1, level) from dual
                     connect by regexp_substr(I_PRODUCT, '[^,]+', 1, level) is not null);

LIKE 运算符会在上面的查询中失败。您需要使用 IN ,内部评估为多个 OR

WITH str_search AS
(
     select regexp_substr(I_PRODUCT,'[^,]+', 1, level) pattern from dual
     connect by regexp_substr(I_PRODUCT, '[^,]+', 1, level) is not null
)
SELECT   *
FROM     Table1
WHERE     EXISTS (
          SELECT     NULL
          FROM     str_search
          WHERE     tab.col     LIKE pattern
                 );

另外,关于varying IN list,请查看我的回答here

<强>更新

基于OP的评论。

将查询修改为:

/var/www/html/moodle/module/submodule/phpserver/api/index.php

答案 1 :(得分:1)

 create or replace PROCEDURE ProcedureName
 (
     I_PRODUCT IN VARCHAR2 :=NULL,
     O_Cursor OUT SYS_REFCURSOR
 ) 
 AS
 BEGIN
     DECLARE I_PRODUCT_    VARCHAR2(1000);
 BEGIN
       I_PRODUCT_ := ',' || nvl(I_PRODUCT,'') || ',';
       Open  O_Cursor FOR
       SELECT * FROM Table1    WHERE 1=1
       And (I_PRODUCT_ = ',,' Or I_PRODUCT_ Like '%,' || I_PRODUCT  ||',%');
  END;
 END ProcedureName;

答案 2 :(得分:0)

使用IN代替like;

Table1.PRODUCT IN( 
    select regexp_substr(I_PRODUCT,'[^,]+', 1, level) from dual
    connect by regexp_substr(I_PRODUCT, '[^,]+', 1, level) is not null
)