模拟 SQL Server 实例上的当前日期?

发布于 2024-12-17 14:12:51 字数 220 浏览 1 评论 0原文

是否可以更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风启觞 2024-12-24 14:12:51

正如其他人所说,不。

一个真正的 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.

雅心素梦 2024-12-24 14:12:51

不幸的是,它与操作系统日期和时间相关。请参阅此处:http://msdn.microsoft.com/en-us/library/ ms188383.aspx

该值源自计算机的操作系统
SQL Server 实例正在运行。

Unfortunately it is tied to the OS date and time. See here: http://msdn.microsoft.com/en-us/library/ms188383.aspx

This value is derived from the operating system of the computer on
which the instance of SQL Server is running.

笙痞 2024-12-24 14:12:51

您可以随时使用它并进行相应调整:

SELECT getutcdate()

请参阅下文了解更多信息
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:

SELECT getutcdate()

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.

茶底世界 2024-12-24 14:12:51

我取得了一些成功的另一个解决方法是将 INSTEAD OF 触发器添加到插入 GETDATE() 值的任何表并在那里修改它,例如:(

ALTER TRIGGER [dbo].[AccountsPayableReceivable_trg_i] ON [dbo].[AccountsPayableReceivable]
INSTEAD OF INSERT
AS
  SET NOCOUNT ON

  SELECT *
  INTO #tmp_ins_AccountsPayableReceivable
  FROM INSERTED

  UPDATE   #tmp_ins_AccountsPayableReceivable 
  SET    dtPaymentMade = '01-Jan-1900'
  WHERE dtPaymentMade between dateadd(ss, -5, getdate()) and dateadd(ss, +5, getdate())

  INSERT INTO AccountsPayableReceivable
  SELECT *
  from #tmp_ins_AccountsPayableReceivable

顺便说一句,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.:

ALTER TRIGGER [dbo].[AccountsPayableReceivable_trg_i] ON [dbo].[AccountsPayableReceivable]
INSTEAD OF INSERT
AS
  SET NOCOUNT ON

  SELECT *
  INTO #tmp_ins_AccountsPayableReceivable
  FROM INSERTED

  UPDATE   #tmp_ins_AccountsPayableReceivable 
  SET    dtPaymentMade = '01-Jan-1900'
  WHERE dtPaymentMade between dateadd(ss, -5, getdate()) and dateadd(ss, +5, getdate())

  INSERT INTO AccountsPayableReceivable
  SELECT *
  from #tmp_ins_AccountsPayableReceivable

(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.)

年少掌心 2024-12-24 14:12:51

我相信您可以创建一个用户函数来为您进行计算并应用它。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文