36

The following line works:

SELECT * FROM [myschema].users

But this does not:

SELECT * FROM users
marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Imran Qadir Baksh - Baloch
  • 30,546
  • 66
  • 170
  • 302

3 Answers3

73

A default schema is user-specific:

USE yourDatabase;
ALTER USER [yourUser] WITH DEFAULT_SCHEMA = myschema;

More information about the ALTER TABLE for SQL 2005 might help you as well.

As this is user-specific, if you have multiple users, you will need to execute this query (on each database) for each user whose default schema you want to update.

It is important to note:

The value of DEFAULT_SCHEMA is ignored if the user is a member of the sysadmin
fixed server role. All members of the sysadmin fixed server role have a default
schema of dbo.
Adam Wenger
  • 16,424
  • 6
  • 50
  • 63
3

You can use:

ALTER USER Mary51 WITH DEFAULT_SCHEMA = Purchasing;

To set the default schema for user.

Pubby
  • 50,432
  • 13
  • 129
  • 175
Ivan
  • 31
  • 1
  • 1
    just notice AdamWenger note [here](http://stackoverflow.com/a/8208124/426315) - `sysadmin` will always have `dbo` as default schema. – itsho Mar 28 '16 at 11:57
2

Like user960567, I tried the answers from both Adam and Ivan and could not get it working.

I was running:

ALTER USER myuser WITH DEFAULT_SCHEMA=newschema

as suggested, but after executing this and executing

SELECT SCHEMA_NAME()

it was still returning 'dbo' as the default schema.

To fix this I executed:

ALTER USER myuser WITH DEFAULT_SCHEMA=newschema EXECUTE AS USER='myuser'

and it worked as expected - now executing:

SELECT SCHEMA_NAME()

returns 'newschema'.

t_warsop
  • 982
  • 1
  • 23
  • 36