SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Updating with correlated subqueries
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Azuki707
Starting Member

1 Posts

Posted - 09/30/2012 :  06:21:43  Show Profile  Reply with Quote
I have a table call CaseTypeCode with 2 columns: typeCode and caseType. Assuming that the CaseTypeCode table has been populated with all possible case types and each case type has been assigned a type code, replace the column caseType in the Case table with a new column typeCode. Add the correct type into the existing rows in the Case table to refer to the corresponding typeCode given the caseType of the case.

I have created a new column caseType in Case table, but it's now filled with NULL. I need to fill them with the caseType from CaseTypeCode. A subquery like below won't work because the subquery result in multiple output. I think I need a correlated subquery.


update [Case]
set [Case].typeCode = 
	(select ctc.caseType
	from [CaseTypeCode] as ctc
	where ctc.caseType=[Case].caseType);

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 10/01/2012 :  10:52:40  Show Profile  Reply with Quote

UPDATE c
SET c.typeCode=cc.TypeCode
FROM Case c
INNER JOIN CaseTypeCode cc
ON cc.caseType = c.caseType


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.16 seconds. Powered By: Snitz Forums 2000