目的:
把数据一条一条地插入,发现重复的执行UPDATE,不重复的插入。
建立如下的存储过程:
CREATE PROCEDURE ss
as
begin
declare @count [nvarchar] (20)
declare cur_custer cursor
for
select [保单号] from ss_temp
print '开始执行'
open cur_custer
fetch next from cur_custer into @count
while(@@fetch_status=0) --循环开始
begin
if EXISTS(select 业务实收表.保单号 from 业务实收表 join ss_temp on 业务实收表.保单号=ss_temp.保单号 where 业务实收表.保单号=@count)
begin
Exec('update 业务实收表 set 财务结清日期=aa.财务结清日期,业务实收表.电子收据保费=业务实收表.电子收据保费+aa.电子收据保费 from ss_temp aa where aa.保单号=业务实收表.保单号 and aa.保单号='+@count)
print '更新'
end
else
begin
Exec('insert 业务实收表 select * from ss_temp where ss_temp.保单号='+@count)
print '插入'
print 'test'
end
fetch next from cur_custer into @count
end
print '结束'
close cur_custer --关闭游标
deallocate cur_custer--释放资源
end
GO调用这个存储过程,查询分析器的结果只显示“开始执行”和“结束”两个,也就是中间的循环根本没有执行,哪位高人帮我看一下是哪里错了,造成这个存储过程没有执行,谢谢!
把数据一条一条地插入,发现重复的执行UPDATE,不重复的插入。
建立如下的存储过程:
CREATE PROCEDURE ss
as
begin
declare @count [nvarchar] (20)
declare cur_custer cursor
for
select [保单号] from ss_temp
print '开始执行'
open cur_custer
fetch next from cur_custer into @count
while(@@fetch_status=0) --循环开始
begin
if EXISTS(select 业务实收表.保单号 from 业务实收表 join ss_temp on 业务实收表.保单号=ss_temp.保单号 where 业务实收表.保单号=@count)
begin
Exec('update 业务实收表 set 财务结清日期=aa.财务结清日期,业务实收表.电子收据保费=业务实收表.电子收据保费+aa.电子收据保费 from ss_temp aa where aa.保单号=业务实收表.保单号 and aa.保单号='+@count)
print '更新'
end
else
begin
Exec('insert 业务实收表 select * from ss_temp where ss_temp.保单号='+@count)
print '插入'
print 'test'
end
fetch next from cur_custer into @count
end
print '结束'
close cur_custer --关闭游标
deallocate cur_custer--释放资源
end
GO调用这个存储过程,查询分析器的结果只显示“开始执行”和“结束”两个,也就是中间的循环根本没有执行,哪位高人帮我看一下是哪里错了,造成这个存储过程没有执行,谢谢!
as
begin
print '开始执行'
declare @count [nvarchar] (20)
declare cur_custer cursor
for
select [保单号] from ss_tempopen cur_custer
fetch next from cur_custer into @count
while(@@fetch_status=0) --循环开始
begin
if EXISTS(select 业务实收表.保单号 from 业务实收表 join ss_temp on 业务实收表.保单号=ss_temp.保单号 where 业务实收表.保单号=@count)
begin
Exec('update 业务实收表 set 财务结清日期=aa.财务结清日期,业务实收表.电子收据保费=业务实收表.电子收据保费+aa.电子收据保费 from ss_temp aa where aa.保单号=业务实收表.保单号 and aa.保单号='+@count)
print '更新'
end
else
begin
Exec('insert 业务实收表 select * from ss_temp where ss_temp.保单号='+@count)
print '插入'
print 'test'
end
fetch next from cur_custer into @count
endclose cur_custer --关闭游标
deallocate cur_custer--释放资源
print '结束'
end
GO
drop proc ss
fetch next from cur_custer into @count
print @@fetch_status --- 看看打印什么东西
while(@@fetch_status=0) --循环开始
begin
删除ss_temp后的空格,是不是有非法字符
插入
test”这个又是为何呢?
Exec('insert 业务实收表 select * from ss_temp where ss_temp.保单号='''+@count+'''')
print '插入'
print 'test'
insert into tb select * from tb a where not exists(select 1 from b where a.id=b.id)
---
只要主键不重复就不会出错啦
set 财务结清日期=aa.财务结清日期,aa.电子收据保费=bb.电子收据保费+aa.电子收据保费
from ss_temp aa
right join 业务实收表 bb
where aa.保单号=bb.保单号
and exists(select 1 from 业务实收表 where 保单号=aa.保单号
insert 业务实收表
select *
from ss_temp a
where not exists( select 1 from 业务实收表 where 保单号=a.保单号)
“服务器: 消息 1032,级别 15,状态 1,过程 ss,行 14
无法使用列前缀 'aa'。此前缀必须与 UPDATE 子句 'bb' 中的对象匹配。
服务器: 消息 156,级别 15,状态 1,过程 ss,行 17
在关键字 'where' 附近有语法错误。”这个是什么原因呢?谢谢!
set 财务结清日期=aa.财务结清日期,bb.电子收据保费=bb.电子收据保费+aa.电子收据保费
from ss_temp aa
right join 业务实收表 bb
on aa.保单号=bb.保单号
and exists(select 1 from 业务实收表 where 保单号=aa.保单号 )
sorry
set 财务结清日期=aa.财务结清日期,bb.电子收据保费=bb.电子收据保费+aa.电子收据保费
from ss_temp aa
right join 业务实收表 bb
on aa.保单号=bb.保单号
where exists(select 1 from 业务实收表 where 保单号=aa.保单号 )
加上
select * from ss_temp 看看有无记录