0

I have a query like so:

SELECT id, @RowNumber = ROW_NUMBER() OVER (ORDER BY id) as 'number' 
FROM WaitList 
WHERE email = @Email

However I am getting error:

Incorrect syntax near the keyword 'as'.

What I am trying to do is assign the ROW_NUMBER() to a variable.

I really need to get the row number from the WaitList table.

Could I put the results in variable table and then get the row number by selecting 'number'?

5
  • 2
    what is your rdbms? Commented Apr 24, 2017 at 20:03
  • I dont even know what that is Commented Apr 24, 2017 at 20:03
  • RDBMS = Relational Database Management System. So which one are you using? The code looks like T-SQL so it might be Microsoft SQL Server? Commented Apr 24, 2017 at 20:05
  • Yes Microsoft SQL Server Commented Apr 24, 2017 at 20:06
  • RDBMS Commented Apr 24, 2017 at 20:06

4 Answers 4

1

If you want the number of the row that has the matching email address, but the table doesn't actually HAVE row numbers, then you should create a CTE to assign row numbers, and then select the specific row from that... something like this:

WITH NumberedRows AS (
    SELECT [id], [email], ROW_NUMBER() OVER (ORDER BY id) as [Number] 
    FROM WaitList 
)
SELECT @RowNumber = Number
FROM NumberedRows
WHERE email = @Email

Note that if there are SELECT statements prior to this, you'll need to make sure you terminate the SELECT statement (or other SQL Statements) with a semi-colon. OR, if you wish, you could just prefix the WITH with a semi-colon, like ";WITH..."

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

3 Comments

I get this error Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
The error message says exactly what it means. Either execute JUST this statement as outlined, or put a GO before this statement if it can be in a batch by itself... or if it's part of a larger batch of SQL, then simply terminate the previous statement with a semi-colon. For example, "DECLARE @RowNumber int; WITH NumberedRows AS (..."
I've updated the answer to be clear about the semi-colon issue. "WITH" is a keyword used all over T-SQL on SQL Server, so it can be ambiguous at times.
1

Your code looks like SQL Server. You cannot assign a variable and return a result set with the same query:

SELECT id, ROW_NUMBER() OVER (ORDER BY id) as number
FROM WaitList 
WHERE email = @Email;

Assigning the value to a variable doesn't really make sense. If you want, say, the total number of matching rows, then use aggregation functions:

select @cnt = count(*)
from waitlist
where email = @email;

1 Comment

I really need to get the actual row number of the item
0
 select a.id , a.number 
 from
 (SELECT id, ROW_NUMBER() OVER (ORDER BY id) as number
 FROM WaitList 
 WHERE email = @Email) a

Comments

-1
SELECT id, ROW_NUMBER() OVER ( ORDER BY id) as number
FROM WaitList 
WHERE email = @Email;

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.