在oracle中动态添加where子句到游标

时间:2008-12-18 10:24:11

标签: oracle plsql database-cursor

我有plsql程序,它接受某些参数,例如v_name,v_country,v_type。

我希望有一个带有这样的select语句的游标:

select column from table1 t1, table2 t2
where t1.name = v_name
and t1.country = v_country
and t1.id = t2.id
and t2.type = v_type

如果某些参数为空,我只能在光标中添加相关的where子句吗?或者有更好的方法来实现这一目标吗?

5 个答案:

答案 0 :(得分:5)

使用它的最佳方法是使用DBMS_SQL。

您创建一个表示您的SQL语句的字符串。您仍然使用绑定变量。这很痛苦。

它是这样的(我没有编译它,但它应该是关闭的): -

CREATE OR REPLACE FUNCTION find_country( v_name  t1.country%TYPE,
                                         v_type  t2.type%TYPE)  /* Hmm, column called type? */
DECLARE
  v_SQL         varchar2(2000);
  v_select          INTEGER;   /* "Pointer" to a DBMS_SQL select statement */
  v_execute         INTEGER;

BEGIN
  v_SQL := 'select column from table1 t1, table2 t2 ||
           'where t1.id = t2.id';

  IF v_name IS NOT NULL THEN
    v_SQL := v_SQL || ' AND t1.country = :v_name'
  END IF;

  IF v_type IS NOT NULL THEN
    v_SQL := v_SQL || ' AND t2.type = :v_type';
  END IF;

  /* Setup Cursor */
  v_select := dbms_sql.open_cursor;     
  dbms_sql.parse( v_select, v_SQL, DBMS_SQL.native);

  IF v_name IS NOT NULL THEN
    dbms_sql.bind_variable( v_select, ':v_name', v_name );
  END IF;

  IF v_type IS NOT NULL THEN
    dbms_sql.bind_variable( v_select, ':v_type', v_type );
  END IF;

  DBMS_SQL.DEFINE_COLUMN(v_select, 1, v_column);  /* This is what we have selected */

  /* Return value from EXECUTE is undefined for a SELECT */     
  v_execute := DBMS_SQL.EXECUTE( v_select );

  IF DBMS_SQL.FETCH_ROWS( v_select ) > 0 THEN

    /* A row was found  
    DBMS_SQL.COLUMN_VALUE( v_select, 1, v_column);

    /* Tidy Up */
    DBMS_SQL.CLOSE_CURSOR(v_select);

    RETURN v_ID_address;

  ELSE

     DBMS_SQL.CLOSE_CURSOR(v_select);

     /* No row */
     RETURN NULL;
  END IF;

  EXCEPTION
    WHEN OTHERS THEN
      IF DBMS_SQL.IS_open(v_select) THEN
        DBMS_SQL.CLOSE_CURSOR(v_select);
      END IF;
      RAISE;
END;

与仅编写SQL内联相比,这种方法非常痛苦,除非你有很多列,否则使用这种语法编写几个不同的版本会更容易:

FOR r IN (SELECT blah FROM blah WHERE t1 = v_t1) LOOP
   func( r.blah );
END LOOP;

答案 1 :(得分:3)

一种方法是将查询构建为字符串,然后使用execute immediate

答案 2 :(得分:3)

这不是你所要求的,但它可能是一个可接受的解决方案:

select column from table1 t1, table2 t2
where
    (v_name is null or t1.name = v_name)
and (v_country is null or t1.country = v_country)
and t1.id = t2.id
and (v_type is null or t2.type = v_type)

答案 3 :(得分:2)

最佳方法是使用Oracle的应用程序上下文功能,最佳定义为最佳性能和安全性。

更快方式将是hamishmcn建议的方式,使用EXECUTE IMMEDIATE。我会选择WW的建议DBMS_SQL 每次

另一种最快写但不会表现良好的方式是这样的:

select column from table1 t1, table2 t2
where t1.name = nvl(v_name, t1.name)
and t1.country = nvl(v_country, t1.country)
and t1.id = t2.id
and t2.type = nvl(v_type, t2.type)

答案 4 :(得分:2)

您不必使用dbms_sql来解决此问题 你仍然可以使用参考光标来使用普通光标。

样品:

DECLARE
  TYPE cursor_ref IS REF CURSOR;
  c1 cursor_ref;
  r1 table1.column%type;
BEGIN
  l_sql := 'select t1.column from table1 t1, table2 t2 where t1.id = t2.id ';
  if v_name is not null then
    l_sql := l_sql||' and t1.name = '||v_name ;
  end if;
  if v_country is not null then
    l_sql := l_sql||' and t1.country = '||v_country';
  end if;
  if v_type is not null then  
    l_sql := l_sql||' and t2.type = '||v_type';
  end if;
  open c1 for l_sql;
  loop
      fetch c1 into r1;
      exit when c1%notfound;
      -- do something
  end loop;
  close c1;
end;
/

你可以通过将变量与'using'这样的命令绑定来实现这一点:

open c1 for l_sql using v_name, v_country;