How does sqlserver obtain the names of all tables in a database and the names of all fields in a table?

Source: Internet
Author: User

1. query all database names in the database:

 
 SelectNameFromMaster.. sysdatabasesOrder ByName

2. query all the table names in a database:

 
 SelectNameFromSysobjectsWhereXtype='U' Order ByName

3. query table structure information:

 1   Select ( Case   When A. colorder =  1   Then D. Name Else   Null   End  ) Table Name,  2   A. colorder FIELD No., A. Name field name,  3 ( Case   When   Columnproperty (A. ID, A. Name, '  Isidentity ' ) =  1   Then   '  √  '  Else   ''   End  ) ID,  4 ( Case   When ( Select   Count (* ) From  Sysobjects  5   Where (Name In ( Select Name From  Sysindexes  6   Where (ID = A. ID) And (Indid In    7 (Select Indid From  Sysindexkeys  8   Where (ID = A. ID) And (Colid In    9 ( Select Colid From Syscolumns Where (ID = A. ID) And (Name=  A. Name )))))))  10   And (Xtype =   '  PK  ' )) >  0   Then   '  √  '   Else   ''   End ) Primary key, B. Name type, A. Length occupies the number of bytes,  11   Columnproperty (A. ID, A. Name, '  Precision  ' ) As  Length,  12   Isnull ( Columnproperty (A. ID, A. Name, '  Scale  ' ), 0 ) As Decimal places ,( Case   When A. isnullable =  1   Then   '  √  '  Else   ''   End  ) Can be empty,  13   Isnull (E. Text ,'' ) Default value, Isnull (G. [  Value  ] , '   ' ) As   [  Description  ]  14   From  Syscolumns  15   Left   Join Policypes B On A. xtype =  B. xusertype  16   Inner   Join Sysobjects d On A. ID = D. id And D. xtype =  '  U  '   And D. Name <> '  Dtproperties  '   17   Left   Join Syscomments E On A. cdefault =  E. ID  18   Left   Join SYS. extended_properties g On A. ID = G. major_id And A. colid=  G. minor_id  19   Left   Join SYS. extended_properties F On D. id = F. Class And F. minor_id =  0  20   Where B. Name Is   Not   Null 21   --  Where D. Name = 'table to be query' -- if only the specified table is queried, add this condition.  22   Order   By A. ID, A. colorder

 

 

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.