CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: SQL

  1. #1
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    78

    SQL

    Hi,

    Is there a way to check if a table exists using SQL? I need to check if a table exists so I can drop it. Currently, I take care of it by capturing the error it gives me when the table doesn't exist on the Access database when I try to drop it. However, I would prefer not to do this by responding to an error.

    Thanks all,

    RS


  2. #2
    Join Date
    May 2001
    Location
    Russia
    Posts
    200

    Re: SQL

    No standard SQL expression for this. You can find this information in system table or use ADOX collection Tables

    Andy Tower

  3. #3
    Join Date
    Aug 2000
    Location
    Namibia
    Posts
    139

    Re: SQL

    To retrieve all the tables something like...
    set cnn = new ADODB.Connection
    cnn.Open DB_CONNECTION

    set rstSchema = cnn.OpenSchema(adSchemaTables, Array(empty, empty, empty, "TABLE"))

    Do Until rstSchema.EOF
    rstSchema.MoveNext 'get nect table
    Loop



    HTH


  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: SQL

    You should be able to query (make a sql on) MSysObjects table. Is a system table where you can find the names of all existing objects in your access Db. Look for field "Name" and put condition where Name = your_serching_for_Table.
    If you do not see the table in Access, go to menu "tools", "options" and in tabs "View" check "system objects"

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    78

    Re: SQL

    Thanks for your suggestions!


  6. #6
    Join Date
    Jun 2001
    Posts
    1

    Re: SQL

    Here's a query you can run to check if a table exists on SQL Server, but I don't know if it will work for an Access Database.

    if exists (select * from sysobjects where id = object_id('dbo.your_table_name') and sysstat = 99)
    print 'it exists'
    else
    print 'doesn''t exist'

    Thanks.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured