How does SQL Server query all the tables in a database with an SQL statement?

Source: Internet
Author: User
Tags sql server query

1, using the sysobjects system table in this table, each object created in the database (for example, constraints, defaults, logs, rules, and stored procedures) has a row, and we filter out all the records in the table that are xtype equals u, which is the table in the database. The example statements are as follows:

SELECT * from sysobjects where xtype= ' U '

Note: In SQL SERVER2005, the sys.objects catalog view appears in place of the sysobjects system table, and in SQL SERVER2005 and later versions, You can query all tables using sysobjects system tables and any object in the Sys.objects catalog view.

2, use the Sys.tables catalog view sys.tables catalog view to return one row for each Table object. The example statements are as follows:

SELECT * FROM Sys.tables

Note: The Sys.tables catalog view is also available only in SQL SERVER2005 and later versions.

3, using stored procedures sp_tables sp_tables stored procedures, you can return a list of objects that can be queried in the current environment. This represents any object that can appear in the FROM clause. We can execute the following statement:

EXEC sp_tables

It is the table information that filters out all records that table_type equals table in the result set.

Other useful system functions

--Read all table names in the library
Select name from sysobjects where xtype= ' u '
--Reads all column names of the specified table
Select name from syscolumns where id= (select Max (id) from sysobjects where xtype= ' u ' and name= ' table name ')
Get database table names and fields
The role of each system table in SQL Server
Sysaltfiles the primary database to save the database file
Syscharsets primary database character set and sort order
Sysconfigures Primary Database configuration options
Syscurconfigs Primary Database Current configuration options
sysdatabases databases in the primary database server
Syslanguages Primary Database language
syslogins Main Database login account information
Sysoledbusers Primary Database link Server login information
sysprocesses master database process
Sysremotelogins master database telnet account
syscolumns each database column
Sysconstrains per database limit
Sysfilegroups per database filegroup
Sysfiles per database file
Sysforeignkeys each database external keyword
Sysindexs per database index
Sysmenbers each database role member
Sysobjects all database objects per database
Syspermissions per database permissions
Systypes per database user-defined data type
Select Column name =name from syscolumns where id=object_id (N ' table name to look up ')

How does SQL Server query all the tables in a database with an SQL statement?

Related Article

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.