cancel
Showing results for 
Search instead for 
Did you mean: 

Is the LIMIT statement supported in a query?

Former Member
6,617

Is the LIMIT statement supported? I'm trying to use the FuelPHP ORM library. It's generating a query that works with MySQL but doesn't seem to with SQLAnywhere. I've traced it down to the query below.

Working MySQL example:

SELECT * FROM users ORDER BY id ASC LIMIT 1

SQLAnywhere example that doesn't work:

SELECT * FROM EM.Users ORDER BY UserId ASC LIMIT 1

Similar SQLAnywhere example that does work (by removing LIMIT):

SELECT * FROM EM.Users ORDER BY UserId ASC

Accepted Solutions (1)

Accepted Solutions (1)

VolkerBarth
Contributor

LIMIT is available with v12 but has to be "allowed" explicitly. From the docs:

The following statement allows the LIMIT keyword to be recognized as a keyword:

SET OPTION PUBLIC reserved_keywords = 'LIMIT';

EDIT: As Reimer has noted, the doc sample is missing a period, it should read

SET OPTION PUBLIC.reserved_keywords = 'LIMIT';
Former Member
0 Kudos

Thanks. Where or how is this setting applied?

VolkerBarth
Contributor
0 Kudos

Use DBISQL (Interactive SQL) to connect as DBA to the database and run that statement. It is a public option, so it applies permantently to all further connections.

Former Member
0 Kudos

Thanks. I get "Syntax error near 'reserved_keywords' at line 1, column 19."

reimer_pods
Participant
0 Kudos

What SQLA version and build are you using (e.g. 12.0.1.3797)

Former Member
0 Kudos

12.0.1.3457

reimer_pods
Participant
0 Kudos

I had to double check twice until I saw the reason you've got an error: the period between PUBLIC and reserved_keywords was missing.

SET OPTION PUBLIC.reserved_keywords = 'LIMIT';
Former Member
0 Kudos

Thanks, this worked.

VolkerBarth
Contributor
0 Kudos

Oops, I had just copied from the docs, and there's no period - I've made a DCX comment:)

Answers (1)

Answers (1)

I believe the equivalent SQL/Anywhere syntax is

SELECT TOP 1 * FROM users ORDER BY id ASC
VolkerBarth
Contributor

Just to clarify:

The TOP / FIRST [START AT] clause is SQL Anywhere'c "classical" row limitation clause, available since v6 or v7, methinks. AFAIK it's not SQL standard, however supported by ASE and MS SQL Server, too.

v12 has added the LIMIT [OFFSET] clause, presumably for MySQL compatibility.