Reputation: 5692
I am using C# and .NET 2.0. I created an OleDbDataAdapter to import a tab delimited file and in the SQL for one of my commands, I am adding 'LIMIT 1' to the end of the query.
string query = string.Format("SELECT DISTINCT *
FROM {0}
ORDER BY ZipCode
LIMIT 1", tableName);
I run the query and my program throws an exception, System.Data.OleDb.OleDbException: Syntax error (missing operator) in query expression 'ZipCode LIMIT 1'.
I am just running some tests now but I'm curious as to why it does not work or if there is an alternate way to limit the amount of rows returned. I am working with files that have tens of thousands of rows and sometimes might need to limit the amount of rows returned.
Upvotes: 2
Views: 5035
Reputation: 53729
As per my comment, the answer depends on the backend DB
For SQL Server you woult use TOP
string query = string.Format("SELECT DISTINCT TOP 1 *
FROM {0}
ORDER BY ZipCode", tableName);
For Oracle you would need to use ROWNUM in the where clause which queries against a sub-select. Something like this (untested)
string query = string.Format("SELECT * FROM (SELECT DISTINCT *
FROM {0}
ORDER BY ZipCode) where ROWNUM = 1", tableName);
And so on, depending on your backend DB.
Upvotes: 2