【发布时间】:2009-11-27 07:05:24
【问题描述】:
当我要通过 sqlserver 的 StoredProceduer 在表中输入记录时,我遇到了异常 我有一个字段,我需要在表中插入新记录后立即更新。 为此,我创建了一个 sotredprocedure,其中我首先编写了插入命令,然后我为相同的记录编写了更新命令,但它给了我以下错误 SQL异常: EXECUTE 之后的事务计数表明 BEGIN 和 COMMIT 语句的数量不匹配。先前计数 = 1,当前计数 = 2。在批处理结束时检测到不可提交的事务。事务被回滚。存储过程执行中的一些错误
我还尝试了另一种方式,例如:我刚刚在存储过程中编写了插入命令,并创建了一个 afterInsert 触发器,我在其中触发了更新命令。但我仍然面临上述异常
详情: 表结构:
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
触发器:
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
【问题讨论】:
标签: exception triggers stored-procedures transactions vb.net