Search code examples
sql.netsql-serverentity-framework-5sql-function

How do i pass list as a input parameter to the TBV function?


I have a TBV function and want to pass my function list type parameter like this

ALTER FUNCTION [dbo].[fn_Functiont]
(
 @listType (what  type i could use here ??)
)
RETURNS TABLE 
AS
RETURN 
(   
SELECT d.*, b.Name AS Name, ps.Name AS PaymentSystemName, c.UserName AS UserName, c.FirstName AS ClientFirstName, c.LastName AS LastName, c.Number AS DocumentNumber, c.Id
FROM Document AS d
JOIN System AS ps ON d.SystemId = ps.Id
JOIN Client AS c ON c.Id = d.ClientId
LEFT JOIN Shop AS b ON b.Id = d.ShopId
WHERE d.OperationTypeId IN (2, 4, 5) AND c.Type = 1
)

And i want to use this @listType in my function,particularly to check is that list contain any value or not and do something with that value.I am calling this function through Entity Framework 5.


Solution

  • In old days we used to pass comma separated string and split it into a table. Now SQL Server supports User-Defined table type, which can be used in your scenario.

    To create a new User Defined table type

     CREATE TYPE CustomList AS TABLE 
     ( 
       ListItem VARCHAR(50)
     )
     GO
    

    How to Declare

    DECLARE @MyList CustomList
    

    How to use

    INSERT INTO @MyList
    SELECT '1'
    GO
    INSERT INTO @MyList
    SELECT '2'
    

    In your case you can your list type as following.

    ALTER FUNCTION [dbo].[fn_Functiont]
    (
     @listType MYLIST
    )
    ....
    ....
    

    To know more about this visit MSDN

    To know how to use table type in entity framework you can check here