且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何避免SQL Server错误在ORDER BY上有重复的列

更新时间:2022-12-10 15:46:32

基本问题-是否可以抑制对ORDER BY列重复的这种SQL Server限制-Venu回答了:不,不是.

The basic question - is it possible to suppress this SQL Server restriction on ORDER BY column duplication - was answered by Venu: No it is not.

关于如何使用通用方式绕过此限制进行编码,有各种建议(大部分来自我).对于将来的读者来说,如果您要改编现有系统,那么这些答案可能是最有用的. (如果您是从头开始的,请尝试并完全避免这种情况.)

There are various suggestions (mostly from me) about how you could possibly code around this limitation in a generic manner. For any future readers, those answers are probably the most helpful if you are adapting an existing system. (If you are starting from scratch, just try and avoid this situation altogether.)

但是,我真正想到的解决方案是为DBAL的内部API添加版本控制. API版本现在为2,但是您可以调用setApiVersion(1)来指示后端使用旧版本的API.

However, the actual solution that I came to was to add versioning to our internal API for our DBAL. The API version is now 2 but you can call setApiVersion(1) to instruct the back-end to use the old version of the API instead.

v2与v1 *相同,只是它不再自动将列1添加到ORDER BY中,除非它完全为空.因此,新的(v2)项目可以解决SQL Server问题,而现有项目可以设置为使用v1 API,因此可以继续正常工作(但没有SQL Server兼容性).

v2 is identical to v1* except it no longer automatically adds column 1 to the ORDER BY unless it is completely blank. Therefore, the SQL Server issue is resolved for new (v2) projects, whilst existing projects can be set to use the v1 API and therefore continue to work correctly (but without SQL server compatibility).

(*实际上,我已经利用这次机会在v2中进行了其他一些重大更改,但这与该答案无关.)

(* Actually, I've taken this opportunity to make some other breaking changes in v2, but that is not relevant to this answer.)