{"id":66323,"date":"2006-10-05T15:38:00","date_gmt":"2006-10-05T15:38:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/10\/05\/how-can-i-tell-whether-a-specified-table-exists-in-a-sql-server-database\/"},"modified":"2006-10-05T15:38:00","modified_gmt":"2006-10-05T15:38:00","slug":"how-can-i-tell-whether-a-specified-table-exists-in-a-sql-server-database","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-tell-whether-a-specified-table-exists-in-a-sql-server-database\/","title":{"rendered":"How Can I Tell Whether a Specified Table Exists in a SQL Server Database?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I tell whether a specified table exists in a SQL Server database?<BR><BR>&#8212; LL<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, LL. Well, as it turns out it\u2019s absolutely impossible to tell whether a specified table exists in a SQL Server database. So, sorry, time to go back to watching the Major League Baseball playoffs. By the way, who else out there hates the Dodgers? Don\u2019t be shy; raise your hands.<\/P>\n<P>Hmmm \u2026. OK, if you say so. Um, we\u2019ve just been informed of two things. First, Microsoft is paying us to answer scripting questions, not to watch the Major League Baseball playoffs. Second, those of us here at Microsoft don\u2019t hate the Dodgers; in fact, we love everyone and everything.<\/P>\n<P>Well, OK, maybe not the Oakland Raiders. But everyone else. <\/P>\n<P>To tell you the truth, answering scripting questions isn\u2019t quite as much fun as watching baseball. (It\u2019s close, but \u2026.) On top of that, if you saw our last paycheck you might not think that Microsoft is paying us at all, let alone paying us to answer scripting questions. Still, duty calls, right? With that in mind, here\u2019s a script that can tell you whether a database named Fabrikam (located on the computer atl-sql-01) contains a table named EmployeeData: <\/P><PRE class=\"codeSample\">Const adOpenStatic = 3\nConst adLockOptimistic = 3<\/p>\n<p>Set objConnection = CreateObject(&#8220;ADODB.Connection&#8221;)\nSet objRecordSet = CreateObject(&#8220;ADODB.Recordset&#8221;)<\/p>\n<p>objConnection.Open _\n    &#8220;Provider=SQLOLEDB;Data Source=atl-sql-01;&#8221; &amp; _\n        &#8220;Trusted_Connection=Yes;Initial Catalog=Fabrikam&#8221;<\/p>\n<p>objRecordSet.Open &#8220;SELECT * FROM sysobjects Where Name= &#8216;EmployeeData&#8217; AND xType= &#8216;U'&#8221;, _\n        objConnection, adOpenStatic, adLockOptimistic<\/p>\n<p>If objRecordset.RecordCount = 0 Then\n    Wscript.Echo &#8220;The table is not in the database.&#8221;\nElse\n    Wscript.Echo &#8220;The table is in the database.&#8221; \nEnd If\n<\/PRE>\n<P>Before we begin we should note that we won\u2019t be able to explain all the intricacies of connecting to databases in today\u2019s column; that\u2019s a bit more than we have room for. (Or time for: the Mets-Dodgers game starts in less than 20 minutes.) If you\u2019re new to writing database scripts then we encourage you to check out our on-demand webcast <A href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkId=22089\" target=\"_blank\"><B>Database Scripting for System Administrators<\/B><\/A>. (OK, <I>not<\/I> one of our more inspired titles. But we were probably distracted by a baseball game or something at the time.)<\/P>\n<P>As for our sample script, we start out by defining a pair of constants, adOpenStatic and adLockOptimistic; we\u2019ll use this constants to assign the cursor and lock types when retrieving records from the database. (Like we said, take a peek at the <A href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkId=22089\" target=\"_blank\"><B>webcast<\/B><\/A> if you aren\u2019t familiar with terms like cursor and lock types.) After creating instances of the <B>ADODB.Connection<\/B> and <B>ADODB.Recordset<\/B> objects we then use this line of code to connect to SQL Server on the computer atl-sql-01:<\/P><PRE class=\"codeSample\">objConnection.Open _\n    &#8220;Provider=SQLOLEDB;Data Source=atl-sql-01;&#8221; &amp; _\n        &#8220;Trusted_Connection=Yes;Initial Catalog=Fabrikam&#8221;\n<\/PRE>\n<P>And yes, as a matter of fact it <I>is<\/I> easy to modify this script for your own use. Just replace <B>atl-sql-01<\/B> with the name of the SQL Server machine you want to connect to, then replace <B>Fabrikam<\/B> with the name of the database you want to check. (Don\u2019t be confused by the label <I>Initial Catalog<\/I>; we\u2019re really just talking about a database here.)<\/P>\n<P>What do you know: Nick Swisher just led off the top of the ninth with a double. <\/P>\n<P>Uh, or so we heard someone mention as they walked by. Too bad those people aren\u2019t busy working like the rest of us.<\/P>\n<P>After we\u2019ve made the connection to the SQL Server computer we then issue the following command in order to return a recordset consisting of all the tables in the Fabrikam database that have the name EmployeeData:<\/P><PRE class=\"codeSample\">objRecordSet.Open &#8220;SELECT * FROM sysobjects Where Name= &#8216;EmployeeData&#8217; AND xType= &#8216;U'&#8221;, _\n        objConnection, adOpenStatic, adLockOptimistic\n<\/PRE>\n<P>What do you mean you don\u2019t see how we did that? Why it\u2019s as plain as the nose on your \u2013 oh, maybe it\u2019s not so plain after all, is it? Well, let\u2019s see what we can do to help clear things up. To begin with, all SQL Server databases have a table named sysobjects. Sysobjects has one function: it keeps track of all the objects (tables, logs, constraints, stored procedures, etc.) that have been created within the database. As you can probably guess, that\u2019s why we query the sysobjects table: if anyone knows whether or not a table exists sysobjects does. We then include the Where clause <B>Where Name = &#8216;EmployeeData&#8217;<\/B> for obvious reasons: that\u2019s the name of the table we\u2019re interested in. <\/P>\n<P>That leaves us with this additional clause:<\/P><PRE class=\"codeSample\">AND xType= &#8216;U&#8217;\n<\/PRE>\n<P>As it turns out, sysobjects includes a field named <B>xType<\/B>. This field is used to keep track of the object type; a user table has an xType of <B>U<\/B> (system tables have an xType of <B>S<\/B>). This AND clause leaves us with a query that says, \u201cBring me back a collection of all the objects in the database that not only have the Name EmployeeData but also (AND) happen to be a user table.\u201d<\/P>\n<P>To determine whether or not the database really <I>does<\/I> contain a user table named EmployeeData all we have to do is take a look at the recordset\u2019s <B>RecordCount<\/B> property, a property which \u2013 logically enough \u2013 tells us how many records are in the recordset. RecordCount can be only one of two values: 0, meaning no such tables exist; or 1, meaning that the EmployeeData table <I>does<\/I> exist. (The RecordCount can never be greater than 1 simply because table names must be unique. That means you can only have, at most, one table named EmployeeData.) In this block of code we examine the value of the RecordCount property and then echo back the appropriate message:<\/P><PRE class=\"codeSample\">If objRecordset.RecordCount = 0 Then\n    Wscript.Echo &#8220;The table is not in the database.&#8221;\nElse\n    Wscript.Echo &#8220;The table is in the database.&#8221; \nEnd If\n<\/PRE>\n<P>Whoa, 5-2 Oakland, bottom of the ninth. That is, for those of you interested in that sort of thing.<\/P>\n<P><I>(Editor\u2019s Note: Update, <\/I><I>Oakland<\/I><I> wins <\/I><I>5-2. Go Dodgers.)<\/I><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I tell whether a specified table exists in a SQL Server database?&#8212; LL Hey, LL. Well, as it turns out it\u2019s absolutely impossible to tell whether a specified table exists in a SQL Server database. So, sorry, time to go back to watching the Major League Baseball playoffs. By the [&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":[146,3,176,5],"class_list":["post-66323","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-databases","tag-scripting-guy","tag-sql-server","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I tell whether a specified table exists in a SQL Server database?&#8212; LL Hey, LL. Well, as it turns out it\u2019s absolutely impossible to tell whether a specified table exists in a SQL Server database. So, sorry, time to go back to watching the Major League Baseball playoffs. By the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66323","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=66323"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66323\/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=66323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}