How SQL Server obtains information

Source: Internet
Author: User

Get the table for the database
SELECTobj.name tablename,schem.name Schemname,CAST(     Case          when(SELECT COUNT(1) fromSys.indexesWHERE object_id=Obj.object_id  andIs_primary_key=1)>=1  Then 1        ELSE 0    END  as BIT) HasPrimaryKey fromsys.objects objInner JoinDbo.sysindexes IDX onObj.object_id=Idx.id andIdx.indid<=1INNER JOINSys.schemas Schem onobj.schema_id=schem.schema_idwhereType='U'  andObj.name='Table name'Order  byObj.name

Gets the field name and type of the table

1, Method one

SELECT * from Information_schema.columns WHERE table_name= ' table name '

2. Method Two

Select A.name tablename, B.name colname, C.name coltype, C.length collength

From sysobjects a inner join syscolumns b
On a.id=b.id and a.xtype= ' U '
INNER JOIN systypes C
On B.xtype=c.xusertype

Where a.name= ' table name '

Law:

Table information is stored in: sys.objects

Schema information in: Sys.schemas

Index information in: sys.indexes

Column information in: sys.columns

These four are all views.

How SQL Server obtains information

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.