Send feedback on this topic.
Teradata.Client.Provider
CommandText Property
Example 



Teradata.Client.Provider Namespace > TdCommand Class : CommandText Property
Gets or Sets the SQL request to execute.
Syntax
'Declaration
 
Public Overrides NotOverridable Property CommandText As String
'Usage
 
Dim instance As TdCommand
Dim value As String
 
instance.CommandText = value
 
value = instance.CommandText
public override string CommandText {get; set;}
public:
property String^ CommandText {
   String^ get() override;
   void set (    String^ value) override;
}

Property Value

The SQL request (Single or Multi-statement, DML or DDL) to execute. The default value is an empty string.
Exceptions
ExceptionDescription
The CommandText property was changed while the command was in execute state or there is an outstanding associated TdDataReader.
The TdCommand is disposed.
Remarks

The CommandText property represents the SQL request sent to the Teradata Vantage Advanced SQL Engine. The .NET Data Provider does not modify or parse the CommandText in most cases.

When the CommandType property is set to System.Data.CommandType.StoredProcedure, the CommandText property can either be set to the name of the stored procedure or the entire CALL statement. When the CommandType property is set to System.Data.CommandType.StoredProcedure ,the .NET Data Provider will verify if the CommandText is a valid CALL statement. The provider will not validate if the object names in the CALL statement follow Teradata DBS object naming conventions. The user defined CALL statement set in the CommandText property also should not include any comments anywhere within the statement. If the CommandText is not a valid CALL statement, the .NET Data Provider will assume the CommandText to be set with the stored procedure name and will generate the stored procedure CALL Statement. For example, if the CommandText is set to "SPNAME", .NET Data Provider will generate the CALL statement as CALL "SPNAME"(?, ?, outparm3). It surrounds the Stored Procedure name with Double Quotes (U0022) to allow for special characters like SPACE (U0020). The exception to this rule is when there are one or more DOTs (U002E) in the Stored Procedure name. DOT is ambiguous, that is it can separate a Database Name from a Stored Procedure Name (e.g. DatabaseName.SP) or it can be part of the Stored Procedure name. Double Quotes must not be added to the former while it can be added to the latter. The .NET Data Provider for Teradata does not add Double Quotes to the CommandText (Stored Procedure Name) when it contains one or more DOTs. Application must add Double Quotes when required by the Advanced SQL Engine Object Names Syntax and Lexicon.

The .NET Data Provider optimizes stored procedure handling by utilizing Output Parameter Names (TdParameter.ParameterName) when specified.

The SQL Engine and the .NET Data Provider for Teradata do not support Named Parameters. In addition, Teradata V2R6.0 and earlier releases requires that the names for all Output Parameters be specified in the CALL statement. The .NET Data Provider will utilize the Output Parameter Name if it has been specified in the TdParameter.ParameterName property. Otherwise, it will need to retrieve the parameters names for out arguments from the SQL Engine. Therefore, it is highly recommended to provide Output Parameter Names to minimize the round-trips to Teradata and back.

The .NET Data Provider does not support System.Data.CommandType.TableDirect CommandType.

Example
The example below shows how to set the CommandType to StoredProcedure and set the CommandText to either the Stored Procedure name or user defined CALL statement. If the CommandText is set to the Stored Procedure name, .NET Data Provider will generate the CALL statement. The example creates an instance of TdConnection class, and executes a LookupCustomerName stored procedure which accepts as input a customer ID and returns customer name as an output parameter.
public void Setup()
{
   TdConnection cn = new TdConnection();
   cn.ConnectionString = "Data Source=Teradata1;User ID=ab;Password=ab;";
            
   String empTable = "CREATE TABLE EmployeeTable(CustID char(6), CustName char(50))";
   String spText   = "REPLACE PROCEDURE LookupCustomerName " + 
                     "( in varCustID char(6), out varCustName char(50))" +
                     "BEGIN" +
                     "Select CustName into varCustName from EmployeeTable where CustID = varCustID;" +
                     "END;";
   try
   {
       cn.Open();
       TdCommand cmd = cn.CreateCommand();
            
       cmd.CommandText = empTable;
       cmd.ExecuteNonQuery();
            
       cmd.CommandText = spText;
       cmd.ExecuteNonQuery();
   }
   finally
   {
       // Close the session to Teradata
       cn.Close();
   }
}
public String LookupCustomerName(String customerId)
{
    String customerName = null;
    TdConnection cn = new TdConnection();
    cn.ConnectionString = "Data Source=Teradata1;User ID=ab;Password=ab;";
    
    Setup();
    
    try
    {
        cn.Open();
        
        // Initialize TdCommand
        TdCommand cmd = cn.CreateCommand();
        
        //CommandText is set to Stored Procedure name, in this case,
        //.NET Data Provider will generate the CALL statement.
        cmd.CommandText = "LookupCustomerName";
        
        //CommandText is set below to user specified CALL statement. To run the
        //example with the below CALL statement, uncomment the below line where 
        //CommandText is set to the CALL statement and comment out the previous 
        //CommandText set to the Stored Procedure name.
        //cmd.CommandText = "Call LookupCustomerName (?,?)";
        
        cmd.CommandType = CommandType.StoredProcedure;
        
        // Create Input Parameter
        TdParameter InParm = cmd.CreateParameter();
        InParm.Direction = ParameterDirection.Input;
        InParm.DbType = DbType.String;
        InParm.Size = 6;
        InParm.Value = customerId;
        cmd.Parameters.Add(InParm);
        
        // Create Output Parameter.
        TdParameter OutParm = cmd.CreateParameter();
        OutParm.Direction = ParameterDirection.Output;
        
        // Specify the output parameter name.
        
        // If parameter name is not specified, then 
        // the .NET Data Provider will retrieve the
        // name of out arguments from the Teradata RDMS
        OutParm.ParameterName = "CustomerName";
        OutParm.DbType = DbType.String; 
        OutParm.Size = 50; 
        cmd.Parameters.Add(OutParm); 
        
        cmd.ExecuteNonQuery();
        
        customerName = OutParm.Value;
    } 
    finally
    {
        // Close the session to Teradata
        cn.Close();
    }
    
    return customerName;
}
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