32

Scalar-valued functions can be called from .NET as follows:

SqlCommand cmd = new SqlCommand("testFunction", sqlConn); //testFunction is scalar
cmd.CommandType = CommandType.StoredProcedure;  
cmd.Parameters.Add("retVal", SqlDbType.Int);
cmd.Parameters["retVal"].Direction = ParameterDirection.ReturnValue;
cmd.ExecuteScalar();
int aFunctionResult = (int)cmd.Parameters["retVal"].Value;

I also know that table-valued functions can be called in a similar fashion, for example:

String query = "select * from testFunction(param1,...)"; //testFunction is table-valued
SqlCommand cmd = new SqlCommand(query, sqlConn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(tbl);

My question is, can table-valued functions be called as stored procedures, like scalar-valued functions can? (e.g., replicate my first code snippet with a table-valued function being called and getting the returned table through a ReturnValue parameter).

1
  • 2
    @ChuckConway Why dropping C# from the title? It was valid, considering the C# tag... but that tag does not help it, to show up in google. Commented Jan 13, 2014 at 23:58

1 Answer 1

19

No because you need to select them. However you can create a stored proc wrapper, which may defeat the point of having a table function.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't think it would really defeat the point of having a table-valued function. One point of having TVFs is that they have certain safety guarantees that stored procedures do not. Another point is that they can be re-used, whereas it is much more difficult to reuse a stored procedure without simply copy and pasting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.