Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Table-Valued SQL Functions From .NET

Tags:

c#

.net

sql

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).

like image 517
goric Avatar asked Aug 12 '08 15:08

goric


People also ask

How do you call a table-valued function in SQL?

The simple definition of the table-valued function (TVF) can be made such like that; a user-defined function that returns a table data type and also it can accept parameters. TVFs can be used after the FROM clause in the SELECT statements so that we can use them just like a table in the queries.

Can we call SQL function from C#?

We can execute a function in C# using a SqlCommand object and passing a SQL defined function in a SELECT SQL query.

How do you call a table-valued function in Entity Framework?

Step 1 − Select the Console Application from the middle pane and enter TableValuedFunctionDemo in the name field. Step 2 − In Server explorer right-click on your database. Step 3 − Select New Query and enter the following code in T-SQL editor to add a new table in your database.


1 Answers

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.

like image 189
Nick Berardi Avatar answered Oct 10 '22 02:10

Nick Berardi