Incorrect syntax near '('. when using Select top (@param)

  • When i run this code below in Query Anayzer SQL Server 2000

    I get error

    Incorrect syntax near '('.

    Any ideas this seems to run on SQL SERVER 2005 is that right

    declare @nrows as int

    declare @term as varchar(50)

    --set @nrows = 2

    select @nrows = 25

    set @term = '%pat%'

    SELECT DISTINCT TOP(@nrows) FirstName FROM Employee WHERE FirstName like @term

    Thanks

  • This syntax was introduced in SQL2005.

    In SQL2000 use SET ROWCOUNT @param instead.

    _____________
    Code for TallyGenerator

  • Thanks i actually solved it using dynamic query like below:

    But now i want to concantenate the FirstName and the LastName

    but want to use the parameter @sqlrest

    but when i run the query i get error

    Incorrect syntax near ' + E.LastName'.

    declare @term as varchar(50)

    declare @MyCount as varchar(2)

    declare @sql as nvarchar(200)

    declare @sqlrest as nvarchar(200)

    set @sqlrest = 'E.FirstName + ' ' + E.LastName'

    set @term = 'pat'

    select @mycount = 6

    --My Solution to the first pboblem

    select @sql ='SELECT distinct top ' + @MyCount + ' FirstName FROM Employee

    WHERE Firstname LIKE '''+ '%' + @term + '%' + ''''

    --my problem now

    select @sql ='SELECT distinct top ' + @MyCount + ' ' + @sqlrest + ' FROM Employee E

    WHERE E.Firstname LIKE '''+ '%' + @term + '%' + ''''

    exec sp_executesql @sql

  • Always do

    [font="Courier New"]PRINT @SQL[/font]

    before you do

    [font="Courier New"]exec sp_executesql @sql[/font]

    _____________
    Code for TallyGenerator

  • I have tried that put nothing is printed out:crying:

  • Firstly, avoid dynamic SQL unless absolutely necessary. Someone gave you an answer that didn't need dyn sql - I suggest you use it.

    But...

    Your problem is that you have a syntax error in the SQL string that you eventually build.

    Try

    set @sqlrest = ', E.FirstName + ', ' + E.LastName'

    instead - you want a comma between the columns you are selecting. Note that there are two commas in the above text - one at the start and one in the middle.

  • Thanks and normally i won't go for dynamic sql.

    I want to use it for my Ajax part of my application which is an auto completebox

    But when i tried your suggestion i get error :-

    Incorrect syntax near ','.

    on the line

    set @sqlrest = ', E.FirstName + ', ' + E.LastName'

    Any ideas

  • [Code]select @sql ='SELECT distinct top ' + @MyCount + ' ' + @sqlrest + ' FROM Employee E

    WHERE E.Firstname LIKE '''+ '%' + @term + '%' + ''''

    print @sql

    [/Code]

    Is it what you tried?

    _____________
    Code for TallyGenerator

  • Yes and getting confused now..

    Still get the error:

    Incorrect syntax near ','.

    on line

    set @sqlrest = ', E.FirstName + ',' + E.LastName'

    declare @term as varchar(50)

    declare @MyCount as varchar(2)

    declare @sql as nvarchar(200)

    declare @sqlrest as nvarchar(200)

    set @sqlrest = ', E.FirstName + ',' + E.LastName'

    set @term = 'pat'

    select @mycount = 6

    select @sql ='SELECT distinct top ' + @MyCount + ' ' + @sqlrest + ' FROM Employee E WHERE E.Firstname LIKE '''+ '%' + @term + '%' + ''''

    PRINT @sql

  • Well i got it working

    this did it

    set @sqlrest = 'E.FirstName' +' , '+ 'E.LastName'

  • better u build the querry in ur application and run/ execute from there

  • Why are you prosposing this.

    I'm thinking of writing a Store proc for this..

    Any reason why thanks..

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply