Select Top @Variable * from table error


This is a common query we can see over the net. I am posting this just because today I saw this query again in DotnetSpider. This is because of simple mistake in the query and can be rectified by just adding braces around the variable.

If you execute below code you will get an error "Incorrect syntax near '@topNum'."


declare @topNum as int
set @topNum = 10

select top @topNum * from Person.Contact


Just change above query like below and see the output!!!


declare @topNum as int
set @topNum = 10

select top (@topNum) * from Person.Contact


Article by Asheej T K
Thanks and Regards Asheej T K Dotnet Galaxy

Follow Asheej T K or read 33 articles authored by Asheej T K

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: