Solved

SQL Select Portin of a table

Posted on 2014-10-10
3
101 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 47

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

831 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