?
Solved

SQL Update Statement

Posted on 2013-01-24
3
Medium Priority
?
444 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 2000 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

764 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