Petter Petter
Petter Petter

Reputation: 103

Clearing a table before insertion

I am using Microsoft SQL Server Management Studio.

I have a script file, like this

SET IDENTITY_INSERT [dbo].[Cars] ON
.....
Many insert statements
.....
SET IDENTITY_INSERT [dbo].[Cars] OFF

Question is if I can add something before this so that the table is cleared and updated with the new insertions?

Upvotes: 0

Views: 107

Answers (2)

René Vogt
René Vogt

Reputation: 43896

You can use TRUNCATE TABLE:

TRUNCATE TABLE [dbo].[Cars]
SET IDENTITY_INSERT [dbo].[Cars] ON
.....
Many insert statements
.....
SET IDENTITY_INSERT [dbo].[Cars] OFF

This removes all records from the table faster and using fewer system resources then DELETE.

Upvotes: 4

Bacon Bits
Bacon Bits

Reputation: 32170

You can use:

TRUNCATE TABLE [dbo].[Cars]

That will delete all records in the specified table.

Note that TRUNCATE TABLE will reset the counter on IDENTITY columns to the initial value in the CREATE TABLE statement. If you don't want to do that, you can use:

DELETE FROM [dbo].[Cars]

Upvotes: 3

Related Questions