Click here to Skip to main content
15,921,212 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a table which consists of a set of students data(Students) and a new table (Applicants). The columns in those tables are:

Students:
Stud_Id
Stud_Name
Institute_Id

Applicants:
App_Id
App_Name
Inst_Id

Stud_Id and App_Id are the same dataset. What I am going to do now is i want to update Applicants table, which I want to insert Institute_Id into Inst_Id using Students.Stud_Id = Applicants.App_Id.

I have tried a little. But error came out. How can i fix this?

SQL
UPDATE a SET a.Inst_Id = (select s.Institute_Id from Applicants a inner join Students s on a.App_Id = s.Stud_Id)
FROM Applicants a
INNER JOIN Students s ON a.App_Id = s.Stud_Id
Posted
Updated 10-Aug-11 18:03pm
v2
Comments
walterhevedeich 11-Aug-11 0:10am    
Whats the error?
And this is somewhat confusing.
What I am going to do now is i want to update Applicants table, which I want to insert Institute_Id into Inst_Id using Students.Stud_Id = Applicants.App_Id. Update and insert? Elaborate more.
snamyna 11-Aug-11 0:24am    
I have Institute_Id data and i want to update the Applicants table by inserting that Institute_Id into Inst_id. But the data must be matched according to their App_Id. The error is, multiple results cannot be inserted. Somehow, the data is different according to Stud_Id.

eg:
Stud_Id : A -----> Institute_Id : UKM
Stud_Id : B -----> Institute_Id : OUM
Stud_Id : C -----> Institute_Id : Nott
Stud_Id : D -----> Institute_Id : BioCorp

I want it to be like this:


App_Id : A -----> Inst_Id : UKM
App_Id : B -----> Inst_Id : OUM
App_Id : C -----> Inst_Id : Nott
App_Id : D -----> Inst_Id : BioCorp

I just want to insert the data into Inst_Id

1 solution

Without seeing sample data for all tables In the query I would look at the select statement as I believe that it is returning more than one result.

You might be able to change your query to this also (but this has'nt been tested!)

SQL
UPDATE a SET a.Inst_Id = s.Institute_Id 
FROM Applicants a
INNER JOIN Students s ON a.App_Id = s.Stud_Id
 
Share this answer
 
v2
Comments
snamyna 11-Aug-11 3:05am    
ok done with success. thanks simon. :)
Simon_Whale 11-Aug-11 3:54am    
your welcome :)

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