Common SQL statements of SQL Server

Source: Internet
Author: User

1. display all user databases:
Select dbid, name, filename from Master. DBO. sysdatabases where dbid> 4

2. display all tables in the database:
Current Database: Select name from {db_name} .. sysobjects where xtype = 'U'
Database creation: Select name from {db_name} .. sysobjects where xtype = 'U'

3. display all stored procedures in the database:
Select name, status from sysobjects where xtype = 'P'

4. view all fields in the table:
Show only field names: Select name from syscolumns where id = object_id ('table _ name ')
Show field details: Select syscolumns. name, policypes. name 'type', syscolumns. length from syscolumns, policypes where syscolumns. xusertype = policypes. xusertype and syscolumns. id = object_id ('table _ name ')

5. Select all different values of a field:
Select distinct field1 from Table1

6. Select a specified number of records:
Select top (10) field1 from Table1

7. Select all different values of a field:
Select distinct field1 from Table1

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.