Solved

SQL Update Statement

Posted on 2013-01-24
3
441 Views
Last Modified: 2013-01-25
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

0
Comment
Question by:CipherIS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 13

Expert Comment

by:AielloJ
ID: 38816640
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
0
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
ID: 38816950
try like this
CREATE TABLE #MyTable
  (
     ID INT,
     Name VARCHAR(200),
     Courses VARCHAR(200)
  ) 


Insert Into #MyTable
(ID, Name)
Select ID, Name From Student 

SELECT ID, CourseID, CourseName
INTO #CourseTable
FROM Courses c
INNER JOIN xferNameCourse xnc ON c.CourseID = xnc.CourseID
WHERE xnc.ID IN (
		SELECT ID FROM #MyTable
	)

;With  cour as ( Select ID, 
case when count(*) > 1 then 'Multiple' else max(CourseID) end CourseID , 
case when count(*) > 1 then 'Multiple' else max(CourseName) end CourseName
from  #CourseTable group by ID ) 
UPDATE mt
	SET CourseName = c.CourseName
FROM #MyTable mt
INNER JOIN cour  c ON mt.ID = c.ID  

Open in new window

0
 
LVL 1

Author Closing Comment

by:CipherIS
ID: 38819216
This helped me.  Had to make some changes but it got me started in the direction I need to go.

Thanks
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question