如何判断已打开的事务没有rollback或者commit,已打开的游标是否关闭。我下面这样写,如果第一次运行有错误发生,则第二次运行时总是提示游标已存在。
ALTER PROCEDURE [dbo].[WMS_TransVoucher_Check]
-- Add the parameters for the stored procedure here
@FlowNo varchar(50), --单据编号
@CheckID varchar(50), --审核人ID
@CheckName varchar(50), --审核人姓名
@SystemID int --系统标识
AS
BEGIN
-- Insert statements for procedure here
BEGIN TRANSACTION
DECLARE @rtn INT
DECLARE @BusinessDate DATETIME
DECLARE @FlowNoIn VARCHAR(50)
DECLARE @FlowNoOut VARCHAR(50)
DECLARE @cWhCodeIn VARCHAR(50)
DECLARE @cWhNameIn VARCHAR(50)
DECLARE @CanUse VARCHAR(50)
DECLARE @cWhCodeOut VARCHAR(50)
DECLARE @cWhNameOut VARCHAR(50)
DECLARE @cInvCode VARCHAR(50)
DECLARE @cInvName VARCHAR(50)
DECLARE @Barcode VARCHAR(50)
DECLARE @cInvStd VARCHAR(50)
DECLARE @BatchNo VARCHAR(50)
DECLARE @PositionsInCode VARCHAR(50)
DECLARE @PositionsInName VARCHAR(50)
DECLARE @PositionsOutCode VARCHAR(50)
DECLARE @PositionsOutName VARCHAR(50)
DECLARE @cComUnitName VARCHAR(50)
DECLARE @cComQuantity FLOAT
DECLARE @cComUnitPrice MONEY
DECLARE @mComUnitName VARCHAR(50)
DECLARE @mComQuantity FLOAT
DECLARE @RETURNCODE INT
DECLARE @RETURNMSG VARCHAR(100)
SET @BusinessDate=GETDATE()
SELECT @cWhCodeIn=cWhCodeIn,
@cWhNameIn=cWhNameIn,
@cWhCodeOut=cWhCodeOut,
@cWhNameOut=cWhNameOut,
@CanUse=CanUse
FROM WMS_TransVoucher
WHERE FlowNo=@FlowNo AND ISNULL(CheckID,'')=''
IF(@@ROWCOUNT>0)
BEGIN
--增加一张调拨入库单
EXEC WMS_GetFormNo '其它入库',@BusinessDate,@FlowNoIn output
EXEC @rtn=dbo.WMS_StockRecord_Add
@FlowNoIn,1,'103001','',@BusinessDate,@cWhCodeIn,@cWhNameIn,
'','','','','','','',@CheckID,@CheckName,@CanUse,'',@FlowNo,'','','',@SystemID,'由调拨单产生的入库单'
IF(@rtn<>1)
BEGIN
SET @RETURNCODE=-1
SET @RETURNMSG='生成其它入库单单头信息失败!'
GOTO ERROR
END
--增加一张调拨出库单
EXEC WMS_GetFormNo '其它出库',@BusinessDate,@FlowNoOut output
EXEC @rtn=dbo.WMS_StockRecord_Add
@FlowNoOut,1,'203001','',@BusinessDate,@cWhCodeOut,@cWhNameOut,
'','','','','','','',@CheckID,@CheckName,@CanUse,'',@FlowNo,'','','',@SystemID,'由调拨单产生的出库单'
IF(@rtn<>1)
BEGIN
SET @RETURNCODE=-2
SET @RETURNMSG='生成其它出库单单头信息失败!'
GOTO ERROR
END
--增加调拨单明细
DECLARE CUR_TransVouchers CURSOR FOR
SELECT cInvCode,cInvName,Barcode,cInvStd,BatchNo,PositionsInCode,PositionsInName,
cComUnitName,cComQuantity,cComUnitPrice,mComUnitName,mComQuantity
FROM WMS_TransVouchers
WHERE FlowNo=@FlowNo
OPEN CUR_TransVouchers
FETCH NEXT FROM CUR_TransVouchers
INTO @cInvCode,@cInvName,@Barcode,@cInvStd,@BatchNo,@PositionsInCode,@PositionsInName,
@cComUnitName,@cComQuantity,@cComUnitPrice,@mComUnitName,@mComQuantity
WHILE(@@FETCH_STATUS=0)
BEGIN
print @cInvCode+','+@cInvName
EXEC @rtn=dbo.WMS_StockRecords_Add
@FlowNoIn,@cInvCode,@cInvName,@Barcode,@cInvStd,@BatchNo,'','',
@PositionsInCode,@PositionsInName,@cComUnitName,@cComQuantity,@cComUnitPrice,@mComUnitName,@mComQuantity,
@CheckID,@CheckName,'',@FlowNo,@CanUse,'由调拨单产生的入库单'
IF(@rtn<>1)
BEGIN
SET @RETURNCODE=-3
SET @RETURNMSG='生成其它入库单明细信息失败!'
GOTO ERROR
END
EXEC @rtn=dbo.WMS_StockRecords_Add
@FlowNoOut,@cInvCode,@cInvName,@Barcode,@cInvStd,@BatchNo,'','',
@PositionsOutCode,@PositionsOutName,@