Deletes all stored procedures, primary keys, foreign keys, and indexes of a database.

Source: Internet
Author: User
-- Delete a stored procedure
DECLARE @ string varchar (8000)
While exists (select name from sysobjects where type = 'p' and status> = 0)
BEGIN
SELECT @ STRING = 'drop PROCEDURE '+ name from sysobjects where type = 'p' and status> = 0
-- SELECT @ STRING
EXEC (@ STRING)
END

GO

-- DEFAULT value or DEFAULT Constraint
DECLARE @ string varchar (8000)
While exists (select name from sysobjects where xtype = 'D ')
BEGIN
SELECT @ STRING = 'alter table' + B. NAME + 'drop constraint' + A. NAME
FROM (SELECT PARENT_OBJ, name from sysobjects where xtype = 'D'),
(Select id, name from sysobjects where objectproperty (ID, N 'isusertable') = 1) B
Where a. PARENT_OBJ = B. ID
EXEC (@ STRING)
END

GO

-- UNIQUE constraints
DECLARE @ string varchar (8000)
While exists (select name from sysobjects where xtype = 'uq ')
BEGIN
SELECT @ STRING = 'alter table' + B. NAME + 'drop constraint' + A. NAME
FROM (SELECT PARENT_OBJ, name from sysobjects where xtype = 'uq'),
(Select id, name from sysobjects where objectproperty (ID, N 'isusertable') = 1) B
Where a. PARENT_OBJ = B. ID
EXEC (@ STRING)
END

GO

-- Foreign key constraint
DECLARE @ string varchar (8000)
While exists (select name from sysobjects where type = 'F ')
BEGIN
SELECT @ STRING = 'alter table' + B. NAME + 'drop constraint' + A. NAME
FROM (SELECT PARENT_OBJ, name from sysobjects where type = 'F'),
(Select id, name from sysobjects where objectproperty (ID, N 'isusertable') = 1) B
Where a. PARENT_OBJ = B. ID
EXEC (@ STRING)
END

GO

-- Primary key constraint
DECLARE @ string varchar (8000)
While exists (select name from sysobjects where xtype = 'pk ')
BEGIN
SELECT @ STRING = 'alter table' + B. NAME + 'drop constraint' + A. NAME
FROM (SELECT PARENT_OBJ, name from sysobjects where xtype = 'pk'),
(Select id, name from sysobjects where objectproperty (ID, N 'isusertable') = 1) B
Where a. PARENT_OBJ = B. ID
EXEC (@ STRING)
END

GO

-- Trigger
DECLARE @ string varchar (8000)
While exists (select name from sysobjects where xtype = 'tr ')
BEGIN
SELECT @ STRING = 'drop trigger' + name from sysobjects where xtype = 'tr'
EXEC (@ STRING)
END

GO

-- Index
Declare @ string varchar (8000)
While exists (
Select TABLE_NAME = o. name, INDEX_NAME = x. name
From sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
Where o. type in ('U ')
And convert (bit, (x. status & 0x800)/0x800) = 0
And x. id = o. id
And o. id = c. id
And o. id = xk. id
And x. indid = xk. indid
And c. colid = xk. colid
And xk. keyno <= x. keycnt
And permissions (o. id, c. name) <> 0
And (x. status & 32) = 0 -- No hypothetical indexes
Group by o. name, x. name)
Begin
Select top 1 @ string = 'drop Index' + o. name + '.' + x. name
From sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
Where o. type in ('U ')
And convert (bit, (x. status & 0x800)/0x800) = 0
And x. id = o. id
And o. id = c. id
And o. id = xk. id
And x. indid = xk. indid
And c. colid = xk. colid
And xk. keyno <= x. keycnt
And permissions (o. id, c. name) <> 0
And (x. status & 32) = 0 -- No hypothetical indexes
Group by o. name, x. name
Exec (@ string)
End

GO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.