When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

The SQL Guru Answers your Questions...


Today's question comes from Keith:

Help!!!!! I am trying to write code that will do a SELECT statement from a table (if it exists). I do not know if the table exists or not however. Is there a SQL statement that will determine if the table exists or not?

In earlier version of SQL Server you could do a query directly to the sysobjects table. (For more information be sure to read: a past SQLGuru question on Determining the Creation Dates of Tables, an article on Listing the Tables and Columns in a Database, and an article on Using the sysobjects Table!) This table had one entry in it for each database object such as tables, views, stored procedures, etc. You could query it just like any other table. You actually still can but there is a better way.

You may not be able to see the sysobjects table in Enterprise Manager. When you register a server in Enterprise Manager, one of the options is "Show System Databases and System Objects." When this option is cleared it will hide the master, model, msdb and tempdb databases. It will still show pubs and Northwind since those are user databases. It will also hide system tables such as sysobjects and syscomments. There are 18 system tables and 20 system views. Even if these tables and views are hidden from Enterprise Manager you can still query them directly in Query Manager or an ASP page.

One word of advice: DON'T UPDATE system tables or views directly without knowing EXACTLY what you are doing! Just don't. Ever.

SQL Server 7.0 added something called Information Schema views to the list of system objects. These views were added to conform to the SQL-92 standards. Basically these views provide a consistent view of certain system objects such as tables and views. Using these views will spare you any problems if Microsoft changes the structure or functionality of the sysobjects table in future versions.

We are concerned with a view called TABLES. It lists all the tables and views in the database including system tables. It even lists itself. We name an object in SQL Server using the syntax database.owner.object. In Information Schema terminology, database is called Catalog, owner is called Schema and object is called Object. These are the first three fields in the TABLES view. SQL Server labels them TABLE_CATALOG, TABLE_SCHEMA and TABLE_NAME. The fourth field is TABLE_TYPE and contains either BASE TABLE or VIEW.

So Keith, back to your question. If you want to select the first record from a table called Table8 but only if it exists, you might write something like this:

if EXISTS (select * from INFORMATION_SCHEMA.tables where table_name = 'Table8')
Select Top 1 * from Table8

Okay, I threw two extras in at the end and they are both in capital letters in the query. The first is the EXISTS keyword. Basically the IF statement will evaluate to true if the query following EXISTS returns one or more records. It doesn't actually run the entire query, it just makes sure there is at least one record to return. This is a very quick way to check for the existance of records.

The second is INFORMATION_SCHEMA. Whenever you refer to one of the Information Schema views you need to qualify it with the owner INFORMATION_SCHEMA. There are also Information Schema views for columns, domain constraints, referential constraints and a few others. You can find more details in Books Online. Hope this helps.

  • Answered by SQLGuru graz - SQLTeam.com


    Read Other SQL Guru Questions


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES