SQL statement to determine if a record exists in the table

Source: Internet
Author: User

To determine if a record exists in the table, the statements we have used routinely are:

Select COUNT (*) from tableName where conditions

If you are simply judging the existence of a record without needing to get the number of records in the actual table, there is a recommended practice on the Web:

if exists (SELECT * from tableName where conditions) SELECT ' 1 ' else select ' 0 '

Determine if there is a return value.

It is recommended that the second method is more efficient, but using the profiler tool to analyze

When more than 100 data is filtered through the Where condition. The durtion of the first method is significantly lower than that of the second method.

In other words, the first is more efficient. May I ask the second kind of situation in which the effect will be obvious?

SQL statement to determine if a record exists in the table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.