如何在 SQL 查询中使用 SQL 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4413694/
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 08:32:56  来源:igfitidea点击:

How do I use a SQL function inside my SQL query

sql

提问by Vivian River

Suppose that I have a SQL function called MAGICthat returns the magical value of a number.

假设我有一个调用的 SQL 函数MAGIC,它返回一个数字的神奇值。

Suppose further that I want to write a SQL query that will return a list of numbers from a SQL table along with their magical values.

进一步假设我想编写一个 SQL 查询,该查询将从 SQL 表中返回一个数字列表及其神奇的值。

My instinct is to write

我的直觉是写作

SELECT number, MAGIC(number) FROM NUMBERS;

However, this returns the error message `MAGIC is not a recognized function name. How can I make this work?

但是,这会返回错误消息“MAGIC 不是可识别的函数名称”。我怎样才能使这项工作?

EDIT: It looks like MAGIC is set up to be a table-valued function even though it returns only a single value. My DBA will not give me access to any database function code, so I have to work with it that way.

编辑:看起来 MAGIC 被设置为一个表值函数,即使它只返回一个值。我的 DBA 不会让我访问任何数据库函数代码,所以我必须以这种方式使用它。

回答by C-Pound Guru

If it is a scalar-value function, fully qualify your function in the TSQL:

如果它是标量值函数,请在 TSQL 中完全限定您的函数:

SELECT number, dbo.MAGIC(number) FROM NUMBERS;

回答by prilldev

If its a table-valued function, then just do this:

如果它是一个表值函数,那么只需执行以下操作:

SELECT n.number, mn.number FROM numbers as n CROSS JOIN dbo.Magic(n.number) as mn

回答by hector teran

Try using the function like a field... this is for TSQL.

尝试使用像字段一样的函数...这是用于 TSQL。

SELECT number,
-- start function
(SELECT function_field_name FROM dbo.MAGIC(number)) AS magic_number
-- end function 
FROM dbo.NUMBERS