如何强制 SQL Azure 在查询字符串中接受 DMY?
我们正在尝试将一系列遗留应用程序迁移到 SQL Azure。这些应用程序是用经典 ASP 编写的,带有大量基于字符串的查询。有很多东西需要重写才能使其值得迁移。
任何带有日期的查询都会失败,因为 SQL 字符串查询使用 UK DMY 格式。 SQL Azure 用户和数据库似乎很难使用美国英语格式。
如果我添加 SET DATEFORMAT dmy;在查询字符串之前,查询将起作用。然而,这需要更改数千个查询。是否有强制 SQL Azure 使用 DMY 格式作为连接字符串的一部分?那个或其他一些解决方案可以帮助我们节省重写数千个查询的时间?有什么方法可以将登录设置为始终使用 DMY 格式或让整个数据库使用 DMY 等?
谢谢格雷姆
We have a range of legacy applications that we are trying to migrate to SQL Azure. The applications are written in Classic ASP with lots of string based queries. There are to many to rewrite to make it worth migrating.
Any queries with dates are failing as the SQL string queries use UK DMY format. SQL Azure user and DB seem hard set to use use US English format.
If I add SET DATEFORMAT dmy; before a query string the query then works. However this requires change thousands of queries. Is there anyway to force SQL Azure to use DMY format as part of the connection string? That or or some other solution that saves us re-writing thousands of queries? Any way to set a login to always use DMY format or to have the whole DB use DMY etc?
Thanks
Graeme
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 Richard 的建议是这样的:
原始代码:
新代码:
如果代码是用 VBScript 编写的,那么您将很难实现这一点。
[如果您尝试在 VBScript 中本地执行此操作],您遇到的第一个问题是 ADO Recordset 对象在其方法中大量使用可选参数,并且您根本无法在 VBScript 中创建函数(或类方法)有可选参数。因此,您将无法对任何有用的内容进行子类化。
不过,您可以在 JScript 中子类化 ADo 记录集对象 - 可以在同一页面中使用的同一脚本中混合和匹配两种语言;只要意识到 JScript 的任何内容都会首先执行,无论它位于页面中的哪个位置。 ASP 首先运行 JScript 解释器,然后运行 VBScript 解释器。
您也许还可以创建自己的基于 Com 的 DLL 来包装 ADO 对象...但您必须自己进行研究。
现在。
如果是我,我会认真考虑搜索和搜索。替换,具体取决于代码的一致性。
如果您可以使代码达到内联查询使用“dd FullMonthName YYYY”(即明确的日期格式)的程度,则您无需担心数据库的 DMY 问题,并且代码将在任何版本上生存它所运行的 SQL 的。
只要日期具有基于文本的月份名称和全年,SQL 就不会关心它们的顺序。
我怀疑您甚至可以在较小的范围内采用理查德的想法,并放置一个包装器只要有内联日期,就可以运行,进行一些运行时嗅探,然后让一切重新工作。
I think what Richard was suggesting is something like this:
Original code:
New code:
If the code is in VBScript, though, you would have a hard time implementing this.
The first problem you'd run in to [if you tried doing it natively in VBScript] is that ADO Recordset object makes heavy use of optional arguments in its methods, and you simply can't create a function (or class method) in VBScript that has optional arguments. Therefore you wouldn't be able to subclass anything useful.
You could possibly subclass the ADo recordset object in JScript though - it is possible to mix and match both languages in the same script using in the same page; just realize anything that is JScript will get executed first, no matter where it is in the page. ASP runs the JScript interpreter first, and then the VBScript interpreter second.
You might also be able to create your own Com-based DLL that wrapped the ADO object... but you'd have to investigate that yourself.
Now.
If it were me, I'd take a serious look at search & replace, depending on how consistent the code is.
If you can get the code to the point where the inline queries are using "dd FullMonthName YYYY", i.e. an unambiguous date format, you won't need to worry about the DMY issue with the database, and the code will survive on whatever version of SQL it's running against.
As long as dates have a text-based month name, and a full year, SQL it doesn't care what order they're in.
I suspect you might even be able to take Richard's idea on a smaller scale, and put a wrapper function wherever there is an inline date, do a bit of runtime sniffing, and get everything working again.
您可以对连接本身进行子类化,以便每当发送命令时都会在其前面加上 SET DATEFORMAT 命令。这将需要重构您的引擎,但这可以通过搜索替换来实现。
You could subclass the connection itself so that whenever a command was sent it was preceded by the SET DATEFORMAT command. This would entail a refactoring of your engine but that could be achieved by a search-replace.