Send feedback on this topic.
Teradata.Client.Provider
ExecuteScalar Method
Example 



Teradata.Client.Provider Namespace > TdCommand Class : ExecuteScalar Method
Executes the SQL statement (CommandText property) and returns the first column of the first row.
Syntax
'Declaration
 
Public Overrides NotOverridable Function ExecuteScalar() As Object
'Usage
 
Dim instance As TdCommand
Dim value As Object
 
value = instance.ExecuteScalar()
public override object ExecuteScalar()
public:
Object^ ExecuteScalar(); override 

Return Value

The first column of the first row.
Exceptions
ExceptionDescription
Can be thrown because of one of the following errors: 1) The TdParameter.Offset is outside of 0 through array size � 1 2) More in-out/out parameters have been specified than returned from the stored procedure.
One or more parameters cannot be converted to Teradata native types.
Can be thrown because of one of the following errors: 1) TdConnection is not in the Open state or is null or 2) The local transaction associated with TdCommand is not the same local transaction associated with the TdConnection.
The TdCommand is closed or disposed.
Can be thrown because of one of the following errors: 1) Teradata returned an error or 2) The Data Provider detected an error.
Remarks

Executes the SQL statement and returns the first column of the first row.

When CommandType property is set to System.Data.CommandType.StoredProcedure, any output parameters are populated with data and a null object reference is returned.

Example
The following example creates a TdConnection object and executes a SQL statement.
public Object ExecuteQuery(string connectionString, string commandText)
{
    Object scalar;
    
    using(TdConnection cn = new TdConnection(connectionString))
    {
        cn.Open();
            
        TdCommand cmd = new TdCommand(commandText, cn);
        scalar = cmd.ExecuteScalar();
    }
            
    return scalar;
}
Requirements

Target Platforms: Windows 8.1, Windows 10, Windows Server 2012 R2, Windows Server 2016, Windows Server 2019

See Also

Reference

TdCommand Class
TdCommand Members