简体   繁体   English

用其他表列和序号创建表名

[英]create table name with other table column and sequence number oracle

Creating a clear log procedure, which deletes and insert into a backup table everyday, what I need to do is after the backup table reaches a certain number of rows, I want my job(procedure) to create a new table with the same fields. 创建一个清晰的日志过程,该过程每天都会删除并插入到备份表中,我需要做的是在备份表达到一定行数之后,我希望我的工作(过程)创建一个具有相同字段的新表。

So I have a table config with the tables I need to backup identified , and I have a column named pre_bck which has the name I want my back up table to have. 因此,我有一个表配置,其中包含需要备份的表,并且有一个名为pre_bck的列,该列具有我希望备份表具有的名称。

So I want to try to make a procedure that will create a table from that 'column'+the sequence number 所以我想尝试创建一个程序,该程序将根据该'column'+the sequence number创建一个表

create table 'column_name'+sequence_id as select * from xyz where 1=0;

so for example if table name was abc and sequece id was 3, table name would be abc3 因此,例如,如果表名是abc且后继ID是3,则表名将是abc3

I dont know if I was clear, as to what I pretend to implement, any help would be appreciated. 对于我假装要实现的目标,我不知道我是否清楚,任何帮助都将不胜感激。

I second Alex and Boneist's opinion that this is probably not a good approach. 我赞同亚历克斯和贝尼斯特的观点,认为这可能不是一个好方法。

However, to answer your question: 但是,要回答您的问题:

The operator to glue together various bits of a string is || 将字符串的各个位粘合在一起的运算符是|| , not + as in other languages: column_name || sequence_id ,而不是其他语言中的+column_name || sequence_id column_name || sequence_id . column_name || sequence_id

To execute SQL from within a procedure, you can use EXECUTE IMMEDIATE 'CREATE ...'; 要从过程中执行SQL,可以使用EXECUTE IMMEDIATE 'CREATE ...';

To select from a sequence, you need to create it with CREATE SEQUENCE my_sequence; 要从序列中进行选择,您需要使用CREATE SEQUENCE my_sequence;创建它CREATE SEQUENCE my_sequence; . To get a value: my_sequence.nextval . 要获取值: my_sequence.nextval

The easiest way to loop over all rows in your config table is a FOR IN ... LOOP : 循环遍历配置表中所有行的最简单方法是FOR IN ... LOOP

CREATE OR REPLACE PROCEDURE my_procedure IS
  stmt VARCHAR2(32000);
BEGIN
  FOR r IN (SELECT * FROM my_config_table ORDER BY xxx) LOOP
    stmt := 'CREATE TABLE ' || pre_bck || my_sequence.nextval || 
            ' AS SELECT * FROM xyz WHERE 1=0';
    EXECUTE IMMEDIATE stmt;
  END LOOP;
END my_procedure;
/

I'm not clear what the xyz part means, but you can surely take it from here... 我不清楚xyz部分的含义,但是您可以肯定地从这里获取它...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM