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中创建非确定性函数
发布评论