0

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Scott Selby
  • 9,420
  • 12
  • 57
  • 96
  • I'm likely wrong on this since I'm not very familiar with SQL Server, but I don't think that + is concatenation. – Corbin Jun 07 '12 at 02:55
  • Is `zipcode` really your table name, or the name of the column you want to retrieve? Basic query format is `SELECT column_name` (or `*` for all columns) ` FROM table_name WHERE ...` – bfavaretto Jun 07 '12 at 02:55
  • @Corbin, `+` does concatenate string in SQL Server. – bfavaretto Jun 07 '12 at 02:56
  • @bfavaretto Ah... Was worth a shot :) – Corbin Jun 07 '12 at 02:56
  • zipcode is the correct table name – Scott Selby Jun 07 '12 at 02:57
  • so what does the the error say ? Are you sure the computer has gas in it? Is it plugged in ? Maybe there's a bug... Seriously, you need to provide more inormation about what is wrong than just "both do not work" ... Are you getting 11 records instead of 10 ? or are you getting records from the wrong server ? or from the wrong table? no records at all? – Charles Bretana Jun 07 '12 at 02:58
  • 5
    TOP 10 is SQL Server specific. LIMIT 10 works on MySQL (maybe also Oracle?). Which DB are you using? What error or incorrect result do you get? – Eric J. Jun 07 '12 at 02:58
  • MSSQL "Incorrect syntax near 'LIMIT'." – Scott Selby Jun 07 '12 at 03:00
  • 3
    SELECT TOP 10 * FROM zipcode WHERE city LIKE @prefixtext + '%' would definitely work in MSSQL, limit 10 is not MSSQL keyword. What is the error here? – fenix2222 Jun 07 '12 at 03:01
  • ok, error with 'LIMIT 10' - SELECT TOP 10 returning nothing , has to be bad query , thank you – Scott Selby Jun 07 '12 at 03:03
  • try replacing star with actual column name you want to return. When you remove TOP 10 from query does it work? – fenix2222 Jun 07 '12 at 03:04
  • 1
    Could you show the code where you populate the `SqlCommand` object? – rikitikitik Jun 07 '12 at 03:11

3 Answers3

2

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
Phil
  • 498
  • 6
  • 14
0
declare @like varchar(50)

set @like = @prefixtext + '%';

SELECT TOP 10 * FROM zipcode  WHERE city LIKE @like
Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82
vijay
  • 58
  • 5
-1
Select * from zipcode where city like @prefixtext + '%'
j0k
  • 22,600
  • 28
  • 79
  • 90
vivek
  • 19
  • 3