Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9230891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:55:50+00:00 2026-06-18T05:55:50+00:00

Using SqlServer2012, Is there any advantage in calling if exists (select * from tablename

  • 0

Using SqlServer2012, Is there any advantage in calling

if exists (select * from tablename where condition) begin
   delete tablename where condition
end

over just calling

   delete tablename where condition

When there often won’t be any rows matching condition?

In particular, will it result in any fewer (or less restrictive) locks being held?

I am in an appropriate transaction, so I avoid race conditions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:55:51+00:00Added an answer on June 18, 2026 at 5:55 am

    SQL Server can’t collapse the two statements and only perform the locks once. But that’s easy to prove, in any case. Let’s make a copy of an AdventureWorks table, perform your two types of operations, and compare the locks taken. You will see that your approach where you check first performs about twice as many locks – you can check this by splitting the two portions of the output into separate files and doing any kind of diff against them (or just compare the size – in my case 1.7 MB vs. 788 KB).

    SELECT * INTO dbo.kablatz
      FROM AdventureWorks2012.Sales.SalesOrderHeader;
    
    SET NOCOUNT ON;
    
    DBCC FREEPROCCACHE WITH NO_INFOMSGS;
    DBCC DROPCLEANBUFFERS WITH NO_INFOMSGS;
    
    PRINT '---------------- Check first';
    
    DBCC TRACEON(1200,3604,-1) WITH NO_INFOMSGS;
    
    IF EXISTS (SELECT 1 FROM dbo.kablatz WHERE OrderDate < '20100101')
      DELETE dbo.kablatz WHERE OrderDate < '20100101';
    
    DBCC TRACEOFF(1200,3604,-1) WITH NO_INFOMSGS;
    
    DBCC FREEPROCCACHE WITH NO_INFOMSGS;
    DBCC DROPCLEANBUFFERS WITH NO_INFOMSGS;
    
    PRINT '---------------- Don''t check first';
    
    DBCC TRACEON(1200,3604,-1) WITH NO_INFOMSGS;
    
    DELETE dbo.kablatz WHERE OrderDate < '20100101';
    
    DBCC TRACEOFF(1200,3604,-1) WITH NO_INFOMSGS;
    
    GO
    DROP TABLE dbo.kablatz;
    

    Logic will tell us that the checking for rows to delete already happens in the delete operation. Why repeat it?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using JQuery,Is there any possible to capture images/scanned documents from digital Camera/Scanner Connected to
Using Microsoft SQL Server 2005, is there any way to see when a table
Using the Haxe programming language, is there any cross-platform way to read a PNG
using ::bb::cascades::Application; #include <bb/cascades/Application> What do these two declaration mean? And are there any
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain
Using SQL Server 2008 R2 we are looking for a way to select the
I'm using NHibernate with Burrow and Fluent to shuttle data from SQLServer2005 into a
I created a Test project that I want to delete, using the following command:
I have following query select * from table1 if table1 contains text type column/columns
Using Visual SourceSafe's Open SourceSafe Database... command, there is an option at the bottom

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.