{"id":555,"date":"2013-02-27T15:35:56","date_gmt":"2013-02-27T10:05:56","guid":{"rendered":"http:\/\/www.harshbaid.in\/?p=555"},"modified":"2020-01-27T23:33:04","modified_gmt":"2020-01-27T18:03:04","slug":"mssql-change-default-database-by-sql-query","status":"publish","type":"post","link":"https:\/\/www.harshbaid.in\/2013\/02\/27\/mssql-change-default-database-by-sql-query\/","title":{"rendered":"MSSQL – Change default database by sql query"},"content":{"rendered":"
\"Microsoft<\/a>
Microsoft SQL Server Management Studio showing a query, the results of the query, and the Object Explorer pane while connected to a SQL Server database engine instance. (Photo credit: Wikipedia)<\/figcaption><\/figure>\n

Dear reader,<\/p>\n

Recently I noticed in my daily work that every time I logged in MS SQL<\/a> I was required to select the database which I had to work on and this was regularly in use by me and this lead me to find the solution to change the default database selection after I log on to MS SQL using SSMS to reduce the hassle and also so that I don’t execute queries on different databases<\/a> by mistake.<\/p>\n

There is an inbuilt stored procedure<\/a> name sp_defaultdb<\/a> which can be used as below
\n<\/a><\/p>\n

Exec sp_defaultdb @loginame='Domain\/UserName', @defdb='MyDefaultDatabaseName'<\/pre>\n

Here value of parameter @loginname<\/em> is User Name such as ‘sa’ and it can change depending on the MSSQL<\/a> login configurations such as Sql Authentication or Windows Authentication<\/a>. Example shown here is for Windows Authentication. Then value of parameter @defdb is the database name such as ‘master’ or ‘AdventureWorks’ and it will change as per the need to set the default database for the particular login specified.<\/p>\n

But this stored procedure seems soon to be obsolete as mentioned in MSDN<\/a> link here<\/a>. There is newer option available for this purpose – ALTER LOGIN<\/a> which has many other usability apart from setting the default database but I would stick to the topic and show its usage.<\/p>\n

ALTER LOGIN [Domain\\UserName] WITH DEFAULT_DATABASE = [MyDefaultDatabaseName]<\/pre>\n

I hope you found this helpful and learnt something new from this.<\/p>\n

Reference links: StackOverflow – How can I change my default database in SQL Server without using MS SQL Server Management Studio?<\/a><\/p>\n

Keep your comments flowing for what you prefer. \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"

Dear reader, Recently I noticed in my daily work that every time I logged in MS SQL I was required to select the database which I had to work on and this was regularly in use by me and this lead me to find the solution to change the default database selection after I log […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,15,16],"tags":[174,41,79,80,234,110,173,172],"yoast_head":"\nMSSQL - Change default database by sql query - Harsh Baid<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.harshbaid.in\/2013\/02\/27\/mssql-change-default-database-by-sql-query\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MSSQL - Change default database by sql query - Harsh Baid\" \/>\n<meta property=\"og:description\" content=\"Dear reader, Recently I noticed in my daily work that every time I logged in MS SQL I was required to select the database which I had to work on and this was regularly in use by me and this lead me to find the solution to change the default database selection after I log […]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.harshbaid.in\/2013\/02\/27\/mssql-change-default-database-by-sql-query\/\" \/>\n<meta property=\"og:site_name\" content=\"Harsh Baid\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/www.facebook.com\/harshbaidblog\" \/>\n<meta property=\"article:author\" content=\"https:\/\/facebook.com\/harshbaid.net\" \/>\n<meta property=\"article:published_time\" content=\"2013-02-27T10:05:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-01-27T18:03:04+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/upload.wikimedia.org\/wikipedia\/en\/thumb\/7\/73\/SQLServerManagementStudio.JPG\/300px-SQLServerManagementStudio.JPG\" \/>\n<meta name=\"author\" content=\"Harsh\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@baid_harsh\" \/>\n<meta name=\"twitter:site\" content=\"@baid_harsh\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Harsh\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.harshbaid.in\/2013\/02\/27\/mssql-change-default-database-by-sql-query\/\",\"url\":\"https:\/\/www.harshbaid.in\/2013\/02\/27\/mssql-change-default-database-by-sql-query\/\",\"name\":\"MSSQL - Change default database by sql query - Harsh Baid\",\"isPartOf\":{\"@id\":\"https:\/\/www.harshbaid.in\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.harshbaid.in\/2013\/02\/27\/mssql-change-default-database-by-sql-query\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.harshbaid.in\/2013\/02\/27\/mssql-change-default-database-by-sql-query\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/upload.wikimedia.org\/wikipedia\/en\/thumb\/7\/73\/SQLServerManagementStudio.JPG\/300px-SQLServerManagementStudio.JPG\",\"datePublished\":\"2013-02-27T10:05:56+00:00\",\"dateModified\":\"2020-01-27T18:03:04+00:00\",\"author\":{\"@id\":\"https:\/\/www.harshbaid.in\/#\/schema\/person\/aca775e041b3f59337de8a66e3b100ef\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.harshbaid.in\/2013\/02\/27\/mssql-change-default-database-by-sql-query\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.harshbaid.in\/2013\/02\/27\/mssql-change-default-database-by-sql-query\/#primaryimage\",\"url\":\"http:\/\/upload.wikimedia.org\/wikipedia\/en\/thumb\/7\/73\/SQLServerManagementStudio.JPG\/300px-SQLServerManagementStudio.JPG\",\"contentUrl\":\"http:\/\/upload.wikimedia.org\/wikipedia\/en\/thumb\/7\/73\/SQLServerManagementStudio.JPG\/300px-SQLServerManagementStudio.JPG\"},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.harshbaid.in\/#website\",\"url\":\"https:\/\/www.harshbaid.in\/\",\"name\":\"Harsh Baid\",\"description\":\"Sitecore & Microsoft Certified Software Engineer\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.harshbaid.in\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.harshbaid.in\/#\/schema\/person\/aca775e041b3f59337de8a66e3b100ef\",\"name\":\"Harsh\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.harshbaid.in\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5408300a5a7ff94b7438a6144a8eeae9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5408300a5a7ff94b7438a6144a8eeae9?s=96&d=mm&r=g\",\"caption\":\"Harsh\"},\"sameAs\":[\"http:\/\/www.harshbaid.in\",\"https:\/\/facebook.com\/harshbaid.net\",\"https:\/\/x.com\/baid_harsh\"],\"url\":\"https:\/\/www.harshbaid.in\/author\/harsh-baid\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MSSQL - Change default database by sql query - Harsh Baid","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.harshbaid.in\/2013\/02\/27\/mssql-change-default-database-by-sql-query\/","og_locale":"en_US","og_type":"article","og_title":"MSSQL - Change default database by sql query - Harsh Baid","og_description":"Dear reader, Recently I noticed in my daily work that every time I logged in MS SQL I was required to select the database which I had to work on and this was regularly in use by me and this lead me to find the solution to change the default database selection after I log […]","og_url":"https:\/\/www.harshbaid.in\/2013\/02\/27\/mssql-change-default-database-by-sql-query\/","og_site_name":"Harsh Baid","article_publisher":"http:\/\/www.facebook.com\/harshbaidblog","article_author":"https:\/\/facebook.com\/harshbaid.net","article_published_time":"2013-02-27T10:05:56+00:00","article_modified_time":"2020-01-27T18:03:04+00:00","og_image":[{"url":"http:\/\/upload.wikimedia.org\/wikipedia\/en\/thumb\/7\/73\/SQLServerManagementStudio.JPG\/300px-SQLServerManagementStudio.JPG"}],"author":"Harsh","twitter_card":"summary_large_image","twitter_creator":"@baid_harsh","twitter_site":"@baid_harsh","twitter_misc":{"Written by":"Harsh","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.harshbaid.in\/2013\/02\/27\/mssql-change-default-database-by-sql-query\/","url":"https:\/\/www.harshbaid.in\/2013\/02\/27\/mssql-change-default-database-by-sql-query\/","name":"MSSQL - Change default database by sql query - Harsh Baid","isPartOf":{"@id":"https:\/\/www.harshbaid.in\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.harshbaid.in\/2013\/02\/27\/mssql-change-default-database-by-sql-query\/#primaryimage"},"image":{"@id":"https:\/\/www.harshbaid.in\/2013\/02\/27\/mssql-change-default-database-by-sql-query\/#primaryimage"},"thumbnailUrl":"http:\/\/upload.wikimedia.org\/wikipedia\/en\/thumb\/7\/73\/SQLServerManagementStudio.JPG\/300px-SQLServerManagementStudio.JPG","datePublished":"2013-02-27T10:05:56+00:00","dateModified":"2020-01-27T18:03:04+00:00","author":{"@id":"https:\/\/www.harshbaid.in\/#\/schema\/person\/aca775e041b3f59337de8a66e3b100ef"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.harshbaid.in\/2013\/02\/27\/mssql-change-default-database-by-sql-query\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.harshbaid.in\/2013\/02\/27\/mssql-change-default-database-by-sql-query\/#primaryimage","url":"http:\/\/upload.wikimedia.org\/wikipedia\/en\/thumb\/7\/73\/SQLServerManagementStudio.JPG\/300px-SQLServerManagementStudio.JPG","contentUrl":"http:\/\/upload.wikimedia.org\/wikipedia\/en\/thumb\/7\/73\/SQLServerManagementStudio.JPG\/300px-SQLServerManagementStudio.JPG"},{"@type":"WebSite","@id":"https:\/\/www.harshbaid.in\/#website","url":"https:\/\/www.harshbaid.in\/","name":"Harsh Baid","description":"Sitecore & Microsoft Certified Software Engineer","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.harshbaid.in\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.harshbaid.in\/#\/schema\/person\/aca775e041b3f59337de8a66e3b100ef","name":"Harsh","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.harshbaid.in\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/5408300a5a7ff94b7438a6144a8eeae9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5408300a5a7ff94b7438a6144a8eeae9?s=96&d=mm&r=g","caption":"Harsh"},"sameAs":["http:\/\/www.harshbaid.in","https:\/\/facebook.com\/harshbaid.net","https:\/\/x.com\/baid_harsh"],"url":"https:\/\/www.harshbaid.in\/author\/harsh-baid\/"}]}},"_links":{"self":[{"href":"https:\/\/www.harshbaid.in\/wp-json\/wp\/v2\/posts\/555"}],"collection":[{"href":"https:\/\/www.harshbaid.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.harshbaid.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.harshbaid.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.harshbaid.in\/wp-json\/wp\/v2\/comments?post=555"}],"version-history":[{"count":8,"href":"https:\/\/www.harshbaid.in\/wp-json\/wp\/v2\/posts\/555\/revisions"}],"predecessor-version":[{"id":1037,"href":"https:\/\/www.harshbaid.in\/wp-json\/wp\/v2\/posts\/555\/revisions\/1037"}],"wp:attachment":[{"href":"https:\/\/www.harshbaid.in\/wp-json\/wp\/v2\/media?parent=555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.harshbaid.in\/wp-json\/wp\/v2\/categories?post=555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.harshbaid.in\/wp-json\/wp\/v2\/tags?post=555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}