Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to set default schema from connection string?

With SQL Server 2005 and 2008 is it possible to set the default schema from the connection string? It'd be a lot easier if we didn't have to manually set the schema with SQL code.

like image 310
Earlz Avatar asked Jul 19 '10 15:07

Earlz


People also ask

How do I change the default database schema?

In the Database Administration view upper pane, select the database. Right-click the database and select Set default schema. The Set default schema dialog appears. Select the schema that you want to make the new default schema for the database from the drop-down list and click Ok to save the changes.

How do I change the default schema in SQL Server?

In that case, just use SQL Server Management Studio > Database > Security > Users > Properties and change the default schema there.

How do I mention a schema name in connection string?

Here's an example connection string with a DEFAULT DATABASE: Server=myServerName\myInstanceName,1433;Database=DEFAULT_DATABASE;User Id=myUsername;Password=myPassword; See link below for more examples: Connection strings for SQL Server 2012.

How do I change the default schema in SQL Developer?

Just create a new connection (hit the green plus sign) and enter the schema name and password of the new default schema your DBA suggested. You can switch between your old schema and the new schema with the pull down menu at the top right end of your window.


4 Answers

No, this is done at the database User level, not in the connection string.

For reference, here are all of the properties which can be set in a connection string: https://www.connectionstrings.com/all-sql-server-connection-string-keywords/

like image 169
TimS Avatar answered Sep 30 '22 18:09

TimS


You set the default schema based on the user who is logging in via the connection, not the connection itself.

ALTER USER Mary51 WITH DEFAULT_SCHEMA = Purchasing;

Reference:

  • ALTER USER
like image 26
OMG Ponies Avatar answered Sep 30 '22 18:09

OMG Ponies


change the default schema associated with that login

example

ALTER USER Mary51 WITH DEFAULT_SCHEMA = Purchasing;

More detail here: http://msdn.microsoft.com/en-us/library/ms176060.aspx

like image 20
SQLMenace Avatar answered Sep 30 '22 19:09

SQLMenace


If when you say "Schema," you mean "Owner" (i.e. dbo), then I believe the selected answer is correct.

However, if you mean "Database" instead, which in some vendor's lingo means the same thing as "Schema," then I have provided some more info below.

In the link that TimS provided:

  • All SQL Server SqlConnection properties

Scroll down to the row with these two properties:

Initial Catalog -or- Database

Here's an example connection string with a DEFAULT DATABASE:

Server=myServerName\myInstanceName,1433;Database=DEFAULT_DATABASE;User Id=myUsername;Password=myPassword;

See link below for more examples:

  • Connection strings for SQL Server 2012
like image 35
JohnB Avatar answered Sep 30 '22 19:09

JohnB