zoukankan      html  css  js  c++  java
  • Drop tables/constraints/view/function/store procedure

    sometimes, we have no rights to drop the whole database in SQL SERVER. But we still want to clear the the database and recreate tables/constraints/views/functions/store procedure etc.

    The following is the script. use at your risk.

      1 /* Drop all non-system stored procs */
      2 DECLARE @name VARCHAR(128)
      3 DECLARE @SQL VARCHAR(254)
      4 
      5 SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P' AND category = 0 ORDER BY [name])
      6 
      7 WHILE @name is not null
      8 BEGIN
      9     SELECT @SQL = 'DROP PROCEDURE [dbo].[' + RTRIM(@name) +']'
     10     EXEC (@SQL)
     11     PRINT 'Dropped Procedure: ' + @name
     12     SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P' AND category = 0 AND [name] > @name ORDER BY [name])
     13 END
     14 GO
     15 
     16 /* Drop all views */
     17 DECLARE @name VARCHAR(128)
     18 DECLARE @SQL VARCHAR(254)
     19 
     20 SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V' AND category = 0 ORDER BY [name])
     21 
     22 WHILE @name IS NOT NULL
     23 BEGIN
     24     SELECT @SQL = 'DROP VIEW [dbo].[' + RTRIM(@name) +']'
     25     EXEC (@SQL)
     26     PRINT 'Dropped View: ' + @name
     27     SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V' AND category = 0 AND [name] > @name ORDER BY [name])
     28 END
     29 GO
     30 
     31 /* Drop all functions */
     32 DECLARE @name VARCHAR(128)
     33 DECLARE @SQL VARCHAR(254)
     34 
     35 SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 ORDER BY [name])
     36 
     37 WHILE @name IS NOT NULL
     38 BEGIN
     39     SELECT @SQL = 'DROP FUNCTION [dbo].[' + RTRIM(@name) +']'
     40     EXEC (@SQL)
     41     PRINT 'Dropped Function: ' + @name
     42     SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 AND [name] > @name ORDER BY [name])
     43 END
     44 GO
     45 
     46 /* Drop all Foreign Key constraints */
     47 DECLARE @name VARCHAR(128)
     48 DECLARE @constraint VARCHAR(254)
     49 DECLARE @SQL VARCHAR(254)
     50 
     51 SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME)
     52 
     53 WHILE @name is not null
     54 BEGIN
     55     SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
     56     WHILE @constraint IS NOT NULL
     57     BEGIN
     58         SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) +'] DROP CONSTRAINT [' + RTRIM(@constraint) +']'
     59         EXEC (@SQL)
     60         PRINT 'Dropped FK Constraint: ' + @constraint + ' on ' + @name
     61         SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND CONSTRAINT_NAME <> @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
     62     END
     63 SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME)
     64 END
     65 GO
     66 
     67 /* Drop all Primary Key constraints */
     68 DECLARE @name VARCHAR(128)
     69 DECLARE @constraint VARCHAR(254)
     70 DECLARE @SQL VARCHAR(254)
     71 
     72 SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME)
     73 
     74 WHILE @name IS NOT NULL
     75 BEGIN
     76     SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
     77     WHILE @constraint is not null
     78     BEGIN
     79         SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) +'] DROP CONSTRAINT [' + RTRIM(@constraint)+']'
     80         EXEC (@SQL)
     81         PRINT 'Dropped PK Constraint: ' + @constraint + ' on ' + @name
     82         SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND CONSTRAINT_NAME <> @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
     83     END
     84 SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME)
     85 END
     86 GO
     87 
     88 /* Drop all tables */
     89 DECLARE @name VARCHAR(128)
     90 DECLARE @SQL VARCHAR(254)
     91 
     92 SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 ORDER BY [name])
     93 
     94 WHILE @name IS NOT NULL
     95 BEGIN
     96     SELECT @SQL = 'DROP TABLE [dbo].[' + RTRIM(@name) +']'
     97     EXEC (@SQL)
     98     PRINT 'Dropped Table: ' + @name
     99     SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 AND [name] > @name ORDER BY [name])
    100 END
    101 GO
    View Code
  • 相关阅读:
    poj 3087 直接模拟
    POJ-3126 BFS,埃式筛选及黑科技
    POJ3278-Catch That Cow
    js变量提升
    饿了么
    2分钟就能学会的【Google/百度搜索大法】了解一下?
    span标签间距
    Vue移动端项目如何使用手机预览调试
    Port 3000 is already in use
    koa2第一天 async详解
  • 原文地址:https://www.cnblogs.com/Jenny90/p/3414717.html
Copyright © 2011-2022 走看看