PRB: Security Context of Dynamic SQL Statements Inside a Stored Procedure (301299)
The information in this article applies to:
- Microsoft SQL Server 2000 (all editions)
- Microsoft SQL Server 7.0
This article was previously published under Q301299 SYMPTOMS
When you are running a stored procedure with a dynamic execution query ( sp_executesql or EXECUTE), you may receive the following error message:
Server: Msg 229, Level 14, State 5, Line 1
'permission' permission denied on object 'object', database 'database', owner 'owner'.
CAUSE
This behavior occurs because a dynamic execution query (sp_executesql or EXECUTE) executes in a separate context from the main stored procedure; it executes in the security context of the user that executes the stored procedure and not in the security context of the owner of the stored procedure.
Note: You should take this behavior into account while you are determining ownership chains.
WORKAROUND
To work around this issue:
- You have to correctly grant the necessary permission for each underlying object that is mentioned in the dynamic execution query.
- You can execute a SELECT statement with the INTO clause to create a temporary table that contains all the data in the original table, and then EXEC your SQL statement against the temporary table. This is a viable solution if the tables that you are dealing with are small.
Modification Type: | Major | Last Reviewed: | 10/16/2003 |
---|
Keywords: | kbprb KB301299 |
---|
|