SQL Exception: ORA-00936: missing expression - java

I am running following query:
my query = insert into tbl_name (ID,name, address,...)" +" values (?,?,?)
then I am using query runner class to insert.
myQueryRunnerObj.insert("my query",
result set handler obj,
generated id,
'my name',
'my address',...);
After this I am getting following exception: Exception in thread "main" java.sql.SQLException: ORA-00936: missing expression and sometimes invalid number of arguments expecting 11 given 10
What could be the reason to get this exception?

Correct syntax is:
INSERT INTO dept (deptno, dname) VALUES (dept_seq.nextval, 1);
or
INSERT INTO dept (deptno, dname)
SELECT dept_seq.nextval, 2
FROM dual;

ORA-00936: missing expression
Cause: A required part of a clause or expression has been omitted. For example, a SELECT statement may have been entered without a list of columns or expressions or with an incomplete expression. This message is also issued in cases where a reserved word is misused, as in SELECT TABLE.
Action: Check the statement syntax and specify the missing component.
check this

Related

ERROR: column reference "key" is ambiguous

Seems I'm rusty at my sql and could use some help.
I'm running into an issue on the search query below:
SELECT COUNT(1) as totalITCount FROM it_table
JOIN i_table on i_table.key = it_table.item_key
WHERE
(key IN (select item_key from it_table where LOWER(it_table.resolution_feild) LIKE ? OR it_table.resolution_field = '' group by item_key) )
and the error coming from the operation is:
nested exception is org.postgresql.util.PSQLException: ERROR: column reference "key" is ambiguous.
I'm not sure what this means. I have another field present in both tables and in that case I "oddly" DON'T see this error. I'd think if anything having a field in two places would make the operation ambiguous, so I'm even more confused.
Thoughts?
Last line references key without specifying table.
(key IN....
should either be
(i_table.key IN....
or
(it_table.key IN....

PostgreSQL function : relation does not exist error

I am getting below error while calling Postgresql function with argument containing dot (.) operator.
SQL Error [42P01]: ERROR: relation "es.article_data" does not exist Where: PL/pgSQL function es.getrowcount(text) line 6 at EXECUTE....
Query: select es.getrowcount(schemawithtable :='es.article_data');
Function:
CREATE FUNCTION es.getrowcount (schemawithtable text)
RETURNS VARCHAR(50) AS $msg$
declare
msg VARCHAR(50);
total integer;
begin
execute format('select count(*) from %I where until_ts is null', schemawithtable) into total;
msg := CONCAT(total, ' records are there in ',schemawithtable);
RETURN msg;
END;
$msg$ LANGUAGE plpgsql;
Error traces from DBeaver:
org.jkiss.dbeaver.model.sql.DBSQLException: SQL Error [42P01]: ERROR: relation "es.article_data" does not exist
Where: PL/pgSQL function es.getrowcount(text) line 6 at EXECUTE
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.executeStatement(JDBCStatementImpl.java:134)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.executeStatement(SQLQueryJob.java:487)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.lambda$0(SQLQueryJob.java:424)
at org.jkiss.dbeaver.model.exec.DBExecUtils.tryExecuteRecover(DBExecUtils.java:164)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.executeSingleQuery(SQLQueryJob.java:416)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.extractData(SQLQueryJob.java:774)
at org.jkiss.dbeaver.ui.editors.sql.SQLEditor$QueryResultsContainer.readData(SQLEditor.java:2914)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetJobDataRead.lambda$0(ResultSetJobDataRead.java:110)
at org.jkiss.dbeaver.model.exec.DBExecUtils.tryExecuteRecover(DBExecUtils.java:164)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetJobDataRead.run(ResultSetJobDataRead.java:108)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetViewer$17.run(ResultSetViewer.java:3421)
at org.jkiss.dbeaver.model.runtime.AbstractJob.run(AbstractJob.java:103)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: org.postgresql.util.PSQLException: ERROR: relation "es.article_data" does not exist
Where: PL/pgSQL function es.getrowcount(text) line 6 at EXECUTE
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2183)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:308)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:307)
at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:293)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:270)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:266)
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.execute(JDBCStatementImpl.java:338)
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.executeStatement(JDBCStatementImpl.java:131)
... 12 more
I have figure out the workaround by passing schema name separately.
Working Query: select es.getrowcount(schemaname :='es',tablename :='article_data');
CREATE FUNCTION es.getrowcount (schemaname text, tablename text)
...
begin
execute format('select count(*) from %I.%I where until_ts is null',schemaname, tablename) into total;
...
$msg$ LANGUAGE plpgsql;
Versions:
PostgreSQL - 10.12
DBeaver (client) - 6.3.5
But I want to know why it is giving an error for argument containing dot (.) operator ?
When you are creating your query with format and %I. It is creating your query like below
With Single Parameter:
select count(*) from "es.article_data" where until_ts is null
With Double Parameter:
select count(*) from "es"."article_data" where until_ts is null
In first case it will show error because you can not use table name with schema like this. But in second case it worked perfectly as this is the right way to use naming convention.
If you want to use the first method only. you should use %s instead of %Ilike below
CREATE FUNCTION es.getrowcount (schemawithtable text)
RETURNS VARCHAR(50) AS $msg$
declare
msg VARCHAR(50);
total integer;
begin
execute format('select count(*) from %s where until_ts is null', schemawithtable) into total;
msg := CONCAT(total, ' records are there in ',tablename);
RETURN msg;
END;
$msg$ LANGUAGE plpgsql;
NOTE : This will work properly if your table names and schema names are in lower case only

The following query causes a MySQLSyntaxErrorException - what is wrong with my query?

The following exception is thrown when ever I execute the following query
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '-name, cpy-address, cpy-contact) VALUES('nauman','ahmad18',12)' at line 1
Query which is causing exception
String query="insert into company(cpy-name, cpy-address, cpy-contact)VALUES(?,?,?)";
Connection con=DataAccessLayer.getConnection();
PreparedStatement stat=con.prepareStatement(query);
stat.setString(1, cname);
stat.setString(2, caddress);
stat.setInt(3,x );
int rowsAffected = stat.executeUpdate();
You cannot use - in SQL queries. Escape the column names using ` back quote or back tick characters.
insert into company(`cpy-name`, `cpy-address`, `cpy-contact`)VALUES(?,?,?)
On another note, if the database is in your control, change to column names to use '_' rather '-'. Having illegal characters and quoting them is not a good practice.
Hope this helps!
As #uday said you should change column name or use
" ` "
You can't use dash - symbol in SQL query in case of column name so that rename your column name or use Escape ` both side of column name and table name.

"Invalid Column id" error when counting unique value in Java

I am using the following sql query in my java program:
SELECT COUNT(*) FROM event WHERE externaleventid ='1256294';
But I have an error as:
Invalid Column id.
Same query works fine in SQL Developer.
For some reason you are attempting to get a column named "externaleventid" from the query, but only "count(*)" is available. You shouldn't try to get back the where clause bind variables from the result set, you should get the actual data back from the result set, by column index or by column name.
Try rs.getInt(1) to get the data from the first column. Or, you can alias the column in the query, e.g. SELECT count(*) cnt FROM..., and you can refer to it by the aliased column name: rs.getInt("cnt").

Insert into statement with parentheses in column names

I am use Java to insert into a MSSQL database. I have column names with parentheses in them like Duration_(Mins).
I am trying to execute a statement like:
INSERT INTO mytable (Duration_(Mins)) VALUES (?)
and I am getting the following error:
Exception in thread "main" java.sql.SQLException: Incorrect syntax near '('
So I am guessing I need some way to "escape" the bracket?
For MS-SQL Server, this should work:
INSERT INTO mytable ([Duration_(Mins)]) VALUES (?)
For details, refer this link:
http://msdn.microsoft.com/en-us/library/ms132046.aspx#DelimitedIdentifiers

Categories

Resources