3 Replies - 3125 Views - Last Post: 19 October 2012 - 06:29 AM Rate Topic: -----

#1 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Retrieving just the first row from a database

Posted 15 October 2012 - 05:04 PM

Is there any way to retrieve just the first row out of a database without knowing what is in the row? For example say the columns are Name, Age, Gender. And under name are the names: Ann, Bob, Carl.

I want to get figure out how to get Ann without knowing that Ann is the first name.

I know i can get them all using "SELECT Name FROM tableName" and that would give me all of the names including Bob and Carl. Or "SELECT * FROM tableName WHERE Name = Ann" But all i want is to pull out the first name in it.

Anyone know how to do that?

[I put this here cause i am technically doing this using asp.net]

This post has been edited by robgw3: 15 October 2012 - 05:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Retrieving just the first row from a database

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Retrieving just the first row from a database

Posted 15 October 2012 - 05:39 PM

Yes, there is a way. You'd first want to be able to sort the result based on the Name column. That you can do using the ORDER BY clause. Then, to get only the first row, you'd want to check out the TOP clause.

For example:
SELECT TOP 1 [Name]
FROM [table_name]
ORDER BY [Name];


Was This Post Helpful? 3
  • +
  • -

#3 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: Retrieving just the first row from a database

Posted 15 October 2012 - 05:59 PM

Thanks, i'll give that a try.
Was This Post Helpful? 0
  • +
  • -

#4 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,675
  • Joined: 24-August 11

Re: Retrieving just the first row from a database

Posted 19 October 2012 - 06:29 AM

Are you using an ASP.NET SqlDataSource, or executing this from code behind? If you're using code behind, check out SqlCommand.ExecuteScalar.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1