Create a table variable instance inline

Viewed 43

I have a custom table type and table function like so...

CREATE TYPE CustomTableType AS TABLE(
    Hello INT NULL
)    

ALTER function [dbo].[myfunc]
(
   @stuff CustomTableType READONLY
    
) RETURNS TABLE
AS RETURN]
...

Is it possible to call this function without first declaring an instance of the table variable, in a view, for example, where that's not possible? e.g...

SELECT * FROM myfunc((SELECT Field1 FROM Table1))
1 Answers

The simple answer is not, you can't do that in this way. You need to insert values to a variable of that type outside the function. Even if your idea would be correct then still you need to have any strongly typed variable and insert into clause to put values in it (your subquery in function argument will still need to return something as instance of your CustomTableType, so there will be no difference between putting this batch inside the parentheses of the function call and outside them).

Related