?
Solved

SQL Select Portin of a table

Posted on 2014-10-10
3
Medium Priority
?
118 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:Murray Brown
[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 Comments
 
LVL 51

Accepted Solution

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

Open in new window

0
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 1000 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:Murray Brown
ID: 40373074
Thanks for the help
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
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
Suggested Courses

762 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