使用RAND()在SQL Server中创建非确定性函数 [英] Creating Nondeterministic functions in SQL Server using RAND()
问题描述
RAND()列在非确定性函数下(参见 msdn文章)。那么为什么我不能在一个函数中使用它?
因为它有副作用。
函数中不允许带副作用的构造。它具有的副作用是改变一些内部状态,以跟踪发布的最后一个 rand()
值。
我认为你可以通过将其包含在视图定义中然后从视图中选择来解决它。
After a bit of searching and reading the documentation, it's clear that you can write user defined functions in SQL Server that are marked as either deterministic or nondeterministic depending on which built-infunctions are used within the body.
RAND() is listed under the nondeterministic functions (see msdn article). So why can't I use it in a function?
Because it has side effects.
Constructs with side effects are not allowed in a function. The side effect that it has is to change some internal state that keeps track of the last rand()
value issued.
I think you can get around it by including it in a View definition then selecting from the View.
这篇关于使用RAND()在SQL Server中创建非确定性函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!