Slet al data i SQL Server database

Hvis det er nødvendigt at slette al data i alle tabeller i en SQL Server database kan følgende T-SQL script hjælpe:

— Source: http://vadivel.blogspot.com/2006/07/easiest-fastest-way-to-delete-all.html
Set nocount on

Exec sp_MSForEachTable ‘Alter Table ? NoCheck Constraint All’

Exec sp_MSForEachTable

If ObjectProperty(Object_ID(”?”), ”TableHasForeignRef”)=1
Begin
— Just to know what all table used delete syntax.
Print ”Delete from ” + ”?”
Delete From ?
End
Else
Begin
— Just to know what all table used Truncate syntax.
Print ”Truncate Table ” + ”?”
Truncate Table ?
End

Exec sp_MSForEachTable ‘Alter Table ? Check Constraint All’

Leave a Reply

Your email address will not be published. Required fields are marked *