0

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.

2
  • 1
    check what was passed to the param @personID? Commented Aug 9, 2017 at 18:46
  • 1
    @FullMoonFisher this shouldn't cause an error on compile, just execution. Commented Aug 9, 2017 at 18:52

3 Answers 3

1

I deleted this line SELECT * FROM #TempSortedDeposits WHERE RecordNo = @RowNumber and manually re-type it then it worked.

This is because the error occurs due to copy and paste. That result to pasted code contains unprintable characters like d non-braking spaces.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. How did you identify that line as being the problem?
because of this error "Incorrect syntax near '­'. is almost always invoked by unprintable characters as nothing printed out after "near".
1

Just a side note... your #TempSortedDeposits table dropping like you think it is.

Here's a quick example:

create table #TempSortedDeposits (i int)
insert into #TempSortedDeposits
values
(1)

IF OBJECT_ID('#TempSortedDeposits') IS NOT NULL DROP TABLE #TempSortedDeposits
select * from #TempSortedDeposits

If you run this the first time, it will return 1 when it shouldn't, because it should have been dropped. On the second run (same connection) you will get this error, showing you that the table still exists and wasn't dropped:

Msg 2714, Level 16, State 6, Line 2 There is already an object named '#TempSortedDeposits' in the database.

To fix this, change the syntax to:

IF OBJECT_ID('tempdb..#TempSortedDeposits') IS NOT NULL DROP TABLE #TempSortedDeposits

Here is a test

create table #TempSortedDeposits (i int)
insert into #TempSortedDeposits
values
(1)

IF OBJECT_ID('tempdb..#TempSortedDeposits') IS NOT NULL DROP TABLE #TempSortedDeposits
select * from #TempSortedDeposits

If you first manually drop the table (since we created it with the last run) and then run this, you will get the error message:

Msg 208, Level 16, State 0, Line 7 Invalid object name '#TempSortedDeposits'.

Which means the select failed, thus showing that the table was in fact dropped.

Comments

1

I have removed the extra character present in below line and re execute this proc. Line is SELECT * FROM #TempSortedDeposits WHERE RecordNo = @RowNumber

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.