选择中的 SQL 用户定义函数

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

SQL User Defined Function Within Select

sqlsql-serverselectuser-defined-functions

提问by madcolor

I have a user defined function in SQL called getBuisnessDays it takes @startdate and @enddate and returns the number of business days between the two dates. How can I call that function within my select?

我在 SQL 中有一个名为 getBuisnessDays 的用户定义函数,它采用 @startdate 和 @enddate 并返回两个日期之间的工作日数。如何在我的选择中调用该函数?

Here's what I'd like to do..

这就是我想要做的..

SELECT getBusinessDays(a.opendate,a.closedate) 
FROM account a
WHERE ...

回答by user17670

Yes, you can do almost that:

是的,你几乎可以做到:

SELECT dbo.GetBusinessDays(a.opendate,a.closedate) as BusinessDays
FROM account a
WHERE...

回答by jerryhung

If it's a table-value function (returns a table set) you simply join it as a Table

如果它是一个表值函数(返回一个表集),你只需将它作为一个表加入

this function generates one column table with all the values from passed comma-separated list

此函数生成一个列表,其中包含传递的逗号分隔列表中的所有值

SELECT * FROM dbo.udf_generate_inlist_to_table('1,2,3,4')

回答by recursive

Use a scalar-valued UDF, not a table-value one, then you can use it in a SELECT as you want.

使用标量值 UDF,而不是表值 UDF,然后您可以根据需要在 SELECT 中使用它。