SQL Limit error with JavaDB - sql

Firstly, I'm new to SQL - not so new to Java anymore.
Secondly, I'm using NetBeans 7.2.1 with JavaDB and when I'm trying to run a SQL query, such as:
SELECT * FROM mydatabase ORDER BY colname DESC LIMIT 0,1
and try to execute the commend, the console displays an error, namely:
Error code -1, SQL state 42X01: Syntax error: Encountered "limit" at line 1, column 55.
Any ideas on what I can do? Do you see something wrong with my query code?

Please consult the Derby FAQ at http://db.apache.org/derby/faq.html#limit

You can easily do it like this.
SELECT *
FROM mydatabase
ORDER BY colname DESC
FETCH FIRST 10 ROWS ONLY;
Reference

Related

Understanding of difference between MariaDB and SQL Server for a query

I am using MS SQL Server Version 2008 and MariaDB 10.5.
My table and data :
Following is the query i am running on both the databases.
select distinct distributor from market order by city
Found this query is failing in MS SQL Server database with error
SQL Error [145] [S0001]: ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
Understood the reason why it failing
Reason for - ORDER BY items must appear in the select list if SELECT DISTINCT is specified
when i ran the same query run with MariaDB it ran successful without any error and gave me below output.
So i have doubt why MariaDB is behave different here? ideally it should fail right?
Thanks in advance.
Please try this query
Select distinct col from (
Select col from table order by col
)

SQL Select INTO Query

I am trying to running the following command to copy a table to another database and it is saying incorrect syntax near "tbl_SecurityNamespace". Any ideas? TIA
select * into ForMSDB tbl_SecurityNamespace
from Tfs_configuration
go
If it's another database in the same environment:
select * into ForMSDB..tbl_SecurityNamespace
from Tfs_configuration
This should be as
select * into ForMSDB..tbl_SecurityNamespace
from Tfs_configuration;

using function in google bigquery

I am trying to use sql function in my BigQuery query :
SELECT FORMAT("%T", NET.HOST(resolved_urls.url)) AS host, FROM [tableName] LIMIT 1000
But I get the following error:
Error: 2.15 - 2.55: Unrecognized function format
I am getting these error for every sql function i am trying to use.
Any Idea ? thank you
Those new functions are supported by BigQuery Standard SQL only
Try below
#standardSQL
SELECT FORMAT("%T", NET.HOST(resolved_urls.url)) AS host, FROM tableName LIMIT 1000
Also note: in Standard SQL you use `tableName` and not [tableName]

how to get databases names in sqlite3?

I'm trying to valid a SQL injection challenge on root-me.org. I want to retrieve the databases names with the command
1' SELECT * FROM my_db.sqlite_master WHERE type='table';
But i get the following error: SQLite3::query(): Unable to prepare statement: 1, near "SELECT"
Any idea ?
SELECT name
FROM master.dbo.sysdatabases
Possible duplicate of
sqlite3 - how to list out database name using .databases command?

Error trying to Select x rows from DB2 (V4R5M0) via sql server linked server using OPENQUERY

I have a Linked Server from SQL Server 2008 R2, to a DB2 Database (V4R5M0) using OLE DB provider "IBMDA400"
Linked Server Detials
EXEC master.dbo.sp_addlinkedserver
#server = N'JTEST', #srvproduct=N'IBM OLE DB Provider for DB2',
#provider=N'IBMDA400', #datasrc=N'TestName'
This works fine:
SELECT * FROM OPENQUERY(JTEST, 'Select * from QSYS2.SYSCOLUMNS')
But the following statement produces an error:
SELECT * FROM OPENQUERY(JTEST, 'Select * from QSYS2.SYSCOLUMNS FETCH FIRST 10 ROWS ONLY')
Error
LE DB provider "IBMDA400" for linked server "JTEST" returned message
"SQL0199: Keyword FETCH not expected. Valid tokens: FOR WITH ORDER
UNION OPTIMIZE. Cause . . . . . : The keyword FETCH was not expected
here. A syntax error was detected at keyword FETCH. The partial list
of valid tokens is FOR WITH ORDER UNION OPTIMIZE. This list assumes
that the statement is correct up to the unexpected keyword. The error
may be earlier in the statement but the syntax of the statement seems
to be valid up to this point. Recovery . . . : Examine the SQL
statement in the area of the specified keyword. A colon or SQL
delimiter may be missing. SQL requires reserved words to be delimited
when they are used as a name. Correct the SQL statement and try the
request again.". Msg 7321, Level 16, State 2, Line 1 An error occurred
while preparing the query "Select * from QSYS2.SYSCOLUMNS FETCH FIRST
10 ROWS ONLY" for execution against OLE DB provider "IBMDA400" for
linked server "JTEST".
I think it's because FETCH FIRST X ROWS ONLY is not supported in this version of DB2? But is there any way of selecting only a limited record set in this version?
Wouldn't FETCH FIRST need an ORDER BY? Otherwise, which 10 rows would it get?
As far as I can see in the DB2 SQL docs, it is supported on current and older versions, although it doesn't state (and it not clear) if ORDER BY is mandatory with FETCH FIRST
Fetch first clause work only from V5R1 OS400 version.
V4R5M0 is too old
There's a workaround:
select * from (
SELECT syscolumns.* , row_number() over() as nre FROM syscolumns
ORDER BY COLUMN_NAME ) as columns
where nre<10
You can try it
DEpe