?
Solved

SQL Server select query with TABLESAMPLE clause

Posted on 2010-11-21
5
Medium Priority
?
714 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 143

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 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

777 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