拒绝对用户定义的表类型使用 EXECUTE 权限?

我有一个关于 SQLServer2008中 用户定义的表类型的问题。

为了满足 ASP.NET 应用程序的需要,我们在 SQL Server 2008上定义了我们自己的表类型,将它们用作存储过程中的参数(在 ASP.NET 应用程序中执行 SQL 命令时,我们将 DataTable 对象作为存储过程 看这里的例子的参数传递)

问题是,当我们从 ASP.NET 运行 Sql 命令(执行存储过程)时,我们会得到一个错误:

拒绝了对象“ ourTableType”的 EXECUTE 权限, 数据库‘ ourDatabase’,模式‘ ourSchema’。

为什么会这样?为什么需要对用户定义的表类型设置权限?为什么仅对使用权限的存储过程设置权限还不够呢?如果我们无论如何都要设置它,为什么在属性窗口中没有设置 EXECUTE权限类型(我只能看到 ControlReferencesTake OwnershipView Definition) ?

我还不明白的是,在属性窗口中设置 Control权限可以解决这个问题,并且存储过程运行时没有问题。

60454 次浏览

If your stored procedure is using dynamic sql, meaning the @sql is generated and then executed via exec @sql, you will need permission granted on the underlying tables.

One work-around is to modify to stored procedure to run as a different user. If you make it run as SELF, it will be ran underneath the creator of the stored proc, which is extremely dangerous. Still, if you have no other option:

CREATE PROCEDURE dbo.usp_Demo
WITH EXECUTE AS SELF

I really hope you've solved this by now, seeing as the question is almost 4 months old, but in case you haven't, here's what I think is the answer.

GRANT EXEC ON TYPE::[schema].[typename] TO [User]
GO

In SSMS you need to click the "View schema permissions" link before clicking the "Search" button.