dpvmtdu364462 2018-03-22 12:51
浏览 38

MS SQL限制选项不起作用

I have built a query with a few condition statements added on. They all work fine, but when I type to had a limit option, I only get errors.

So I am using go 1.10 with Gorm and the Gin framework. This is my current working code,

qb := myDB.Table("table").Select("xx, xxx, xxx, xxx")

rows, err := qb.Rows()

if err != nil {
    fmt.Println(err)
}

defer myDB.Close()

return rows

This is all working, but when I add a limit anywhere, e.g. before the table or after, I tried both but did not really think it made a difference? For example,

qb := myDB.Table("table").Limit(3).Select("xx, xxx, xxx, xxx")

Now I know MS SQL does not use limit but uses TOP within the select statement (forgive me if that is not the only use case, still not used MS SQL much).

The error I get back is,

mssql: Invalid usage of the option NEXT in the FETCH statement.

Now I have found the following on Gorm's GitHub, https://github.com/jinzhu/gorm/issues/1205

They have a work around but that will not work for me. The guy has also posted an updated function to correct the issue. However, I am not sure how I could update the code in a third party lib. Also this was posted in 2016, so not sure if that code has already been added to the Gorm code base.

  • 写回答

3条回答 默认 最新

  • dongyu4863 2018-03-22 13:01
    关注

    You need an Order By to use OFFSET + FETCH in the generated SQL sent to SQL Server, which would be added by your ORM

    ...the following conditions must be met:

    ...

    The ORDER BY clause contains a column or combination of columns that are guaranteed to be unique.

    How GORM does it, I don't know

    评论

报告相同问题?

悬赏问题

  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果