I am trying to write a stored procedure that returns a deposit record for a specific person, the number of deposits for that person and the row number of that deposit for that person. The parameter @personID is always supplied the correct personID; however @SpecificRow is either 0 (meaning: return most recent deposit) or a row number (meaning:return record at this specific row).
Script:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [schema].[procedure]
@personID varchar(5),
@SpecificRow int,
@RowNumber INT OUTPUT,
@RowCount INT OUTPUT
AS
BEGIN
IF OBJECT_ID('#TempSortedDeposits') IS NOT NULL
DROP Table #TempSortedDeposits
-- get all deposits with record number into temp file for specific personID
SELECT
ROW_NUMBER() OVER (ORDER BY Date ASC) AS RecordNo, *
INTO
#TempSortedDeposits
FROM
persons.Deposits
WHERE
personID = @personID
-- get record count in file
SELECT @RowCount = COUNT(personID)
FROM persons.Deposits
WHERE personID = @personID
IF @SpecificRow = 0 --get most recent record
SET @RowNumber = @RowCount;
ELSE
--get record by rownumber
SET @RowNumber = @SpecificRow;
SELECT *
FROM #TempSortedDeposits
WHERE RecordNo = @RowNumber
END
When I try running the alter statement, I get the following error:
Msg 102, Level 15, State 1, Procedure procedure, Line 33 [Batch Start Line 9]
Incorrect syntax near ''.
Any insight?
Thanks.