How to transfer postgres script to hsqldb script? - java

I have a script, which I run successfully in postgres db, however it was failed when I run in hsqldb.
Can someone help me change this sql to make it work for both HSQLDB and Postgres DB?
Below is my script:
UPDATE tableA af
SET columnA2 = b.columnB2
from
( select columnB1, columnB2 from.....) as b
Where af.columnA1 = b.columnB1;
This throws the following exception when I run it in hsqldb:
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or
object not found: b.columnB2 Caused by: org.hsqldb.HsqlException: user
lacks privilege or object not found: b.columnB2
Thanks.
Updated:
I created another view to make my question more clear.
table_A has 2 columns: company_code, company_number
and view_B has 2 columns: company_code, company_number_correct_answer
table_A has 10000 rows, and view_B has only 2 rows.
What I want is updating 2 record in table_A, with company_code existing in view_B and set table_A.company_number = view_B.company_number_correct_answer

In standard SQL, the FROM clause (or JOIN or similar things) is not valid for the UPDATE statement.
If you want an UPDATE statement that works across multiple database products, you will have to use a co-related sub-query:
update table_a
set columna2 = (select columnb2
from table_b
where table_b.columnb1 = table_a.columna1)
where exists (select *
from table_b
where table_b.columnb1 = table_a.columna1);
Note that this requires that table_b.columnb1 is a unique or primary key, otherwise the sub-query would return more than one row which will lead to an error).

You can also use MERGE in HSQLDB
MERGE INTO tableA af
USING (select columnB1, columnB2 from.....) as b
ON af.columnA1 = b.columnB1
WHEN MATCHED THEN
UPDATE SET af.columnA2 = b.columnB2

Related

Doing an INSERT ALL using JDBC in ORACLE

I am using Oracle Database 12c Enterprise Edition
I want to insert records into 2 tables say TABLE1 and TABLE2 back to back using JDBC. These 2 tables have a primary key and foreign key relationship based on a common column say ID_COLUMN
I am planing to use the following single query and fire it via my Java application via JDBC:
insert all
into TABLE1 (ID_COLUMN,COL2,COL3,COL4,COL5,COL6) values(?,?,?,?,?,?)
into TABLE2 (COL1_1,COL_1_2,COL_1_3,ID_COLUMN) values('blah',42,'rubbish',
select test_ctrl.seq_test_id.nextval FROM dual)
select * from dual;
My basic requirement is that I need to INSERT TABLE2 with the latest ID_COLUMN from TABLE1 from my current session.
I know the usage of select test_ctrl.seq_test_id.nextval FROM dual in the INSERT ALL statement is not correct. But it being Oracle I cant use SCOPE_IDENTITY()
Please suggest how can I make this query work
"But it being Oracle I cant use SCOPE_IDENTITY()"
Ah but you can. In Oracle 12c they introduced identity columns: these are a special variant of virtual columns.
create table my_table (
id number generated always as identity
....
, constraint my_table_pk primary key (id)
Find out more.
I seem to have found out the answer to my question.Modified the query like this.Please take note of edme_ctrl.seq_ts_annotation_id.nextval and edme_ctrl.seq_ts_annotation_id.currval
INSERT ALL INTO "SPI7CG_CgNvI".X$ANNOTATIONS(ANNOTATION_ID,CATEGORY,REASON,COMMENTS,AUTHOR,ADJUSTMENT_TYPE,ADJUSTMENT_VALUE) VALUES (edme_ctrl.seq_ts_annotation_id.nextval, '51','33','Test Bulk Insert','kshiam','A',10) INTO "SPI7CG_CgNvI".X$DATA_ANNOTATIONS(ANNOTATION_ID, TABLE_NAME, TABLE_ROW_ID,COLUMN_NAME) VALUES (edme_ctrl.seq_ts_annotation_id.currval,'W$XXXXXGNVBSNSSNDCTRSSR007',164921155,'IVXXXXXGNVBXWGSQDTWQRTR0003') select * from dual

Merge and When Matched query giving an error sql server

I have a query which I am trying to test. The query should update the data if it finds data in the table with existing primary key. If it doesn't then insert into the table.
The Primary key is of type int and in the properties I can see Identity is set to "True" which I assume it means that it will automatically set the new id for the primary if it is inserted.
MERGE INTO Test_table t
USING (SELECT 461232 ID,'Test1-data' Fascia FROM Test_table) s
ON (t.ID = s.ID)
WHEN MATCHED THEN
UPDATE SET t.Fascia = s.Fascia
WHEN NOT MATCHED THEN
INSERT (Fascia)
VALUES (s.Fascia);
The issue here is this query doesn't work and it never inserts the data or updates. Also, query gets compiled and I don't get any compilation error
Also the reason I want this query is to work because then I will use Java prepared statement to query the database so I am assuming I can do
SELECT ? ID,? Fascia FROM Test_table
So that I can pass the values with set methods in java.
Please let me know if there is something wrong in my query.
You are selecting from the target table as your source.
You either need to remove your FROM Test_table or have at least 1 row in Test_table prior to your merge.
rextester demo: http://rextester.com/XROJD28508
MERGE INTO Test_table t
USING (SELECT 461232 ID,'Test1-data' Fascia --FROM Test_table
) s
ON (t.ID = s.ID)
WHEN MATCHED THEN
UPDATE SET t.Fascia = s.Fascia
WHEN NOT MATCHED THEN
INSERT (Fascia)
VALUES (s.Fascia);

How to update 2 tables using one query in JPA native query?

I have 2 tables TABLE1 and TABLE2.Table1 is having name and Table2 is having email and Phone.
To get the name,email and phone,I query as below
query = entityManagerUtil.createNativeQuery("select s.Name,c.Phone1,c.Email1 from Table1 s,Table2 c where c.id= s.NodeID and s.NodeID =21")
Now my next requirement is to update name,email and phone.As these parameters are present in different tables so I am searching for single query which will update 2 tables.Unfortunately I am using sql server and there is no way to update 2 tables using single query
So I am thinking to use #Transactional and 2 queries to update 2 tables like the follow
#Transactional
public void updateDetails()
{
Query query1= entityManagerUtil.entityManager.createNativeQuery("update Table1 set Name='' where id in (select NodeID from Table 2) and NodeID=21");
Query query2= entityManagerUtil.entityManager.createNativeQuery("update Table2 set Email='' and phone1='' where NodeID in (select id from Table 2) and NodeID=21");
query1.executeUpdate();
query2.executeUpdate();
}
Is there any other better way to update 2 tables?
you can use JDBCTemplate
http://sujitpal.blogspot.com.es/2007/03/spring-jdbctemplate-and-transactions.html
It allows to do multiple queries with one connection, so you save some time instead of doing it twice.
Why don't you use Hibernate entities for that. Just load the entities associated with Table1 and table2, modify them and let the automatic dirty checking mechanism to update the tables on your behalf. That's one reason for using an ORM by the way.

Oracle Exception: SQL command not properly ended

I am developing web application using JSP and Servlet (IDE: Eclipse, Container: Tomcat7.0, DB: Oracle 10)
I want to get data from two table in a single query
Query:
query = "select * from PROTOCOL as a, ACTIONS as b where a.PROTOCOL_ID = b.PROTOCOL_ID";
But after running the application I am getting the following exception:
java.sql.SQLException: ORA-00933: SQL command not properly ended
is there anything wrong in query?
The problem you have is keyword AS. This is used for columns in SELECT section. It is not valid for FROM where you specify the tables.
You have
select * from PROTOCOL as a, ACTIONS as b
should be
select * from PROTOCOL a, ACTIONS b...
From Oracle Docs
t_alias
Specify a correlation name, which is alias for the table, view,
materialized view, or subquery for evaluating the query. This alias is
required if the select list references any object type attributes or
object type methods. Correlation names are most often used in a
correlated query. Other references to the table, view, or materialized
view throughout the query must refer to this alias.
Example:
SELECT select_list
FROM table1 t_alias1
WHERE expr operator
(SELECT column_list
FROM table2 t_alias2
WHERE t_alias1.column
operator t_alias2.column);
wrong alias syntax. try the following:
query = "select * from PROTOCOL a,ACTIONS b where a.PROTOCOL_ID = b.PROTOCOL_ID";
A comment here for anyone may need it.. PostgreSQL seems to accept AS next to FROM/JOIN

Why does my update query that works in SQL Server 2005 Management Studio not work as a Prepared Query in Java?

I have a Table [Name Table] and an associated table workSchedule. I'm using Hibernate 3.6.7.Final to generate my query. The result is:
update Personnel.dbo.[Name Table] set workSchedule=? where [Name IRC]=?
Which throws an Exception:
11-22#10:30:41 WARN [] JDBCExceptionReporter - SQL Error: 8624, SQLState: S0001
11-22#10:30:41 ERROR [] JDBCExceptionReporter - Internal Query Processor Error: The query processor could not produce a query plan. For more information, contact Customer Support Services.
[Name Table].workSchedule is a foreign key defined thus:
ALTER TABLE [Name Table] ADD workSchedule VARCHAR(34)
FOREIGN KEY REFERENCES workSchedule(id);
workSchedule.id is defined like this:
CREATE TABLE workSchedule
( /* format = letter-days-lunch EX: A-5-1 */
id AS CASE lunch
WHEN 1 THEN scheduleLetter+'-'+CONVERT(VARCHAR, scheduleDays)+'-'+'1'
ELSE scheduleLetter+'-'+CONVERT(VARCHAR, scheduleDays)+'-'+'5'
END PERSISTED NOT NULL,
/* rest of table follows */
);
If I copy paste the above update query into SSMS and plug values directly in for the ?s it works.
UPDATE:
I just tried changing out the PRIMARY KEY of the table WorkSchedule for an INT IDENTITY column. I left renamed id to shift and otherwise left it as column on the table. I also updated both POJO's and .hbm.xml files. The update query still fails, with the same exception.
I'm listing this as an answer because it's what I ended up doing to solve my problem.
I completely removed the calculated field on WorkSchedule.
I implemented the calcualations as a getter in the the POJO instead.
Re-tooled workschedule to use an IDENTITY for the PK
and re-linked [Name Table] to the new IDENTITY field instead. now it all works.

Categories

Resources