存储过程中的语法不正确

时间:2016-10-14 16:40:26

标签: sql-server tsql stored-procedures syntax

我编写了一个存储过程,并且在第64行提交

附近得到了错误的语法
[Serializable]
public class ErrorInfo : ISerializable
{
    public enum ErrorCode {
        [.....]
    }

    public ErrorCode Code { get; set; }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("Code", this.Code , typeof(ErrorCode ));
    }

    public Error(SerializationInfo info, StreamingContext context)
    {
        this.Code = (ErrorCode)Enum.Parse(typeof(ErrorCode), info.GetInt32("Code").ToString());
    }
}

我不确定我的语法有什么问题。一切看起来都对我来说,从我的理解,一切都应该进入BEGIN TRANSACTION COMMIT TRANSACTION块,所以我不明白为什么会失败

2 个答案:

答案 0 :(得分:0)

在AS语句之前声明所有变量并开始事务。应该有所帮助

CREATE PROCEDURE dbo.Transactions_Create

@AccountID INT, 
@EmplNo smallint,
@Amount Money,
@Description VARCHAR(100),
@EnteredBy VARCHAR(41),
@Type char(1),
@TransId INT OUTPUT,
@Withdraw smallint,
@ServiceFee Money,
@withdrawchange smallInt,
@balancechange Money,
@ActType dbo.AccountType

AS
    SET XACT_ABORT ON;
    BEGIN Transaction
        select @ActType =Type from dbo.Accounts
        where AccountId=@AccountID;


select @Withdraw = WithdrawalCount from dbo.Accounts
where AccountId=@AccountId

if @Type='D'
    BEGIN 
        SET @ServiceFee=0.0;
        SET @withdrawchange=0;
        SET @balancechange=@Amount;

    end;
else 
    BEGIN       
        SET @withdrawchange=1;
        if @ActType='cheaquing' 
            begin
                SET @ServiceFee=0.50;
            end
            else
                begin
                    if @Withdraw<2
                        begin 
                            SET @ServiceFee=0.0;
                    end
                    else
                        begin
                            SET @ServiceFee=1.00;
                        end
     SET @balancechange=(@Amount-@ServiceFee)*-1;
    end
update dbo.Accounts 
SET Balance= balance+@balancechange, WithdrawalCount=WithdrawalCount+@withdrawchange
where AccountId=@AccountId;

INSERT Into dbo.Transactions(AccountID, EmplNo ,Amount, Description ,EnteredBy ,Type)

values (@AccountID, @EmplNo ,@Amount, @Description ,@EnteredBy ,@Type );

SET @TransID = SCOPE_IDENTITY();

commit Transaction;

答案 1 :(得分:0)

是的,它看起来只是一个缺失的结局