How to avoid Foreign Keys constraints for all tables in DB truncate ?

Posted by eugeneK on Stack Overflow See other posts from Stack Overflow or by eugeneK
Published on 2010-06-03T11:19:43Z Indexed on 2010/06/03 11:54 UTC
Read the original article Hit count: 143

Filed under:
|

Hi, for designing purposes i need to truncate all DB which has lots of FK's. I cannot use DELETE command simply because some tables set with Identity of TinyInts and contain about 150 items.

this is a query ( truncate all tables in selected DB ) i'm trying to run

Declare @t varchar (1024)
Declare tbl_cur cursor for  
select TABLE_NAME from INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'

OPEN tbl_cur

FETCH NEXT  from tbl_cur INTO @t

WHILE @@FETCH_STATUS = 0
BEGIN
EXEC ('TRUNCATE TABLE '+ @t)
FETCH NEXT  from tbl_cur INTO @t
END

CLOSE tbl_cur
DEALLOCATE tbl_Cur

What the best and easiest way to achieve truncate on DB with many FK's ?

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server