views:

30

answers:

2

In Microsoft SQL Server, I have a schema XXX. Q: How do I create a user XXX such that issuing the following command:

SELECT * FROM Table

is the same as

SELECT * FROM XXX.Table

Here's what I have so far:

CREATE SCHEMA XXX authorization dbo -- I think
CREATE LOGIN XXX
WITH PASSWORD = '123';
CREATE USER ItDontMatter FOR LOGIN XXX
WITH DEFAULT_SCHEMA = XXX;
+4  A: 

Correct.

The DEFAULT_SCHEMA option is what you use

Specifies the first schema that will be searched by the server when it resolves the names of objects for this database user.

and

If DEFAULT_SCHEMA is left undefined, the database user will use dbo as its default schema. DEFAULT_SCHEMA can be set to a schema that does not currently exist in the database. DEFAULT_SCHEMA can be set before the schema that it points to is created.

gbn
In the "Schemas owned by this user box", I have to also check db_owner or it doesn't work.
cf_PhillipSenn
+2  A: 

As a small note to gbn's answer, the two are never exactly the same. If you do not specify the table owner, SQL Server will not cache your query because it's not sure about access rights. So for performance, always specify schema.table in your queries, procedures, functions and views.

Andomar
Have you a reference for that?
Rowland Shaw
@Rowland Shaw: Interesting. I ran some tests and it looks like the schema name makes no difference (cached plans are in the `syscachedobjects` table.) Where I work, we have a standing rule to include dbo everywhere to increase caching, but I guess it's not useful after all. Myth busted!
Andomar
@Andomar There are many other reasons to include schema of course, but it seems performance isn't one after all :)
Rowland Shaw