有没有一种方法可以在 Microsoft SQL Server 查询上使用函数而不使用“dbo”。在函数之前?
有没有办法在不使用“dbo”的情况下调用用户定义的函数。函数名和参数之前?
使用:
SELECT USERFUNCTION(PARAM1, PARAM2, PARAM3, PARAMN)
代替:
SELECT dbo.USERFUNCTION(PARAM1, PARAM2, PARAM3, PARAMN)
Is there a way to call a User defined function without using "dbo." before the function name and parameters?
Using:
SELECT USERFUNCTION(PARAM1, PARAM2, PARAM3, PARAMN)
instead of:
SELECT dbo.USERFUNCTION(PARAM1, PARAM2, PARAM3, PARAMN)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这对于
SELECT
语法来说是不可能的。 BOL 状态:“标量值函数必须至少使用以下两个调用:函数的部分名称”但是,此语法有效。
最佳实践是始终显式地限定您正在引用的对象,但可以避免解析架构的一些开销(并避免架构无法隐式解析或以不希望的方式解析的可能性)
This isn't possible for the
SELECT
syntax. BOL States: "Scalar-valued functions must be invoked by using at least the two-part name of the function"This syntax works however.
It is best practice to always explicitly schema qualify objects you are referencing anyway though to avoid some overhead for resolving the schema (and avoid the possibility that the schema cannot be resolved implicitly or is resolved in a way that is undesired)
如果我们认为您对看到“dbo.”有负面反应,则有多种方法可以做到这一点。
在 SQL Server 2000 中,有一种方法可以通过稍微切换将 UDF 转换为系统函数。这个“功能”已经从 SQL Server 2005 开始删除,所以我不会详细介绍,除非您确实仍在使用 2000。
您可以使用 OPENQUERY 和 PROC 语法,类似于 Martin 所展示的。
您可以通过重写或将其包装在 TVF 中,将标量函数转换为表值函数。然而语法发生了变化,所以
上面的内容看起来都不比仅仅添加一个简单的“dbo”更简单。函数名称的前缀,那么为什么要这样做呢?
There are various ways to do this, if we take it that you have a negative reaction to seeing "dbo.".
In SQL Server 2000, there is a way to turn UDFs into system functions by toggling a bit. This "feature" has been removed from SQL Server 2005 onwards, so I won't go into detail unless you really are still using 2000.
You can use OPENQUERY with PROC syntax similar to what Martin has shown.
You can turn the Scalar function into a Table Valued Function, either by rewriting it, or by wrapping it in a TVF. The syntax changes however, so
But none of the above looks simpler than just tacking a simple "dbo." prefix to the function name, so why would you do it?
是的,可能,
实际上,当函数返回标量值时,您必须使用模式名称进行调用,例如 dbo.yourfunction ,如果您不想在没有模式名称的情况下调用函数,您应该按如下方式创建函数。
示例代码:
原因是您将值作为 table 返回。
Yes Possible,
Actually when function returning scalar value you must call with schema name like dbo.yourfunction , If you don't want to call function without schema name you should create function as follows.
Sample Code:
The reason is you are returning the value as table .