Send feedback on this topic.
Teradata.Client.Provider
TdDecimal Structure
Members  Example 



Teradata.Client.Provider Namespace : TdDecimal Structure
Teradata can support decimals that have a precision/scale up to 38. The .NET Framework can only support a decimal that has a scale of up to 28. TdDecimal enables an application to receive and send Large Decimals from and to Teradata. It also contains arithmetic, comparison, and conversion methods that can be used to operate on Large Decimals. A numeric value between - 10^38 +1 and 10^38 - 1 is supported.
Object Model
TdDecimal StructureTdDecimal StructureTdDecimal StructureTdDecimal StructureTdDecimal StructureTdDecimal StructureTdDecimal StructureTdDecimal StructureTdDecimal StructureTdDecimal StructureTdDecimal StructureTdDecimal StructureTdDecimal Structure
Syntax
Remarks

An application can use the TdDecimal structure the same way that a System.Decimal is used. TdDecimal can be used with decimals of any precision/scale and can be used with any version of Teradata supported by the .NET Data Provider for Teradata. However, the maximum precision of the Decimal that can either be retrieved or sent to Teradata is dependent upon the version of Teradata that is being accessed.

A TdDecimal value can also be specified as an In, Out, or InOut parameter to a Stored Procedure. In order to maintain backward compatibility with previous versions of the provider --versions earlier than 2.0--, the Connection String Attribute Enable TdDecimal must be set to false, and the precision of the Decimal returned from Teradata must have a precision that is 18 or less. When these two conditions are satisfied, a TdDecimal is only returned to an application using the TdParameter.ProviderSpecificValue property. The object that is returned by accessing the TdParameter.Value property remains a System.Decimal.

If the attribute Enable TdDecimal is set to true or the precision of the Decimal returned from the Advanced SQL Engine has a precision greater than 18, a TdDecimal object is returned from either TdParameter.Value or TdParameter.ProviderSpecificValue.

Example

EXAMPLE 1

The following coding example retrieves Large Decimals from Teradata, modifies the Large Decimal, and then sends the results back to Teradata. The simplified tables that are used in this example have the following definition:

EXAMPLE 2

The following example shows how a TdDecimal is used when calling a Stored Procedure:

CREATE TABLE DetailedSales (TransId INTEGER, RegionId INTEGER, SalesAmt DECIMIAL(38,2))
                
CRETE TABLE RankSalesByRegion (RegionId INTEGER, TotalSales DECIMAL(38,2), RegionRank INTEGER)
public void ManipulateLargeDecimal(TdConnection cn)
{
    // creating commands that will be used to execute SQL statements
    TdCommand cmd1 = cn.CreateCommand();
    TdCommand cmd2 = cn.CreateCommand();
 
    // query to rank the sales by region
    cmd1.CommandText = "select RegionId, TotalSales, Rank(TotalSales) " +
       "from (select RegionId, sum(SalesAmt) as TotalSales " +
               "from DetailedSales group by RegionId) as SalesByRegion";
   
    TdDataReader reader = cmd1.ExecuteReader();
 
    // setting up command that will insert rows returned from query into
    // the RankSalesByRegion table
    cmd2.CommandText = "insert into RankSalesByRegion (RegionId, " +
       "TotalSales, RegionRank) values (?, ?, ?)";
       
    cmd2.Parameters.Add(null, TdType.BigInt, 0, 
       System.Data.ParameterDirection.Input, 
       true, 0, 0, null, System.Data.DataRowVersion.Default, null);
       
    cmd2.Parameters.Add(null, TdType.Decimal, 0, 
       System.Data.ParameterDirection.Input, 
       true, 38, 2, null, System.Data.DataRowVersion.Default, null);
       
    cmd2.Parameters.Add(null, TdType.Integer, 0, 
       System.Data.ParameterDirection.Input,
       true, 0, 0, null, System.Data.DataRowVersion.Default, null);
 
    // TdDecimal sumOfSales = new TdDecimal(0);
    // could also have been used
    TdDecimal sumOfSales = 0;
    TdDecimal avgSalesByRegion = 0;
 
    // Loop through the result set:
    //   Read data returned, and set the Value property of the corresponding
    //   TdParameter with the data returned for each column.
    while (true == reader.Read())
    {
        // Reading a BIGINT and
        // Setting the RegionId parameter
        cmd2.Parameters[0].Value = reader.GetInt64(0);
 
        // Reading a Large Decimal and
        // Setting the TotalSales parameter
        cmd2.Parameters[1].Value = reader.GetTdDecimal(1);
 
        // Seting the RegionRank parameter
        cmd2.Parameters[2].Value = reader.GetInt32(2);
 
        // Summing up on the total sales
        sumOfSales += (TdDecimal)(cmd2.Parameters[1].Value);
 
        cmd2.ExecuteNonQuery();
    }
 
    // Calculating the avg sales by region
    // The reader.RecordsReturned is explicitly converted to TdDecimal
    avgSalesByRegion = sumOfSales / (TdDecimal)(reader.RecordsReturned);
 
    reader.Close();
 
    cmd1.Dispose();
    cmd2.Dispose();
}
public void CalculateSalesForRegion(TdConnection cn, Int32 RegionId)
{
    // Creating commands that will be used to execute SQL statements
    TdCommand cmd1 = cn.CreateCommand();
 
    // The CalculateSalesForRegion stored procedure has the 
    // following parameters:
    //     in RegionId Integer
    //     out Rank    Integer
    //     out SalesForRegion Decimal(38, 2)
    //     out PercentOfTotalSales Decimal(7, 4)
    cmd1.CommandText = "CaculateSalesForRegion";
    cmd1.CommandType = System.Data.CommandType.StoredProcedure;
   
    // Setting up parameters to return sales information for the 
    // specified region
    // 
    // Parameter for the RegionId
    Cmd1.Parameters.Add(null, TdType.Integer, 0, 
       System.Data.ParameterDirection.Input, 
       true, 0, 0, null, System.Data.DataRowVersion.Default, RegionId);
 
    // Parameter for the Rank
    cmd1.Parameters.Add(null, TdType.Integer, 0, 
       System.Data.ParameterDirection.Output, 
       true, 0, 0, null, System.Data.DataRowVersion.Default, null);
 
    // Parameter for the SalesForRegion
    cmd1.Parameters.Add(null, TdType.Decimal, 0, 
       System.Data.ParameterDirection.Output,
       true, 38, 2, null, System.Data.DataRowVersion.Default, null);
 
    // Parameter for the PercentOfTotalSales
    cmd1.Parameters.Add(null, TdType.Decimal, 0, 
       System.Data.ParameterDirection.Output,
       true, 7, 4, null, System.Data.DataRowVersion.Default, null);
 
    // Execute the Stored Procedure
    cmd1.ExecuteNonQuery();
 
    // Retrieve the data for each of the out parameters
   
    // Getting the data returned for the Rank
    Int32 rank = cmd1.Parameters[1].Value;
 
    // Getting the data returned for the SalesForRegion
    //    In the instance this parameter was defined as a 
    //    DECIMAL(38,2).  If the TdParameter.Value property is
    //    accessed an OverFlowException will be thrown.
    TdDecimal salesForRegion = cmd1.Parameters[2].ProviderSpecificValue;
 
    // Getting the data returned for the PercentOfTotalSales
    //    Since the parameter is defined as a Decimal(7,4) an
    //    application can either retrieve the data using the
    //    TdParameter.ProviderSpecificValue or TdParameter.Value property.  
    //    ProviderSpecificValue will contain a TdDecimal structure. 
    //    Value will contains a System.Decimal structure.
    TdDecimal tdPercentOfTotalSales = cmdl.Parameters[3].TdValue;
    System.Decimal sysPercentOfTotalSales = cmd1.Parameters[3].Value;
 
    //
    // CONTINUE PROCESSING THE DATA RETURNED FROM THE STORED PROCEDURE
    //
 
    cmd1.Dispose();
}
Inheritance Hierarchy

System.Object
   System.ValueType
      Teradata.Client.Provider.TdDecimal

Requirements

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

See Also

Reference

TdDecimal Members
Teradata.Client.Provider Namespace