Sqlserver compares columns in two tables

Source: Internet
Author: User

I. Problems
Two tables with 40 or 50 columns are given to find their same columns and different columns.

2. query the columns in two tables. A temporary table exists.

-- # A, # B is a temporary table, which is automatically deleted after the current connection is disconnected
-- RANK () OVER (order by syscolumns. name DESC) AS is supported BY SQL2005, and the auto-increment sequence number is added before each record row.
-- The IDENTITY (INT,) function must be used with.

1. Store the column of the table into the name of the table # a -- 'destbl'

Select * into # a from (select RANK () OVER (order by syscolumns. name DESC) AS No., syscolumns. name
From syscolumns, sysobjects
Where syscolumns. [id] = sysobjects. [id]
And sysobjects. [name] = 'destbl') as t

Select * from #

1 Name
2 course
3 id
4 cno

2. Store the table columns in the # B -- 'student 'table name

Select No. = IDENTITY (INT,), 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

Iii. analyze and compare the similarities and differences of each table column

Use the following statement or make a comparison later
Select * from # B where name in (select name from #)
Select * from # a where name not in (select name from # B)
Select * from # a, # B where a. name = B. name
Select * from # a left join # 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.