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

存储过程和游标有关问题,请高手解释一段代码

2014-01-03 
存储过程和游标问题,请高手解释一段代码!最近公司的项目出了点问题,让我修改,我是新手看不懂这段代码,说是

存储过程和游标问题,请高手解释一段代码!
最近公司的项目出了点问题,让我修改,我是新手看不懂这段代码,说是这段代码的问题,
请高手们给我这段代码翻译一下好吗!越详细越好,每行都翻译就再好不过了,纠结了我一天了
马上都下班了还没弄好,愁死了!
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


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

引用:
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

真心感谢!我就在刷新等高手帮忙的!

逐行读取游标,作为存储过程参数调用Pos_AddSaleInfo.
[解决办法]
begin try   --用于处理异常的

exec Pos_AddSaleInfo @SaleListContent
end try

begin catch  --捕获异常,进行判断,如果有错误,就进行其他操作
if @@error<>0
[解决办法]
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

哪个部分你不懂?


引用:
哪个部分你不懂?


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

真心感谢!我就在刷新等高手帮忙的!


打开curs游标,然后把游标中的字段RowGuid, SaleListContent 的值,放到变量@RowGuid, @SaleListContent中。

@@fetch_status = 0 表示游标的状态正常,也就是后面还有记录没有遍历,可以继续循环。


如果游标正常,下面的代码就不会执行了吗?
begin try

exec Pos_AddSaleInfo @SaleListContent
end try
begin catch
try是会执行的,不管是否正常

热点排行