Link to home
Start Free TrialLog in
Avatar of maqskywalker
maqskywalker

asked on

SQL Server select query with TABLESAMPLE clause

Hi experts,

I'm trying out some simple select queries in SQL Server 2008 and I'm testing out the TABLESAMPLE clause.

http://msdn.microsoft.com/en-us/library/ms189108.aspx

I'm using the Adventureworks database.
I'm using the Person.Contact table in the Adventureworks database.

FIrst I tried out the following query (Selecting a percentage of rows) and this query worked just fine.

SELECT FirstName, LastName
FROM Person.Contact
TABLESAMPLE (10 PERCENT);

But then I tried the following query to test (Selecting a number of rows).
When I executed this query the result set I get is blank as shown in the picture below.

SELECT FirstName, LastName
FROM Person.Contact
TABLESAMPLE (4 ROWS);
 

Does anyone know why the result set is not showing the values for the first 4 rows?
Thanks for your help.
Avatar of amarsale
amarsale
Flag of India image

does you query return 4 or more rows without using tablesample??
maybe Following is the solution to ur problem given in your own link....

C. Selecting a number of rows
The following statement returns approximately 100 rows. The actual number of rows that are returned can vary significantly. If you specify a small number, such as 5, you might not receive results in the sample.

Copy USE AdventureWorks2008R2 ;
GO
SELECT FirstName, LastName
FROM Person.Person
TABLESAMPLE (100 ROWS) ;
yes, i have tested tablesample on my database and as the no. of rows you specified is less no rows are shown
Avatar of Guy Hengel [angelIII / a3]
from the doc:
http://msdn.microsoft.com/en-us/library/ms189108.aspx

C. Selecting a number of rows
The following statement returns approximately 100 rows

you will see a workaround to get exactly number of rows using the TOP x with the ORDER BY newid() function ...
ASKER CERTIFIED SOLUTION
Avatar of subhashpunia
subhashpunia
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial