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

在使用游标插入数据时总是提示临时表已经存在的有关问题

2012-09-10 
在使用游标插入数据时总是提示临时表已经存在的问题我用游标对数据进行读取,在存储过程A中调用了存储过程B

在使用游标插入数据时总是提示临时表已经存在的问题
我用游标对数据进行读取,在存储过程A中调用了存储过程B,执行的时候总是提示存储过程B的临时表已经存在


create PROCEDURE [dbo].[存储过程A]
AS
....

 if exists (select * from dbo.sysobjects where name='##t_tem_A' and xtype='U')
 drop table ##t_tem_A

 select top 20* into ##t_tem_A from ...

 declare cur_A cursor for select ....

 open cur_A

 fetch next from cur_A into ...
 while(@@fetch_status=0)
 begin
  ....
  exec 存储过程B,'参数'
  ....

  fetch next from cur_A into ...
 end

  close from cur_A 
  deallocate cur_A 


存储过程B的代码


create PROCEDURE [dbo].[存储过程B]
AS
....

 if exists (select * from dbo.sysobjects where name='##t_tem_B' and xtype='U')
 drop table ##t_tem_B

 select top 20* into ##t_tem_B from ...

 declare cur_B cursor for select ....

 open cur_B

 fetch next from cur_B into ...
 while(@@fetch_status=0)
 begin
  ....
  exec 存储过程B,'参数'
  ....

  fetch next from cur_B into ...
 end

  close from cur_B
  deallocate cur_B 



-------------------------------

运行的时候调用存储过程B时只能插入一条记录,然后就提示存储过程B的临时表##t_tem_B已经存在?高手指点一下,问题出在哪?



   
 

[解决办法]
不要用两个##的临时表 这个是全局的

改成一个#
[解决办法]

SQL code
/*把 ##t_tem_A ##t_tem_B修改为 #t_tem_A #t_tem_B不要用全局临时表*/
[解决办法]
SQL code
if object_id('tempdb..#t_tem_B') is not null drop table #t_tem_B;还有你的所有的##用一个#,不要用两个,两个是定义全局临时表,不要定义全局临时表 

热点排行