Automate Teradata Integration Tasks from PowerShell
The CData ADO.NET Provider for Teradata is a standard ADO.NET Provider that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Teradata.
ADO.NET Provider
The ADO.NET Provider provides a SQL interface for Teradata; this tutorial shows how to use the Provider to create, retrieve, update, and delete Teradata data.
Once you have acquired the necessary connection properties, accessing Teradata data in PowerShell can be enabled in three steps.
To connect to Teradata, provide authentication information and specify the database server name.
- User: Set this to the username of a Teradata user.
- Password: Set this to the password of the Teradata user.
- DataSource: Specify the Teradata server name, DBC Name, or TDPID.
- Port: Specify the port the server is running on.
- Database: Specify the database name. If not specified, the default database is used.
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Teradata\lib\System.Data.CData.Teradata.dll") -
Connect to Teradata:
$conn= New-Object System.Data.CData.Teradata.TeradataConnection("User=myuser;Password=mypassword;Server=localhost;Database=mydatabase;") $conn.Open() -
Instantiate the TeradataDataAdapter, execute an SQL query, and output the results:
$sql="SELECT ProductId, ProductName from NorthwindProducts" $da= New-Object System.Data.CData.Teradata.TeradataDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.productid $_.productname }
Update Teradata Data
$cmd = New-Object System.Data.CData.Teradata.TeradataCommand("UPDATE NorthwindProducts SET CategoryId='5' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Teradata.TeradataParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert Teradata Data
$cmd = New-Object System.Data.CData.Teradata.TeradataCommand("INSERT INTO NorthwindProducts (CategoryId) VALUES (@myCategoryId)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Teradata.TeradataParameter("@myCategoryId","5")))
$cmd.ExecuteNonQuery()
Delete Teradata Data
$cmd = New-Object System.Data.CData.Teradata.TeradataCommand("DELETE FROM NorthwindProducts WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Teradata.TeradataParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject