Solved

SQL Select Portin of a table

Posted on 2014-10-10
3
88 Views
Last Modified: 2014-10-10
Hi
I am using the following SQL  code to select a hundred rows from a table called Orders. Is there a way to
do this without using the primary key [Order ID]. I want to select parts of other tables just knowing the table name

SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY [Order ID]) AS RowNum FROM Orders) AS MyDerivedTable WHERE MyDerivedTable.RowNum BETWEEN 1 AND 100
0
Comment
Question by:murbro
3 Comments
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 250 total points
ID: 40372702
You can replace 'Orders' with any table name:
SELECT TOP 100 * FROM Orders

Open in new window

0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 250 total points
ID: 40372749
IF using TOP (100) then you should use an ORDER BY statement.

SELECT TOP (100) * FROM Orders ORDER BY ID
if you don't use ORDER BY " it returns the first N number of rows in an undefined order."

If you want specifically want random rows you can order by newid()

SELECT TOP (100) * FROM Orders ORDER BY NEWID()


I'm not sure what you mean by this: " I want to select parts of other tables just knowing the table name"
0
 

Author Closing Comment

by:murbro
ID: 40373074
Thanks for the help
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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.

705 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

15 Experts available now in Live!

Get 1:1 Help Now