I wrote following stored procedure in SQL Server 2005 and I get error message:
Msg 102, Level 15, State 1, Procedure sp_InsertCustTrans, Line 12
Incorrect syntax near ‘@TrID’.
My stored procedure is:
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
All assignment operations must be preceded by the keyword
set.Also you are using IsNull incorrectly as Ivan points out.
The proper usage is: