How list out all tables in MSSQL?
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 = m开发者_开发问答ssql_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);
?>
Alternate way, also fetches schema name
SELECT TABLE_CATALOG ,
TABLE_SCHEMA ,
TABLE_NAME ,
TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES
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
I do this:
// Check if table exists
$sqlExist = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'dem';";
$stmtExist = sqlsrv_query( $conn_mssql, $sqlExist );
$exist = $row = sqlsrv_fetch_array( $stmtExist, SQLSRV_FETCH_ASSOC);
if ($exist == "") {
echo "Die abgefragte Tabelle existiert nicht oder ist nicht erreichbar!";
} else {
// MSSQL QUERY ITSELF
$sql = "SELECT * from dem;";
$stmt = sqlsrv_query( $conn_mssql, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
echo $row['id'] . " " . $row['cdemo2'] . ", " . $row['cdemo1'] . "<br>";
}
sqlsrv_free_stmt( $stmt);
}
精彩评论