Determining if a Database or Table Exists in SQL Server

This is, most likely, not going to be that practical for really anyone out there. But since I'm working on the ORM, I came across a way of determining whether or not a database exists in SQL Server. So let's look at the code:

SELECT * FROM Master.sys.Databases where name='DatabaseName'

Obviously this can be modified but what you should learn from the above one liner is that database information can be found in the Master.sys.Databases table. And in fact we have a very similar one liner that can be used to determine whether or not a table exists within a database:

SELECT * FROM sys.Tables where name='TableName'

Note that the above is using the database that you want to search within and not the master database. Anyway, that's all there is to it. But then again where you'd end up using this, I don't know. Anyway, hope this helps out someone. So try it out and happy coding.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 6/4/2009 at 11:30 AM
Tags: , , ,
Categories: General
Post Information: Permalink | Comments (1) | Post RSSRSS comment feed

Comments

Gut Instinct

Friday, March 05, 2010 2:00 PM

trackback

Helpful SQL Queries for SQL Server in C#

Helpful SQL Queries for SQL Server in C#

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading