Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What permission do I need to use an SQL Server Table Valued Parameter (TVP) as a stored proc parameter?

I'm using SQL Server 2008 R2 and I've created a TVP that I want to use as a parameter to a stored proc but I get a message saying that it can't be found or I don't have permission.

I can use the TVP in a script or in the body of the stored proc, but when I try to use it as a parameter I get the error.

Any thoughts?

Edit: For clarification, the error I'm getting is on the creation of the stored proc

like image 524
Josh Russo Avatar asked May 15 '14 13:05

Josh Russo


People also ask

How do you execute a stored procedure with table valued parameters in SQL?

First a Table Variable of User Defined Table Type has to be created of the same schema as that of the Table Valued parameter. Then it is passed as Parameter to the Stored Procedure and the Stored Procedure is executed using the EXEC command in SQL Server.

Can we pass table as a Parameter to stored procedure in SQL Server?

Table-valued parameters are declared by using user-defined table types. You can use table-valued parameters to send multiple rows of data to a Transact-SQL statement or a routine, such as a stored procedure or function, without creating a temporary table or many parameters.

How do I grant permission for a stored procedure?

To grant permissions on a stored procedureExpand Stored Procedures, right-click the procedure to grant permissions on, and then select Properties. From Stored Procedure Properties, select the Permissions page. To grant permissions to a user, database role, or application role, select Search.

Can we use table variable in stored procedure?

SQL Server can declare the table variables within batches, functions, and stored procedures. Table variables go out of the scope at the end of the batch, similar to local variables.


1 Answers

In order for a caller to use a PROC with a table valued parameter, you'll need to (unintuitively) grant execute permissions on the TVP type to those calling the PROC i.e.

GRANT EXECUTE ON TYPE::[schema].[MyTVP] to [SomeRole]

Edit

I believe I was able to replicate the issue, viz working from a minimal set of permissions granted to a user. The vital step is for the DBO or Schema Owner of your TVP to grant you the following access to it, in order to be able to use it in a PROC (without this access, I was able to declare a loose variable of the TVP type, but not use it in a PROC).

GRANT REFERENCES ON TYPE::[schema].[MyTVP] to YOURROLE -- Or User.

Grant Reference here (Obviously you'll also need CREATE PROCEDURE permission, plus relevant access to any objects used in the PROC)

Consumers of the PROC will need to also have the GRANT EXECUTE permission on the Proc and on the Type as per the initial answer.

like image 53
StuartLC Avatar answered Oct 23 '22 10:10

StuartLC