SQL query database name, table name, table column name

Source: Internet
Author: User
Query the database select * From master. dbo. sysdatabases where name = 'database name' and status <> 512 -- read all table names in the database (current database)
Select name from sysobjects where xtype = 'U'

-- Read all column names of a specified table
Select name from syscolumns where id = (select max (id) from sysobjects where xtype = 'U' and name = 'table name ')
Obtain database table names and fields
Functions of various system tables in sqlserver
Sysaltfiles: files stored in the database in the master database
Syscharsets primary database character set and sorting order
Sysconfigures master database configuration options
Current configuration options of the syscurconfigs master database
Database in the sysdatabases master database server
Main database language of syslanguages
Syslogins primary database Logon account information
Sysoledbusers master database connection Server login information
Sysprocesses master database Process
Sysremotelogins master database remote login account

Syscolumns each database Column
Sysconstrains per database limit
Sysfilegroups each database file group
Sysfiles: each database file
Sysforeignkeys external keywords for each database
Sysindexs each database index
Sysmenbers each database role member
Sysobjects all database objects in each database
Syspermissions permissions for each database
Systypes user-defined data types for each database
Sysusers each database user

//
How can I obtain all the column names in a table. SQL statement.
Select column name = name from syscolumns where id = object_id (n' name of the table to be queried ')

Use gpStrudy
Select name = "name" from syscolumns where id = object_id (N 'booktable ')
Obtain the attribute of a field
// This is obtained from a piece of code, using System. Data. OleDb;
Public int GetTableFields (String tableName, out String [] fields, out String [] fieldTypes)
...{
Try
...{
OleDbCommand dc = m_OleDb.CreateCommand (); // create an execution object for executing SQL queries
Dc. CommandText = "select * from" + tableName;
Dc. Transaction = m_OleTrans;

OleDbDataReader dr = dc. ExecuteReader (); // Execute SQL query
// Obtain the database architecture Information
DataTable schemaTable = dr. GetSchemaTable ();

Int fieldColumnCount = 0;

Fields = new String [schemaTable. Rows. Count];
FieldTypes = new String [schemaTable. Rows. Count];

For (int I = 0; I <schemaTable. Columns. Count; I ++)
...{
If (schemaTable. Columns [I]. ColumnName. IndexOf ("ColumnName")> = 0)
...{
// Obtain the field name

For (int k = 0; k <schemaTable. Rows. Count; k ++)
Fields [k] = schemaTable. Rows [k]. ItemArray [I]. ToString ();

FieldColumnCount ++;
If (fieldColumnCount> = 2)
...{
Dr. Close ();
Dc. Dispose ();

Dr = null;
Dc = null;
Return schemaTable. Rows. Count;
}
}
Else if (schemaTable. Columns [I]. ColumnName. IndexOf ("DataType")> = 0)
...{
// Obtain the field type

For (int k = 0; k <schemaTable. Rows. Count; k ++)
FieldTypes [k] = schemaTable. Rows [k]. ItemArray [I]. ToString ();

FieldColumnCount ++;
If (fieldColumnCount> = 2)
...{
Dr. Close ();
Dc. Dispose ();

Dr = null;
Dc = null;
Return schemaTable. Rows. Count;
}
}

}

Dr. Close ();
Dc. Dispose ();

Dr = null;
Dc = null;

Return 0;

}
Catch (Exception ee)
...{
Fields = new String [1];
FieldTypes = new String [1];
M_ErrorString = "Ado_Application: GetTableFields: error:" + ee. Message;
Return 0;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.