将函数指定为JOOQ中的值

发布于 2025-01-23 21:53:29 字数 113 浏览 2 评论 0 原文

使用JOOQ,我想在插入过程中指定字段的值。

在SQL Server中,等效查询是:

插入表(a,t)value('foo',sysdateetime())

Using Jooq, I would like to specify the value of a field during insert as a function.

In SQL Server the equivalent query is:

insert into table (a, t) values ('foo', SYSDATETIME())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南薇 2025-01-30 21:53:29

将绑定值与表达式混合:

假设您使用生成的代码,编写以下内容:

// Assuming this static import, as always:
import static org.jooq.impl.DSL.*;

ctx.insertInto(TABLE)
   .columns(TABLE.A, TABLE.T)
   .values(val("foo"), currentTimestamp()) // or currentLocalDateTime()
   .execute();

values()条款仅具有2个重载:

  • 仅接受绑定变量
  • 仅接受表达式,

它没有过多加载,因为会有一个所需过载的指数数量。因此,这里要做的关键是要包裹您的绑定变量使用 dsl.val() 显式。

另请参阅此问题:
如何创建一个字段< t>从jooq中的值t明确?

sysdateTime

jooq上的注释支持标准SQL current_timestamp 通过 。如果您喜欢使用 sysdateTime ,则可以创建 plain sql模板为此,或使用 dsl.function()

Mixing bind values with expressions:

Assuming you're using generated code, write this:

// Assuming this static import, as always:
import static org.jooq.impl.DSL.*;

ctx.insertInto(TABLE)
   .columns(TABLE.A, TABLE.T)
   .values(val("foo"), currentTimestamp()) // or currentLocalDateTime()
   .execute();

The VALUES() clause only has 2 overloads:

  • Accepting only bind variables
  • Accepting only expressions

It doesn't have an overload mixing both things, because there would be an exponential number of needed overloads. So the key thing to do here is to wrap your bind variables using DSL.val() explicitly.

See also this question:
How do I create a Field<T> from a value T in jOOQ, explicitly?

A note on SYSDATETIME

jOOQ supports the standard SQL CURRENT_TIMESTAMP expression via DSL.currentTimestamp() and DSL.currentLocalDateTime(). If you prefer using SYSDATETIME, you can create a plain SQL template for that, or use DSL.function()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文