Click here to Skip to main content
15,920,383 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Studenteducation table as follows

Studid Mobilenum
1
2
3
4


Student table as follows

Studid
1
2
3
4
5
6
7

Temptable as follows

Studid Mobilenum
1 9789512450
2 9789512650
3 9789455564
4 9785654450
5 9785657888
6 9789518780
7 9789518899
8 9789518890
9 9785654870



I want to compare the Temptable and Studenttable studid.
if Temptable and Studenttable studid matches update the mobileno of particular studid in Studenteducation table

if Temptable and Studenttable studid not matches insert the student id and mobileno of particular studid in Studenteducation table.

for that how can i write the query in sql server.

Rgds,
Narasiman P.
Posted

Try this queries.

SQL
UPDATE   se
 SET     se.Mobilenum = t.Mobilenum
FROM    Temptable t INNER JOIN Student s ON t.studid = s.studid inner join  Studenteducation se on se.studid = s.studid

 select *from Studenteducation
 insert into Studenteducation
 select *   from Temptable
 where studid not in (select Studid from Studenteducation  )
 
Share this answer
 
Comments
King Fisher 28-Apr-14 8:23am    
skilful ;)
You can use merge also :
SQL
MERGE INTO STUDENTEDUCATION SE USING (SELECT t.studid,t.mobilenum 
                                         FROM TEMPTABLE T,STUDENTTABLE S 
                                        WHERE T.STUDID=S.STUDID) ST 
                                  ON (ST.STUDID=SE.STUDID)
         WHEN NOT MATCHED THEN INSERT (SE.STUDID,SE.MOBILENUM) 
                               VALUES (ST.STUDID,ST.MOBILENUM)
         when matched     then update set se.Mobilenum = st.Mobilenum;
 
Share this answer
 

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