150

How to delete all the records in SQL Server 2008?

Jim G.
  • 15,141
  • 22
  • 103
  • 166
kaveh
  • 1,509
  • 2
  • 9
  • 3

10 Answers10

222

To delete all records from a table without deleting the table.

DELETE FROM table_name use with care, there is no undo!

To remove a table

DROP TABLE table_name

Dan Watkins
  • 918
  • 9
  • 25
PA.
  • 28,486
  • 9
  • 71
  • 95
54

from a table?

You can use this if you have no foreign keys to other tables

truncate table TableName

or

delete TableName

if you want all tables

sp_msforeachtable 'delete ?'
SQLMenace
  • 132,095
  • 25
  • 206
  • 225
19

Use the DELETE statement

Delete From <TableName>

Eg:

Delete from Student;
ttarchala
  • 4,277
  • 2
  • 26
  • 36
sachind
  • 387
  • 5
  • 8
15

I can see the that the others answers shown above are right, but I'll make your life easy.

I even created an example for you. I added some rows and want delete them.

You have to right click on the table and as shown in the figure Script Table a> Delete to> New query Editor widows:

enter image description here

Then another window will open with a script. Delete the line of "where", because you want to delete all rows. Then click Execute.

enter image description here

To make sure you did it right right click over the table and click in "Select Top 1000 rows". Then you can see that the query is empty.

rxmxn
  • 499
  • 1
  • 5
  • 17
Cyberguille
  • 1,552
  • 3
  • 28
  • 58
8

If you want to reset your table, you can do

truncate table TableName

truncate needs privileges, and you can't use it if your table has dependents (another tables that have FK of your table,

fdaines
  • 1,216
  • 10
  • 12
4

For one table

truncate table [table name]

For all tables

EXEC sp_MSforeachtable @command1="truncate table ?"
Dumitrescu Bogdan
  • 7,127
  • 2
  • 23
  • 31
2

Delete rows in the Results pane if you want to delete records in the database. If you want to delete all of the rows you can use a Delete query.

Delete from Table_name
Ihtisham Khattak
  • 170
  • 1
  • 2
  • 11
2
delete from TableName

isn't a good practice.

Like in Google BigQuery, it don't let to use delete without "where" clause.

use

truncate table TableName

instead

0

When the table is very large, it's better to delete table itself with drop table TableName and recreate it, if one has create table query; rather than deleting records one by one, using delete from statement because that can be time consuming.

Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
Shahzod1011
  • 69
  • 1
  • 5
0

The statement is DELETE FROM YourDatabaseName.SomeTableName; if you are willing to remove all the records with reasonable permission. But you may see errors in the constraints that you defined for your Foreign Keys. So that you need to change your constraints before removing the records otherwise there is a command for MySQL (which may work for others) to ignore the constraints.

SET foreign_key_checks = 0;

Please be aware that this command will disable your foreign keys constrain check, so this can be dangerous for the relationships you created within your schema.

Shrm
  • 426
  • 4
  • 8