//  home   //  advanced search   //  news   //  categories   //  sql build chart   //  downloads   //  statistics
 ASP FAQ 
Home
ASP FAQ Tutorials

   8000XXXX Errors
   ASP.NET 2.0
   Classic ASP 1.0
   Databases
      Access DB & ADO
      General SQL Server & Access Articles
      MySQL
      Other Articles
      Schema Tutorials
      Sql Server 2000
      Sql Server 2005
   General Concepts
   Search Engine Optimization (SEO)

Contact Us
Site Map

Search

Web
aspfaq.com
tutorials.aspfaq.com
databases.aspfaq.com

ASP FAQ Tutorials :: Databases :: Schema Tutorials :: Schema: How do I show the tables in a database?


Schema: How do I show the tables in a database?

There are several ways to retrieve table names from a database. Here is code that uses ADOX. This was tested with MDAC 2.5, and will work with either MS Access or SQL Server. 
 
<% 
    dbname = "databasename" 
 
    ' Use this string if using Access: 
    ' ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" 
    ' ConnStr = ConnStr & "<path>\" & dbname & ".mdb" 
 
    ' Use this string if using SQL Server: 
    ConnStr = "provider=SQLOLEDB;network=DBMSSOCN;" 
    ConnStr = ConnStr & "uid=<uid>;pwd=<pwd>;server=" 
    ConnStr = ConnStr & "<x.x.x.x>;database=" & dbname 
 
    set adoxConn = CreateObject("ADOX.Catalog") 
    set adodbConn = CreateObject("ADODB.Connection") 
    adodbConn.open ConnStr 
    adoxConn.activeConnection = adodbConn 
    for each table in adoxConn.tables 
        if table.type="TABLE" then 
            response.write table.name & "<br>" 
        end if 
    next 
    adodbConn.close: set adodbConn = nothing 
    set adoxConn = nothing 
%>
 
And here is the code that uses a system stored procedure (note that <uid> needs to have at least 'datareader' access to <dbname>). 
 
<% 
    dbname = "databasename" 
 
    ConnStr = "provider=SQLOLEDB;network=DBMSSOCN;" 
    ConnStr = ConnStr & "uid=<uid>;pwd=<pwd>;server=" 
    ConnStr = ConnStr & "<x.x.x.x>;database=" & dbname 
     
    set adodbConn = CreateObject("ADODB.Connection") 
    adodbConn.Open ConnStr 
     
    set rs = adodbConn.execute("EXEC sp_tables") 
    do while not rs.eof 
        if rs("table_type") = "TABLE" then 
            Response.Write rs("table_name") & "<br>" 
        end if 
        rs.movenext 
    loop 
     
    rs.close: set rs = nothing 
    adodbConn.Close: set adodbConn = nothing 
%>
 
For SQL Server, if you have access to Query Analyzer (and just want a table list, not necessarily for code), you can simply type the following and hit F5: 
 
EXEC sp_tables 
 
-- or 
 
SELECT 
    Owner = TABLE_SCHEMA, 
    TableName = TABLE_NAME 
FROM 
    INFORMATION_SCHEMA.TABLES 
WHERE 
    TABLE_TYPE = 'BASE TABLE' 
    AND OBJECTPROPERTY(OBJECT_ID(TABLE_NAME), 'IsMsShipped') = 0 
ORDER BY 
    TABLE_SCHEMA, 
    TABLE_NAME
 
If you want to include views, you can do this: 
 
SELECT 
    Type = TABLE_TYPE, 
    Owner = TABLE_SCHEMA, 
    TableName = TABLE_NAME 
FROM 
    INFORMATION_SCHEMA.TABLES 
WHERE 
    TABLE_TYPE IN ('VIEW', 'BASE TABLE') 
    AND OBJECTPROPERTY(OBJECT_ID(TABLE_NAME), 'IsMsShipped') = 0 
ORDER BY 
    TABLE_TYPE, 
    TABLE_SCHEMA, 
    TABLE_NAME
 
For more information on retrieving view information specifically, see Article #2526
 
And finally, for Access, you can run this query: 
 
SELECT [Name] 
    FROM MSysObjects 
    WHERE [Type] = 1 
        AND Flags = 0

Related Articles

Schema: How do I list all the indexes in a database?
Schema: How do I list the databases on my server?
Schema: How do I show all the primary keys in a database?
Schema: How do I show all the triggers in a database?
Schema: How do I show the columns for a table?
Schema: How do I show the description property of a column?
Schema: How do I show the parameters for a function or stored procedure?
Schema: How do I show the stored procedures in a database?
Schema: How do I show the user-defined functions (UDFs) in a database?
Schema: How do I show the views in a SQL Server database?

 

 


Created: 7/16/2000 | Last Updated: 6/9/2004 | broken links | helpful | not helpful | statistics
© Copyright 2006, UBR, Inc. All Rights Reserved. (86)

 

Copyright 1999-2006, All rights reserved.
Finding content
Finding content.  An error has occured...