具有连接表条件的JOOQ更新表

问题描述 投票:1回答:1

我有两个这样的表:招生和学生

create table [LECTURE_DB].[dbo].[ENROLLMENT](
[EXAM_PASSED] bit null default ((0)),
[EXAM_TAKEN] bit null default ((0)),
[YEAR] int not null,
[LECTURE_ID] numeric(19) not null,
[STUDENT_ID] numeric(19) not null,
constraint [PK__ENROLLME__FE468F5B2739D489]
  primary key (
    [YEAR], 
    [LECTURE_ID], 
    [STUDENT_ID]
  )
)  

create table [LECTURE_DB].[dbo].[STUDENT](
[ID] numeric(19) identity(1, 1) not null,
[FIRSTNAME] varchar(255) null,
[GENDER] varchar(255) null,
[LASTNAME] varchar(255) null,
[YEAR_OF_BIRTH] int null,
constraint [PK__STUDENT__3214EC273493CFA7]
  primary key ([ID])
)

我现在尝试用一些exam_passedlastnameLecture_ID更新一堆注册(year列)。从this中我得到了使用联接表的想法。

private void updateStudentExamPassed(boolean passed, int lidx, int year, String... studentName) {
    Enrollment enrollment = Enrollment.ENROLLMENT;
    Student student = Student.STUDENT;

    Table<?> joined = enrollment.leftJoin(student).on(enrollment.STUDENT_ID.eq(student.ID));
    ctx.update(joined)        
        .set(enrollment.EXAM_PASSED, passed)
        .where(student.LASTNAME.in(studentName))
        .and(enrollment.YEAR.eq(year))
        .and(enrollment.LECTURE_ID.eq(BigInteger.valueOf(lidx)))
        .execute();       
    }

但是我在DataAccessException上得到了.execute()

Exception in thread "main" org.jooq.exception.DataAccessException: SQL [update [LECTURE_DB].[dbo].[ENROLLMENT] left outer join [LECTURE_DB].[dbo].[STUDENT] on [LECTURE_DB].[dbo].[ENROLLMENT].[STUDENT_ID] = [LECTURE_DB].[dbo].[STUDENT].[ID] set [LECTURE_DB].[dbo].[ENROLLMENT].[EXAM_PASSED] = ? where ([LECTURE_DB].[dbo].[STUDENT].[LASTNAME] in (?, ?) and [LECTURE_DB].[dbo].[ENROLLMENT].[YEAR] = ? and [LECTURE_DB].[dbo].[ENROLLMENT].[LECTURE_ID] = ?)]; Falsche Syntax in der Nähe des left-Schlüsselworts.
at org.jooq_3.12.1.SQLSERVER2014.debug(Unknown Source)
at org.jooq.impl.Tools.translate(Tools.java:2717)
at org.jooq.impl.DefaultExecuteContext.sqlException(DefaultExecuteContext.java:755)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:383)
at org.jooq.impl.AbstractDelegatingQuery.execute(AbstractDelegatingQuery.java:119)
at de.esteam.lecturedb.jooq.tasks.RecordFeaturesTask.updateStudentExamPassed(RecordFeaturesTask.java:42)
at de.esteam.lecturedb.jooq.tasks.RecordFeaturesTask.run(RecordFeaturesTask.java:28)
at de.esteam.lecturedb.jooq.common.LectureDBAnalysis.run(LectureDBAnalysis.java:100)
at de.esteam.lecturedb.jooq.common.LectureDBAnalysis.main(LectureDBAnalysis.java:56)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Falsche Syntax in der Nähe des left-Schlüsselworts.

该文档没有帮助我,因为条件依赖于另一个表。是否有希望使这项工作正常进行,还是我需要自行获取每个注册并单手更新?

java sql-server sql-update jooq
1个回答
0
投票

使用特定于供应商的语法

The question you've linked与MySQL有关。 SQL Server不支持这种语法,但是您可以使用UPDATE .. FROM子句来实现等效的语义。即试试这个:

UPDATE .. FROM

[注意,如果通过在ctx.update(enrollment) .set(enrollment.EXAM_PASSED, passed) .from(joined) .where(student.LASTNAME.in(studentName)) .and(enrollment.YEAR.eq(year)) .and(enrollment.LECTURE_ID.eq(BigInteger.valueOf(lidx))) .execute(); 子句中在LEFT JOIN表上使用谓词再次将其转换为INNER JOIN,则student在这里没有任何意义。

使用标准语法

我个人更喜欢在这些情况下尽可能使用标准SQL语法,因为这些WHEREUPDATE .. FROM语句通常可以用UPDATE .. JOININ谓词代替。例如:

EXISTS
© www.soinside.com 2019 - 2024. All rights reserved.