Send feedback on this topic.
Teradata.Client.Provider
Configuring Parameters and Parameter Data Types
.NET Data Provider for Teradata > Developer's Guide > Data Types > Configuring Parameters and Parameter Data Types

.NET Applications must utilize TdParameter class to pass values to the Advanced SQL Engine or receive parameter values from the SQL Engine. .NET Applications must configure each TdParameter object and associate the TdParameter object with a TdCommand object.

Working with Parameter Markers

The SQL Text must have a Parameter Marker (Question-Mark ?) for each TdParameter object when TdCommand.CommandType property is set to Text. The .NET Data Provider for Teradata does not support Named-Parameters. The parameters are positional and there is a one to one relationship between the Parameter-Marker in the SQL-Text and the TdParameter object in TdCommand.Parameters collection.

For example the SQL-Text shown below has three parameters and the TdCommand Object has three Parameters corresponding to the three Parameter-Markers.

C#
Copy Code
TdCommand cmd = new TdCommand();

cmd.CommandText = "Select OrderId, OrderDate, ShipDate, ShipAddress FROM Order " +
                    "Where Price >= ? and Quantity >= ? and OrderDate < ? ";

cmd.Parameters.Add("Price", TdType.Decimal);
cmd.Parameters.Add("Quantity", TdType.Integer);
cmd.Parameters.Add("OrderDate", TdType.Timestamp);

Supplying the Parameter Direction Property

.NET applications must set the TdParameter.Direction property when retrieving Output parameter values or when sending and retrieving InputOutput parameters to and from the SQL Engine. The Direction property cannot be set to ReturnValue because the SQL Engine does not support Return Value from Stored Procedures. The Output and InputOutput parameter directions are only applicable to Stored Procedures.

Specifying Parameter Data Type

.NET Applications set TdParameter.TdType or TdParameter.DbType property in order to declare the SQL data type of the parameter send to or received from the SQL Engine.

The Data Provider infers the SQL Engine data type from the TdParameter.Value when application does not explicitly set the parameter data type. It is highly recommend to always set the TdType property because DbType enumeration values are a subset of the SQL data types. Also in some cases one BCL type can map to two or more SQL data types. For example DbType enumeration does not have a value corresponding to the "Interval Day To Second" and System.TimeSpan type maps to the SQL Time and "Interval Day To Second" data types. In this case application must set DbType to Object (vs. Time) to resolve the ambiguity associated with System.TimeSpan or alternatively set TdType to IntervalDayToSecond. The Data Provider maps System.TimeSpan to the SQL Time data type when TdType and DbType properties are not explicitly set.

The Data Provider infers the TdType of a parameter when an application sets the DbType property. The table below shows the mappings.

DbType TdType Comment / Description
AnsiString VarChar or Clob TdParameter.Size property reflects the maximum number of bytes. If TdParameter.Size is less than or equal to the maximum length for VarChar data type (see ColumnSize in the DataTypes Common Schema Collection), the Data Provider infers TdType of VarChar. Otherwise it infers TdType of Clob.
AnsiStringFixedLength Char  
Binary VarByte  
Byte    
Boolean   Inferring a TdType from Boolean is not supported.
Currency   Inferring a TdType from Currency is not supported.
Date Date  
DateTime Timestamp  
DateTime2   Inferring a TdType from DateTime2 is not supported.
DateTimeOffset TimestampWithZone  
Decimal Decimal  
Double Double  
Guid   Inferring a TdType from Guid is not supported.
Int16 SmallInt  
Int32 Integer  
Int64 BigInt  
Object   IntervalDayToSecond when the Parameter Value is of type System.TimeSpan.
SByte ByteInt  
Single Double  
String VarChar  
StringFixedLength Char  
Time Time  
UInt16   Inferring a TdType from UInt16 is not supported.
UInt32   Inferring a TdType from UInt32 is not supported.
UInt64   Inferring a TdType from UInt64 is not supported.
VarNumeric   Inferring a TdType from VarNumeric is not supported.

The Data Provider infers the TdType of a parameter from the type of the TdParameter.Value property when an application does not explicitly set the TdParameter.TdType and TdParameter.DbType properties. The following table shows the mappings.

Base Class Library Type TdType Comment / Description
Int16 SmallInt  
Int32 Integer  
Int64 BigInt  
Decimal Decimal  
Double Double  
Char[] VarChar or Clob  
Byte[] VarByte or Blob  
String VarChar or Clob  
DateTime Timestamp  
TimeSpan Time System.TimeSpan is mapped to the SQL "Interval Day to Second" when DbType is set to Object.
SByte ByteInt  
Single Double  
DBNull VarChar  
System.IO.Stream Blob  
System.IO.TextReader Clob  

Specifying Parameter Size, Precision and Scale

The SQL Data Types have additional attributes, for example VarChar data type has "maximum number of characters" and Decimal data type has Precision and Scale attributes. The TdParameter properties should reflect the SQL data type attributes as outlined in the following table:

TdType TdParameter.Size TdParameter.Precision TdParameter.Scale
Character, CLOB and JSON Data Types Char Maximum number of characters.    
Clob Maximum number of characters.    
JSON Maximum number of characters.    
Graphic Maximum number of characters.    
VarChar Maximum number of characters.    
VarGraphic Maximum number of characters.    
Byte and BLOB Data Types Byte Maximum number of bytes.    
Blob Maximum number of bytes.    
VarByte Maximum number of bytes.    
Numeric Data Types BigInt
ByteInt
Decimal   Total number of digits.
The range is from 1 to 38
Number of digits to the right of decimal point.
The range is from 0 to 38 but it must be equal to or less than the Precision.
Double
Integer      
SmallInt      
Data and Time Data Types Date      
Time     Number of fractional seconds.
The range is from 0 to 6.
Timestamp     Number of fractional seconds.
The range is from 0 to 6.
TimestampWithZone     Number of fractional seconds.
The range is from 0 to 6.
TimeWithZone     Number of fractional seconds.
The range is from 0 to 6.
Interval Data Types IntervalDay   The permitted range of digits for Day.
The range is from 0 to 4.
 
IntervalDayToHour   The permitted range of digits for Day.
The range is from 0 to 4.
 
IntervalDayToMinute   The permitted range of digits for Day.
The range is from 0 to 4.
 
IntervalDayToSecond   The permitted range of digits for Day.
The range is from 0 to 4.
Number of fractional seconds.
The range is from 0 to 6.
IntervalHour   The permitted range of digits for Hour.
The range is from 0 to 4.
 
IntervalHourToMinute   The permitted range of digits for Hour.
The range is from 0 to 4.
 
IntervalHourToSecond   The permitted range of digits for Hour.
The range is from 0 to 4.
Number of fractional seconds.
The range is from 0 to 6.
IntervalMinute   The permitted range of digits for Minute.
The range is from 0 to 4.
 
IntervalMinuteToSecond   The permitted range of digits for Minute.
The range is from 0 to 4.
Number of fractional seconds.
The range is from 0 to 6.
IntervalSecond   The permitted range of digits for Second.
The range is from 0 to 4.
Number of fractional seconds.
The range is from 0 to 6.
IntervalYear   The permitted range of digits for Year.
The range is from 0 to 4.
 
IntervalYearToMonth   The permitted range of digits for Year.
The range is from 0 to 4.
 
IntervalMonth   The permitted range of digits for Month.
The range is from 0 to 4.
 
Period Data Types PeriodDate      
PeriodTime     Number of fractional seconds.
The range is from 0 to 6.
PeriodTimeWithTimeZone     Number of fractional seconds.
The range is from 0 to 6.
PeriodTimestamp     Number of fractional seconds.
The range is from 0 to 6.
PeriodTimestampWithTimezone     Number of fractional seconds.
The range is from 0 to 6.
Note

TdType Enumeration does not have a member corresponding to User Defined Types (UDT), User Defined Array Types or Geospatial Types. The SQL Engine transforms the aforementioned types to and from predefined SQL types.

Configuring TD_ANYTYPE Parameters Using Parameter SecondaryTdType

Configuring TD_ANYTYPE parameters is different than other TdType parameters, where the data type is known by the database (as defined by the stored procedure). TD_ANYTYPE is a placeholder that must be defined in the external stored procedure. By defining TdParameter.TdType to TdType.AnyType, the CALL statement by the .NET Data Provider generates an additional clause transmitted to the database to define the OUT parameter (when setting Command.CommandType to CommandType.StoredProcedure). The actual database data type is defined by TdParameter.SecondaryTdType and is included within the additional clause to the database. A short example follows to illustrate setting the SecondaryTdType.

C#
Copy Code
public static void ExampleTdAnyTypeParameters(TdCommand cmd)
{
   // REPLACE PROCEDURE xspAnyTypeProcedure(IN a INTEGER, OUT result TD_ANYTYPE)
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.CommandText = "xspAnyTypeProcedure";

   cmd.Parameters.Add(null, TdType.Integer);
   cmd.Parameters[0].Direction = Parameter.Direction.Input;
   cmd.Parameters[0].Value = 4;

   cmd.Parameters.Add(null, TdType.AnyType);
   cmd.Parameters[1].Direction = ParameterDirection.Output;
   cmd.Parameters[1].SecondaryTdType = TdType.VarChar;
   cmd.Parameters[1].Size = 20;

   cmd.ExecuteNonQuery();

   // Going to process data of output parameter
   String result = (String)cmd.Parameters[1].Value;

}