{"id":66473,"date":"2006-09-14T14:58:00","date_gmt":"2006-09-14T14:58:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/09\/14\/how-can-i-get-a-list-of-all-the-databases-on-a-sql-server-computer\/"},"modified":"2006-09-14T14:58:00","modified_gmt":"2006-09-14T14:58:00","slug":"how-can-i-get-a-list-of-all-the-databases-on-a-sql-server-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-a-list-of-all-the-databases-on-a-sql-server-computer\/","title":{"rendered":"How Can I Get a List of All the Databases on a SQL Server Computer?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I get a list of all the databases on a SQL Server computer?<BR><BR>&#8212; ER<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, ER. Before we answer your question, we\u2019d like to take a moment to offer our opinions on all the major \u2013 and controversial \u2013 issues of the day. We can do that because the Scripting Editor is on vacation and, well, you know what they say: when the cat\u2019s away, the mice will play.<\/P>\n<TABLE id=\"E2C\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Did we forget to call this a \u201cwell-deserved\u201d vacation? No; forgetfulness had nothing to do with us not calling it that.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>To tell you the truth, having the opportunity to show off the depth and breadth of his knowledge is important to the Scripting Guy who writes this column. That\u2019s because he has long been typecast as some sort of nerd who cares only about computers. For example, when he worked at the University of Washington he was on his way out for the day when he was asked to stop by the Dean\u2019s office and fix a computer. To do so he set down his book \u2013 a collection of short stories by P.G. Wodehouse \u2013 on a nearby table.<\/P>\n<P>\u201cP.G. Wodehouse! I just <I>love<\/I> P.G. Wodehouse!\u201d declared the administrator when she saw the book lying there. \u201cWhose book is this?\u201d<\/P>\n<P>\u201cMine,\u201d said the future Scripting Guy.<\/P>\n<P>&#8220;No, really, whose book is this?\u201d she said. \u201cI\u2019d love to talk to them about it.\u201d<\/P>\n<P>\u201cIt\u2019s mine,\u201d insisted the Scripting Guy.<\/P>\n<P>\u201cNever mind,\u201d she said. \u201cI\u2019ll find out later whose book it is.\u201d<\/P>\n<P>As she later explained, &#8220;I had no idea you&#8217;d ever read anything other than computer books.&#8221; <\/P>\n<TABLE id=\"ERD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. True fact: the Scripting Guy who writes this column has <I>never<\/I> read a book about computers or technology, at least not all the way through. Well, provided you don\u2019t count <I>The Brave Little Toaster<\/I>, of course.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>But now, at long last, the completely un-nerdish Scripting Guy who writes this column has a chance to show off his knowledge of current events. That\u2019s right, current events. All the important things going on in the world today. Important things he knows all about. Yes. All his knowledge and wisdom, all those things he\u2019s been dying to say but has never been allowed to.<\/P>\n<P>Right \u2026. <\/P>\n<P>So, um, that Dred Scott decision of 1857: what was up with <I>that<\/I>? <\/P>\n<P>You know what? When the Scripting Editor gets back she\u2019ll no doubt go through and delete all these insightful &#8212; albeit controversial &#8212; opinions anyway. It wouldn\u2019t be very nice to have her to come back from vacation and then have to do all that work, doesn&#8217;t? Therefore, solely in the spirit of making life easier for the Scripting Editor, let\u2019s just show you a script that can list all the databases on a computer running Microsoft SQL Server:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-ds-01&#8221; <\/p>\n<p>Set objConnection = CreateObject(&#8220;ADODB.Connection&#8221;)<\/p>\n<p>objConnection.Open _\n    &#8220;Provider=SQLOLEDB;Data Source=&#8221; &amp; strComputer &amp; &#8220;;&#8221; &amp; _\n        &#8220;Trusted_Connection=Yes;Initial Catalog=Master&#8221;<\/p>\n<p>Set objRecordset = objConnection.Execute(&#8220;Select Name From SysDatabases&#8221;)<\/p>\n<p>If objRecordset.Recordcount = 0 Then\n    Wscript.Echo &#8220;No databases could be found.&#8221;\nElse\n    Do Until objRecordset.EOF\n        Wscript.Echo objRecordset.Fields(&#8220;Name&#8221;)\n        objRecordset.MoveNext\n    Loop\nEnd If\n<\/PRE>\n<P>So how does this script work? How the heck would we know? What do we look like, a bunch of nerds?<\/P>\n<P>Good point; we probably <I>should<\/I> get rid of pocket protectors, shouldn\u2019t we? OK, well, in that case, the script starts out by assigning the name of the target computer to a variable named strComputer. We then use this line of code to create an instance of the <B>ADODB.Connection<\/B> object, the object we need to connect to the SQL Server service on the target computer:<\/P><PRE class=\"codeSample\">Set objConnection = CreateObject(&#8220;ADODB.Connection&#8221;)\n<\/PRE>\n<P>As soon we have a Connection object we can then call the <B>Open<\/B> method to actually connect to SQL Server:<\/P><PRE class=\"codeSample\">objConnection.Open _\n    &#8220;Provider=SQLOLEDB;Data Source=&#8221; &amp; strComputer &amp; &#8220;;&#8221; &amp; _\n        &#8220;Trusted_Connection=Yes;Initial Catalog=Master&#8221;\n<\/PRE>\n<P>We won&#8217;t explain SQL Server connections and connection strings in any detail today; however, we will note that we pass the Open method several parameters:<\/P>\n<TABLE id=\"E3E\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Parameter<\/B><\/P><\/TD>\n<TD>\n<P class=\"lastInCell\"><B>Description<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">Provider<\/P><\/TD>\n<TD>\n<P>Essentially indicates the type of database we&#8217;re connecting to. Because we&#8217;re connecting to SQL Server, we use the SQLOLEDB provider, which is short for &#8220;Microsoft OLE DB Provider for SQL Server.\u201d<\/P>\n<P>And no, you don\u2019t have to be a nerd to know what SQLOLEDB is short for.<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">Data Source<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Denotes the computer we&#8217;re connecting to. Here we indicate the computer by using the variable strComputer.<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">Trusted_Connection<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Indicates the user authentication mode. Setting this value to <I>Yes<\/I> causes us to connect uses Windows Authentication mode, which happens to be the recommended way of connecting to SQL Server.<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">Initial Catalog<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Specifies the default catalog to use when making the connection. We use the Master catalog because that&#8217;s where we&#8217;ll find the list of databases.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After opening the Master catalog we can then use the <B>Execute<\/B> command to return a recordset consisting of all the databases found on this SQL Server machine. To do that we execute the following query, which simply selects the <B>Name<\/B> of each item (that is, each database) stored in the table <B>SysDatabases<\/B>:<\/P><PRE class=\"codeSample\">Set objRecordset = objConnection.Execute(&#8220;Select Name From ysDatabases&#8221;)\n<\/PRE>\n<P>So what do we do with that recordset once we have it? Well, to begin with, we check the value of the <B>RecordCount<\/B> which tells us how many items are in the recordset. If the RecordCount is 0, then there aren\u2019t <I>any<\/I> databases on this computer; in turn, we echo back a statement to that effect.<\/P>\n<P>Of course, the odds are pretty good that the RecordCount <I>won\u2019t<\/I> be 0, meaning the computer has at least one database. If that\u2019s the case, we then set up a Do Until loop that loops through the entire collection of records (or, as we nerds would put it, until the recordset\u2019s <B>EOF<\/B> property \u2013 the End of File property \u2013 is True). Inside that loop we do two things: we echo back the value of the each record&#8217;s Name field, and we use the <B>MoveNext<\/B> method to advance from one record to the next:<\/P><PRE class=\"codeSample\">Wscript.Echo objRecordset.Fields(&#8220;Name&#8221;)\nobjRecordset.MoveNext\n<\/PRE>\n<P>Incidentally, don\u2019t forget the MoveNext; if you do, your script will simply echo back the name of the first database in the collection, and then continue to echo that one name forever and ever. That\u2019s because, with databases, you have to explicitly tell them to move on to the next record.<\/P>\n<TABLE id=\"EGH\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Do we know that through personal experience, like, say, because we left that line out when we first sat down to write the script for today\u2019s column? Well, we \u2013 um, sorry, but Microsoft doesn&#8217;t like us to talk about personal experiences in our columns &#8230;.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>And what do we get when we\u2019re all done? Why, we get a list of all the databases on the computer, of course:<\/P><PRE class=\"codeSample\">master\ntempdb\nmodel\nmsdb\npubs\nNorthwind\nCustomerScripts\nCommunityScripts\n<\/PRE>\n<P>Which is all we ever really wanted to get.<\/P>\n<P>Now, tell us the truth, ER: do <I>you<\/I> think the Scripting Guy who writes this column is a nerd? (Note: No fair asking the Scripting Son for <I>his<\/I> opinion of dear old dad.) No, really, we want to know your honest opinion.<\/P>\n<P>Yeah, well, who asked you anyway?<\/P>\n<P>But it really <I>was<\/I> his book. Really.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I get a list of all the databases on a SQL Server computer?&#8212; ER Hey, ER. Before we answer your question, we\u2019d like to take a moment to offer our opinions on all the major \u2013 and controversial \u2013 issues of the day. We can do that because the Scripting [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[19,146,3,176,5],"class_list":["post-66473","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-activex-data-objects-ado","tag-databases","tag-scripting-guy","tag-sql-server","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I get a list of all the databases on a SQL Server computer?&#8212; ER Hey, ER. Before we answer your question, we\u2019d like to take a moment to offer our opinions on all the major \u2013 and controversial \u2013 issues of the day. We can do that because the Scripting [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66473"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66473\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=66473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}