有没有方法可以把一个表里读出来的几条数据一下就插入到另外一个表里去?
比如两个表table_A和table_B的结构是一样的,从table_A里读出几条信息,直接插入到B表里面去?

解决方案 »

  1.   

    用select * into tB   from tA  不知道楼主是不是这样的意思
      

  2.   

    insert into table_B select * from Table_A where 1=1
      

  3.   

          .NET Framework 开发人员指南  
    使用 DataAdapter 执行批量更新  
    请参见  
     语言筛选器: 全部 语言筛选器: 多个 语言筛选器: Visual Basic 语言筛选器: C# 语言筛选器: C++ 语言筛选器: J# 语言筛选器: JScript  
     Visual Basic(声明) 
     Visual Basic(用法) 
     C# 
     C++ 
     J# 
     JScript 在以前版本的 ADO.NET 中,使用 DataSet 中的更改来更新数据库时,DataAdapter 的 Update 方法每次更新数据库的一行。因为该方法循环访问指定 DataTable 中的行,所以,会检查每个 DataRow,确定是否已修改。如果该行已修改,将根据该行的 RowState 属性值调用相应的 UpdateCommand、InsertCommand 或 DeleteCommand。每一次行更新都涉及网络与数据库之间的双向数据传输。 在 ADO.NET 2.0 中,DataAdapter 公开了 UpdateBatchSize 属性。将 UpdateBatchSize 设置为正整数值将使对数据库的更新以指定大小的批次进行发送。例如,如果将 UpdateBatchSize 设置为 10,会将 10 个独立的语句组合在一起并作为一批提交。将 UpdateBatchSize 设置为 0 将导致 DataAdapter 使用服务器可以处理的最大批次的大小。如果将其设置为 1,则禁用批量更新,因为此时每次发送一行。执行非常大的批次可能会降低性能。因此,在实现应用程序之前,应测试最佳的批次大小设置。使用 UpdateBatchSize 属性
    启用了批量更新后,DataAdapter 的 UpdateCommand、InsertCommand 和 DeleteCommand 的 UpdatedRowSource 属性值应设置为 None 或 OutputParameters。在执行批量更新时,命令的 FirstReturnedRecord 或 Both 的 UpdatedRowSource 属性值无效。下面的过程演示如何使用 UpdateBatchSize 属性。该过程采用两个参数,一个 DataSet 对象,其中包含代表 Production.ProductCategory 表中的 ProductCategoryID 和 Name 字段的列,一个代表批次大小的整数(批次中的行数)。该代码创建一个新的 SqlDataAdapter 对象,设置其 UpdateCommand、InsertCommand 和 DeleteCommand 属性。该代码假定 DataSet 对象已修改了行。它设置 UpdateBatchSize 属性并执行更新。Visual Basic  复制代码 
    Public Sub BatchUpdate( _
      ByVal dataTable As DataTable, ByVal batchSize As Int32)
        ' Assumes GetConnectionString() returns a valid connection string.
        Dim connectionString As String = GetConnectionString()    ' Connect to the AdventureWorks database.
        Using connection As New SqlConnection(connectionString)
            ' Create a SqlDataAdapter.
            Dim adapter As New SqlDataAdapter()        'Set the UPDATE command and parameters.
            adapter.UpdateCommand = New SqlCommand( _
              "UPDATE Production.ProductCategory SET " _
              & "Name=@Name WHERE ProductCategoryID=@ProdCatID;", _
              connection)
            adapter.UpdateCommand.Parameters.Add("@Name", _
              SqlDbType.NVarChar, 50, "Name")
            adapter.UpdateCommand.Parameters.Add("@ProdCatID",  _
              SqlDbType.Int, 4, " ProductCategoryID ")
            adapter.UpdateCommand.UpdatedRowSource = _
              UpdateRowSource.None        'Set the INSERT command and parameter.
            adapter.InsertCommand = New SqlCommand( _
              "INSERT INTO Production.ProductCategory (Name) VALUES (@Name);", _
      connection)
            adapter.InsertCommand.Parameters.Add("@Name", _
              SqlDbType.NVarChar, 50, "Name")
            adapter.InsertCommand.UpdatedRowSource = _
              UpdateRowSource.None        'Set the DELETE command and parameter.
            adapter.DeleteCommand = New SqlCommand( _
              "DELETE FROM Production.ProductCategory " _
              & "WHERE ProductCategoryID=@ProdCatID;", connection)
            adapter.DeleteCommand.Parameters.Add("@ProdCatID", _
               SqlDbType.Int, 4, " ProductCategoryID ")
            adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.None        ' Set the batch size.
            adapter.UpdateBatchSize = batchSize        ' Execute the update.
            adapter.Update(dataTable)
        End Using
    End Sub
     
    C#  复制代码 
    public static void BatchUpdate(DataTable dataTable,Int32 batchSize)
    {
        // Assumes GetConnectionString() returns a valid connection string.
        string connectionString = GetConnectionString();    // Connect to the AdventureWorks database.
        using (SqlConnection connection = new 
          SqlConnection(connectionString))
        {        // Create a SqlDataAdapter.
            SqlDataAdapter adapter = new SqlDataAdapter();        // Set the UPDATE command and parameters.
            adapter.UpdateCommand = new SqlCommand(
                "UPDATE Production.ProductCategory SET "
                + "Name=@Name WHERE ProductCategoryID=@ProdCatID;", 
                connection);
            adapter.UpdateCommand.Parameters.Add("@Name", 
               SqlDbType.NVarChar, 50, "Name");
            adapter.UpdateCommand.Parameters.Add("@ProdCatID", 
               SqlDbType.Int, 4, "ProductCategoryID");
             adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;        // Set the INSERT command and parameter.
            adapter.InsertCommand = new SqlCommand(
                "INSERT INTO Production.ProductCategory (Name) VALUES (@Name);", 
                connection);
            adapter.InsertCommand.Parameters.Add("@Name", 
              SqlDbType.NVarChar, 50, "Name");
            adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;        // Set the DELETE command and parameter.
            adapter.DeleteCommand = new SqlCommand(
                "DELETE FROM Production.ProductCategory "
                + "WHERE ProductCategoryID=@ProdCatID;", connection);
            adapter.DeleteCommand.Parameters.Add("@ProdCatID", 
              SqlDbType.Int, 4, "ProductCategoryID");
            adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.None;        // Set the batch size.
            adapter.UpdateBatchSize = batchSize;        // Execute the update.
            adapter.Update(dataTable);
        }
    }
     请参见
    概念
    处理与批量更新有关的事件和错误
    使用 DataAdapter 更新数据源其他资源
    使用 DataAdapter 执行批处理操作
     要提出有关“帮助”或本产品其他功能的建议或错误报告,请转到反馈站点。 
      

  4.   

    table_B不存在
    select *  into table_B FROM Table_A
    table_B已经存在
    INSERT into table_B SELECT * from Table_A 
      

  5.   

    SQL: 
    insert into B 
    select *
    from A
    where ....
      

  6.   

    直接用存储过程
    create procedure p_xxx
    (
       @xxx int
    )
    as
    begin
       insert into table2 select * from table1 where id=@xxx
    end
      

  7.   

    一条sql语句不就搞定了
    insert into Table-b select * from Table-A where ...