Delete all rows from all tables in a SQL Server database

Home / SQL Server / Delete all rows from all tables in a SQL Server database

I have seen most of the time Developer asked me this question .. Following below is solutio

 
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’

edgedoll.com