从一个表读出数据, 然后插入到另外一个表

chokochu 2013-02-27 03:24:26
在存储过程中, 从一个表1读出若干条数据, 然后插入到另一个表2中.
在插入表2之前, 要检查表2中是否已经存在该条数据, 如果存在则跳过, 继续插入下一条...

我知道用游标肯定能实现, 但是不太熟悉使用方法, 所以, 使用该方法的答案我也会给分.
如果有其他更好的答案也请大家赐教.

2013-2-27 15:45分前结贴!
谢谢!
...全文
228 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
-Tracy-McGrady- 2013-02-27
  • 打赏
  • 举报
回复

exists
-Tracy-McGrady- 2013-02-27
  • 打赏
  • 举报
回复

select * into #lsb from 表1
while exsts(select top(1)* from #lsb where ....)
begin
  if not exsts(select ... from 表2 where ...)
    insert into 表2(....) select ...from #lsb
  detele #lsb where .....
end
發糞塗牆 2013-02-27
  • 打赏
  • 举报
回复
create proc test as if .....--检查表#tb是否存在 ..... insert into #tb(列) exec 存储过程B insert into tb select * from #tb a where not exists (select 1 from tb b where a.主键=b.主键)
昵称被占用了 2013-02-27
  • 打赏
  • 举报
回复
insert 表2(...) select ... from 表1 a where ... and not exists ( select 1 from 表2 b where b.key = a.key )

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧