Move row from one table to another?

后端 未结 5 1248
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 06:25

I have two tables with the same column definitions. I need to move (not copy) a row from one table to another. Before I go off and use INSERT INTO/DELETE (in a transaction

5条回答
  •  借酒劲吻你
    2020-12-15 07:16

    You can try Insert into abc (a,b,c) select(a,b,c) from def

    doing above so will insert column a, b,c of def into column a,b,c of abc. after inserting run a delete table, drop table or truncate whatever is your criteria.

    sample is:

    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
    

提交回复
热议问题