Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Changing owners in a sproc

Author  Topic 

X002548
Not Just a Number

15586 Posts

Posted - 2008-02-01 : 10:26:27
Is there a way to change an owner inside the context of a sproc?

Like in DB2 you can Bind code to a particular owner, such that one code would execute against dbo, and another to HR (or whatever)

I've never heard or seen of this before in sql server

Anything like that conceptualy in SQL?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-02-01 : 12:05:50
EDIT:
lost part of my post...

I don't know of a way to "bind" a block of code to a specific user but you could create 2 versions of the SP, one owned by dbo, the other owned by HR.

exec myProc (as HR)
exec myProc (as dbo)

Of course you could also create an IF block with the <condition> being a test for user_name() or system_user

Be One with the Optimizer
TG
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2008-02-01 : 12:28:13
yeah, that's what I thought

I say it's a design issue



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-02-01 : 14:28:17
This what you want to do? The stored proc uses EXECUTE AS to set the user context dynamically.

create proc dbo.xx (@user sysname =null)
with execute as 'dbo'
as
if @user is not null begin execute as user = @user end
select [Current user] = user_name()
go
exec dbo.xx
go
exec dbo.xx 'dummy'
go
drop proc dbo.xx


Results:
Current user
------------------------
dbo

(1 row(s) affected)

Current user
------------------------
Dummy

(1 row(s) affected)


CODO ERGO SUM
Go to Top of Page
   

- Advertisement -