将数据从一个表移动到另一个表

时间:2012-03-22 13:02:50

标签: c# sql-server dataadapter

Hie frnds,

我有两个表说“Orders”和“OrdersXML”

现在我希望删除超过7天的订单到OrdersXML表,我已经使用 sql adapter 更新函数对数据集进行了处理,这样做一次只需100行。 我想删除已移动到OrdersXML表的orders表中的行 我怎么能实现这个?我想确保Orders中的行只有在插入OrdersXML后才会被删除。我不想丢失任何数据..甚至不是偶然的..

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

作为m使用数据适配器我不能得到插入的ID,如果之间有任何异常..我可以吗?

3 个答案:

答案 0 :(得分: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.
        }
    }

答案 1 :(得分:2)

DELETE OUTPUT 子句一起使用,并在一个语句中执行:

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 (...)进行限制。

答案 2 :(得分:1)

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

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