Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table-Valued Parameter in Stored Procedure and the Entity Framework 4.0

I have a stored procedure in SQL Server 2008 called 'GetPrices' with a Table-Valued Parameter called 'StoreIDs'.

This is the type i created for this TVP:

CREATE TYPE integer_list_tbltype AS TABLE (n int)

I would like to call the SP from my Entity Framework. But when I try to add the Stored Procedure to the EDM, i get the following error:

The function 'GetPrices' has a parameter 'StoreIDs' at parameter index 2 that has a data type 'table type' which is not supported. The function was excluded.

Is there any workaround this? Any thoughts?

Fabio

like image 863
Fabio Costa Avatar asked May 14 '10 20:05

Fabio Costa


People also ask

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.

How do you declare a table valued parameter?

Table-valued parameters are declared by using user-defined table types. You can use table-valued parameters to send multiple rows of data to a Transact-SQL statement or a routine, such as a stored procedure or function, without creating a temporary table or many parameters.


1 Answers

I agree that passing in a CSV sting is the best solution in this case. I would like to propose simpler way to split csv string, without creating tables and functions, by using CTE:

declare @separator char(1);
set @separator = ',';

;with baseCte as
(select left(@ValueList, charindex(@separator, @ValueList) - 1) as Value,
substring(@ValueList, charindex(@separator, @ValueList) + 1, len(@ValueList)) 
as rest
union all
select left(rest, charindex(@separator, rest) - 1) as Value, 
substring(rest, charindex(@separator, rest) + 1, len(rest)) from baseCte
where len(rest) > 1
)
select Value from baseCte
OPTION (MAXRECURSION 0);
like image 187
Pavel Sinkevich Avatar answered Oct 14 '22 11:10

Pavel Sinkevich