provide access to metadata in a relational database. Metadata is data about data and it contains information about all of the tables, views, columns, constraints, procedures, routines, and more.
According to MSDN, Information Schema Views (Transact-SQL)
"Information schema views provide an internal, system table-independent view of the SQL Server metadata. Information schema views enable applications to work correctly although significant changes have been made to the underlying system tables."
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqijA9pDeLh2FgoYIWUxzky3AnC3dfDtSa7lB8AbMByUZkt1kboH3mhPtR9rm3o_y4WsmJqjxyxXl5E3R7NKxtYEjLmp2OcqlkGCBngLdkWcqH4-uGFTjsp35hA_555dKyHl6zrtADLw-i/s320/InformationSchema-01.jpg)
Each information schema view contains metadata for all data objects stored in that particular database.
Show all Tables in a Database:
SELECT * FROM AdventureWorksLT.information_schema.tables
WHERE table_type = 'Base Table'
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVA_Zp3s2791l93OdDAT7hIEtdvqdquYX61uSn_2ZUUF-ANfmmQeCaZKQo0cG-4j5dXTRIfc8Z25caNPEoAGEsCPiTWOJ6N51YBDNfeOS6IDQ5558xoi8_Ab7MRpNMbh9MAJxxo9sktFGd/s320/InformationSchema-02.jpg)
The following T-SQL give us back the list of tables and related columns:
Because we needed to collect info about user generated tables, 'U' for 'xtype'
has been used in the code:
* xtype has two values - ‘U’ or ‘S’
* U = User generated tables
* S = System generated tables
USE AdventureWorksLT
SELECT table_name,
column_name,
data_type,
character_maximum_length
FROM Information_Schema.columns
WHERE table_name IN (Select name FROM sysobjects WHERE xtype = 'U')
ORDER BY table_name
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVPM-n5iYLgrx_V6RBFMexc8OHJFB7hOvWkXau3nNqCndDAiYPHg49-l9fCSZSNQ1ONFA3GM7lrMyyqGWMplcbQpdx2wGloon_BfzzjsRCaqoH2pnPc_8FVTmmqCBxestVXFzZ6UE2AHSg/s320/InformationSchema-03.jpg)
If you need to have access to other columns about the tables, please look at the available columns in Information_Schema.columns.
SQL Server 2005 System Tables and Views
SQL Server 2005 System Tables and Views
Using the Information Schema Views
No comments:
Post a Comment