17

我有两个具有相同列定义的表。我需要将一行从一个表移动(而不是复制)到另一个表。在我离开并使用 INSERT INTO/DELETE(在事务中)之前,有没有更聪明的方法?

SQL 服务器 2005

4

5 回答 5

32

对于 SQL Server 2005 及更高版本,请尝试使用OUTPUT Clause (Transact-SQL)子句:

DELETE OldTable
  OUTPUT DELETED.col1, DELETED.col2...
      INTO NewTable
  WHERE ID=...

工作示例:

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 @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)
于 2010-05-13T19:14:35.920 回答
3

您可以尝试 Insert into abc (a,b,c) select(a,b,c) from def

这样做会将 def 的 a、b、c 列插入 abc 的 a、b、c 列。插入后运行删除表、删除表或截断您的标准。

样本是:

Begin
    Begin try

         Begin Transaction

               Insert into emp(name, department, salary)                    
                       Select empName,empDepartment,empSal from employees
                       Where  employees.empID = 211

               Truncate table employees

          End Transaction  

    End try

    Begin Catch

         if @@Error > 0
              Rollback Transaction

    End Catch

End
于 2010-05-13T19:39:32.457 回答
0

SQL 中没有 MOVE 命令之类的东西。您必须先从表 1 插入表 2,然后从表 1 中删除副本。

于 2010-05-13T19:13:55.320 回答
0

不,您几乎被包含在事务中的插入和删除所困扰

于 2010-05-13T19:13:56.763 回答
0
INSERT dbo.newtable(
      name,
      department,
      Salary
) SELECT 
            name,
            FirstName,
            Lastname
      FROM    (
           DELETE dbo.oldtable
           OUTPUT
                   DELETED.name,
                   DELETED.department,
                   DELETED.Salary
           WHERE ID  IN ( 1001, 1003, 1005 )
      ) AS RowsToMove  

SELECT * FROM dbo.newtable
SELECT * FROM dbo.oldtable
于 2015-07-23T13:49:39.103 回答