transact sql query删除以aspnet_开头的所有表

时间:2013-06-14 05:07:00

标签: tsql asp.net-membership

我在网站主机上创建了一个新数据库,它给了我一堆表,视图和存储过程,这些都是我的需求(我使用自定义成员资格提供程序)。

所有表和存储过程都以dbo.aspnet _

开头

所有视图都以dbo.vw_aspnet _

开头

我对sql语法不是很熟悉,但是有一个sql命令可以删除以字符序列开头的所有表,视图和存储过程。

谢谢

修改

有几十个无用的数据库对象,但在其中,我有有用的数据库对象(我认为当我开始设置它时它是一个空的数据库)我想保留。关键是名称STARTS WITH aspnet _

请注意,这与删除所有表

不同

1 个答案:

答案 0 :(得分:3)

以防万一其他人最终处于同一位置 - 这是我最终使用的SQL,它成功地'取消'了我的数据库,但留下了有用的(非ASP)数据库对象

DECLARE @name VARCHAR(128), @SQL VARCHAR(254), @prefixLen int, @prefix nvarchar(30);
SET @prefix = 'aspnet_';
SET @prefixLen = LEN(@prefix);

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE LEFT(name,@prefixLen) = @prefix AND [type] = 'P' AND category = 0 ORDER BY [name])

WHILE @name is not null
BEGIN
    SELECT @SQL = 'DROP PROCEDURE [dbo].[' + RTRIM(@name) +']'
    EXEC (@SQL)
    PRINT 'Dropped Procedure: ' + @name
    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE LEFT(name,@prefixLen) = @prefix AND [type] = 'P' AND category = 0 AND [name] > @name ORDER BY [name])
END
GO

/* Drop all views */
DECLARE @name VARCHAR(128), @SQL VARCHAR(254), @prefixLen int, @prefix nvarchar(30);
SET @prefix = 'vw_aspnet_';
SET @prefixLen = LEN(@prefix);

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE LEFT(name,@prefixLen) = @prefix AND [type] = 'V' AND category = 0 ORDER BY [name])

WHILE @name IS NOT NULL
BEGIN
    SELECT @SQL = 'DROP VIEW [dbo].[' + RTRIM(@name) +']'
    EXEC (@SQL)
    PRINT 'Dropped View: ' + @name
    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE LEFT(name,@prefixLen) = @prefix AND [type] = 'V' AND category = 0 AND [name] > @name ORDER BY [name])
END
GO

/* Drop all functions */
DECLARE @name VARCHAR(128), @SQL VARCHAR(254), @prefixLen int, @prefix nvarchar(30);
SET @prefix = 'aspnet_';
SET @prefixLen = LEN(@prefix);

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE LEFT(name,@prefixLen) = @prefix AND [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 ORDER BY [name])

WHILE @name IS NOT NULL
BEGIN
    SELECT @SQL = 'DROP FUNCTION [dbo].[' + RTRIM(@name) +']'
    EXEC (@SQL)
    PRINT 'Dropped Function: ' + @name
    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE LEFT(name,@prefixLen) = @prefix AND [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 AND [name] > @name ORDER BY [name])
END
GO

/* Drop all Foreign Key constraints */
DECLARE @name VARCHAR(128), @SQL VARCHAR(254), @prefixLen int, @prefix nvarchar(30), @constraint VARCHAR(254);
SET @prefix = 'aspnet_';
SET @prefixLen = LEN(@prefix);

SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE LEFT([TABLE_NAME],@prefixLen) = @prefix AND constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME)

WHILE @name is not null
BEGIN
    SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE LEFT([TABLE_NAME],@prefixLen) = @prefix AND constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
    WHILE @constraint IS NOT NULL
    BEGIN
        SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) +'] DROP CONSTRAINT [' + RTRIM(@constraint) +']'
        EXEC (@SQL)
        PRINT 'Dropped FK Constraint: ' + @constraint + ' on ' + @name
        SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND LEFT([TABLE_NAME],@prefixLen) = @prefix AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND CONSTRAINT_NAME <> @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
    END
SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE LEFT([TABLE_NAME],@prefixLen) = @prefix AND constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME)
END
GO

/* Drop all Primary Key constraints */
DECLARE @name VARCHAR(128), @SQL VARCHAR(254), @prefixLen int, @prefix nvarchar(30), @constraint VARCHAR(254);
SET @prefix = 'aspnet_';
SET @prefixLen = LEN(@prefix);

SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE  LEFT([TABLE_NAME],@prefixLen) = @prefix AND constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME)

WHILE @name IS NOT NULL
BEGIN
    SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND LEFT([TABLE_NAME],@prefixLen) = @prefix AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
    WHILE @constraint is not null
    BEGIN
        SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) +'] DROP CONSTRAINT [' + RTRIM(@constraint)+']'
        EXEC (@SQL)
        PRINT 'Dropped PK Constraint: ' + @constraint + ' on ' + @name
        SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND LEFT([TABLE_NAME],@prefixLen) = @prefix AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND CONSTRAINT_NAME <> @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
    END
SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND LEFT([TABLE_NAME],@prefixLen) = @prefix AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME)
END
GO

/* Drop all tables */
DECLARE @name VARCHAR(128), @SQL VARCHAR(254), @prefixLen int, @prefix nvarchar(30);
SET @prefix = 'aspnet_';
SET @prefixLen = LEN(@prefix);

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE  LEFT([name],@prefixLen) = @prefix AND [type] = 'U' AND category = 0 ORDER BY [name])

WHILE @name IS NOT NULL
BEGIN
    SELECT @SQL = 'DROP TABLE [dbo].[' + RTRIM(@name) +']'
    EXEC (@SQL)
    PRINT 'Dropped Table: ' + @name
    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE LEFT([name],@prefixLen) = @prefix AND [type] = 'U' AND category = 0 AND [name] > @name ORDER BY [name])
END
GO