CipherIS
asked on
SQL Update Statement
Need help with a SQL Statement.
I have an internal table - below is an example
The issue I have is setting the value of Courses. If there are multiple courses then I want the value 'Multiple' to appear there otherwise the name of the course. (My internal table is more complicated than above just trying to make it easy.
So my code goes as follows
Below would work if there was only one course for the student but if there are multiple I want to update Coursename with 'Multiple'.
I have an internal table - below is an example
CREATE TABLE #MyTable
(
ID INT,
Name VARCHAR(200),
Courses VARCHAR(200)
)
The issue I have is setting the value of Courses. If there are multiple courses then I want the value 'Multiple' to appear there otherwise the name of the course. (My internal table is more complicated than above just trying to make it easy.
So my code goes as follows
Insert Into #MyTable
(ID, Name)
Select ID, Name From Student
Below would work if there was only one course for the student but if there are multiple I want to update Coursename with 'Multiple'.
SELECT ID, CourseID, CourseName
INTO #CourseTable
FROM Courses c
INNER JOIN xferNameCourse xnc ON c.CourseID = xnc.CourseID
WHERE nc.ID IN (
SELECT ID FROM #MyTable
)
UPDATE mt
SET mt.CourseName = s.CourseName
FROM #MyTable mt
INNER JOIN #Courses c ON mt.ID = c.ID
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This helped me. Had to make some changes but it got me started in the direction I need to go.
Thanks
Thanks
I'm assuming that Students has a relation to the table xferNameCourse (which relates students to multiple courses) that has a relationship to Courses. How do you relate the students to the Courses? If it's through the xferNameCourse table can you provide the column names that form the relationship?
Best regards,
AielloJ