Did you know that TABLESAMPLE can be used after FROM to select random data from a table?
For example you can use the following:
SELECT * FROM Sales.SalesOrderDetail TABLESAMPLE SYSTEM (2 PERCENT)
This command is quite unstable. When you run this query to return 2% of the records in the table, a different number of results will be returned each time.
You can also use this command in the following way:
SELECT * FROM Sales.SalesOrderDetail TABLESAMPLE SYSTEM (500 rows)
But this query will not return 500 records. It will return between 300 and 800 records.
Another use is as follows:
SELECT * FROM Sales.SalesOrderDetail TABLESAMPLE SYSTEM (500 rows) REPEATABLE (123456)
With REPEATABLE option, you can specify a “bigint” value. So that the query returns the same result every time. But the result set returned by the query will change when the records in the table are changed or the bigint value that you specify changes.
If an INSERT, UPDATE, DELETE, or Index Rebuilding operation is performed, a different set will return even if the value you give in REPEATABLE is the same.