Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi ,

below are my scripts which i have created using subqueries

SQL
UPDATE Report
SET ReporttypeId = (SELECT ReportTypeId
FROM ReportType AS rt WITH (NOLOCK)
WHERE code = 'SYSTEM' AND Name= 'System')
WHERE ReportTypeId = (SELECT ReportTypeId
FROM ReportType AS rt WITH (NOLOCK)
WHERE code = 'ADMIN' AND Name= 'Admin')

GO

Delete from ReportType
Where ReportTypeId =
(SELECT ReportTypeId
FROM ReportType AS rt WITH (NOLOCK)
WHERE code = 'ADMIN' AND Name= 'Admin')



Is there any better way to achive the same using joins ??
Posted
Comments
jaket-cp 24-Nov-15 4:05am    
Have a read of DELETE (Transact-SQL):
https://msdn.microsoft.com/en-us/library/ms189835.aspx
Using joins and subqueries to data in one table to delete rows in another table

Should help you out on how to use joins with delete

That is a good query. I don't see any wrong with it, except you might want to consider refactoring the table hint NO LOCK from it. I am not a fan of NO LOCK as it can cause dirty reads and it can not only miss records but read one before and after it has been updated by a different process.
 
Share this answer
 
This looks like a 1 off maintenance problem so there should be no need for optimisation. You could get fancy and create a table var to hold the from/to values but it is hardly worth it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900