Stu
2007-04-23 09:52:02 UTC
Why can't my stored procedures resolve table names to the current user's
default schema?
I have so-called "multi-tenant" SQL Server 2005 system where
- each customer's data is in a separate schema.
- each customer has a SQL login/user.
- the customer's schema is set as their default schema
e.g.
CREATE SCHEMA [customer12] AUTHORIZATION [customer12User]
GO
ALTER USER [customer12User] WITH DEFAULT_SCHEMA=[customer12]
GO
- there is also a single shared schema containing common data and stored
procedures
So, say we have the following tables...
customer11.Payments
customer12.Payments
...and the following stored procedure
shared.GetPayments
Inside GetPayments I would expect to be able to use unqualified table names
so that the actual table would be resolved by looking in the default schema
of the executing user, e.g:
create procedure shared.GetPayments
as
select * from Payments
However, I get the error: Invalid object name 'Payments'.
To double-check that I am actually executing as a user with the default
schema I do the following:
alter procedure shared.GetPayments
as
select current_user + ' (in proc)'
select * from Payments
...and then I execute like this:
select current_user
exec shared.GetPayments
Results:
----------
customer12User
(1 row(s) affected)
----------
customer12User (in proc)
(1 row(s) affected)
Msg 208, Level 16, State 1, Procedure GetPayements, Line 4
Invalid object name 'Payments'.
So it is executing as the right user...why can't it resolve Payments to
customer12.Payments?
Many thanks
Stu
default schema?
I have so-called "multi-tenant" SQL Server 2005 system where
- each customer's data is in a separate schema.
- each customer has a SQL login/user.
- the customer's schema is set as their default schema
e.g.
CREATE SCHEMA [customer12] AUTHORIZATION [customer12User]
GO
ALTER USER [customer12User] WITH DEFAULT_SCHEMA=[customer12]
GO
- there is also a single shared schema containing common data and stored
procedures
So, say we have the following tables...
customer11.Payments
customer12.Payments
...and the following stored procedure
shared.GetPayments
Inside GetPayments I would expect to be able to use unqualified table names
so that the actual table would be resolved by looking in the default schema
of the executing user, e.g:
create procedure shared.GetPayments
as
select * from Payments
However, I get the error: Invalid object name 'Payments'.
To double-check that I am actually executing as a user with the default
schema I do the following:
alter procedure shared.GetPayments
as
select current_user + ' (in proc)'
select * from Payments
...and then I execute like this:
select current_user
exec shared.GetPayments
Results:
----------
customer12User
(1 row(s) affected)
----------
customer12User (in proc)
(1 row(s) affected)
Msg 208, Level 16, State 1, Procedure GetPayements, Line 4
Invalid object name 'Payments'.
So it is executing as the right user...why can't it resolve Payments to
customer12.Payments?
Many thanks
Stu