Binding empty list or null value to table valued parameter on a stored procedure (.NET)

前端 未结 4 1373
北海茫月
北海茫月 2021-02-01 00:09

I have created a stored procedure that takes a table valued parameter that is a table with a single column of type int. The idea is to simply pass a list of ids in

4条回答
  •  梦如初夏
    2021-02-01 00:59

    Not passing a value does work but not in the case where I had multiple table value parameters to pass into the procedure. How I solved it was to specify a value of DEFAULT in my query string. For example,

    string sqlQuery = "[dbo].[GetOrderData] @QueueId";
    
    if (OrderIdList.Any())
    {
        sqlQuery = sqlQuery + ", @OrderIdList";
    }
    else
    {
        sqlQuery = sqlQuery + ", DEFAULT";
    }
    
    if (RegionIdList.Any())
    {
        sqlQuery = sqlQuery + ", @RegionIdList";
    }
    else
    {
        sqlQuery = sqlQuery + ", DEFAULT";
    }
    

    Kudos to http://www.sommarskog.se/arrays-in-sql-2008.html#Invoking where I found the solution for this.

提交回复
热议问题