使用RAND()在SQL Server中创建非确定性函数

编程入门 行业动态 更新时间:2024-10-15 14:18:27
本文介绍了使用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中创建非确定性函数

本文发布于:2023-05-29 03:33:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/335815.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词库加载错误:Could not find file 'D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!