Why does this UPDATE statement fail in H2? - sql

This UPDATE statement works when run against Postgres, but fails when run against H2. What's the equivalent H2 statement?
UPDATE award_details
SET award_id = a.id
FROM awards a
WHERE a.award_details_id = award_details.id;
The H2 error message is:
SQL State : 42000
Error Code : 42000
Message : Syntax error in SQL statement "​​[*]
UPDATE AWARD_DETAILS
SET AWARD_ID = A.ID
FROM AWARDS A
WHERE A.AWARD_DETAILS_ID = AWARD_DETAILS.ID"; SQL statement:

Your current update syntax looks to be Postgres style. Instead use correlated subquery update syntax:
UPDATE award_details aw
SET award_id = (SELECT a.id FROM awards a WHERE a.award_details_id = aw.id);

Related

Error "Token unknown - line 4, column 2; SET" creating trigger with variables

I want to create a trigger, but I'm getting an error.
I have 2 tables STOK and STOK_HAREKET:
STOK = (ID,URUN,FIYAT,MINIMUM,MAXIMUM)\
STOK_HAREKET = (STOK_ID,FİYAT,GIRIS,CIKIS)\
I want to build a trigger to STOK_HAREKET. On insert and update, the GIRIS and CIKIS ın STOK_HAREKET table is collected and submitted
from the output and save this value to the MAXIMUM value ın the STOK table.
CREATE TRIGGER T_STOK_BIU FOR STOK_HAREKET BEFORE INSERT OR UPDATE
AS
begin
Set #gsum = ( SELECT SUM(GIRIS) FROM STOK_HAREKET WHERE STOK_ID = new.STOK_ID );
Set #csum = ( SELECT SUM(CIKIS) FROM STOK_HAREKET WHERE STOK_ID = new.STOK_ID );
Set #toplm = (#gsum-#csum)
update STOK set MAKSIMUM = #toplm where ID = new.STOK_ID
end
This code gives an error:
org.jkiss.dbeaver.model.sql.DBSQLException: SQL Error [335544634] [42000]: Dynamic SQL Error; SQL error code = -104; Token unknown - line 4, column 2; SET [SQLState:42000, ISC error code:335544634]
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.executeStatement(JDBCStatementImpl.java:133)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.executeStatement(SQLQueryJob.java:578)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.lambda$1(SQLQueryJob.java:487)
at org.jkiss.dbeaver.model.exec.DBExecUtils.tryExecuteRecover(DBExecUtils.java:173)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.executeSingleQuery(SQLQueryJob.java:494)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.extractData(SQLQueryJob.java:913)
at org.jkiss.dbeaver.ui.editors.sql.SQLEditor$QueryResultsContainer.readData(SQLEditor.java:3760)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetJobDataRead.lambda$0(ResultSetJobDataRead.java:123)
at org.jkiss.dbeaver.model.exec.DBExecUtils.tryExecuteRecover(DBExecUtils.java:173)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetJobDataRead.run(ResultSetJobDataRead.java:121)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetViewer$ResultSetDataPumpJob.run(ResultSetViewer.java:5033)
at org.jkiss.dbeaver.model.runtime.AbstractJob.run(AbstractJob.java:105)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: java.sql.SQLSyntaxErrorException: Dynamic SQL Error; SQL error code = -104; Token unknown - line 4, column 2; SET [SQLState:42000, ISC error code:335544634]
at org.firebirdsql.gds.ng.FbExceptionBuilder$Type$1.createSQLException(FbExceptionBuilder.java:534)
at org.firebirdsql.gds.ng.FbExceptionBuilder.toFlatSQLException(FbExceptionBuilder.java:304)
at org.firebirdsql.gds.ng.wire.AbstractWireOperations.readStatusVector(AbstractWireOperations.java:140)
at org.firebirdsql.gds.ng.wire.AbstractWireOperations.processOperation(AbstractWireOperations.java:204)
at org.firebirdsql.gds.ng.wire.AbstractWireOperations.readSingleResponse(AbstractWireOperations.java:171)
at org.firebirdsql.gds.ng.wire.AbstractWireOperations.readResponse(AbstractWireOperations.java:155)
at org.firebirdsql.gds.ng.wire.AbstractWireOperations.readGenericResponse(AbstractWireOperations.java:257)
at org.firebirdsql.gds.ng.wire.AbstractFbWireDatabase.readGenericResponse(AbstractFbWireDatabase.java:201)
at org.firebirdsql.gds.ng.wire.version11.V11Statement.prepare(V11Statement.java:89)
at org.firebirdsql.jdbc.FBStatement.prepareFixedStatement(FBStatement.java:881)
at org.firebirdsql.jdbc.FBStatement.internalExecute(FBStatement.java:868)
at org.firebirdsql.jdbc.FBStatement.executeImpl(FBStatement.java:496)
at org.firebirdsql.jdbc.FBStatement.execute(FBStatement.java:482)
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.execute(JDBCStatementImpl.java:329)
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.lambda$0(JDBCStatementImpl.java:131)
at org.jkiss.dbeaver.utils.SecurityManagerUtils.wrapDriverActions(SecurityManagerUtils.java:96)
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.executeStatement(JDBCStatementImpl.java:131)
... 12 more
Caused by: org.firebirdsql.jdbc.FBSQLExceptionInfo: Dynamic SQL Error
The syntax you use is not valid syntax for Firebird. I recommend you review the Firebird 4.0 Language Reference, chapter Procedural SQL (PSQL) Statements for the correct syntax for Firebird.
Specifically, you need to explicitly declare variables before use, and use select ... into ... to assign values from a select.
However, there are more problems with your trigger:
You execute two separate selects, which could have been one select
You retrieve values using two selects, and then use those for an update. This can be done in one merge statement
As user13964273 remarks in the comments, doing this is a bad idea because of transaction visibility and performance (or having to retry concurrent updates which conflict)
Taken at face value, your trigger could be rewritten to:
create trigger T_STOK_BIU for STOK_HAREKET before insert or update
as
begin
merge into STOK
using (
select STOK_ID, sum(GIRIS) - sum(CIKIS) as toplm
from STOK_HAREKET
where STOK_ID = new.STOK_ID
group by STOK_ID
) as src
on STOK.STOK_ID = src.STOK_ID
when matched then update set STOK.MAKSIMUM = src.toplm;
end
However, you should consider instead using a view to calculate this on the fly when needed. Or use a table which has a summarized aggregate row, and insert changes (delta rows) into the table through a trigger, and on a schedule recompute the summarized aggregate row and delete those delta rows. You can then - use a view to calculate the actual value when needed, while reducing performance concurrency issues.

SQL update values based on join

I'm trying to update values of one table based on criteria of second table.
But something is wrong. Can you advise what I'm a doing wrong?
UPDATE food_serve
SET food_serve_cost = food_serve_cost*1.15
FROM food_serve JOIN fooditem
ON fooditem.food_item_no = food_serve.food_item_no
WHERE food_type = 'M' ;
Using oracle sql. Error report -
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
Hello I think you miss something in food_serve_cost You didnt use any alias
UPDATE ( SELECT food_serve.food_serve_cost ,
fooditem.food_serve_cost
FROM food_serve
INNER JOIN fooditem ON fooditem.food_item_no =
food_serve.food_item_no
WHERE fooditem.food_type = 'M')
SET food_serve.food_serve_cost=fooditem.food_serve_cost*1.15
Or You can use merge
MERGE into food_serve
USING fooditem
ON (fooditem.food_item_no = food_serve.food_item_no)
when matched then update SET
food_serve.food_serve_cost=fooditem.food_serve_cost*1.15
WHERE fooditem.food_type = 'M'
Seems you have ambiguous columns try add the table name for example:
UPDATE food_serve
SET food_serve.food_serve_cost = fooditem.food_serve_cost*1.15
FROM food_serve JOIN fooditem
ON fooditem.food_item_no = food_serve.food_item_no
WHERE fooditem.food_type = 'M' ;
You can make use of Merge Operator better in this situation
select * into #Target from fooditem WHERE food_type ='M'
MERGE food_serve AS T
USING #Target AS S
ON S.food_item_no = T.food_item_no
WHEN MATCHED THEN
UPDATE SET T.food_serve_cost = T.food_serve_cost*1.15

Where Clause is not working in Update but working in Select in Oracle DB

I am trying to execute the below update query
update custom_field cfe set cfe.field_value =:valueId where cp_entity_id = :cId
0 rows updated.
This is not updating any row but same where clause is working fine with select query and returns 1 row
select * from custom_field where cp_entity_id = :cId
Also, if i hardcode the value of cId parameter then update works fine but I am executing it from java program so it's not possible for me to hardcode the value
Also cp_entity_id column is a foreign key.
Try this, I faced similar issue.
Use this
select primary_key from custom_field where cp_entity_id = :cId query to find out primary key and Then use that primary key in your where clause of update query.
One of the ways to set parameter is explained here.
PreparedStatement ps = conn.prepareStatement(
"UPDATE Messages SET description = ?, author = ? WHERE id = ? AND seq_num = ?");
// set the preparedstatement parameters
ps.setString(1,description);
ps.setString(2,author);
ps.setInt(3,id);
ps.setInt(4,seqNum);
// call executeUpdate to execute our sql update statement
ps.executeUpdate();
ps.close();

Oracle SQL update from one Table to another table throws syntax error

Oracle SQL update from one Table to another table throws syntax error for following simple update query.
UPDATE Sales_Import SI
SET AccountNumber = RAN.AccountNumber
FROM RetrieveAccountNumber RAN
WHERE RAN.LeadID = SI.LeadID;
Error:
Error starting at line 1 in command:
Error at Command Line:2 Column:37
Error report:
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
I believe the same query will work in other databases like postgres etc.
Could any one tell the correct query ?
and whatever i tried is this ANSI standard query ?
and whatever i tried is this ANSI standard query ?
No. Oracle Oracle doesn't support join in update statement.
In Oracle you could do it in two ways -
**Merge statement **
with only update clause in merge statement
MERGE INTO sales_import s
USING (SELECT *
FROM retrieveaccountnumber) u
ON (u.leadid = s.leadid)
WHEN matched THEN
UPDATE SET u.accountnumber = s.accountnumber;
Correlated query
UPDATE sales_import t1
SET accountnumber = (SELECT t2.accountnumber
FROM retrieveaccountnumber t2
WHERE t1.leadid = t2.leadid )
WHERE EXISTS (
SELECT 1
FROM retrieveaccountnumber t2
WHERE t1.leadid = t2.leadid );
I will write your sql like this:
UPDATE Sales_Import SI
SET AccountNumber = (Select RAN.AccountNumber
FROM RetrieveAccountNumber RAN
WHERE RAN.LeadID = SI.LeadID);
You can do this by joining those tables:
UPDATE SI
SET AccountNumber = RAN.AccountNumber
FROM RetrieveAccountNumber RAN
JOIN Sales_Import SI ON RAN.LeadID = SI.LeadID;

How to write UPDATE SQL with Table alias in SQL Server 2008?

I have a very basic UPDATE SQL -
UPDATE HOLD_TABLE Q SET Q.TITLE = 'TEST' WHERE Q.ID = 101;
This query runs fine in Oracle, Derby, MySQL - but it fails in SQL server 2008
with following error:
"Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'Q'."
If I remove all occurrences of the alias, "Q" from SQL then it works.
But I need to use the alias.
The syntax for using an alias in an update statement on SQL Server is as follows:
UPDATE Q
SET Q.TITLE = 'TEST'
FROM HOLD_TABLE Q
WHERE Q.ID = 101;
The alias should not be necessary here though.
You can always take the CTE, (Common Tabular Expression), approach.
;WITH updateCTE AS
(
SELECT ID, TITLE
FROM HOLD_TABLE
WHERE ID = 101
)
UPDATE updateCTE
SET TITLE = 'TEST';