Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the default database in Sql Server from code?

I can't seem to figure out how to set the default database in Sql Server from code. This can be either .Net code or T-Sql (T-Sql would be nice since it would be easy to use in any language). I searched Google and could only find how to do it in Sql Server Management Studio.

like image 465
Brian Avatar asked Sep 19 '08 21:09

Brian


People also ask

What is the default database in SQL Server?

master - keeps the information for an instance of SQL Server. msdb - used by SQL Server Agent. model - template database copied for each new database.

How do I change my default database location?

View or change the default locations for database files In Database default locations, view the current default locations for new data files and new log files. To change a default location, enter a new default pathname in the Data or Log field, or click the browse button to find and select a pathname.

How do I create a default schema in SQL Server?

In order to set the default schema for a Windows Group, open SQL Server Management Studio, navigate to Security > Logins, right click on the Windows Group that you want to change and choose Properties. The below window will then open.


1 Answers

ALTER LOGIN should be used for SQL Server 2005 or later:

http://technet.microsoft.com/en-us/library/ms189828.aspx

ALTER LOGIN <login_name> WITH DEFAULT_DATABASE = <default_database>

sp_defaultdb eventually will be removed from SQL Server:

http://technet.microsoft.com/en-us/library/ms181738.aspx

like image 126
Tim Avatar answered Sep 18 '22 01:09

Tim