将数据从一个表移动到另一个表的正确方法是什么?

时间:2015-01-20 02:44:08

标签: c# sql

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["InventoryConnectionString"].ConnectionString);
        conn.Open();
        string del = "Delete From Temp_Items";
        string mov = "INSERT into Items (Serial, Date, Location, Type, Condition, Color, Loaned_To, Product_NumberREF, Department) Select (Serial, Date, Location, Type, Condition, Color, Loaned_To, Product_NumberREF, Department) From TempItems";

        SqlCommand Delete = new SqlCommand(del, conn);
        SqlCommand move = new SqlCommand(mov, conn);
        move.ExecuteNonQuery();
        Delete.ExecuteNonQuery();
        conn.Close();

使用此代码,我尝试将表中的所有数据移动到另一个表,然后删除复制过的数据。如何在不中断的情况下正确移动数据,因为目前我的第4行代码存在问题。它说有一个错误。

错误是=','

附近的语法错误

1 个答案:

答案 0 :(得分:0)

将数据从一个表移动到另一个表的最佳方法是使用SELECT INTO,然后从第一个表中删除数据......

http://www.w3schools.com/sql/sql_select_into.asp

示例...

    SELECT *
    INTO newtable [IN externaldb]
    FROM table1;

DELETE * FROM table1;

编辑: 您当然也可以删除原始表,这意味着您已将所有数据从一个表完全移动到另一个表!

DROP table1;