Good evening
i have problem about a query of insert if not exist i have do it in another query and it worked
but now i have an array of class java Service
so i want test about the id : VLAN
because i execut every time and i want that not insert many time if it exist already on table sql
This is my query but they give me an error about it
for(Service srv:service){
srvDataLst.add(srv.getvlan());
PreparedStatement pst=conn.prepareStatement(
"INSERT INTO tout (VLAN,client,JR,vrf,address) "
+ "VALUES(?,?,?,?,?) SELECT DISTINCT "
+ "'"+srv.getvlan()+"' FROM dual WHERE NOT EXISTS("
+ "SELECT * FROM tout WHERE 'VLAN'='"+srv.getvlan()+"') ");
pst.setInt(1,srv.getvlan());
pst.setString(2,convertNullToEmptyString(srv.getdesc()));
pst.setString(3,convertNullToEmptyString(srv.getjr()));
pst.setString(4,convertNullToEmptyString(srv.getvrf()));
pst.setString(5,convertNullToEmptyString(srv.getaddress()));
pst.executeUpdate();
Thank you for help
The error is:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 DISTINCT '20' FROM dual WHERE NOT EXISTS(SELECT * FROM tout
WHERE 'VLAN'=' at line 1
It's either insert into ... values(...) or insert into ... select ..., you can't use both in the same statement.
Related
I'm having issues when using this:
WARN com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'WHERE Accounts.UUID = WarAccounts.UUIDAND Accounts.UUID = 'c7a00fe7-826d-46da-b4' at line 1
Here is the SQL:
SELECT
*
FROM
Accounts,
WarAccounts
WHERE
Accounts.UUID = WarAccounts.UUID
AND Accounts.UUID = ?
(Using prepared statements)
I'm very confused as to what is wrong with this to cause that issue, I've googled it but cannot find anything since this seems to be the correct way.
Server type: MySQL
Server version: 5.5.58-0ubuntu0.14.04.1 - (Ubuntu)
Wild ass guess:
In C# i would do this:
string query = "SELECT *" +
"FROM MYTABLE" +
"WHERE 1=1"
..to get your error..
Concat the string and you'll see the problem:
"SELECT *FROM MYTABLEWHERE 1=1"
The concatination should be:
string query = "SELECT * " +
"FROM MYTABLE " +
"WHERE 1=1 "
(see the spaces at the end of every line?)
How to execute SQL Server query into JDBC like this SQL?
select * into bk_table from existing_table
Expected: Above query should be executed through JDBC.
Actual: Query is not working through JDBC in any way.I tried following two ways but nothing happens or affected in DBMS.
String sql="select * into bk_table from existing_table";
//tried way 1-> result: false
statement.execute(sql);
//tried way 2-> result: -1
statement.executeUpdate(sql);
What to do now?
You need to use an insert-select statement. It can be called with executeUpdate:
String sql = "INSERT INTO bk_table SELECT * FROM existing_table";
statement.executeUpdate(sql);
To select data from old table, "SELECT * FROM bk_table";
To insert the selected data from old to existing table, "INSERT INTO existing_table SELECT * FROM bk_table";
I need delete from table on operation of same table .JPA query is
DELETE FROM com.model.ElectricityLedgerEntity a
Where a.elLedgerid IN
(SELECT P.elLedgerid FROM
(SELECT MAX(b.elLedgerid)
FROM com.model.ElectricityLedgerEntity b
WHERE b.accountId='24' and b.ledgerType='Electricity Ledger' and b.postType='ARREARS') P );
I got this error:
with root cause org.hibernate.hql.ast.QuerySyntaxException: unexpected
token: ( near line 1, column 109 [DELETE FROM
com.bcits.bfm.model.ElectricityLedgerEntity a Where a.elLedgerid IN (
SELECT P.elLedgerid FROM ( SELECT MAX(b.elLedgerid) FROM
com.bcits.ElectricityLedgerEntity b WHERE b.accountId='24'
and b.ledgerType='Electricity Ledger' and b.postType='ARREARS') P ) ]
at
org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at
org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at
org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
at
org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)
Same query is running on mysql terminal ,but this is not working with jpa .Can any one tell me how i can write this query using jpa .
I don't understand why do you use Pbefore the last parenthesis...
The following code is not enough ?
DELETE FROM com.model.ElectricityLedgerEntity a
Where a.elLedgerid IN
(SELECT MAX(b.elLedgerid)
FROM com.model.ElectricityLedgerEntity b
WHERE b.accountId='24' and b.ledgerType='Electricity Ledger' and
b.postType='ARREARS')
Edit for bypassing mysql subquery limitations :
The new error java.sql.SQLException: You can't specify target table 'LEDGER' for update in FROM clause
is known in mysql when you use it with JPA. It's one MySQL limitation.
A recent stackoverflow question about it
In brief, you cannot "directly" updated/deleted a table that you query in a select clause
Now I understand why your original query did multiple subqueries seemingly not necessary (while it was useful for mysql) and had a "special" syntax.
I don't know tricks to solve this problem in JPA (I don't use the MySQL DBMS for a long time now).
At your place, I would do two queries. The first where you select the expected max elLedgerid and the second where you could delete line(s) with the id retrieved in the previous query.
You should not have performance issues if your sql model is well designed, the sql indexes well placed and the time to access to the database is correct.
You cannot do this in a single query with Hibernate. If you want to delete the max row(s) with Hibernate you will have to do so in two steps. First, you can find the max entry, then you can delete using that value in the WHERE clause.
But the query you wrote should actually run as a raw MySQL query. So why don't you try executing that query as a raw query:
String sql = "DELETE FROM com.model.ElectricityLedgerEntity a " +
"WHERE a.elLedgerid IN (SELECT P.elLedgerid FROM " +
"(SELECT MAX(b.elLedgerid) FROM com.model.ElectricityLedgerEntity b " +
"WHERE b.accountId = :account_id AND b.ledgerType = :ledger_type AND " +
" b.postType = :post_type) P );";
Query query = session.createSQLQuery(sql);
query.setParameter("account_id", "24");
query.setParameter("ledger_type", "Electricity Ledger");
query.setParameter("post_type", "ARREARS");
Just want to extend existing answer:
In brief, you cannot "directly" updated/deleted a table that you query in a select clause
This was lifted with starting from MariaDB 10.3.1:
Same Source and Target Table
Until MariaDB 10.3.1, deleting from a table with the same source and target was not possible. From MariaDB 10.3.1, this is now possible. For example:
DELETE FROM t1 WHERE c1 IN (SELECT b.c1 FROM t1 b WHERE b.c2=0);
I want to run the query: "DESCRIBE table_name;"
statement = this.connection.createStatement();
ResultSet rset = statement.executeQuery("DESCRIBE table_name");
and I got this error:
" java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL statement"
what is the problem?
DESC[RIBE] is a SQL*Plus command, not a SQL statement. The DESC command queries the Oracle data dictionary, something like:
select COLUMN_NAME, DATA_TYPE
from USER_TAB_COLUMNS
where TABLE_NAME = 'YOUR_TABLE'
DESC is a SQL*Plus command. SO, you cannot use it via JDBC/ODBC.
An alternative can be like this below.
select RPAD(COLUMN_NAME,30)||' '||DATA_TYPE||'('||DATA_LENGTH||')' as descr
FROM all_tab_cols
WHERE TABLE_NAME = UPPER('YOUR_TABLE') and owner=UPPER('SCHEMA_NAME');
all_tab_cols is a data dictionary table(view) which contains the table metadata
Oracle's Reference
describe user2.flights;
Here user2 is database name and flights is table name. Try this.
Or use next query
select *
from user_tab_columns
where table_name = 'MY_TABLE'
order by column_id;
Use this query.
column_id is the "order" of the column in the table.
You should ensure that 'MY_TABLE' is capitalised unless you've been adding tables with casing ( a bad idea ) in which case you need to use something like = "MyTable"
Good evening
i have problem about a query of insert if not exist i have do it in another query and it worked
but now i have an array of class java Service
so i want test about the id : VLAN
because i execut every time and i want that not insert many time if it exist already on table sql
This is my query but they give me an error about it
for(Service srv:service){
srvDataLst.add(srv.getvlan());
PreparedStatement pst=conn.prepareStatement(
"INSERT INTO tout (VLAN,client,JR,vrf,address) "
+ "VALUES(?,?,?,?,?) SELECT DISTINCT "
+ "'"+srv.getvlan()+"' FROM dual WHERE NOT EXISTS("
+ "SELECT * FROM tout WHERE 'VLAN'='"+srv.getvlan()+"') ");
pst.setInt(1,srv.getvlan());
pst.setString(2,convertNullToEmptyString(srv.getdesc()));
pst.setString(3,convertNullToEmptyString(srv.getjr()));
pst.setString(4,convertNullToEmptyString(srv.getvrf()));
pst.setString(5,convertNullToEmptyString(srv.getaddress()));
pst.executeUpdate();
Thank you for help
The error is:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 DISTINCT '20' FROM dual WHERE NOT EXISTS(SELECT * FROM tout
WHERE 'VLAN'=' at line 1
It's either insert into ... values(...) or insert into ... select ..., you can't use both in the same statement.