Query Parameter on TVP - parameters cannot by DbNull

NachitoMax 411 Reputation points
2023-01-31T01:31:54.24+00:00

Hey

Until recently this worked ok (or at least i thought it did). I am passing a table row in a parameter to a stored procedure but i am getting an error

Table-valued parameters cannot be DbNull

Im assuming its a prevention on SQL server side as in my stored procedure there isnt a default value set because i cant find a way of doing that.

My table parameter if of a type that i have created, here is my top code

ALTER PROC [dbo].[usp_tbl_KLA_ItemUpdateRowByTable] 
  @tblItemListType ItemListType READONLY,
  @job_id int = NULL,
  @item_id int = NULL,
  @rev_no int = NULL

and this is the error i get

User's image

Because of this i am unable to proceed with the query. Whats is a reasonable solution to get around passing a default value of a TableType to the stored procedure?

Thanks

Nacho

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,346 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,654 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Olaf Helper 40,656 Reputation points
    2023-01-31T06:39:34.2666667+00:00

    Whats is a reasonable solution to get around passing a default value of a TableType to the stored procedure?

    How can we say, without knowing what your procedure do with the variable?

    0 comments No comments

  2. CosmogHong-MSFT 22,621 Reputation points Microsoft Vendor
    2023-01-31T06:49:20.9866667+00:00

    Hi @NachitoMax

    As the User Define table Types are created as table-valued, so you cannot assign null to a table.

    Most parameters to a SP are required unless a default value is set with = in the parameters list. In the case of a TVP, you cannot set a default value but you don't have to. SQL doesn't complain if you execute the SP without specifying a value for that parameter.

    See this similar thread for more details: Can table-valued parameter be null?

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Erland Sommarskog 100.8K Reputation points MVP
    2023-01-31T23:05:33.08+00:00

    The simple answer is that you leave it out. All TVPs have an implicit default value of the empty table.

    0 comments No comments

  4. NachitoMax 411 Reputation points
    2023-01-31T23:08:44.1333333+00:00

    Hi

    I have referred this back to DevExpress. The editor they provide cannot validate the parameter but if i code the query & parameters in the designer, it qualifies and works. It seems the query editor cannot allow an empty parameter value.

    Thanks for everyones help :)

    0 comments No comments