【问题标题】:Moving Data from one table to another将数据从一个表移动到另一个表
【发布时间】:2012-04-07 01:07:46
【问题描述】:

朋友们,

我有两个表,分别是“Orders”和“OrdersXML”

现在我希望将超过 7 天的订单删除到 OrdersXML 表中,我使用 sql 适配器 更新数据集的函数完成了这项工作,一次需要 100 行。 我想从订单表中删除已移动到 OrdersXML 表的行 我怎样才能做到这一点?我想确保 Orders 中的一行仅在插入 OrdersXML 后才被删除。我不想丢失任何数据.. 甚至不是意外..

我应该使用触发器吗?还是我应该用 C# 自己编写代码?

作为 m 使用数据适配器,如果中间出现任何异常,我无法获取已插入的 id。我可以吗??

【问题讨论】:

    标签: c# sql-server dataadapter


    【解决方案1】:

    我个人建议编写一个存储过程,这样您就没有使用 C# 客户端的延迟。然后,您可以编写一个脚本来每天或其他任何时间调用此存储过程。

    查找“事务”,您可以这样做,如果查询的一部分失败(即插入),那么查询的其余部分会回滚到之前的良好状态。

    【讨论】:

      【解决方案2】:

      如果要使用脚本编写 SQL,请使用带有 SQL 事务的 SqlCommand:

      BEGIN TRANSACTION
      
      -- Copy rows from Orders to OrdersXML
      
      -- Delete rows from Orders that were copied
      
      COMMIT TRANSACTION
      

      如果您想对对象和代码执行此操作,请使用 SqlTransaction 对象:

      // code sample adapted from MSDN
      using (SqlConnection connection = new SqlConnection(connectionString))
      {
          connection.Open();
          SqlTransaction transaction = connection.BeginTransaction("SampleTransaction");
          SqlCommand command = connection.CreateCommand();
          command.Transaction = transaction;
      
          try
          {
              command.CommandText = "TODO"; // Copy rows from Orders to OrdersXML
              command.ExecuteNonQuery();
              command.CommandText = "TODO"; // Delete copied rows from Orders
              command.ExecuteNonQuery();
      
              // Attempt to commit the transaction.
              transaction.Commit();
          }
          catch (Exception ex)
          {
              try
              {
                  // Attempt to roll back the transaction.
                  transaction.Rollback();
              }
              catch (Exception ex2)
              {
                  // This catch block will handle any errors that may have occurred
                  // on the server that would cause the rollback to fail, such as
                  // a closed connection.
              }
          }
      

      【讨论】:

      • 那真是太好了..请解释一下“TODO”是什么意思,我需要把我的 t-sql 查询放在那里吗..
      • 当你可以在一个命令中完成时,为什么要作为复制+删除呢?请参阅我的答案。
      • @Sirwani - 是的,“TODO”是您的 SQL(我假设您已经拥有)来执行复制和删除。由于您没有发布 Orders 或 OrdersXML 的架构,因此我没有尝试将 SQL 用于复制和删除。
      • @KM 单个命令也不错,它可能是最快的方法。我假设 OP 已经有用于复制和删除的 SQL,所以很容易用事务包装它。如果除了基本的复制和删除之外还有其他操作(换句话说,需要更多单个语句的操作),那么您可能需要一个事务。
      • 被选为使用过的东西真的很有帮助
      【解决方案3】:

      使用带有 OUTPUT 子句的 DELETE 并在一个语句中执行:

      DECLARE @OldTable table(col1 int, col2    varchar(5), col3 char(5), col4     datetime)
      DECLARE @NewTable table(col1 int, column2 varchar(5), col3 int    , col_date char(23), extravalue int, othervalue varchar(5))
      INSERT @OldTable VALUES (1 , 'AAA' ,'A'  ,'1/1/2010'           )
      INSERT @OldTable VALUES (2 , 'BBB' ,'12' ,'2010-02-02 10:11:22')
      INSERT @OldTable VALUES (3 , 'CCC' ,null ,null                 )
      INSERT @OldTable VALUES (4 , 'B'   ,'bb' ,'2010-03-02'         )
      
      DELETE /*top (1000)*/ @OldTable
          OUTPUT DELETED.col1
                ,DELETED.col2
                ,CASE
                     WHEN ISNUMERIC(DELETED.col3)=1 THEN DELETED.col3 
                     ELSE NULL END
                ,DELETED.col4
                ,CONVERT(varchar(5),DELETED.col1)+'!!'
              INTO @NewTable (col1, column2, col3, col_date, othervalue)
          OUTPUT 'Rows Deleted: ', DELETED.* --this line returns a result set shown in the OUTPUT below
          WHERE col1 IN (2,4)
      
      SELECT * FROM @NewTable
      

      输出:

                     col1        col2  col3  col4
      -------------- ----------- ----- ----- -----------------------
      Rows Deleted:  2           BBB   12    2010-02-02 10:11:22.000
      Rows Deleted:  4           B     bb    2010-03-02 00:00:00.000
      
      (2 row(s) affected)
      
      col1        column2 col3        col_date                extravalue  othervalue
      ----------- ------- ----------- ----------------------- ----------- ----------
      2           BBB     12          Feb  2 2010 10:11AM     NULL        2!!
      4           B       NULL        Mar  2 2010 12:00AM     NULL        4!!
      
      (2 row(s) affected)
      

      您可以根据需要使用TOP (...) 对其进行限制。

      【讨论】:

      • 这是一个非常好的机制,可以在单个语句中复制和删除数据。
      猜你喜欢
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      相关资源
      最近更新 更多