Zcorps
Zcorps

Reputation: 102

Getting Unique result set from two table in SQL Server

I m new to SQL Server, please help me with the solution to following problem.

I have two tables which have the following columns:

I need to insert contents of table B into table A.

Conditions are

  1. if table A already contains any alphabet in B then that alphabet should be ignored.
  2. Only a unique alphabet needs to be inserted into table A. i.e. Only one E can be inserted from two set of E and
  3. if F and G also needs to be inserted.

How can I possibly solve this problem?

Thanking you in advance....

Upvotes: 0

Views: 113

Answers (2)

user2989408
user2989408

Reputation: 3137

Try this....

INSERT INTO Table (Column)
SELECT DISTINCT tt.Column 
FROM TempTab tt
WHERE tt.Column NOT IN (SELECT DISTINCT Column FROM Table)

Here is the SQL FIDDLE with this query.

Upvotes: 2

Venkatesh K
Venkatesh K

Reputation: 4584

INSERT INTO A(col) 
 (SELECT distinct col from B 
    minus 
  (SELECT distinct col FROM A INTERSECT SELECT distinct col FROM B))

Upvotes: 1

Related Questions