当前位置: 代码迷 >> Sql Server >> 怎样用sql语句把一个表内容导入另一个一样结构的表中,同一数据库,该如何解决
  详细解决方案

怎样用sql语句把一个表内容导入另一个一样结构的表中,同一数据库,该如何解决

热度:105   发布时间:2016-04-27 21:04:23.0
怎样用sql语句把一个表内容导入另一个一样结构的表中,同一数据库
我想用作业   每晚在2点钟   运行一个存储过程   ,把一个表的部分内容(如前10000行)全部导入到另一个表中,他们表结构一样,然后把原表已导入过去的数据删除。

原表中有id自增列。




------解决方案--------------------
--try

begin tran

insert B(colName)
select top 10000 colName
from A
order by id

delete A
where id in(select top 10000 colName from A order by id)

if @@error=0
commit tran
else
rollback tran

------解决方案--------------------
begin tran
drop table b
if @@error <> 0 goto err_a
select top 10000 * insert into b from a
if @@error <> 0 goto err_a
commit tran
return
err_a:
rollback tran
  相关解决方案