Solved

SQL Server select query with TABLESAMPLE clause

Posted on 2010-11-21
5
684 Views
Last Modified: 2012-05-10
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
Comment
Question by:maqskywalker
  • 3
5 Comments
 
LVL 1

Expert Comment

by:amarsale
ID: 34185386
does you query return 4 or more rows without using tablesample??
0
 
LVL 1

Expert Comment

by:amarsale
ID: 34185397
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
 
LVL 1

Expert Comment

by:amarsale
ID: 34185406
yes, i have tested tablesample on my database and as the no. of rows you specified is less no rows are shown
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34186222
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
 
LVL 6

Accepted Solution

by:
subhashpunia earned 500 total points
ID: 34187143
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now