Tuesday, July 7, 2009

TABLESAMPLE in SQL Server 2005

Hi,

Let's look at TABLESAMPLE clause of SQL Server 2005. This clause returns a random, representative sample of the table expressed as either an approximate number of rows or a percentage of the total rows. TABLESAMPLE returns a result set containing a sampling og rows from all rows processed by the query.

select * from cd_country TABLESAMPLE (10 ROWS)
or
select * from cd_country TABLESAMPLE (10 PERCENT)
The sample of rows retrieved is different every time. The tablesample clause can't be used with views or in an inline table-values function.

Using Repeatable clause

Adding a REPEATABLE clause returns the same sample result set each time as long as no changes are made to the data in the table. Repeatable indicates that the selected sample can be returned more than once. If the same seed is ised, the same rows will be returned each time the query is run as long as no changes have been made to the data in the table.
select * from cd_country TABLESAMPLE (10 ROWS)  Repeatable (5)
or
select * from cd_country TABLESAMPLE (10 PERCENT) Repeatable (5)
Enjoy SQL

No comments: