Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the default schema of a database in SQL Server 2005?

The following line works:

SELECT * FROM [myschema].users 

But this does not:

SELECT * FROM users 
like image 358
Imran Qadir Baksh - Baloch Avatar asked Nov 21 '11 06:11

Imran Qadir Baksh - Baloch


People also ask

How do I find the default schema for a database in SQL Server?

How can you tell the name of the current default schema? SELECT SCHEMA_NAME(); The SCHEMA_NAME() function will return the name of a schema if you pass it the schema_id but if you don't pass anything, it will return the name of the current default schema.

What is default schema in SQL Server?

The dbo schema is the default schema of every database. By default, users created with the CREATE USER Transact-SQL command have dbo as their default schema. The dbo schema is owned by the dbo user account.

How do I set default schema in DBO?

Set the Default Schema for a Windows Group First you need to map the login to the database by checking the Map column, then click on the Default Schema column to select the schema. In our example, we selected dbo as the default.

How do you change the schema of a table in SQL Server?

To change the schema of a table by using SQL Server Management Studio, in Object Explorer, right-click on the table and then click Design. Press F4 to open the Properties window. In the Schema box, select a new schema. ALTER SCHEMA uses a schema level lock.


2 Answers

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. 
like image 113
Adam Wenger Avatar answered Oct 09 '22 02:10

Adam Wenger


You can use:

ALTER USER Mary51 WITH DEFAULT_SCHEMA = Purchasing; 

To set the default schema for user.

like image 24
Ivan Avatar answered Oct 09 '22 02:10

Ivan