[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 727
  • Last Modified:

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.
0
maqskywalker
Asked:
maqskywalker
  • 3
1 Solution
 
amarsaleCommented:
does you query return 4 or more rows without using tablesample??
0
 
amarsaleCommented:
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) ;
0
 
amarsaleCommented:
yes, i have tested tablesample on my database and as the no. of rows you specified is less no rows are shown
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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 ...
0
 
subhashpuniaCommented:
TABLESAMPLE always returns an approximate number of rows because of the way it samples data. The required percentage is the number of data pages SQL Server retreves in full to fulfill your TABLESAMPLE request. The number of rows returned by TABLESAMPLE will often be slightly less, or greater, than the specified amount.”

This is from below blog:

http://blog.sqlauthority.com/2007/09/15/sqlauthority-news-book-review-pro-t-sql-2005-programmers-guide-paperback/
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now