Search code examples
c#sql-serversqlcommandselectcommand

SELECT SqlCommand


I know this is a simple question, but I can't get it to work.

This is my query in my SqlCommand:

SELECT * FROM zipcode  WHERE city LIKE @prefixtext + '%' ;

I only want 10 results, every other answer suggests

SELECT TOP 10 * FROM zipcode  WHERE city LIKE @prefixtext + '%' ;
SELECT * FROM zipcode  WHERE city LIKE @prefixtext + '%'  LIMIT 10 ;

both do not work


Solution

  • I believe these are all correct.

    Oracle:

    select * from zipcode where city like @prefixtext + '%' and rownum <=10
    

    SQL Server/Sybase:

    select top 10 * from zipcode where city like @prefixtext + '%'
    

    DB2/PostgreSQL:

    select * from zipcode where city like @prefixtext || '%' fetch first 10 rows only
    

    MySQL:

    select * from zipcode where city like @prefixtext + '%' limit 10