1

I'm trying to grab some data from my SQL database as below;

USE exampleDatabase
SELECT TOP(1) [Name] FROM [Peeps] ORDER BY [Weight] DESC
SELECT TOP(1) [Name] FROM [Peeps] ORDER BY [Age] DESC

The problem is when I read the data I get an error 'Name'.

Dim byWeight As String = sqlReader.GetValue(sqlReader.GetOrdinal("Name"))
Dim byAge As String = sqlReader.GetValue(sqlReader.GetOrdinal("Name"))

How would I read this data as above considering I can't use name twice?

4 Answers 4

1

I think you are missing a semi-colon after the first SELECT statement. Here's a sample app I put together (note the semicolon in my sql statement):

var sql = "Select TOP 1 name from sys.columns;"
          + "Select TOP 1 name from sys.objects";

var firstname = string.Empty;
var secondname = string.Empty;

var connString = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
using ( var conn = new SqlConnection( connString ) )
{
    conn.Open();
    using ( var cmd = new SqlCommand( sql, conn ) )
    {
        var reader = cmd.ExecuteReader();
        if ( reader == null )
            return;
        if ( reader.Read() )
            firstname = reader.GetString( reader.GetOrdinal( "Name" ) );

        reader.NextResult();
        if ( reader.Read() )
            secondname = reader.GetString( reader.GetOrdinal( "Name" ) );

    }
}

Response.Write( firstname + "<br />" );
Response.Write( secondname + "<br />" );

You can achieve the same goal as the semi-colon by using the "GO keyword like so:

    var sql = "Select TOP (1) name from sys.columns GO "
              + "Select TOP (1) name from sys.objects";
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. The semi-colons made no difference. I tested and the Select statement works fine in SQL Management Studio.
Yes, but you aren't using these select statements in SMS. You are trying to use this through ADO.NET and ADO.NET gets confused if you do not provide a means to terminate the select statements. In my example, if I remove the semi-colon, it throws a runtime error.
0

You can use the 'as' keyword to rename columns in your results, like so:

SELECT TOP(1) [Name] AS ByWeight FROM [Peeps] ORDER BY [Weight] DESC

2 Comments

Thanks - I've tried this and it make no difference. Visual Studio throws an exception with the message 'ByWeight'.
The message is ByWeight? Really?
0

hmmm... well you could make it one select statement....

USE exampleDatabase

SELECT W.[Name] AS W_Name,  A.[Name] AS A_Name FROM
(SELECT TOP(1) [Name] FROM [Peeps] ORDER BY [Weight] DESC) W 
JOIN (SELECT TOP(1) [Name] FROM [Peeps] ORDER BY [Age] DESC) A

Comments

0

What if you UNION your SQL together into one result set?

USE exampleDatabase
SELECT TOP(1) [Name] FROM [Peeps] ORDER BY [Weight] DESC
UNION ALL
SELECT TOP(1) [Name] FROM [Peeps] ORDER BY [Age] DESC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.