模拟 SQL Server 实例上的当前日期?
是否可以更改 SQL Server 上特定数据库的日期时间?
它与操作系统的日期/时间相关吗?
我们希望模拟未来的日期时间以进行测试,即 GETDATE()
返回未来的日期。
它必须处于半生产(暂存)环境中,因此不幸的是,更改操作系统日期/时间对我们来说不是一个选择。
在理想的情况下,我们会启动虚拟服务器,但目前还不是一个真正的选择。
Is it possible to change the datetime for a particular database on SQL Server?
Is it tied to the operating system's date/time?
We wish to simulate a future datetime for testing purposes i.e. so the GETDATE()
returns a date in the future.
It's got to be in a semi-production (staging) environment so unfortunately changing the OS date / time isn't an option for us.
In an ideal world we'd spin up a virtual server, but also not really an option at the moment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如其他人所说,不。
一个真正的 hacky 解决方法是编写自己的函数来返回所需的日期,并在完成测试后让它返回 GETDATE() ,然后调用该函数。这样做可能会产生一些轻微的开销,但它会满足您的需要。
As stated, by others, No.
A really hacky workaround, would be be to write your own function to return the date you want and have it return GETDATE() when you're done testing, and call that function instead. There's probably some slight overhead in doing this, but it'll do what you need.
不幸的是,它与操作系统日期和时间相关。请参阅此处:http://msdn.microsoft.com/en-us/library/ ms188383.aspx
Unfortunately it is tied to the OS date and time. See here: http://msdn.microsoft.com/en-us/library/ms188383.aspx
您可以随时使用它并进行相应调整:
请参阅下文了解更多信息
StackOverflow问题
但是,如果不更改服务器的日期,就无法更改
GETDATE()
的结果。Added:
You could do a
EXEC xp_cmdshell 'DATE 10/10/2011'
if you wish... but it's not advised.You can always use this and adjust accordingly:
Please see below for more information
StackOverflow Question
But there is no way to change the results from a
GETDATE()
without changing the server's date.Added:
You could do a
EXEC xp_cmdshell 'DATE 10/10/2011'
if you wish... but it's not advised.我取得了一些成功的另一个解决方法是将 INSTEAD OF 触发器添加到插入 GETDATE() 值的任何表并在那里修改它,例如:(
顺便说一句,where 子句在那里,因为我的测试脚本自动生成这些触发器,为每个日期时间列添加更新,因此我只想更新那些看起来像是用 GETDATE() 值插入的列。)
Another workaround I've had some success with is to add an INSTEAD OF trigger to any table where a GETDATE() value is being inserted and modify it there e.g.:
(Incidentally, the where clause is there because my test script autogenerates these triggers, adding an update for every datetime column, so I only want to update those that look like they are being inserted with a GETDATE() value.)
我相信您可以创建一个用户函数来为您进行计算并应用它。
http://msdn.microsoft.com/en-us/library/ms186755。 aspx
此外,它还可以用作列的默认值。
将列默认值绑定到 SQL 中的函数2005年
I believe you can create a user function that would do the calculation for you and apply that.
http://msdn.microsoft.com/en-us/library/ms186755.aspx
Also, it can be used as the default value for a column.
Bind a column default value to a function in SQL 2005