35

Is it possible to create a procedure with a table value parameter as an optional parameter.

I tried the following code:

CREATE PROCEDURE SP
@Table testteype = null READONLY
AS 
....

But I get this error:

Operand type clash: void type is incompatible with test type 

ps: I use sql server with C#.Net

3
  • Did you tried to look at my answer? any feedback? Commented Feb 8, 2013 at 15:23
  • 3
    @chuckp16 is the way to go.. Table-Valued parameters always have an implicit value of an empty table. So you can actually call that procedure without any parameters and it would execute but the table would be empty. Commented Dec 4, 2013 at 14:37
  • Please mark Chuck's answer as correct. Commented Nov 17, 2016 at 2:13

4 Answers 4

69

Table-Valued parameters always have an implicit value of an empty table. So you can actually call that procedure without any parameters and it would execute but the table would be empty.

So it doesn't really make sense to label a table-value parameter with a default value. Remove the "=null", check the table contents, and you should be good to go.

Sign up to request clarification or add additional context in comments.

2 Comments

What version of SQL Server did this work on, back in 2013? Doesn't work in SQL Server 2017. "Msg 313, Level 16, State 2, Procedure xxx, An insufficient number of arguments were supplied for the procedure or function xxx"
Never mind, that was with a user-defined function rather than a stored proc. And for some reason, UDFs don't support optional parameters. :(
7

Basically, having default value "= null" makes no sense and is the reason of the error.

By default, @Table testteype gets value of an empty table. Thus, you may remove = null:

CREATE PROCEDURE SP
@Table testteype  READONLY
AS 
....

Reference: for a sample on how to use this with C# ADO.NET i would recommend to use this post - Using SQL Server’s Table Valued Parameters

Comments

0
--exa:
--create TYPE  Table_tt   as table(id int,name  varchar(10))

create table #a  (aid int,price int)
insert into #a (aid  ,price  )
select 1,10
union
select 2,50

create PROCEDURE SP
@Table_tt Table_tt  null READONLY     
AS 
 begin 
 select * into #tem from @Table_tt
 select * from #a where aid in(select id from #tem)  or aid='' 
 end

 exec SP 

1 Comment

Giving a description of what the code does and how it answers the question is much better than just posting a code snippet. The Why is almost more important than the actual code.
-1

Not sure why the answer above states making default value = NULL is incorrect but this works for me.

CREATE PROCEDURE SP
(
    @Param1 VARCHAR(10),
    @Param2 VARCHAR(10)=NULL
)

SELECT......
WHERE @Param1 = SOMETHING
AND (@Param2 = SOMETHING OR @Param2 IS NULL)

1 Comment

Different situation. The question is specifically about table-valued parameters. They are treated differently than a varchar parameter is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.