2012-03-22 129 views
0

枝frnds我,从一个表将数据移动到另一个

我有两个表说,“订单”和“OrdersXML”

现在我想删除的订单,其超过7天前到OrdersXML表,我已经完成了使用sql适配器更新数据集的功能,该功能每次执行100行。 我想从订单表中删除已经移动到OrdersXML表的行 我如何实现这个?我想确保订单中的行只有在插入OrdersXML后才被删除。我不想丢失任何数据..甚至不会意外......

我应该使用触发器吗?或者我应该用C#编码它本身?

作为m使用数据适配器我无法获得已插入的ID如果有任何异常之间进来..我可以??

回答

2

如果你想编写SQL使用脚本,使用SqlCommand用SQL事务:

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. 
     } 
    } 
+0

这真的很不错..请解释一下什么是“TODO”的意思,我是否需要把我的t-sql查询放在那里.. – 1Mayur 2012-03-22 13:27:16

+0

为什么做它作为一个副本+删除时,你可以在一个单一的命令?看到我的答案如何。 – 2012-03-22 13:30:20

+0

@Sirwani - 是的,“TODO”是你的SQL(我假设你已经拥有)来执行复制和删除。由于您没有发布Orders或OrdersXML的架构,因此我没有尝试将SQL用于复制和删除。 – Ryan 2012-03-22 16:20:21

1

我个人建议编写一个存储过程,以便不会有使用C#客户端的延迟。然后你可以编写一个脚本来每天或任何地方调用这个存储过程。

查找“事务”,你可以这样做,如果查询的一部分失败(即插入),则查询的其余部分回滚到先前的正常状态。

2

使用DELETEOUTPUT条款,这样做在一个声明:

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 (...)必要对其加以限制。

+0

这是一个非常好的机制,可以在单个语句中复制和删除数据。 – Ryan 2012-03-22 16:43:46

相关问题