壹佰网|ERP100 - 企业信息化知识门户

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2370|回复: 6

建立好的client如何删除?

[复制链接]
发表于 2008/8/29 21:59:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。如果您注册时有任何问题请联系客服QQ: 83569622  。

您需要 登录 才可以下载或查看,没有帐号?注册

x
我建了几个实验性的client,可是竟然删不掉,谁可教我?
发表于 2008/9/1 21:36:45 | 显示全部楼层

很久以前写过一个脚本,算是给你个思路

发表于 2008/9/1 21:37:25 | 显示全部楼层

回复 2楼 pshen 的帖子

/*************************************************************************
* The contents of this file are subject to the LGPL.  
* The Original Author is Peter Shen
* The Personal Information of the contributors:
*      1,  Peter Shen
*          zpshen@gmail.com
*          Shanghai, China
*
*************************************************************************
* $Id: RemoveClient.sql,v 1.1 2005/01/26 14:47:29 pshen Exp $
***
* Title:   Remove a Client in Compiere.
* Description:
*        - Disable all the constraints and triggers
*  - Delete the records from all the table
*  - Enable all  the constraints and triggers
*
* Warning:
*  - This script is only for Oracle 9.
*  - Please backup all the data before you use this script
*
* Guide:
*  - Log in the oracle sqlplus (or other sqlplus tools) with the schema from which you want to delete data
*  - Changed the v_Client_ID to AD_Client_ID which you want to delete
*  - This script would be a little slow, please wait patiently
************************************************************************/

  DECLARE
    /**
     * Please change this one to any client id you want to delete
     **/
    v_Client_ID                                                 NUMBER      := 1000003;
   
    v_SQL1                               VARCHAR2(1024);
   
    CURSOR Cur_Contraints  IS
                select table_name,constraint_name
                from user_constraints
                where constraint_type = 'R'
                AND status='ENABLED';
       
        CURSOR Cur_Triggers  IS
                select TRIGGER_NAME
                from user_triggers
                where status='ENABLED';
                       
   CURSOR Cur_RemoveData  IS
            select 'delete from '|| TABLENAME ||' where AD_Client_ID=' || v_Client_ID
            AS v_SQL
                from AD_Table a
                where a.ISVIEW='N'
                AND exists ( select AD_Column_ID from AD_Column c where a.AD_Table_ID=c.AD_Table_ID
                and upper(c.COLUMNNAME)= upper('AD_Client_ID') );
            
               
BEGIN
   
    DBMS_OUTPUT.PUT_LINE('  Delete Client Where AD_Client_ID=' || v_Client_ID);
   
    /****************************************************************
     *  Disable all the constraints one by one
     ****************************************************************/
     DBMS_OUTPUT.PUT_LINE(' Disable the contraints ');
     FOR p IN Cur_Contraints  LOOP
       v_SQL1 := 'alter table '|| p.table_name ||' disable constraint '|| p.constraint_name;
        EXECUTE IMMEDIATE v_SQL1;
     END LOOP;        --        Disable contraints
     
     
     DBMS_OUTPUT.PUT_LINE(' Disable the triggers ');
     FOR p IN Cur_Triggers  LOOP
       v_SQL1 := 'alter trigger '|| p.TRIGGER_NAME ||' disable ';
        EXECUTE IMMEDIATE v_SQL1;
     END LOOP;        --        Disable contraints
     
     /****************************************************************
     *  Remove all the records belongs to that client
     ****************************************************************/
     FOR p IN Cur_RemoveData LOOP
        v_SQL1 := p.v_SQL;
        EXECUTE IMMEDIATE v_SQL1;
     END LOOP;        --        Remove data
     
     
     /****************************************************************
     *  Disable all the constraints one by one
     ****************************************************************/
     DBMS_OUTPUT.PUT_LINE(' Enable the contraints ');
     FOR p IN Cur_Contraints  LOOP
        v_SQL1 := 'alter table '|| p.table_name ||' enable constraint '|| p.constraint_name;
        EXECUTE IMMEDIATE v_SQL1;
     END LOOP;        --        Enable contraints
     
     DBMS_OUTPUT.PUT_LINE(' Enable the triggers ');
     FOR p IN Cur_Triggers  LOOP
       v_SQL1 := 'alter trigger '|| p.TRIGGER_NAME ||' enabled ';
        EXECUTE IMMEDIATE v_SQL1;
     END LOOP;        --        Enable contraints
     
     COMMIT;
END;
发表于 2008/9/2 10:07:49 | 显示全部楼层

热烈欢迎老大!

牛人啊~
发表于 2008/12/15 17:53:43 | 显示全部楼层
我也在琢磨这个问题,系统初始化了好多个实体(客户),怎么把不要的实体数据删除,而且各个表之间都有记录相互引用的,要直接删除很麻烦,看了很受启发。
发表于 2009/6/12 16:17:47 | 显示全部楼层
pshen,好久不见了啊!期待中
发表于 2012/1/13 13:10:28 | 显示全部楼层
SET SERVEROUTPUT ON;
EXEC DBMS_OUTPUT.ENABLE(200000);
DECLARE
  /*  Specify the Client you want to remove  */
  v_Client_ID NUMBER := 11;
  v_SQL       VARCHAR2(1024);
  v_Code      NUMBER;
  v_ErrMsg    VARCHAR2(500);

  TYPE ArrayType IS TABLE OF VARCHAR2(200);
  all_rec ArrayType;
  err_rec ArrayType;
  
  PROCEDURE performDelete(deleteSQList ArrayType) AS     
  BEGIN   
    FOR i IN deleteSQList.FIRST .. deleteSQList.LAST LOOP
      BEGIN
        v_SQL := deleteSQList(i);
        DBMS_OUTPUT.PUT_LINE('  performDelete==> Executing: ' || v_SQL);
        EXECUTE IMMEDIATE v_SQL;
      EXCEPTION
        WHEN OTHERS THEN
          v_Code   := SQLCODE;
          v_ErrMsg := SQLERRM;
          IF v_Code = 02292 THEN
            err_rec.extend;
            err_rec(i) := v_SQL;
          END IF;
      END;
    END LOOP;
    COMMIT;
    IF (err_rec IS NOT NULL AND err_rec.COUNT > 0) THEN
      performDelete(err_rec);
    END IF;
  EXCEPTION
    WHEN OTHERS THEN
      v_Code   := SQLCODE;
      v_ErrMsg := SQLERRM;
      DBMS_OUTPUT.PUT_LINE('  performDelete Error==> ' || v_ErrMsg);
  END;

BEGIN
  SELECT 'delete from ' || TABLENAME || ' where AD_Client_ID=' || v_Client_ID BULK COLLECT INTO all_rec
    FROM AD_Table a
   WHERE a.ISVIEW = 'N'
     AND EXISTS
   (SELECT AD_Column_ID
            FROM AD_Column c
           WHERE a.AD_Table_ID = c.AD_Table_ID
             AND upper(c.COLUMNNAME) = upper('AD_Client_ID'));
  DBMS_OUTPUT.PUT_LINE('Begin: ' || current_timestamp ||' ==> delete count: ' || all_rec.count);
  performDelete(all_rec);
  DBMS_OUTPUT.PUT_LINE('END: ' || current_timestamp );
  
EXCEPTION
  WHEN OTHERS THEN
    v_Code   := SQLCODE;
    v_ErrMsg := SQLERRM;
    DBMS_OUTPUT.PUT_LINE('Error: ' || v_Code || ': ' || v_ErrMsg);
END;
/
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|手机版|壹佰网 ERP100 ( 京ICP备19053597号-2 )

Copyright © 2005-2012 北京海之大网络技术有限责任公司 服务器托管由互联互通
手机:13911575376
网站技术点击发送消息给对方83569622   广告&合作 点击发送消息给对方27675401   点击发送消息给对方634043306   咨询及人才点击发送消息给对方138011526

GMT+8, 2025/11/29 02:31 , Processed in 0.019394 second(s), 16 queries , File On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表