In SQL Server, the Union operator is written at once (a few points worth attention)

Source: Internet
Author: User

 

UnionAn operator combines two or more query results into a single result set.

This result set contains all rows of all queries in the Union query. This is different from using join to combine columns in two tables.

 

UseUnionThe result set of a combined query has two basic rules:

1. The columns and columns in all queries must be in the same order.

2. Data type must be compatible

. union name of the result set column and the first select the column names in the result set in the statement are the same, others select the column name of the result set in the statement is ignored.

B. by default, Union the operator deletes duplicate rows from the result set. If all keyword, the result set contains all rows without deleting duplicate rows.

C. SQLIs left to right to includeUnionThe statement of the operator. You can use parentheses to change the order of evaluation.

 

--For example:

Select * From tablea

Union all

(

Select * From tableb

Union all

Select * From tablec

)

 

In this way, you can firstTablebAndTablecMerge and then mergeTablea

 

D. to save the merged result set to a new data table, then into the statement must be added to the first select medium

E. only the last select used in the statement order by and compute clause, which affects the sorting and counting summary of the final merge results

F. GroupAndHavingThe sub-statement can beSelectUsed in the query, they do not affect the final result

 

-- for example, :

select name as name , class as class , grade as Grade

Into # students

From stud87

Union all

Select * From stud88

Union all

Select * From stud89

OrderGrade

 

 

Set3Merge results of classes (GradeSort) insert to temporary table# StudentsInside

 

I hope the Knowledge mentioned above will prompt you.

Of course, you are welcome to contact us and correct us.

 

Author: aierong

Blog: http://www.cnblogs.com/aierong

Email: aierong@126.com

 

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.