Exception vb.net中的事务计数异常(SQL异常)

Exception vb.net中的事务计数异常(SQL异常),exception,triggers,stored-procedures,transactions,vb.net,Exception,Triggers,Stored Procedures,Transactions,Vb.net,当我要通过sqlserver的StoredProceduer在表中输入记录时,出现以下异常 在表中插入新记录后,我需要立即更新一个字段。 为此,我创建了一个sotredprocedure,在该过程中,我首先编写了insert命令,然后为相同的RECORD编写了update命令,但它给出了以下错误 SQlException: EXECUTE后的事务计数表示BEGIN和COMMIT语句的数量不匹配。上一次计数=1,当前计数=2。在批处理结束时检测到不可提交的事务。事务被回滚。存储过程执行中的一些错误

当我要通过sqlserver的StoredProceduer在表中输入记录时,出现以下异常 在表中插入新记录后,我需要立即更新一个字段。 为此,我创建了一个sotredprocedure,在该过程中,我首先编写了insert命令,然后为相同的RECORD编写了update命令,但它给出了以下错误 SQlException: EXECUTE后的事务计数表示BEGIN和COMMIT语句的数量不匹配。上一次计数=1,当前计数=2。在批处理结束时检测到不可提交的事务。事务被回滚。存储过程执行中的一些错误

我还尝试了另一种方式:我只是在storedprocedure中编写了insert命令,并创建了一个afterInsert触发器,在该触发器中触发update命令。但我仍然面临着上述例外

详情: 表结构:

CREATE TABLE [dbo].[TabStoreGRNMaster](
    [pk_grnm_id] [bigint] IDENTITY(1,1) NOT NULL,
    [fk_col_id] [bigint] NULL,
    [fk_sup_id] [bigint] NULL,
    [grnm_grnno] [varchar](50) NULL,
    [grnm_date] [nvarchar](10) NULL,
    [grnm_partyinvno] [varchar](50) NULL,
    [grnm_invdate] [nvarchar](10) NULL,
    [grnm_freight] [numeric](7, 2) NULL,
    [grnm_otherchrg] [numeric](7, 2) NULL,
    [grnm_roundoff] [bit] NULL,
    [Fk_User_Id] [bigint] NULL,
    [grnm_EntryDate] [nvarchar](8) NULL,
 CONSTRAINT [PK_TabStoreGRNMaster] PRIMARY KEY CLUSTERED 
(
    [pk_grnm_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
存储过程:

USE [Kollege]
GO
/****** Object:  StoredProcedure [dbo].[StoreGRNMaster]    Script Date: 11/27/2009 10:20:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[StoreGRNMaster]
    (
    @pk_grnm_id         [bigint] output ,
    @fk_col_id          [bigint] = 0 ,
    @fk_sup_id          [bigint] = 0 ,
    @grnm_grnno         [varchar](50) = '',
    @grnm_date          [nvarchar](10) = null,
    @grnm_partyinvno            [varchar](50) = '',
    @grnm_invdate           [nvarchar](10) = null,
    @grnm_freight           [numeric] = 0 ,
    @grnm_otherchrg         [numeric] = 0 ,
    @grnm_roundoff          [bit] = 0 ,
    @Fk_User_Id         [bigint] = 0 ,
    @grnm_EntryDate         [nvarchar](8) = null,
    @opt                        [BIGINT] =0,
    @del                        [bit] =0

    )
 as 
 begin 
 SET NOCOUNT ON; 
 BEGIN TRY 
 begin transaction 
 if isnull(@opt,0) = 0 begin
 INSERT INTO TabStoreGRNMaster
    (
    fk_col_id,
    fk_sup_id,
    grnm_grnno,
    grnm_date,
    grnm_partyinvno,
    grnm_invdate,
    grnm_freight,
    grnm_otherchrg,
    grnm_roundoff,
    Fk_User_Id,
    grnm_EntryDate
    )
VALUES
    (
    @fk_col_id,
    @fk_sup_id,
    @grnm_grnno,
    @grnm_date,
    @grnm_partyinvno,
    @grnm_invdate,
    @grnm_freight,
    @grnm_otherchrg,
    @grnm_roundoff,
    @Fk_User_Id,
    @grnm_EntryDate
    )
set @pk_grnm_id = @@identity 
 end 
 else 
 begin 
 if @del = 0 
 UPDATE TabStoreGRNMaster
SET
        fk_col_id           =   @fk_col_id,
        fk_sup_id           =   @fk_sup_id,
        grnm_grnno          =   @grnm_grnno,
        grnm_date           =   @grnm_date,
        grnm_partyinvno         =   @grnm_partyinvno,
        grnm_invdate            =   @grnm_invdate,
        grnm_freight            =   @grnm_freight,
        grnm_otherchrg          =   @grnm_otherchrg,
        grnm_roundoff           =   @grnm_roundoff,
        Fk_User_Id          =   @Fk_User_Id,
        grnm_EntryDate          =   @grnm_EntryDate
WHERE
(
pk_grnm_id  =   @opt
)  

if @del=1 and isnull(@opt,0) <> 0 

 DELETE from [TabStoreGRNMaster]
WHERE
    ( [pk_grnm_id]   = @opt)
end 
 commit transaction 
 END TRY 
 BEGIN CATCH 
 raiserror ('Some Error in stored procedure execution',1,1) 
 END CATCH; 
 END 

在我看来,如果在TRY块中发现错误,您似乎没有对打开的事务执行任何操作-COMMIT不会运行,但是CATCH块没有回滚。

您对另一个答案的评论提到“调用此存储过程时,我也在维护Vb.net中的事务”。这就是原因

您必须平衡BEGIN TRAN和提交/回滚,以便@TRANCOUNT在同一范围内以相同的值开始和结束


要么在存储过程中全部执行,要么在客户机中全部执行

调用此存储过程时,我还要从Vb.net维护事务
alter TRIGGER TGRStoreGRNMasterCode
ON tabStoreGRNMaster
AFTER INSERT 
AS
begin
declare @pk varchar(10)
declare @colcode varchar(10)
declare @current_session varchar(20)
declare @colid bigint
set @pk  = (select pk_grnm_id from Inserted)
--set @colid = (select fk_col_id from Inserted)
set @colcode= 'jiet'--(select col_code from tabcollegemaster where pk_col_id =5)  (when i replace word 'Jiet' with commented qry i gets sql exception otherwise its working fine)
select @current_session = Ses_abbrev  from TabAcaSessionMast where ses_current = 1 
--update tabStoreGRNMaster set grnm_grnno= @colcode +'/' +@current_session+ '/' + @pk where pk_grnm_id=convert(bigint,@pk)
update tabStoreGRNMaster set grnm_grnno= (select col_code from tabcollegemaster where pk_col_id=5) +'/' +@current_session+ '/' + @pk where pk_grnm_id=convert(bigint,@pk)

end