sql-server - 如何使存储过程可供特定用户访问

标签 sql-server stored-procedures

我正在使用 SSMS 10.50 访问 SQL Server 2008 R2,我对 SQL 编码非常陌生。我的登录ID是SA。我创建了一个存储过程,我想让它可由特定用户执行。但我没能做到这一点。

当我写下这些行时,

create proc GetCustomerDetailsCompanyWise
    (@comp varchar(40))
as
begin
    grant execute on GetCustomerDetailsCompanyWise to [sgp\deepak.b]
    execute AS USER='sgp\deepak.b'; 

    select * 
    from DD_Customer 
    where Company = @comp;
end 

exec GetCustomerDetailsCompanyWise 'Google'

我收到此错误

Msg 916, Level 14, State 1, Procedure 
GetCustomerDetailsCompanyWise, Line 6
The server principal "sgp\deepak.b" is not able to access the 
database "TEMP" under the current security context.

您能否解释一下我可能做错了什么以及如何解决它?

最佳答案

首先创建过程

create proc GetCustomerDetailsCompanyWise
    (@comp varchar(40))
as
begin
    select * 
    from DD_Customer 
    where Company = @comp;
end
go

然后授予权限

grant execute on GetCustomerDetailsCompanyWise to [sgp\deepak.b];
go

然后尝试运行它。

关于sql-server - 如何使存储过程可供特定用户访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37627253/

相关文章:

sql-server - 使用存储过程中的表名称填充数据集

MySQL 函数占用太多 CPU

c# - sql本地数据库连接

sql - 在 SQL Server 中查找在特定[月份]内未晋升的员工

sql - 在数据库中查找不同时间之间的同时发生的事件

SQL Server 2008 : setting default location for mdf/ldf

SQL Server WITH 语句

sql-server - 截断与删除和表锁 SQL Server 2012

python - 如何使用 FreeTDS ODBC 连接到 SQL Server

mysql - 选择查询案例问题