Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete all rows in sql except one

Tags:

sql

sql-server

I have the following query:

DELETE FROM [DSPCONTENT01].[dbo].[Contact Center]
WHERE [Contact Center] IS NULL
  AND [F2] IS NULL

How can I modify the query so after execution ONLY row 15 is still showing and every other row is deleted?

like image 284
Si8 Avatar asked Jan 22 '26 00:01

Si8


1 Answers

Some other way to delete except the specific row:

DELETE FROM [DSPCONTENT01].[dbo].[Contact Center] WHERE F10 NOT IN (2096)

You can also Select the particular record into a temporary table, then drop the original table and finally copy the data from the temporary table to the original table. Something like this:

create table #ContactCenter as
select * from [DSPCONTENT01].[dbo].[Contact Center] where F10 = 2096

truncate table [DSPCONTENT01].[dbo].[Contact Center]

insert into [DSPCONTENT01].[dbo].[Contact Center]
    select * from #ContactCenter
like image 147
arin1405 Avatar answered Jan 24 '26 18:01

arin1405