sql-serversql-server-2008stored-procedures

Running Sql Server stored proc in context of caller


This is making me nuts and I'm sure the answer is SO easy.

I have multiple schemas that each have a view named "Task". I want to make a single stored proc that users running in multiple default schemas can execute successfully -- that stored proc does a select on the Task view.

So say I have these objects:

View: fr.Task (users with default schema of 'fr' get this if they just type "select * from Task" View: de.Task (ditto, but for users with default schema of 'de')

StoredProc: dbo.MyProc -- all users have execute permissions. The proc is simply:

select count(*) from Task 

Now I would expect (and I want) that if a user with default schema 'fr' did

exec dbo.MyProc

Then they would get the count of rows from view fr.Task. But instead they get error "Invalid object name 'Task'."

Is it not possible to make a generic storedproc that will execute a select in the schema of the running user?

Thx, Bill


Solution

  • To run a stored procedure in the context of the CALLER you can use the Execute As clause however, I suspect that this is not really what you are wanting to do.

    http://msdn.microsoft.com/en-us/library/ms188354.aspx