T-SQL 中的睡眠命令?
有没有办法编写一个T-SQL命令让它休眠一段时间? 我正在异步编写一个 Web 服务,并且希望能够运行一些测试来查看异步模式是否真的会使其更具可扩展性。 为了“模拟”缓慢的外部服务,我希望能够使用运行缓慢但实际上不处理大量内容的脚本来调用 SQL 服务器。
Is there to way write a T-SQL command to just make it sleep for a period of time? I am writing a web service asynchronously and I want to be able to run some tests to see if the asynchronous pattern is really going to make it more scalable. In order to "mock" an external service that is slow, I want to be able to call a SQL server with a script that runs slowly, but isn't actually processing a ton of stuff.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
查看 WAITFOR 命令。
例如,
此命令允许您实现高精度,但在典型计算机上仅在 10ms - 16ms 内准确,因为它依赖于 GetTickCount。 因此,例如,调用
WAITFOR DELAY '00:00:00:001'
可能会导致根本不等待。Look at the WAITFOR command.
E.g.
This command allows you a high degree of precision but is only accurate within 10ms - 16ms on a typical machine as it relies on GetTickCount. So, for example, the call
WAITFOR DELAY '00:00:00:001'
is likely to result in no wait at all.我相信最长可以等待的时间是23小时59分59秒。
这是一个标量值函数来展示它的用法; 下面的函数将采用秒的整数参数,然后将其转换为 HH:MM:SS 并使用 EXEC sp_executesql @sqlcode> 命令来执行查询。 下面的函数仅用于演示,我知道它并不适合作为标量值函数! :-)
如果您希望延迟超过 24 小时,我建议您使用 @Days 参数来持续几天,并将函数可执行文件包装在循环内...例如。
I believe the maximum time this can wait for is 23 hours, 59 minutes and 59 seconds.
Here's a Scalar-valued function to show it's use; the below function will take an integer parameter of seconds, which it then translates into HH:MM:SS and executes it using the
EXEC sp_executesql @sqlcode
command to query. Below function is for demonstration only, i know it's not fit for purpose really as a scalar-valued function! :-)IF you wish to delay longer than 24 hours, I suggest you use a @Days parameter to go for a number of days and wrap the function executable inside a loop... e.g..
您还可以“等待”一个“时间”:
You can also "WAITFOR" a "TIME":
这是一段非常简单的 C# 代码,用于测试 CommandTimeout。
它创建一个新命令,该命令将等待 2 秒。
将 CommandTimeout 设置为 1 秒,运行时您会看到异常。
将 CommandTimeout 设置为 0 或大于 2 的值即可正常运行。
顺便说一下,默认的 CommandTimeout 是 30 秒。
Here is a very simple piece of C# code to test the CommandTimeout with.
It creates a new command which will wait for 2 seconds.
Set the CommandTimeout to 1 second and you will see an exception when running it.
Setting the CommandTimeout to either 0 or something higher than 2 will run fine.
By the way, the default CommandTimeout is 30 seconds.
这是一个很好的解决方案。
This is a good solution.