x

Incorrect syntax near '@n'

Hi friends,

i am new to the sql, ------------------------------------------------------------------------------------ my query is to find the nth highest salary, the problem is, i pass the argument(@n) to the query, when is execute this stored procedure am getting an

"Incorrect syntax near '@n' " -------------------------------------------------------------------------------------- create procedure proc_nthsalary (@n int) as begin select top 1 name, salary from emp_details where salary in(select distinct top @n salary from emp_details order by salary desc) order by salary end

-------------------------------------------------------------------------------- am passing argument to this query, it works fine, then why the above stored procedure

throws an error ------------------------------------------------------------------------------- create procedure proc_getname (@n salary) as begin select name from emp_details where salary=@salary end

more ▼

asked Aug 30, 2012 at 07:18 PM in Default

jaijune gravatar image

jaijune
0 1 1 1

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

Add parentheses around the variable in the TOP clause

(select distinct top (@n) salary from emp_details order by salary desc

Here is the Books Online topic - http://msdn.microsoft.com/en-us/library/ms189463.aspx
more ▼

answered Aug 30, 2012 at 08:09 PM

KenJ gravatar image

KenJ
20.3k 1 4 12

Mr. ken, as per your instruction i added parenthesis, whereas still throws an error

create procedure proc_nthsalary (@n int) AS BEGIN select top 1 name, salary from emp_details where salary in(select distinct top (@n) salary from emp_details order by salary desc) order by salary END
Aug 31, 2012 at 02:59 AM jaijune

Pretty sure @Ken has it right. I tested it with this:

CREATE PROCEDURE proc_nthsalary (@n INT) AS BEGIN SELECT TOP 1 eph.PayFrequency, eph.Rate FROM HumanResources.EmployeePayHistory AS eph WHERE eph.Rate IN (SELECT DISTINCT TOP (@n) eph2.rate FROM HumanResources.EmployeePayHistory AS eph2 ORDER BY eph2.Rate DESC) ORDER BY eph.Rate END

Had to change stuff to use a table in Adventureworks, but it's the same as yours. Just needed the parens to fix it up.
Aug 31, 2012 at 11:34 AM Grant Fritchey ♦♦
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x19

asked: Aug 30, 2012 at 07:18 PM

Seen: 983 times

Last Updated: Aug 31, 2012 at 11:34 AM