SQL Server GetDate function to return a future date

时间:2016-02-12 20:49:02

标签: sql-server getdate

This is an existing application that has about ~1400 stored procedures, that was built many years ago. We have used GetDate() to get current date within many of the stored procedures.

Now we want to test this application with "Aging" scenarios. We were told by our DBAs that GetDate() function derives date from operating system. So if we need getdate() to return future date, we need to change OS date. Data Center guys won't let us change Operating System date.

One obvious solution is to create a separate GetApplicationDate() function which returns what ever date we want and review all stored procedures and update them with this new custom function.

I want to avoid changing ~1400 stored procedures and override GetDate() function provided by SQL Server. Is that possible?

Thanks, Gaja

2 个答案:

答案 0 :(得分:1)

There is a similar question here Changing the output of Getdate and it pretty much states that it is not possible to change the value getdate return other than changing the o/s time. Something you could consider is modifyiing all the stored procs by replacing the getdate statements with a wrapper or datetime provider where you can control the value it returns, an offset or the like. This could be controlled by placing the date or offset in a table which the wrapper refers to so that you only need to change it in one place.

Another thing you could consider if this is a large job (due to the number of objects requiring a change) would be to programmatically (automate) make the change to all the objects. There are quite a few hits on google on how to do this.

答案 1 :(得分:1)

看起来你终于达到了不再重构源代码的可能性。此外,我认为<date>必须是存储过程的参数 可能你有一些理由在整个系统中类似地在sp内获得日期,在这种情况下,改变对新函数的引用看起来是合理的。

为避免手动修改每个sp,您可以通过运行硬盘驱动器上的文件的regexp脚本对其进行修改,并将getgate()替换为新的func名称。如果将sql-code存储在vcs中。或者通过使用sys.sql_modules->definition数据的t-sql脚本。这意味着您可以以编程方式生成新的sp内容。但你必须小心。

另外你应该记住

  • 您的存储过程中可能仍需要当前日期
  • 更改新功能返回的日期可能会导致性能问题
  • 新功能必须具有确定性,否则会导致其他性能问题