来自查询的Oracle 11g临时列表对象

时间:2013-10-07 11:00:40

标签: oracle oracle11g

我试图找出Oracle 11g中是否存在一种方法,我可以将结果列表从SELECT存储到变量(对象)中,然后循环遍历该列表,对结果执行第二次查询?

基本上我要做的是获取所有带有列名的表的列表,然后从表中删除某些数据。

类似的东西:

var PRODUCTID_TABLE = SELECT table_name 
                        FROM user_tab_columns 
                       WHERE column_name = 'PRODUCT_ID' 
                         AND table_name NOT LIKE 'BIN%';

FOR T IN PRODUCTID_TABLE LOOP

    DELETE FROM T.TABLE_NAME WHERE PRODUCT_ID = {value};

END LOOP;
COMMIT;

提前致谢

KS

1 个答案:

答案 0 :(得分:2)

您始终可以像这样生成DELETE语句:

SELECT 'DELETE FROM ' || table_name ||
       ' WHERE PRODUCT_ID = {value}; '
  FROM user_tab_columns
WHERE column_name = 'PRODUCT_ID' AND table_name NOT LIKE 'BIN%';

或者,如果使用PL / SQL是一个选项,您可以在PL / SQL块中使用EXECUTE IMMEDIATE:

BEGIN
  FOR v_rec IN (SELECT table_name
                  FROM user_tab_columns
                WHERE column_name = 'PRODUCT_ID'
                  AND table_name NOT LIKE 'BIN%')
  LOOP
    EXECUTE IMMEDIATE 'DELETE FROM ' || v_rec.table_name ||
                      ' WHERE PRODUCT_ID = {value}; ';
  END LOOP;
END;