9.4.10

Delete data permanently in SQL Server

Delete data permanently in SQL Server:

Scenario: I want to delete records from my table.

Query:
drop table venkattable
create table venkattable(id int identity, program_id varchar(10), subprogram_id varchar(10))
insert into venkattable(program_id,subprogram_id) values('a','a1')
insert into venkattable(program_id,subprogram_id) values('a','a2')
insert into venkattable(program_id,subprogram_id) values('a','a3')


Option 1:

delete from venkattable
-- The above statement will delete the record but maintain the identity value.
insert into venkattable(program_id,subprogram_id) values('a','a3')
select * from venkattable


Above statement will insert a record with id 4. The data is removed but the identity value retains.

To resolve this problem we can use reseed option.

DBCC CHECKIDENT (venkattable, RESEED, 0)

Option 2:

We can go for truncate option.
TRUNCATE TABLE venkattable

Cheers,
Venkatesan Prabu .J

No comments:

Post a Comment