Save other objects in the database unchanged, delete all data in the database implementation method

Source: Internet
Author: User
Tags join key reset table name
Objects | data | database
Original post content:
How to delete all the data in the database and then reset all the automatic increments?
There are too many tables to complete manually.
http://community.csdn.net/Expert/topic/3094/3094555.xml?temp=.2920954

/*
--originally intended to
--Disable all foreign KEY constraints first
EXEC sp_msforeachtable "ALTER TABLE"? Nocheck CONSTRAINT All "
--and then delete the data
EXEC sp_msforeachtable "TRUNCATE table?"
--Enable all foreign key constraints
EXEC sp_msforeachtable "ALTER TABLE"? Check constraint All "
--but when disabled, TRUNCATE table does not work, it prompts for conflict
*/

--Now my idea is (statement to be optimized):


--the first part, generate the statement to establish the foreign key save to #tmp
DECLARE @name varchar, @tmp1 varchar (+), @tmp2 varchar (500)

CREATE TABLE #tmp
(
String varchar (8000)
)

SELECT table name =object_name (B.fkeyid)
, FOREIGN key name =a.name
, the referenced column name = (SELECT name from syscolumns WHERE Colid=b.fkey and Id=b.fkeyid)
, referenced table name =object_name (B.rkeyid)
, the referenced column name = (SELECT name from syscolumns WHERE Colid=b.rkey and Id=b.rkeyid)
into #t from sysobjects a
Join Sysforeignkeys B on A.id=b.constid
Join sysobjects C on a.parent_obj=c.id
Where a.xtype= ' F ' and c.xtype= ' U '

DECLARE cur_test cursor FOR
Select A.name from sysobjects a join sysobjects C on a.parent_obj=c.id where a.xtype= ' f ' and c.xtype= ' U '
Open Cur_test
FETCH NEXT from Cur_test into @name
while (@ @fetch_status <>-1)
BEGIN
IF (@ @fetch_status <>-2)
BEGIN
Select @tmp1 = ', @tmp2 = '
Select @tmp1 = @tmp1 + ' [' + referenced column name + '], ', @tmp2 = @tmp2 + ' [' + referenced column name + '], ' from #t where foreign key name = @name
Insert INTO #tmp select top 1 ' ALTER TABLE [DBO]. [' + table name + '] ADD CONSTRAINT [' + @name + '] FOREIGN KEY (' +left (@tmp1, Len (@tmp1)-1) + ') REFERENCES [' + Referenced table name + '] (' +left (@tmp2, Len (@tmp2)- 1 + ') ' from #t where foreign key name = @name
End
FETCH NEXT from Cur_test into @name
End

Close Cur_test
Deallocate cur_test
drop table #t

--Part Two, delete all foreign keys
DECLARE @STRING VARCHAR (8000)
While EXISTS (SELECT NAME from sysobjects WHERE type= ' F ')
BEGIN
SELECT @STRING = ' ALTER TABLE ' +b.name+ ' DROP CONSTRAINT ' +a.name+char (13)
From (SELECT parent_obj,name from sysobjects WHERE type= ' F ') A,
(SELECT id,name from sysobjects WHERE objectproperty (ID, N ' isusertable ') = 1) B
WHERE a.parent_obj=b.id
EXEC (@STRING)
End

--Part III, delete all the records of the table and reset the identity
EXEC sp_msforeachtable "TRUNCATE table?"

--The 4th part, executes the #tmp inside the statement which establishes the foreign key, restores the foreign key
Declare cur_test2 cursor for select string from #tmp

Open Cur_test2
FETCH NEXT from Cur_test2 into @string
while (@ @fetch_status <>-1)
BEGIN
IF (@ @fetch_status <>-2)
BEGIN
EXEC (@string)
PRINT @STRING
End
FETCH NEXT from Cur_test2 into @string
End

Close Cur_test2
Deallocate Cur_test2

drop table #tmp



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.