SQL error while using limit keyword in Derby - sql

I have used limit keyword as:
"select * from empl3 limit "+4
But I am getting the error as:
Syntax error: Encountered "4" at line 1, column 27.
I am using Derby database.

As documented in the manual there is no LIMIT clause in Derby.
Derby uses the SQL standard for limiting the number of rows:
select *
from empl3
fetch first 4 rows only;

I believe the +4 after the select statement is giving you the error. You should return all of your data necessary for you page select * from empl3, but handle the paging in your page itself.

The key is in your words I am using derby database - see Does Derby support a LIMIT command.
You can either use the workaround suggested in the linked FAQ, or implement paging on your own, as suggested by WEI_DBA.

Related

Select from a SQL table starting with a certain index?

I'm new to SQL (using postgreSQL) and I've written a java program that selects from a large table and performs a few functions. The problem is that when I run the program I get a java OutOfMemoryError because the table is simply too big. I know that I can select from the beginning of the table using the LIMIT operator, but is there a way I can start the selection from a certain index where I left off with the LIMIT command? Thanks!
There is offset option in Postgres as in:
select from table
offset 50
limit 50
For mysql you can use the follwoing approaches:
SELECT * FROM table LIMIT {offset}, row_count
SELECT * FROM table WHERE id > {max_id_from_the previous_selection} LIMIT row_count. First max_id_from_the previous_selection = 0.
This is actually something that the jdbc driver will handle for you transparently. You can actually stream the result set instead of loading it all into memory at once. To do this in MySQL, you need to follow the instructions here: http://javaquirks.blogspot.com/2007/12/mysql-streaming-result-set.html
Basically when you create you call connection.prepareStatement you need to pass ResultSet.TYPE_FORWARD_ONLY and ResultSet.CONCUR_READ_ONLY as the second and third parameters, then call setFetchSize(Integer.MIN_VALUE) on your PreparedStatement object.
There are similar instructions for doing this with other databases which I could iterate if needed.
EDIT: now we know you need instructions for PostgreSQL. Follow the instructions here: How to read all rows from huge table?

How do I use TOP instead of them LIMIT keyword in CakePHP with MSSQL?

CakePHP constructs all its queries ending with "LIMIT X", which is the MySQL syntax, not plain SQL and Microsoft SQL Server won't accept it. Even if you're using the ODBC driver it'll generate queries like:
SELECT "Content"."id" AS "Content_dot_id",
"Content"."name" AS "Content_dot_name"
FROM "contents" AS "Content" WHERE (1=1) LIMIT 1
Which gives:
37000: [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'LIMIT'.
Because you want:
SELECT TOP 1 "Content"."id" AS "Content_dot_id",
"Content"."name" AS "Content_dot_name"
FROM "contents" AS "Content" WHERE (1=1)
The code that takes on the "LIMIT" is in dbo_source.php rather than one of the DB-specific files (I think it's function limit($limit, $offset = null)).
Is there some setting I can toggle to switch to the TOP syntax instead of LIMIT?
The resolution that I found from a Google cached version of an old CakePHP trouble ticket was "Use the MSSQL dbo".
So what you need to do is to configure CakePHP to use the MSSql Driver and it will start using the "TOP" syntax. Here is a blog post talking about how to do that: http://www.johnmckinzie.com/2007/06/29/sql-server-2005-and-cakephp/

Hibernate setMaxResults() not working for Sybase database query

Either of the following two approaches to executing a simple Hibernate query with limited results do not work in Sybase. They both result in a SybSQLException: Incorrect syntax near '#p0'.
Query q = session.createQuery( "from Record" );
q.setMaxResults( 50 );
q.list();
or
Criteria criteria = session.createCriteria( Record.class );
criteria.setMaxResults( 50 );
criteria.list();
It appears the actual SQL generated in both of these cases looks like...
select top ? record_id, etc...
and Sybase is balking at the ?, which Hibernate is not filling in with the value 50 (this is my guess). I've searched everywhere and while others have encountered a similar error, it was not due to attempting to limit the results.
I can execute a direct SQL statement such as 'select top 50 from Record' and it works perfectly, so I know my version of Sybase supports the syntax.
I'm using Hibernate 3.2 and Sybase ASE 15.0.2
Perhaps you configured Hibernate to use a wrong SQL dialect.
It looks like HSQLDialect is the only dialect that can produce limit ? ?, and it's definitely a wrong choice for Sybase.
See also:
3.4.1. SQL Dialects
Try putting setFirstResult(1) as well like:
Criteria criteria = session.createCriteria(Record.class);
criteria.setFirstResult(1);
criteria.setMaxResults(50);
criteria.list();
Do you still get the same error?
setMaxResults() is typically used together with setFirstResult() to implement paging. Eg. the first query returns records 1 to 1000, the second query returns 1001 to 2000, and so on. Try using together.
setFetchSize() control how many rows are fetched at a time by the JDBC driver.(if implemented) So, if you for example have setMaxResults(1000) and setFetchSize(100) the query will return no more than 1000 rows, and will do so in batches of 100 rows at a time.
You can use createSQLQuery option which works with TOP.

MySQL Subquery is failing on MySQL 4.0 with invalid syntax error

I'm running a pretty basic subquery on MySQL 4.0.30.
My goal is to get the user permissions from the mysql.user table for any user with grants on a specific database, as noted in the mysql.db table. The query looks like this:
mysql> select * from mysql.user where User IN
(select User from mysql.db where Db='db_name')\G
As you can see, it's pretty basic and follows the subquery syntax in the MySQL manual. However, when I execute that, it errors with the following response:
ERROR 1064: You have an error in your SQL syntax. Check the manual that
corresponds to your MySQL server version for the right syntax to use near 'select
User from mysql.db where Db='db_name')' at line 1
I also tried the command with = ANY instead of IN. I've run the same query on 4.1 and 5.0 versions of MySQL. Any help or insight on this is appreciated. Thanks
Ok, so it turns out I just didn't check the manual closely enough:
Starting with MySQL 4.1, all subquery forms and operations that the SQL standard requires are supported, as well as a few features that are MySQL-specific.
http://dev.mysql.com/doc/refman/4.1/en/subqueries.html

Which database invented the "limit" SQL query syntax?

MySQL has a nice feature (although non standard) which allow to query resultsets' limit, offset as
SELECT * FROM TABLE LIMIT M, N;
Is it created by MySQL? or Postgres?
According to Wikipedia, Rasmus Lerdorf (the original creator of PHP) first used the "LIMIT x" syntax in the mSQL database:
He has contributed to the Apache HTTP Server and he also came up with the LIMIT clause and added it to the mSQL Database in 1995. It is the origin of the LIMIT clauses found in MySQL and PostgreSQL.
Limiting the result set is now also standardized, but with a more verbose syntax:
SELECT *
FROM T
FETCH FIRST 10 ROWS ONLY
Postgres added the LIMIT syntax in v6.5, released on June 9th, 1999.
Based on the documentation, MySQL had LIMIT syntax starting at v3.23 (production release Jan, 2001). But the docs in the URL are for 4.1, which wasn't released until 2004.
SQL Server didn't have TOP until SQL Server 2000, shipping in late 2000.
Oracle has had ROWNUM since Oracle 6, released in 1988. Scarier still, is that it can perform better in cases than ROW_NUMBER!
MySQL copied it from mSQL, which Rasmus Lerdorf lays claim to implementing:
http://itc.conversationsnetwork.org/shows/detail3298.html
That's not to say mSQL was the first, but it looks like earliest of those mentioned so far.
Between PostgreSQL and MySQL, PostgreSQL copied the syntax LIMIT from MySQL (in v6.5), and added the OFFSET syntax (it may be that that was copied as well, but I think mysql only had the comma-syntax back then). It was then (7.2) changed to only allow "LIMIT foo OFFSET bar" because the MySQL syntax was unclear.
I know that Rdb (originally a DEC Corporation product, now available from Oracle) had LIMIT TO n ROWS back in 1991.
Share and enjoy.