EXECUTE之后的事务计数表示BEGIN和COMMIT语句的数量不匹配

时间:2015-04-16 18:58:46

标签: sql asp.net sql-server sql-server-2008 asp.net-mvc-4

当我执行以下存储过程时,我收到此错误:

  

System.Data.SqlClient.SqlException:EXECUTE后的事务计数   表示BEGIN和COMMIT语句的数量不匹配。   先前的计数= 0,当前计数= 1。

代码:

create procedure [dbo].[sp_crm_diler_master]
(
            @Fullname varchar(100),
            @Email varchar(50),
            @Mobile varchar(12),
            @qualification varchar(50),
            @presentaddress varchar(250),
            @permanentaddress varchar(250),
            @location varchar(50),
            @skills varchar(100),
            @Dob varchar(15),
            @myphoto varbinary(Max),
            @uniqueid varchar(25),
            @Message varchar(150) output
)
AS
BEGIN
 if not exists (select emailid,phone from crm_masterdata where emailid=@Email And phone=@Mobile)
  begin
 begin transaction
 declare @small smalldatetime = (select  CAST(@Dob as smalldatetime))
 declare @todaydate datetime=(select getdate())
  insert into crm_masterdata(uniqueid,fullname,phone,dob,photo,emailid,qualification,location,present_address,permanent_address,skillsets,datasource,entrydate,active)
 values(@uniqueid,@Fullname,@Mobile,@small,@myphoto,@Email,@qualification,@location,@presentaddress,@permanentaddress,@skills,'reception',@todaydate,1)
Set @Message=' Registration Successfull,Please Login'
end
 else
 begin
  set @Message='This User Already Registered'
  end
  end

我的错误在哪里?

2 个答案:

答案 0 :(得分:1)

create procedure [dbo].[sp_crm_diler_master]
(
@Fullname       varchar(100),
@Email          varchar(50),
@Mobile         varchar(12),
@qualification  varchar(50),
@presentaddress varchar(250),
@permanentaddress varchar(250),
@location       varchar(50),
@skills         varchar(100),
@Dob            varchar(15),
@myphoto        varbinary(Max),
@uniqueid       varchar(25),
@Message        varchar(150) output
)
AS
BEGIN
  SET NOCOUNT ON;

   declare @small smalldatetime = (select  CAST(@Dob as smalldatetime));
   declare @todaydate datetime=getdate();
   declare @Error INT;

BEGIN TRANSACTION;    --<-- You need to commit it
     IF not exists (select 1 from crm_masterdata  where emailid=@Email And phone=@Mobile)
       BEGIN
            insert into crm_masterdata(uniqueid,fullname,phone,dob,photo,emailid
                     ,qualification,location,present_address,permanent_address,skillsets,datasource,entrydate,active)
            values(@uniqueid,@Fullname,@Mobile,@small,@myphoto,@Email,@qualification
                        ,@location,@presentaddress,@permanentaddress,@skills,'reception',@todaydate,1)

             SET @Error = @@ERROR;
             Set @Message =' Registration Successfull,Please Login'
       END
    ELSE
      BEGIN
          SET @Message='This User Already Registered'
      END

    IF (@Error = 0)
        COMMIT TRANSACTION; --<-- Commit tran 
    ELSE
        ROLLBACK TRANSACTION;

END

答案 1 :(得分:0)

我没有看到与您的BEGIN TRANSACTION匹配的COMMIT TRANSACTION。