Saturday, 8 May 2021

Select and delete in single statement in SQL

Below SQL Query , Is to get the data after deleted from the table.

It can be used in the store procedure , in scenario of getting the data and deleting the data for same. So that when procedure run the thread then it will not lead to error due to no data present in the table .

Same thing can be achieved in two separate query with example below.

Begin tran t1

Select * from contacts where Id=2

Delete from contacts where Id = 2

Commit tran t1

Below is the operation ,  done in single query instead of two query

Begin tran t1

Delete from 

Contacts

output

Deleted.id,

Deleted.Name,

Deleted.Address,

Deleted.Phone

where id =2

Commit tran t1




No comments:

Post a Comment