SQL Server compares two-table columns _mssql

Source: Internet
Author: User
Tags comparison table

I. Problems
Gives two tables with forty or fifty columns and finds the same columns and different columns

Second, query two table columns, there is a temporary table

--#a, #b都是临时表, automatically deleted after the current connection is disconnected
--rank () over (ordered by Syscolumns.name DESC) as is supported by SQL2005, plus a self increment number before each line record
--identity (int,1,1) function must be used in conjunction with into

1, the table column into the #a--' desttbl ' comparison table name

SELECT * Into #a from (select RANK () syscolumns.name DESC) as ordinal number, syscolumns.name
From Syscolumns,sysobjects
where syscolumns. [Id]=sysobjects. [ID]
and sysobjects. [name]= ' desttbl ') as T

SELECT * FROM #a

1 Name
2 Course
3 ID
4 CNO

2, the table column into the #b--' student ' comparison table name

Select serial Number = IDENTITY (int,1,1), Syscolumns.name
Into #b from syscolumns,sysobjects
where syscolumns. [Id]=sysobjects. [ID]
and sysobjects. [name]= ' Student '


SELECT * FROM #b

1 ID
2 Name
3 CNO

Third, analyze and compare the similarities and differences of various table columns

Use the following statement, or slightly change the comparison
SELECT * from #b where name in (select name from #a)
SELECT * from #a where name isn't in (select name from #b)
SELECT * from #a A, #b b where a.name=b.name
SELECT * FROM #a a LEFT join #b B on a.name=b.name

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.