在 SQL 中调用标量值函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7275443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:57:56  来源:igfitidea点击:

Calling Scalar-valued Functions in SQL

sqltsqlsql-function

提问by Madam Zu Zu

I have migrated a database from oracle, and now have a few Scalar-valued Functions.

我已经从 oracle 迁移了一个数据库,现在有一些标量值函数。

However, when I call them, I get an error saying:

但是,当我打电话给他们时,我收到一条错误消息:

Cannot find either column "dbo" or the user-defined function or aggregate "dbo.chk_mgr", or the name is ambiguous.

找不到列“dbo”或用户定义的函数或聚合“dbo.chk_mgr”,或者名称不明确。

I'm calling it like this:

我这样称呼它:

SELECT dbo.chk_mgr('asdf')

What am I doing wrong?

我究竟做错了什么?

采纳答案by 8kb

Are you sure it's not a Table-Valued Function?

你确定这不是一个Table-Valued Function

The reason I ask:

我问的原因:

CREATE FUNCTION dbo.chk_mgr(@mgr VARCHAR(50)) 
RETURNS @mgr_table TABLE (mgr_name VARCHAR(50))
AS
BEGIN 
  INSERT @mgr_table (mgr_name) VALUES ('pointy haired boss') 
  RETURN
END 
GO

SELECT dbo.chk_mgr('asdf')
GO

Result:

结果:

Msg 4121, Level 16, State 1, Line 1
Cannot find either column "dbo" or the user-defined function 
or aggregate "dbo.chk_mgr", or the name is ambiguous.

However...

然而...

SELECT * FROM dbo.chk_mgr('asdf') 

mgr_name
------------------
pointy haired boss

回答by BJ Patel

Can do the following

可以做到以下几点

PRINT dbo.[FunctionName] ( [Parameter/Argument] )

E.g.:

例如:

PRINT dbo.StringSplit('77,54')

回答by Tom H

That syntax works fine for me:

该语法对我来说很好用:

CREATE FUNCTION dbo.test_func
(@in varchar(20))
RETURNS INT
AS
BEGIN
    RETURN 1
END
GO

SELECT dbo.test_func('blah')

Are you sure that the function exists as a function and under the dbo schema?

您确定该函数作为函数存在并且位于 dbo 架构下吗?

回答by Julien Vavasseur

You are using an inline table value function. Therefore you must use Select * From function. If you want to use select function() you must use a scalar function.

您正在使用内联表值函数。因此您必须使用 Select * From 功能。如果要使用 select function(),则必须使用标量函数。

https://msdn.microsoft.com/fr-fr/library/ms186755%28v=sql.120%29.aspx

https://msdn.microsoft.com/fr-fr/library/ms186755%28v=sql.120%29.aspx

回答by Kev

Make sure you have the correct database selected. You may have the master database selected if you are trying to run it in a new query window.

确保您选择了正确的数据库。如果您尝试在新的查询窗口中运行它,您可能选择了主数据库。