Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to execute my store function this way:
C#
using (var cmd = new SqlCommand("XXX.MyFunction", con))
{
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@myparDateFrom", from);
    cmd.Parameters.AddWithValue("@myparDateTill", due);
    cmd.Parameters.AddWithValue("@myparId1",id);                                                                                                              cmd.Parameters.Add("@Table_Var", new SqlDbType());
    cmd.Parameters["@Table_Var"].Direction = ParameterDirection.ReturnValue;

    con.Open();
    var result = cmd.ExecuteScalar();
    con.Close();
}

And I get error on executing :The request failed for '' (procedure), because '' is a table valued function object.
I've read that I need to wrap this function this SP. When I did it, everything works. But I need a function to return a table, and SP cannot do that. How can I return a table with my function?
Posted

1 solution

Consider the below example:
SQL
CREATE FUNCTION RemoveMe
(	
	
)
RETURNS TABLE 
AS
RETURN 
(
	select * from dbo.xxxx
)


In order to run the above function, a standard T-SQL statement would be:
SQL
select * from RemoveMe()


Therefore, in order to run it from C#, you would have to run a sql query as CommandType.Text by building the query along with parameters like

SQL
select * from RemoveMe(param1, param2...)


or build a stored procedure to call the table valued function and pass the parameters to the stored procedure.

Hope this help.

Regards,
Nayan
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900