如何从参数(光标,varchar2等)中进行选择

时间:2014-08-06 09:40:48

标签: oracle select

我有一点问题,如果你能帮助我,那就太好了。

DECLARE
      CURSOR cur1 IS 
         SELECT table_name 
           FROM all_tab_columns 
          WHERE column_name LIKE 'DESCRIPTION';
      tableName varchar2(100);
BEGIN
      OPEN cur1;
      LOOP
        FETCH cur1 INTO tableName;
        EXIT WHEN cur1%NOTFOUND;
        dbms_output.put_line(tablename);
      END LOOP;
END;

我需要的是从条件中获取所有可能的信息:

WEHRE DESCRIPTION LIKE 'Transaction%'  

像这样的东西

SELECT *
  FROM tableName ---- from above
 WEHRE DESCRIPTION LIKE 'Transaction%'

我想从光标中的表中选择所有数据。

表格没有相同的结构,一个可以有4个颜色,一个可以有10个颜色。

可以在匿名块中完成吗? 有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

DECLARE
      CURSOR cur1 IS 
         SELECT table_name 
           FROM user_tab_columns 
          WHERE column_name LIKE 'DESCRIPTION';
      tableName varchar2(100);
      l_description varchar2(100);
      l_str_val varchar2(100) := 'Transaction%';
      l_cursor sys_refursor;
BEGIN
      OPEN cur1;
      LOOP
        FETCH cur1 INTO tableName;
        EXIT WHEN cur1%NOTFOUND;
        open l_cursor for 'select DESCRIPTION from ' || tableName || ' where DESCRIPTION like :description' using l_str_val;
        loop
          fetch l_cursor into l_description; 
          exit when l_cursor%NOTFOUND;
          dbms_output.put_line(tablename || '  ' || l_description);
        end loop;
        close l_cursor;
      END LOOP;
      close cur1;
END;

我只提取了描述列,因为我确定此列在表中,如果要提取更多列,则必须重写此列:

WHERE column_name = ('DESCRIPTION', 'ID', NAME)

然后:

execute immediate 'select DESCRIPTION, ID, NAME from ' || tableName || ' where DESCRIPTION like :description' into l_description, l_id, l_name using l_str_val;