Link to home
Start Free TrialLog in
Avatar of CipherIS
CipherISFlag for United States of America

asked on

SQL Update Statement

Need help with a SQL Statement.

I have an internal table - below is an example
CREATE TABLE #MyTable
  (
     ID INT,
     Name VARCHAR(200),
     Courses VARCHAR(200)
  )

Open in new window


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

Open in new window


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 

Open in new window

Avatar of AielloJ
AielloJ
Flag of United States of America image

CipherIS,

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
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CipherIS

ASKER

This helped me.  Had to make some changes but it got me started in the direction I need to go.

Thanks