Solved

SQL Update Statement

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
migration MS SQL database to Oracle 30 59
Need help on t-sql 2012 10 53
sql server insert 12 30
Better way to make a query with date filter. 5 23
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

786 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