Solved

SQL Server select query with TABLESAMPLE clause

Posted on 2010-11-21
5
697 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

809 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