Msg 102,Level 15,State 1,

时间:2012-07-02 16:50:15

标签: sql-server sql-server-2005

我在SQL Server 2005中编写了以下存储过程,并收到错误消息:

  

Msg 102,Level 15,State 1,Procedure sp_InsertCustTrans,Line 12
  ' @ TrID'附近的语法不正确。

我的存储过程是:

ALTER PROCEDURE [dbo].[sp_InsertCustTrans] 
-- Add the parameters for the stored procedure here
@CuID int, @TrType nvarchar(10), @TrAmt int
AS
BEGIN
SET NOCOUNT ON;
    declare @TrID int;
    Select @TrID = MAX(TransactionID) from CustTrans;
    if Isnull(@TrID)
        @TrID = @TrID + 1
    else
        @TrID = 1

-- Insert statements for procedure here
if (@TrType = 'Deposit')
    begin
        INSERT INTO CustTrans (TransactionID, TransactionDate, CustID, TransactonType, CreditAmount)
            Values(@TrID, GetDate(), @CuID, @TrType, @TrAmt);
    end
else
    begin
        INSERT INTO CustTrans (TransactionID, TransactionDate, CustID, TransactonType, DebitAmount)
            Values(@TrID, GetDate(), @CuID, @TrType, @TrAmt);
    end
END
GO

1 个答案:

答案 0 :(得分:4)

所有作业操作必须以关键字set开头。

Set @TrID = @TrID + 1

Ivan指出,你也正在使用IsNull。 正确的用法是:

If @TrID Is Null