Methods to get all libraries, tables, and field names in SQL Server database

Source: Internet
Author: User
Tags joins

1. Get all database names: SELECT name fromMaster.. sysdatabases ORDER by Name2. Get all table names: SELECT name fromDatabaseName. SysObjects Where xtype='U'ORDER by Namextype='U': represents all user tables; XType='S': represents all system tables;3. Get all field names: SELECT name fromsyscolumns WHERE id=object_id ('TableName'access: How do I get this table if the table exists? SELECT* fromMsysobjectswhereName='Examresulttime'and type=1and flags=0//get all the table names in the databaseSELECT Name fromMsysobjectswhereType=1and flags=0      1, get all user names: Select name from Sysuserswherestatus='2'and islogin='1'IsLogin='1': Represents the account IsLogin='0': Indicates role status='2': Indicates the user account status='0': Represents a System account2, get all database names: Select name from Master. sysdatabases OrDER by Name3, get all table names: Select name from DatabaseName. SysObjects Where XType='U'OrDER by Name XType='U': represents all user tables; XType='S': represents all system tables; 4, get all field names: Select name from syscolumns Where ID=OBJECT_ID ('Table name"')             5, get all types of databases:SelectName fromsystypes6, gets the primary key field: Select name from syscolumns Where ID=OBJECT_ID ('Table name') and Colid= (SelectTop1Keyno fromSysindexkeyswhereID=OBJECT_ID ('Table name'))             7, gets the basic information of the table field: Program code Select fields name=RTrim (b.name), primary key=case when H.id isn't NULL then'PK'ELSE"'END, field type=type_name (B.xusertype) +case when b.colstat&1=1Then'[ID ('+ CONVERT (varchar, ident_seed (a.name)) +','+convert (VARCHAR,IDENT_INCR (a.name)) +')]'ELSE"'END, Length=b.length, allow empty=case b.isnullable when0Then'N'ELSE'Y'END, default value=isnull (E.text,"'), field description=isnull (C.value,"') from sysobjects A, syscolumns B left OUTER joins sysproperties C on b.ID= C.id and B.colid =C.smallid left OUTER joins syscomments E on B.cdefault=E.id left OUTER joins (Select g.id, g.colid from sysindexes F, Sysindexkeys G Where (F.id=g.id) and (F.indid=g.indid) and (f.indid>0) and (f.indid<255) and (f.status&2048) <>0) h on (b.id=h.id) and (b.colid=h.colid) Where (a.id=b.id) and (a.id=object_id ('the table to query'))  --to query the table, change the name of the table you want to query. OrDER by B.colid program code Select table name= CaseWhen a.colorder=1Then D.nameElse "'End, table description= CaseWhen a.colorder=1Then IsNull (F.value,"')Else "'End, field ordinal=a.colorder, field name=a.name, Logo= CaseWhen ColumnProperty (A.id,a.name,'isidentity')=1Then'√' Else "'end, primary key= CaseWhen exists (Select1From sysobjectswhereXtype='PK'and nameinch(Select name from sysindexes Where indidinch(Select indid from Sysindexkeys Where id=a.id and Colid=a.colid))) Then'√' Else "'End, type=b.name, field length=a.length, number of bytes occupied=columnproperty (A.id,a.name,'PRECISION'), number of decimal digits=isnull (ColumnProperty (A.id,a.name,' Scale'),0), allow null= CaseWhen a.isnullable=1Then'√'Else "'end, default value=isnull (E.text,"'), field description=isnull (G.[value],"') from syscolumns a LEFT join systypes B on A.xusertype=b.xusertype INNER join sysobjects D on (a.id=d.id) and (d.xtype='U') and (d.name<>'dtproperties') LEFT join syscomments e on A.cdefault=E.id left joins Sysproperties G on (a.id=g.id) and (a.colid=g.smallid) LEFT join Sysproperties F on (D.id=f.id) and (f.smallid=0)       --whereD.name='the table to query'--If you query only the specified table, add this condition to order by A.id,a.colorder

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.