首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2013-03-01 
从一个表读出数据, 然后插入到另外一个表在存储过程中, 从一个表1读出若干条数据, 然后插入到另一个表2中.

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

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

 2013-2-27 15:45分前结贴!
谢谢!
[解决办法]
insert 表2(...)
select ...
from 表1 a
where ...
and not exists (
select 1 
from 表2 b
where b.key = a.key
)

[解决办法]
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.主键)
[解决办法]


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

热点排行