使用RAND()在SQL Server中创建非确定性函数 [英] Creating Nondeterministic functions in SQL Server using RAND()

查看:176
本文介绍了使用RAND()在SQL Server中创建非确定性函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过一番搜索和阅读文档之后,很明显,你可以在SQL Server中编写用户定义函数,这些函数被标记为确定性或非确定性,具体取决于正文中使用的内置函数。



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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆