Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Force truncate table

[es] :: MS SQL :: Force truncate table

[ Pregleda: 2386 | Odgovora: 1 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

Pharos
Pančevo

Član broj: 20664
Poruke: 1029
..et.174.106.194.in-addr.arpa.



+2 Profil

icon Force truncate table07.05.2008. u 08:25 - pre 194 meseci
Imam bazu sa preko 300 tabela koja mora da se pročisti - obrišu podaci iz oko 250 tabela.
Problem je što mnoge tabele imaju foreign key constraint pa za njihovo uklanjanje treba baš baš mnogo vremena.
Postoji li neki toolić ili neka naredba kojom bih mogao da odradim truncate, a da se ne obazirem na FKC?
77 77 77 2E 65 73 6E 69 70 73 2E 63 6F 6D
 
Odgovor na temu

adopilot
Admir Hodžić
It menager
Sarajevo BiH

Član broj: 123492
Poruke: 134
217.199.133.*

Sajt: nemam ja to


Profil

icon Re: Force truncate table10.05.2008. u 09:36 - pre 194 meseci
Ovo je copy / paste odgovro
sa sqlteam.com foruma (tamo je na tu temu pričano više puta)
a meni se čini da je to to
Code:

/*

This batch t-sql deletes data from all the tables in the database.

Here is what it does:
1) Disable all the constraints/triggers for all the tables
2) Delete the data for each child table & stand-alone table
3) Delete the data for all the parent tables
4) Reseed the identities of all tables to its initial value.
5) Enable all the constraints/triggers for all the tables.


Note: This is a batch t-sql code which does not create any object in database. 
If any error occurs, re-run the code again. It does not use TRUNCATE statement to delete
the data and instead it uses DELETE statement. Using DELETE statement can increase the 
size of the log file and hence used the CHECKPOINT statement to clear the log file after 
every DELETE statement.

Imp: You may want to skip CHECKIDENT statement for all tables and manually do it yourself. To skip the CHECKIDENT,
set the variable @skipident to "YES" (By default, its set to "NO")

Usage: replace #database_name# with the database name (that you wanted to truncate) and just execute the script in query analyzer.

*/

use [#database_name#]

Set NoCount ON


Declare @tableName varchar(200)
Declare @tableOwner varchar(100)
Declare @skipident varchar(3)
Declare @identInitValue int

set @tableName = ''
set @tableOwner = ''
set @skipident = 'NO'
set @identInitValue=1

/*
Step 1: Disable all constraints
*/

exec sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL' 
exec sp_MSforeachtable 'ALTER TABLE ? DISABLE TRIGGER ALL' 

/*
Step 2: Delete the data for all child tables & those which has no relations
*/

While exists

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 top 1 @tableOwner=T.table_schema,@tableName=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
order by t.table_name


--Delete the table
Exec('DELETE FROM '+ @tableOwner + '.' + @tableName)

--Reset identity column 
If @skipident = 'NO'
If exists(
SELECT * FROM information_schema.columns 
WHERE COLUMNPROPERTY(OBJECT_ID( 
QUOTENAME(table_schema)+'.'+QUOTENAME(@tableName)), 
column_name,'IsIdentity')=1

begin
set @identInitValue=1
set @identInitValue=IDENT_SEED(@tableOwner + '.' + @tableName)
DBCC CHECKIDENT (@tableName, RESEED, @identInitValue)
end

checkpoint
End

/*
Step 3: Delete the data for all Parent tables
*/

set @TableName=''
set @tableOwner=''

While exists

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 top 1 @tableOwner=T.table_schema,@tableName=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
order by t.table_name

--Delete the table
Exec('DELETE FROM '+ @tableOwner + '.' + @tableName)

--Reset identity column 
If @skipident = 'NO'
If exists(
SELECT * FROM information_schema.columns 
WHERE COLUMNPROPERTY(OBJECT_ID( 
QUOTENAME(table_schema)+'.'+QUOTENAME(@tableName)), 
column_name,'IsIdentity')=1

begin
set @identInitValue=1
set @identInitValue=IDENT_SEED(@tableOwner + '.' + @tableName)
DBCC CHECKIDENT (@tableName, RESEED, @identInitValue)
end

checkpoint

End

/*
Step 4: Enable all constraints
*/

exec sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL' 
exec sp_MSforeachtable 'ALTER TABLE ? ENABLE TRIGGER ALL' 

Set NoCount Off

S poštovanjem
 
Odgovor na temu

[es] :: MS SQL :: Force truncate table

[ Pregleda: 2386 | Odgovora: 1 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.