Use SQL statements to query the table structure!

Source: Internet
Author: User
Tags class generator
I am just working on a small project. A table in the database has 20 fields and I used one previously. NET Entity class generator input one by one is still idle, so I plan to find a time to rebuild one. NET Entity class, which can automatically generate an entity class through the selected database and user table, and the entity class also has annotations, that is, the description field of the field, and then add a function to generate a database document, in fact, it is just to output a beautiful table with the field characters, types, and field descriptions. It is easier to generate an HTML table as long as it can be obtained. I searched for it online, obtain the following useful SQL statements: -- Query non-system databases
Select Name From Master.. sysdatabases Where Dbid > 4

-- Select all tables in the water database
Use   [ Water ]   Select Name From Sysobjects Where Xtype =   ' U '   Or Xtype =   ' S '

-- Select all user tables under the water database
Use   [ Water ]   Select Name From Sysobjects Where Xtype =   ' U '   And   Objectproperty (ID, ' Ismsshipped ' ) =   0

-- Query the field name, length, type, and field description of the Admin table in the water database.
Use   [ Water ]   Select A. [ Name ]   As   ' Field name ' , A. Length ' Length ' , C. [ Name ]   ' Type ' , E. Value As   ' Field description '   From Syscolumns
Left Join Policypes B On A. xusertype = B. xusertype
Left Join Policypes C On A. xtype = C. xusertype
Inner Join Sysobjects d On A. ID = D. id And D. xtype = ' U '  
Left   Join SYS. extended_properties E On A. ID = E. major_id And A. colid = E. minor_id And E. Name = ' Ms_description '
Where D. Name = ' Admin '  

Here, sys. the extended_properties system built-in view is used to store field descriptions. This table is available only in mssql2005 and later versions. it cannot be removed. If SQL2000 is used, it indicates that there is another table, and the table name is forgotten. I am too lazy to check it if I don't need it...
We hereby record it !!!

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.