Solved

SQL Update Statement

Posted on 2013-01-24
3
435 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
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now