Use tablesample to limit the result set [reprinted]

Source: Internet
Author: User

This article Reprinted from: http://msdn.microsoft.com/zh-cn/library/ms189108.aspx

 

Tablesample is a new syntax introduced from SQL 2005. It can sample table data.

 

A. Select the row percentage

Person. ContactThe table contains 19,972 rows. The following statement returns approximately10%. The number of rows returned for each execution of this statement is usually different.

 
Use adventureworks; goselect firstname, lastnamefrom person. Contact tablesample (10 percent );
B. Select the percentage of rows with a seed value

Each execution, the following statements return the same group of rows. Seed Value205Is optional.

 
Use adventureworks; goselect firstname, lastnamefrom person. Contact tablesample (10 percent) repeatable (205 );
C. select several rows

The following statement returns approximately100Line. The actual number of returned rows may vary greatly. If a small value is specified, for example, 5, no result can be received in the example.

 
Use adventureworks; goselect firstname, lastnamefrom person. Contact tablesample (100 rows );
 
 
Note that the tablesample clause is different from the top clause, and the top clause is sequential. Tablesample is a random sample.
 
The result set of tablesample may be uncertain. Either.
 
 

 

If you want to get a fixed number of random rows, you may need to consider other methods.

For example

Select top (10) * From northwind .. orders order by newid ()

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.