Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to compare different database table and If two columns are not identical, add to other table.And then if not equal status get 0.

What I have tried:

DATABASE STUDENT -TABLE STUDENT

ID   NAME   COUNTRYNO   AGE   BRANCHCODE
1    Alex    185        25       15
2    Mary    146        26       19
3    Emily   102        45       14


DATABASE PERSON - TABLE PERSON

ID   NAME   COUNTRYNO   AGE   BRANCHCODE  
1    John     127        45      65
2    Elize    125        54      39
3    Toms     127        42      94



<pre>I would like to look at two columns and two columns, and then look at the country no and the branchcode that are in the person. I want to insert into the table named NewTable if these two columns are different.


 DATABASE STUDENT - NEW STUDENT 

 
ID   NAME   COUNTRYNO   AGE   BRANCHCODE  DURUM
1    John     127        45      65        0
2    Elize    125        54      39        0
3    Toms     127        42      94        0



var common =query1.Intersect(query2);

string query= "SELECT COUNTRYNO,BRANCHCODE FROM " + DATABASE.SCHEMA + ".dbo.STUDENT";

string query2= "SELECT COUNTRYNO,BRANCHCODE FROM " + DATABASE.SCHEMA + ".dbo.PERSON";



How can I do with sql query or entity (add range).
Posted
Updated 7-Nov-18 9:30am
v2

1 solution

A MERGE statement would do it (based on your limited - and nonsensical - example data set).
SQL
MERGE table1 AS t
USING table2 AS s
ON (t.id = s.id)
WHEN MATCHED THEN
SET t.name = s.name,
    t.countryno = s.countryno,
    t.age = s.age,
    t.branchcode = s.branchcode
WHEN NOT MATCHED
INSERT (name, countryno, age, branchcode)
VALUES (s.name, s.countryno, s.age, s.branchcode);

The code above will update any row where the ID from the target table matches the one from the source table. Otherwise, if a match isn't found, the source row will be added to the target table.

If my example, you will end up with three records in the target table.

CAVEAT: I typed this off the top of my head, so it may need some minor tweaking to actually execute.
 
Share this answer
 
v2
Comments
[no name] 7-Nov-18 15:34pm    
+5
[no name] 7-Nov-18 15:39pm    
Hi.I have to select between two tables with select and except I should add these values to the new table and set the value to zero.

Select countryno,branchcode from student.dbo.student

except

Select countryno,branchcode from student.dbo.person

and then if the query results in a value I need to insert into the new table.
#realJSOP 7-Nov-18 15:43pm    
I have no idea what you're trying to say.
[no name] 7-Nov-18 16:12pm    
I would like to add the new table to the new table if the table of countryno and branchcode is not in the student table.So new student table is include person table value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900