SqlServer 游标不能循环
有很多条记录只能操作第一条请问循环哪里出错ALTERPROCEDUREABCD_AAASBEGINSETNOCOUNTONDECLARE@ordnoINT,@total_...
有很多条记录 只能操作第一条
请问循环哪里出错
ALTER PROCEDURE ABCD_AA
AS
BEGIN
SET NOCOUNT ON
DECLARE @ordno INT ,
@total_num INT;
SELECT @total_num = COUNT(*) FROM [contract] WHERE [status] = 0 ;
IF @total_num = 0
BEGIN
RETURN
END
ELSE
BEGIN
DECLARE Contact_cursor CURSOR FOR SELECT ordno FROM [contract] WHERE [status] = 0 ;
OPEN Contact_cursor
FETCH NEXT FROM Contact_cursor INTO @ordno
WHILE @@FETCH_STATUS=0
BEGIN
BEGIN TRY
BEGIN TRANSACTION
UPDATE 语句;
SELECT 语句;
INSERT 语句;
UPDATE 语句;
COMMIT;
END TRY
BEGIN CATCH
ROLLBACK;
END CATCH
FETCH NEXT FROM Contact_cursor INTO @ordno ;
END
END
CLOSE Contact_cursor
DEALLOCATE Contact_cursor
END 展开
请问循环哪里出错
ALTER PROCEDURE ABCD_AA
AS
BEGIN
SET NOCOUNT ON
DECLARE @ordno INT ,
@total_num INT;
SELECT @total_num = COUNT(*) FROM [contract] WHERE [status] = 0 ;
IF @total_num = 0
BEGIN
RETURN
END
ELSE
BEGIN
DECLARE Contact_cursor CURSOR FOR SELECT ordno FROM [contract] WHERE [status] = 0 ;
OPEN Contact_cursor
FETCH NEXT FROM Contact_cursor INTO @ordno
WHILE @@FETCH_STATUS=0
BEGIN
BEGIN TRY
BEGIN TRANSACTION
UPDATE 语句;
SELECT 语句;
INSERT 语句;
UPDATE 语句;
COMMIT;
END TRY
BEGIN CATCH
ROLLBACK;
END CATCH
FETCH NEXT FROM Contact_cursor INTO @ordno ;
END
END
CLOSE Contact_cursor
DEALLOCATE Contact_cursor
END 展开
1个回答
展开全部
语法逻辑上看没什么问题,建议在存储过程中加入PRINT语句,再调用存储过程看一直处理的过程是否有循环
WHILE @@FETCH_STATUS=0
BEGIN
print @ordno
WHILE @@FETCH_STATUS=0
BEGIN
print @ordno
追问
实验证明
WHILE里加了TRY CATCH之后就不能循环
我把TRY CATCH删了
改成 commit之前加
if @@error!=0
begin
rollback tran
return
end
这样可以吗
任何一句出错都会回滚吗
追答
在存储过程开头加上SET XACT_ABORT on,任意一个出错就会自动回滚了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询