Calling an in-line TVF with named parameters, what is the proper syntax?

孤街醉人 提交于 2020-02-11 13:30:51

问题


I tried calling an in-line TVF with positional parameters and it works fine:

SELECT MyTable.Col1,
       (SELECT TvfColumn FROM ufnGetData(MyTable.Col1, MyTable.Col2)),
       MyTable.Col2
FROM MyTable

Then I tried to name the parameters to the TVF and it does not parse:

SELECT MyTable.Col1,
       (SELECT TvfColumn FROM ufnGetData(@Param1=MyTable.Col1, @Param2=MyTable.Col2)),
       MyTable.Col2
FROM MyTable

The function looks something like:

CREATE FUNCTION dbo.ufnGetData
(   
    @Param1 INT,
    @Param2 INT
)
RETURNS TABLE 
AS
RETURN 
(
SELECT 
       blah blah blah AS TvfColumn
)
GO

Giving an error similar to:

Msg 137, Level 15, State 2, Line 23 Must declare the scalar variable "@Param2".

Msg 102, Level 15, State 1, Line 24 Incorrect syntax near ','.

What am I doing wrong?


回答1:


Table-Valued User-Defined Functions can't be treated like a Stored Procedure. You don't use named parameters while calling them, it is position-based. So in other words, what you are trying to do isn't possible.

Is there a reason why you want to use named parameters? You need to have all parameters defined, so there is no real benefit that you would normally see with stored procedures.



来源:https://stackoverflow.com/questions/8219163/calling-an-in-line-tvf-with-named-parameters-what-is-the-proper-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!