2014-11-05 83 views
0

我有一個項目,我需要查詢Teradata數據庫,然後將返回的記錄複製到SQL Server數據庫。我可以打Teradata db沒有問題,我可以將結果導入DataTable。 SQL服務器數據庫已經安裝並且具有與Teradata結果相同的列(自動編號列除外)。我無法弄清楚如何獲取DataTable中的記錄並將它們插入到SQL服務器數據庫中。將記錄從一個數據庫複製到另一個數據庫(從Teradata到SQL Server)

這裏是我有一些僞代碼,我沒有想到的細節是相關的:

 Using cn As New TdConnection("User Id=XYZ12345;Password=XYZ12345;Data Source=teradataserver.company.com;Persist Security Info=False") 
      cn.Open() 

      Dim cmd As TdCommand = cn.CreateCommand() 

      'build the SELECT part of the command we will issue 
      cmd.CommandText = GetTeradataSqlString() 

      'setup the DataAdapter 
      Dim da As New TdDataAdapter(cmd) 

      ' Provider specific types will be used in the data table 
      da.ReturnProviderSpecificTypes = False 'True=Use Teradata types, False=Use .NET types 

      ' Adapter will determine how many statements will be batched 
      da.UpdateBatchSize = 0 

      Dim cb As New TdCommandBuilder(da) 

      'create a DataTable to hold our returned data 
      Dim dtCheck As New DataTable("TableCheck") 
      ' Filling the data table with data retrieved from the select statement 
      da.Fill(dtCheck) 

      'create a DataSet to hold all of our tables 
      Dim dsMain As New DataSet("MainDataset") 

      'now we add the DataTable to our DataSet 
      dsMain.Tables.Add(dtCheck) 

      'at this point a cycle through the DataTable to the debug window shows we have the data we need from the Teradata db. 

      'now we will pump it into our SQL server database 
      Dim connSqlSvr As New System.Data.SqlClient.SqlConnection 
      connSqlSvr.ConnectionString = "Data Source=DestSqlServer;Initial Catalog=DestDb;Connect Timeout=15" 
      connSqlSvr.Open() 

      'now we create a SQL command to take the data in the Teradata DataTable and insert it into the SQL server table 
      Dim sqlCmd As New SqlCommand 
      With sqlCmd 
       .CommandType = CommandType.Text 

       Dim sbSqlCmd As New StringBuilder 
       sbSqlCmd.AppendLine("INSERT INTO [DestDb].[dbo].[Events] ([CityCode],[CarNum],[VIN],[Fleet],[EventItm])") 
       sbSqlCmd.AppendLine("SELECT City,CarNo,VIN,Fleet,EventDesc FROM @MyTable;") 
       .CommandText = sbSqlCmd.ToString 
       Dim sqlParam As New SqlParameter 
       sqlParam.ParameterName = "@MyTable" 
       sqlParam.SqlDbType = SqlDbType.Structured 
       sqlParam.Value = dtCheck 
       sqlParam.TypeName = "TableCheck" 
       .Parameters.Add(sqlParam) 

       .Connection = connSqlSvr 

       Dim rowsAffectedLoad As Integer = .ExecuteNonQuery() 
       debug.print(rowsAffectedLoad & " rows were loaded into the SQL server table.") 
      End With 

      'close and dispose the SQL server database connection 
      connSqlSvr.Close() 
      connSqlSvr.Dispose() 
     End Using 

運行代碼我得到一個異常:

 "Column, parameter, or variable @MyTable. : Cannot find data type TableCheck." 

我已經看了一種將數據表插入數據庫的方法,並注意到許多樣本正在使用INSERT INTO。我只是不認爲我正確使用SqlParameter。

回答

0

您的示例似乎使用TableCheck類型的Table Valued參數,但您尚未在SQL Server中定義該類型。見http://msdn.microsoft.com/en-us/library/bb510489.aspx

CREATE TYPE LocationTableType AS TABLE 
(LocationName VARCHAR(50) 
, CostRate INT); 

雖然我不能保證你可以直接傳遞一個TVP到原始的SQL語句。

我實際上建議你使用不同的方法,使用SqlBulkCopy,http://msdn.microsoft.com/en-us/library/ex21zs8x(v=vs.110).aspx

+0

好的,我會嘗試。現在我越來越關注這個問題,我認爲除了一件事情之外,這將起作用。我將如何檢測重複記錄並跳過將其添加到SQL服務器? – sinDizzy 2014-11-06 16:42:36

+0

我用SqlBulkCopy進行了這個練習,效果很好。謝謝。 – sinDizzy 2014-11-06 21:33:28

+0

如果您需要在源數據中進行重複數據刪除,或者與目標數據匹配,或兩者兼有,我的建議是插入到SQL Server上的臨時表中。然後,將您的重複數據刪除查詢寫入TSQL中,以便將臨時表批量插入到實際的目標表中。 – 2014-11-07 05:02:59