Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[h2] Failed on update with inner join #636

Closed
Tapac opened this issue Sep 6, 2019 · 1 comment
Closed

[h2] Failed on update with inner join #636

Tapac opened this issue Sep 6, 2019 · 1 comment
Assignees
Labels

Comments

@Tapac
Copy link
Contributor

Tapac commented Sep 6, 2019

Example:

FooTable.innerJoin(BarTable).update({ FooTable.foo eq BarTable.bar }) {
   it[BarTable.baz] = 123
}

Exception:

org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "UPDATE FOOTABLE INNER[*] JOIN BARTABLE ON FOOTABLE.ID = BARTABLE.FOO_ID SET BAZ =? WHERE FOOTABLE .FOO = BARTABLE.BAR"; expected "., AS, SET"; 
@JustPRV
Copy link

JustPRV commented Oct 3, 2019

It seems like H2 doesn't support update with join, but exists can be used instead.

BarTable.update({
    exists(FooTable.slice(FooTable.id).select {
        (BarTable.fooId eq FooTable.foo) and (BarTable.bar eq FooTable.foo)
    }
)}) {
    it[baz] = 123
}
UPDATE BARTABLE SET BAZ=? 
WHERE EXISTS (
	SELECT FOOTABLE.ID 
	FROM FOOTABLE 
	WHERE BARTABLE.FOO_ID = FOOTABLE.FOO 
	AND BARTABLE.BAR = FOOTABLE.FOO
)

For cases when a column needs to be updated with value from the joined table, there is MERGE INTO ... WHEN MATCHED THEN UPDATE ... statement.

Tapac added a commit that referenced this issue Mar 1, 2020
@Tapac Tapac self-assigned this Mar 1, 2020
Tapac added a commit that referenced this issue Mar 1, 2020
Tapac added a commit that referenced this issue Mar 1, 2020
Tapac added a commit that referenced this issue Mar 1, 2020
@Tapac Tapac closed this as completed Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants