How list out all tables in MSSQL?

19,410

Solution 1

Alternate way, also fetches schema name

SELECT TABLE_CATALOG ,
        TABLE_SCHEMA ,
        TABLE_NAME ,
        TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES

Solution 2

SELECT * FROM sys.Tables;

Should do the magic :-D

And if u want to see all columns, i would do

SELECT TOP 1 * From Tablename;

so u'll get one row with all Columns, its not perfect but it does the trick if u just want to know sth

Share:
19,410
ADM
Author by

ADM

Programming enthusiast, delighted to have been born in this era. At this moment I’m looking to work remotely so I can continue giving the best of my work in the moments of better productivity within the connectivity of team work

Updated on June 15, 2022

Comments

  • ADM
    ADM almost 2 years

    I'm using the code below to show the tables in my database.

    I get "Connected to database" but nothing else. Is my code correct? can I use another way to get the info I need?

    <?php 
    $link = mssql_connect('HOST', 'user', 'pass');
    
    if (!$link || !mssql_select_db('dbname', $link)) {
        die('Unable to connect or select database!');
    }else{
    echo"Connected to database";
    }
    
    
    $v = mssql_query("Select name from sysobjects where type like 'u'");
    $row = mssql_fetch_array($v);
    
    echo "<br>";  echo $row[0]; echo "<br>";
    
    
    mssql_free_result($v);
    ?>
    
  • ADM
    ADM about 13 years
    Thaks MadBender, but still no results, blank page. Do you think the problem may be database permissions or something like that? I'm not hosting the database and the people hosting it may have forgotten something. Thanks again
  • MadBender
    MadBender about 13 years
    Yes, it's possible. Try to connect and execute this query with some application like SSMS or SQL Explorer. It may give you more detailed error message.
  • MadBender
    MadBender about 13 years
    Or maybe there are no tables in that DB
  • ADM
    ADM about 13 years
    Hmm I wish I could install any of this in the host, but I cannot, another way of doing it? Thanks once again
  • ADM
    ADM about 13 years
    Probably you're right. I'll get to the hosting service. Thanks a ton!