如何从表列存储和调用sql查询?

时间:2019-02-05 22:11:37

标签: oracle plsql

下面的插入语句包含另一个具有3列(id,ins_dt,text_stuff)的表的详细信息

INSERT INTO swpurge_config
       (schema
       ,table_name
       ,table_alias
       ,driving_column
       ,deletion_predicate
       ,retention_period_type
       ,retention_period_value)
    VALUES
       ('CUSTOMERS_OWNER'
       ,'LAST_NAMES'
       ,'LN'
       ,'ins_dt'
       ,'WHERE ln.ins_dt < SYSDATE - p_retention_period_value
      AND ora_hash(ln.rowid, 8) = 1'
       ,'month'
       ,'6');

目标: 我实质上是想在varchar2列中添加一个删除谓词。想法是在过程中调用此列,该过程将删除多达1000行的记录:

    PROCEDURE delete_rows
   (
      p_schema                 IN VARCHAR2
     ,p_table_name             IN VARCHAR2
     ,p_table_alias            in varchar2
     ,p_retention_period_value IN VARCHAR2
     ,p_delete_predicate       IN VARCHAR2
   ) IS
      v_sql varchar2 (32000);
      v_row_limit pls_integer (1000);
   BEGIN

    v_sql := 'delete from ' || p_schema ||'.'|| table_name ||p_table_alias
     'older than '|| p_retention_period_value || p_delete_predicate;

    dbms_output.put_line(v_sql);

   END delete_rows;

不确定两件事: 1.如何在表列中存储sql where子句? 2.如何在过程中将where子句作为语句执行?

谢谢

1 个答案:

答案 0 :(得分:1)

您正在谈论动态SQL

您可以将其以字符串格式存储在VARCHAR2中。然后取回

 select deletion_predicate into v_sql from  swpurge_config where ...

然后

v_sql := "SELECT ...... FRom .... " || v_sql;

最终执行

 EXECUTE IMMEDIATE v_sql;

如果您的sql语句还包含参数,则

EXECUTE IMMEDIATE query_str
    using param1;