sqlserver存储过程中使用游标,查询结果没有第一条数据,但循环次数是...

发布网友 发布时间:2022-04-23 02:40

我来回答

3个回答

懂视网 时间:2022-04-09 02:22

插入客户信息 create synonym synonym_project for [192.168.159.134].[VisualNet].[dbo].project declare @prjkey varchar(50) declare @customername varchar(50) declare @khbh char(4) declare cursor_project cursor for select prjname,prjkey from synonym_project open cursor_project fetch next from cursor_project into @customername,@prjkey if not exists(select 1 from Customers where gsmc = @customername) select @khbh = MAX(khbh)+1 from Customers insert into Customers(khbh,gsmc,gsdz,gsdh) values(@khbh,@customername,‘‘,‘‘) while @@FETCH_STATUS=0 begin fetch next from cursor_project into @customername,@prjkey if not exists(select 1 from Customers where gsmc = @customername) select @khbh = MAX(khbh)+1 from Customers insert into Customers(khbh,gsmc,gsdz,gsdh) values(@khbh,@customername,‘‘,‘‘) end close cursor_project deallocate cursor_project drop synonym synonym_project

  这里使用同义词和游标远程查询数据。

 

  因为:

open cursor_project

fetch next from cursor_project into @customername,@prjkey

  打开游标后需要先取一条数据才能进行while循环读取,所以我在取到数据后进行插入操作 

  然后while循环中也会再进行数据读取,在读取后再进行插入操作。

 

  但是执行后发生错误:违反唯一约束

  我查看了远程表,所有的数据都完整的插入到表中,并没有遗失的数据,不知道从哪里多了一条数据,所以讲数据处理换为打印查看数据: 

--插入客户信息
create synonym synonym_project for [192.168.159.134].[VisualNet].[dbo].project
declare @prjkey varchar(50)
declare @customername varchar(50)
declare @khbh char(4)

declare cursor_project cursor for
select prjname,prjkey from synonym_project

open cursor_project

fetch next from cursor_project into @customername,@prjkey
print @customername

while @@FETCH_STATUS=0
begin
 fetch next from cursor_project into @customername,@prjkey
 print @customername
 
end

close cursor_project
deallocate cursor_project

drop synonym synonym_project

  结果如下:

Test1      
南通心智慧     
港闸区      
港闸区 

  但是在远程表中‘港闸区‘只有一个,所以是最后一条数据重复了,然后百度一下看看别人是不是出现过:http://zhidao.baidu.com/link?url=wNi65XcABENt3DV_VJJHILlHWTNYdsMYzX7b25RmGACPIwsLjAUkWVz0qIFzlfwC7fK5S5-71t5196I5wJ4gRa

 

  回答是说取数据应该是在while后面,所以我把游标获取数据放在循环最后: 

--插入客户信息
create synonym synonym_project for [192.168.159.134].[VisualNet].[dbo].project
declare @prjkey varchar(50)
declare @customername varchar(50)
declare @khbh char(4)

declare cursor_project cursor for
select prjname,prjkey from synonym_project

open cursor_project

fetch next from cursor_project into @customername,@prjkey
print @customername

while @@FETCH_STATUS=0
begin
 
 print @customername
 fetch next from cursor_project into @customername,@prjkey
end

close cursor_project
deallocate cursor_project

drop synonym synonym_project

  运行结果:

Test1      
Test1      
南通心智慧     
港闸区      

  发现第一条数据重复,也就是说两次打印有一次是多余的,因为要遍历数据,所以循环里面的打印肯定是必须的,上面的打印可能是多余的,如果把上面的打印删除,游标取数据在打印之后,循环内部打印在游标取数据之前,就不会有上一条数据丢失:

--插入客户信息
create synonym synonym_project for [192.168.159.134].[VisualNet].[dbo].project
declare @prjkey varchar(50)
declare @customername varchar(50)
declare @khbh char(4)

declare cursor_project cursor for
select prjname,prjkey from synonym_project

open cursor_project

fetch next from cursor_project into @customername,@prjkey

while @@FETCH_STATUS=0
begin
 
 print @customername
 fetch next from cursor_project into @customername,@prjkey
end

close cursor_project
deallocate cursor_project

drop synonym synonym_project

  结果如下:

Test1      
南通心智慧     
港闸区      

  这样就正确了,相同于打印, 对数据进行插入操作的逻辑也是这样,讲打印改成需要的数据操作:

--插入客户信息
 create synonym synonym_project for [192.168.159.134].[VisualNet].[dbo].project
 declare @prjkey varchar(50)
 declare @customername varchar(50)
 declare @khbh char(4)
 
 declare cursor_project cursor for
 select prjname,prjkey from synonym_project
 
 open cursor_project
 
 fetch next from cursor_project into @customername,@prjkey
 
 while @@FETCH_STATUS=0
 begin
  
  if not exists(select 1 from Customers where gsmc = @customername)
  select @khbh = MAX(khbh)+1 from Customers
  insert into Customers(khbh,gsmc,gsdz,gsdh) values(@khbh,@customername,‘‘,‘‘)
  fetch next from cursor_project into @customername,@prjkey
 end
 
 close cursor_project
 deallocate cursor_project

  这样就正确了,对游标使用很少经常会出现这个问题,因为普通的逻辑就会出现最后数据重复,必须使用游标的逻辑格式。

 

sqlserver游标使用误区

标签:

热心网友 时间:2022-04-08 23:30

将fetch next from mycursor into @i,@name
放在while循环体的最后即可,即:
while(@fetch_status=0)
begin
print 'id: '+@i+ ' name: '+@name
fetch next from mycursor into @i,@name
end
这样应该就可以了。

热心网友 时间:2022-04-09 00:48

尝试下把你的数据库打下SP4补丁

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com