如何在批量插入中附加数据

时间:2013-11-26 07:50:46

标签: c# sql sql-server

我正在尝试读取保存在数据表中的数据块。使用查询下面我将此答案保存在数据表3中,然后使用buk insert TO Table_1。

adapter.SelectCommand = new SqlCommand("SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY DistrictName) AS Row, *FROM Dwh_staging_table) AS a WHERE row BETWEEN 0 AND 1000001", con1);

这是SQL语句:

SELECT * FROM 
    (SELECT ROW_NUMBER() OVER (ORDER BY DistrictName) AS Row, *
     FROM Dwh_staging_table
    ) AS a
WHERE row BETWEEN 0 AND 1000001

我如何在Table_1中追加1000001之后的行。

3 个答案:

答案 0 :(得分:0)

我们在select

中使用Limit查询SQLITE
  

SELECT * from [table-name] Limit LowerBound,UpperBound

所以你的查询就像是,

SELECT * FROM Dwh_staging_table ORDER BY DistrictName Limit 0, 100000

对于下一个区块,

SELECT * FROM Dwh_staging_table ORDER BY DistrictName Limit 100000, 200000

答案 1 :(得分:0)

这是关于如何在TSql中进行分页的好文章

http://joelabrahamsson.com/my-favorite-way-to-do-paging-with-t-sql/

答案 2 :(得分:0)

您可以使用SqlBulkCopy类。

以下代码将有效地将数据从SourceTableName @ SourceConnectionString复制到DestinationTableName @ DestinationConnectionString,一次100000行。显然用你的连接字符串和表名替换标记。

using (var sourceConnection = new SqlConnection("[SourceConnectionString]"))
using (var destinationConnection = new SqlConnection("[DestinationConnectionString]"))
{
    sourceConnection.Open();
    destinationConnection.Open();

    var sourceCommand = sourceConnection.CreateCommand();
    sourceCommand.CommandType = CommandType.Text;
    sourceCommand.CommandText = "SELECT * FROM [SourceTableName]";
    var sourceReader = sourceCommand.ExecuteReader();

    var bulkCopy = new SqlBulkCopy(destinationConnection)
    {
        DestinationTableName = "[DestinationTableName]",
        BatchSize = 100000
    };

    bulkCopy.WriteToServer(sourceReader);
}