创建备份表以备份多个具有不同条件的表的步骤

huangapple go评论49阅读模式
英文:

Procedure to Create Backup Table For multiple table each having different Where condition

问题

    创建或替换过程 PROC AS
    V_TABLE_NAME  VARCHAR2(255);
    V_LIST SYS_REFCURSOR;
    DATE_VALUE_INS VARCHAR2(10);
    开始
    	DATE_VALUE_INS:=TO_CHAR(SYSDATE,'YYMMDD');
        开启 V_LIST 用于
        选择 NAME  DW.table_name_list ;
         循环 
    		获取 V_LIST
    		进入 V_TABLE_NAME;
    		 V_LIST%NOTFOUND 时退出;
    		执行立即 'CREATE TABLE Schema.'||V_TABLE_NAME||'_'||DATE_VALUE_INS||' AS SELECT * FROM DW.'||V_TABLE_NAME||' where some condition';
    	 结束循环;
    	关闭 V_LIST; 
    结束;
英文:
Create or replace procedure PROC AS
V_TABLE_NAME  VARCHAR2(255);
V_LIST SYS_REFCURSOR;
DATE_VALUE_INS VARCHAR2(10);
BEGIN
	DATE_VALUE_INS:=TO_CHAR(SYSDATE,'YYMMDD');
    OPEN V_LIST FOR
    SELECT NAME FROM DW.table_name_list ;
     LOOP 
		FETCH V_LIST
		INTO V_TABLE_NAME;
		EXIT WHEN V_LIST%NOTFOUND;
		EXECUTE IMMEDIATE 'CREATE TABLE Schema.'||V_TABLE_NAME||'_'||DATE_VALUE_INS||' AS SELECT * FROM DW.'||V_TABLE_NAME;
	 END LOOP;
	CLOSE V_LIST; 
end;

I have created this Proc which takes value from a table which has Table_name and create Backup using Execute Immediate.

Now the requirement has changed that i only need to create backup for partial records (i.e. where clause on each table )

I have 6 tables as such .

New Approach i am thinking is :

 EXECUTE IMMEDIATE 'CREATE TABLE Schema.'||V_TABLE_NAME||'_'||DATE_VALUE_INS||' AS SELECT * FROM DW.'||V_TABLE_NAME where some condition;

But the problem becomes all 6 have different column to filter on.

My Ask is How should I change my design of proc to Adjust this new Requirement.

答案1

得分: 0

6张表?何必呢?创建一个过程,根据传递的表名作为参数,在IF-THEN-ELSE中运行6个不同的CREATE TABLE语句。

另一种方法是在SQL级别预先创建备份表,为每个表添加BACKUP_DATE列,并在过程中执行不需要动态SQL的INSERT操作。

例如:

create table emp_backup as select * from emp where 1 = 2;
alter table emp_backup add backup_date date;

create or replace procedure p_backup (par_table_name in varchar2) is
begin
  if par_table_name = 'EMP' then
     insert into emp_backup (empno, ename, job, sal, backup_date)
     select empno, ename, job, sal, trunc(sysdate)
     from emp 
     where deptno = 20;       --这是你的WHERE条件
  elsif par_table_name = 'DEPT' then
     insert into dept_backup (...)
     select ..., trunc(sysdate)
     from dept
     where loc = 'DALLAS';
  elsif ...
     ...
  end if;
end;
/

这样做,您可以更容易访问备份数据,因为您只需要查询一个表,通过BACKUP_DATE进行筛选。如果您需要搜索几天前发生变化的数据,但不知道确切的日期,您宁愿做什么:查询10个表(并且仍然找不到所需的信息),还是查询一个表并立即找到所需信息?

英文:

6 tables? Why bother? Create a procedure which - depending on table name passed as a parameter - in IF-THEN-ELSE runs 6 different CREATE TABLE statements.

On the other hand, another approach would be to create backup tables in advance (at SQL level), add BACKUP_DATE column to each of them, and - in procedure - just perform INSERT operation which doesn't require dynamic SQL at all.

For example:

create table emp_backup as select * from emp where 1 = 2;
alter table emp_backup add backup_date date;

create or replace procedure p_backup (par_table_name in varchar2) is
begin
  if par_table_name = 'EMP' then
     insert into emp_backup (empno, ename, job, sal, backup_date)
     select empno, ename, job, sal, trunc(sysdate)
     from emp 
     where deptno = 20;       --> here's your WHERE condition
  elsif par_table_name = 'DEPT' then
     insert into dept_backup (...)
     select ..., trunc(sysdate)
     from dept
     where loc = 'DALLAS';
  elsif ...
     ...
  end if;
end;
/

Doing so, you'd easier access backup data as you'd query only one table, filtered by BACKUP_DATE. That's also good if you have to search for some data that changed several days ago, but you don't know exact day. What would you rather do: query 10 tables (and still not find what you're looking for), or query just one table and find that info immediately?

答案2

得分: 0

创建过程 create_backup_table
@table_name nvarchar(max) = 'Repository.UserOrganizationUserGroup'
作为
开始
声明 @TABLE NVARCHAR(MAX) = @table_name+'_backup_'+format(getdate(),'ddmmyyyy')
声明 @SQL NVARCHAR(MAX) = 'select * into '+@TABLE+' from '+@table_name
执行 sp_executesql @sql
打印 @TABLE
返回
结束
英文:
create proc create_backup_table
@table_name nvarchar(max)  = 'Repository.UserOrganizationUserGroup'
AS
BEGIN
DECLARE @TABLE NVARCHAR(MAX) = @table_name+'_backup_'+format(getdate(),'ddmmyyyy')
DECLARE @SQL NVARCHAR(MAX) = 'select * into '+@TABLE+' from ' +@table_name
EXEC sp_executesql @sql
PRINT @TABLE
RETURN
END

huangapple
  • 本文由 发表于 2023年2月10日 03:20:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定