zoukankan      html  css  js  c++  java
  • 清除 数据库表 上面 清除 没有外间关系 下面 清除 有外键关系的

    SET NoCount ON  
       DECLARE @tableName varchar(512)   
       Declare @SQL varchar(2048)   
       SET @tableName=''  
       WHILE EXISTS   
       (      
       --Find all child tables and those which have no relations   
       SELECT T.table_name   FROM INFORMATION_SCHEMA.TABLES T   
              LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC    ON T.table_name = TC.table_name   
         WHERE ( TC.constraint_Type = 'Foreign Key' OR TC.constraint_Type IS NULL )   
             AND T.table_name NOT IN ( 'dtproperties', 'sysconstraints', 'syssegments' )   
             AND Table_type = 'BASE TABLE'  
             AND T.table_name > @TableName   
             )   
        Begin  
            SELECT @tableName = min(T.table_name)    FROM INFORMATION_SCHEMA.TABLES T   
            LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC    ON T.table_name=TC.table_name   
               WHERE ( TC.constraint_Type = 'Foreign Key' OR TC.constraint_Type IS NULL )   
             AND T.table_name NOT IN ( 'dtproperties', 'sysconstraints', 'syssegments' )   
             AND Table_type = 'BASE TABLE'  
             AND T.table_name > @TableName   
             --Truncate the table   
             SET @SQL = 'Truncate table '+ @TableName    
             print (@SQL)   
             Exec(@SQL)   
         End  
         
       SET @TableName=''  
       WHILE EXISTS   
       (    
       --Find all Parent tables   
         SELECT T.table_name     FROM INFORMATION_SCHEMA.TABLES T   
         LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC     ON T.table_name = TC.table_name   
         WHERE TC.constraint_Type = 'Primary Key'  
         AND T.table_name <> 'dtproperties'  
         AND Table_type='BASE TABLE'  
         AND T.table_name > @TableName   
         )   
       Begin  
         SELECT @tableName = min(T.table_name)   FROM INFORMATION_SCHEMA.TABLES T   
              LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC   ON T.table_name=TC.table_name   
         WHERE TC.constraint_Type = 'Primary Key'  
         AND T.table_name <> 'dtproperties'  
         AND Table_type = 'BASE TABLE'  
         AND T.table_name > @TableName   
         --Delete the table   
           
            SET @SQL = ' delete from '+ @TableName    
             print (@SQL)   
             Exec(@SQL)   
         --Reset identity column   
             IF EXISTS ( SELECT *   FROM INFORMATION_SCHEMA.COLUMNS   
                 WHERE COLUMNPROPERTY(   
                 OBJECT_ID( QUOTENAME(table_schema)+ '.' + QUOTENAME(@tableName) ),   
                 column_name,'IsIdentity'  
                 ) = 1   
               )   
         DBCC CHECKIDENT(@tableName,RESEED,0)   
       End  
       SET NoCount OFF  
    
    SET NoCount ON
       DECLARE @tableName varchar(512)
       Declare @SQL varchar(2048)
       SET @tableName=''
       WHILE EXISTS
       (   
       --Find all child tables and those which have no relations
       SELECT T.table_name   FROM INFORMATION_SCHEMA.TABLES T
              LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC    ON T.table_name = TC.table_name
         WHERE ( TC.constraint_Type = 'Foreign Key' OR TC.constraint_Type IS NULL )
             AND T.table_name NOT IN ( 'dtproperties', 'sysconstraints', 'syssegments' )
             AND Table_type = 'BASE TABLE'
             AND T.table_name > @TableName
             )
        Begin
            SELECT @tableName = min(T.table_name)    FROM INFORMATION_SCHEMA.TABLES T
            LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC    ON T.table_name=TC.table_name
               WHERE ( TC.constraint_Type = 'Foreign Key' OR TC.constraint_Type IS NULL )
             AND T.table_name NOT IN ( 'dtproperties', 'sysconstraints', 'syssegments' )
             AND Table_type = 'BASE TABLE'
             AND T.table_name > @TableName
             --Truncate the table
             SET @SQL = 'Truncate table '+ @TableName 
             print (@SQL)
             Exec(@SQL)
         End
      
       SET @TableName=''
       WHILE EXISTS
       ( 
       --Find all Parent tables
         SELECT T.table_name     FROM INFORMATION_SCHEMA.TABLES T
         LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC     ON T.table_name = TC.table_name
         WHERE TC.constraint_Type = 'Primary Key'
         AND T.table_name <> 'dtproperties'
         AND Table_type='BASE TABLE'
         AND T.table_name > @TableName
         )
       Begin
         SELECT @tableName = min(T.table_name)   FROM INFORMATION_SCHEMA.TABLES T
              LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC   ON T.table_name=TC.table_name
         WHERE TC.constraint_Type = 'Primary Key'
         AND T.table_name <> 'dtproperties'
         AND Table_type = 'BASE TABLE'
         AND T.table_name > @TableName
         --Delete the table
        
            SET @SQL = ' delete from '+ @TableName 
             print (@SQL)
             Exec(@SQL)
         --Reset identity column
             IF EXISTS ( SELECT *   FROM INFORMATION_SCHEMA.COLUMNS
                 WHERE COLUMNPROPERTY(
                 OBJECT_ID( QUOTENAME(table_schema)+ '.' + QUOTENAME(@tableName) ),
                 column_name,'IsIdentity'
                 ) = 1
               )
         DBCC CHECKIDENT(@tableName,RESEED,0)
       End
       SET NoCount OFF
    

      

  • 相关阅读:
    curl java 模拟http请求
    Redis 个人理解总结
    算法时间复杂度的表示法O(n²)、O(n)、O(1)、O(nlogn)等是什么意思?
    RESTful 个人理解总结
    springcloud(五):熔断监控Hystrix Dashboard和Turbine
    springcloud(四):熔断器Hystrix
    springcloud(三):服务提供与调用
    springcloud(二):注册中心Eureka
    springcloud(一):大话Spring Cloud
    Spring Cloud在国内中小型公司用的起来吗?
  • 原文地址:https://www.cnblogs.com/laojiefang/p/2575925.html
Copyright © 2011-2022 走看看