Search code examples
sql-server-2008stored-procedurestable-valued-parameters

Call MS SQL Server Stored Procedure with multiple parameters inclusive of table-value parameter


After doing some research here and online I am at a loss as to whether this is possible. What I want to do is call a stored procedure that has several parameters one of which is a table-value parameter.

This is my stored procedure snippet:

    ALTER PROCEDURE [dbo].[procName]
@Action nvarchar(10) = 'view'
,@var1 int = 0
,@var2 int = 0
,@var3 myType ReadOnly

I now have another procedure (proc2) that has the following lines:

    insert into @varX
    select top 5
        field1, field2
    from
        sourceTable
print 'Processing from table values...'
exec dbo.procName  'refresh', -1, 0, @varX

Note that varX and var3 are of the same type MyType When I execute proc2 I get the error that I am specifying too many arguments for dbo.procName

I am at the point in thinking it is not possible to specify multiple parameters inclusive of a table-value parameter to a stored procedure. I am now tending towards the thought of changing my procName definition to only have one parameter (as all of the examples online seem to have) and have my table-value paramter act as an array of parameter values inclusive of the information I had in my previous select statement (in proc2). If however it is possible to do this call, please illustrate how this is done.

Thanks


Solution

  • This compiles and runs for me:

    create type TT as table (ID int not null);
    go
    create procedure P1
        @Val1 int,
        @Val2 TT readonly,
        @Val3 int
    as
        select @Val1 as Val1,ID,@Val3
        from @Val2;
    go
    create procedure P2
    as
        declare @T TT;
        insert into @T(ID) values (1),(2)
    
        exec P1 10,@T,13
    go
    exec P2
    

    Result:

    Val1        ID          
    ----------- ----------- -----------
    10          1           13
    10          2           13
    

    So, I don't know what your issue is, but it's not being able to mix table and non-table parameters.