2014-11-05 84 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