问题
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