【问题标题】:Knex query builder tool's update() queryKnex 查询生成器工具的 update() 查询
【发布时间】:2020-11-19 15:09:00
【问题描述】:

我正在使用 knex 查询生成器和 SQL Server。

所以有一个用例,我不希望将select @@rowcount 附加到update()

这是使用 knex 的查询:

let updateQuery = knex('details').where('id', studentId).andWhere({name: Name}).update({
'value': Value,
'updated_at': new Date()});

qb生成的SQL:

update [details] 
set [value] = ?, [updated_at] = ? where [id] = ? and [name] = ?; select @@rowcount

那么,我应该做哪些更改以使其不附加select @@rowcount

【问题讨论】:

  • 为什么它这样做是一个“问题”?
  • 对不起,不是问题!刚刚更新了问题。
  • 那么为什么它比附加SELECT @@ROWCOUNT更重要?
  • 所以我正在尝试实现类似这样的 upsert。 update table1 set name = 'val2', itemname = 'val3', itemcatName = 'val4', itemQty = 'val5' where id = 'val1' if @@ROWCOUNT= 0 insert into table1(id, name) values('val1 ', 'val2')
  • 看起来像你在问什么,你想做的是两件完全不同的事情。

标签: node.js sql-server knex.js


【解决方案1】:

您无法阻止 .update() 创建它。

编写使用knex.raw 生成查询的knex 扩展/插件听起来对您来说是一个正确的选择。扩展文档缺少链接,因此可以在此位置附近找到它们 http://knexjs.org/#Builder-queryContext

【讨论】:

  • 嘿 Mikael,如果我在查询中使用返回('id'),update() 将不会添加 select @@rowcount 部分,对吗?相反,它插入了一个输出子句!这种方法正确吗?
  • 是的,这似乎删除了@@rowcount 并添加了output 子句。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
  • 1970-01-01
  • 1970-01-01
  • 2020-11-24
  • 1970-01-01
  • 1970-01-01
  • 2015-12-13
相关资源
最近更新 更多