日期:2014-05-17  浏览次数:20630 次

存储过程和游标问题,请高手解释一段代码!
最近公司的项目出了点问题,让我修改,我是新手看不懂这段代码,说是这段代码的问题,
请高手们给我这段代码翻译一下好吗!越详细越好,每行都翻译就再好不过了,纠结了我一天了
马上都下班了还没弄好,愁死了!
USE [YJ_Standard]
GO
/****** Object:  StoredProcedure [dbo].[MoveSaleList]    Script Date: 11/18/2013 08:04:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[MoveSaleList] 
@Mark nvarchar(10)
AS
BEGIN

--exec Proc_UpdateNTextField

update dbo.YJ_StoreSaleListInfo_Temp
set Mark=@Mark
where rowguid in (select top 25 rowguid from YJ_StoreSaleListInfo_Temp where status='N' and Mark is null)


declare @stringXML xml
declare @RowGuid nvarchar(50)
declare @SaleListContent xml
declare curs cursor local fast_forward for select  RowGuid, SaleListContent from YJ_StoreSaleListInfo_Temp where status='N' and Mark=@Mark
open curs 
fetch next from curs into @RowGuid, @SaleListContent
while @@fetch_status = 0 
begin  

begin try

exec Pos_AddSaleInfo @SaleListContent
end try
begin catch
if @@error<>0
    insert into YJ_StoreSaleListInfo_Error([RowGuid],[StoreCode],[SaleListCode],[OldSaleListCode],[SaleListContent],[AddDate],[MoveDate],[Status])
select [RowGuid],[StoreCode],[SaleListCode],[OldSaleListCode],[SaleListContent],[AddDate],[MoveDate],[Status]
from YJ_StoreSaleListInfo_Temp where rowguid=@RowGuid

    update YJ_StoreSaleListInfo_Temp set Status='E' where rowguid=@RowGuid
end catch

fetch next from curs into @RowGuid, @SaleListContent
end
close curs
deallocate curs

END


谢谢了,在线等
存储过程 游标 游标循环 数据库循环

------解决方案--------------------
open curs --打开游标
fetch next from curs into @RowGuid, @SaleListContent--逐行获取数据并插入到这两个变量中
while @@fetch_status = 0 --如果游标没错误,就执行下面部分
begin  

begin TRY--错误捕获

exec Pos_AddSaleInfo @SaleListContent--执行存储过程,@SaleListContent这个参数是传入存储过程中,由游标获取
end try
begin CATCH--错误捕获
if @@error<>0


其实最好的方法是自己执行并看结果,如果一次性执行有难度,可以用联机丛书上的最简单的例子执行来看看结果
------解决方案--------------------