Click here to Skip to main content
15,916,945 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
\]
SQL
This makes total sense to me but it fails with an error.  I am not much of a SQL programmer.  I have researched this error but still curious why this will not work


<pre lang="SQL">
				
BEGIN
UPDATE [AdventureWorks].[dbo].[tbl_FocusAgents]
WHEN @agency_lob = 'Farm' THEN SET Focus_FarmFocus = 'Y' 
WHEN @agency_lob = 'Commerical' THEN SET Focuse_CommericalFocus = 'Y'
WHERE Focus_FocusAgentCd= @agency_entire_code
END	
Posted
Comments
Herman<T>.Instance 4-Jun-14 13:55pm    
SET Focus_FarmFocus = Case WHEN @.... OR ..... END
WHERE....
END

You can't set columns or not based on conditions: the syntax doesn't allow for it.
What you will have to do is two UPDATES or
SQL
UPDATE [AdventureWorks].[dbo].[tbl_FocusAgents]
   SET Focus_FarmFocus = CASE WHEN @agency_lob = 'Farm' THEN 'Y' ELSE Focus_FarmFocus END,
       Focuse_CommericalFocus = WHEN @agency_lob = 'Commerical' THEN 'Y' ELSE Focuse_CommericalFocus END
WHERE Focus_FocusAgentCd= @agency_entire_code
 
Share this answer
 
SQL
BEGIN

BEGIN
UPDATE [AdventureWorks].[dbo].[tbl_FocusAgents]
SET Focus_FarmFocus = CASE WHEN   @agency_lob = 'Farm' THEN 'Y' ELSE ''  END,
 Focuse_CommericalFocus = CASE WHEN   @agency_lob = 'Commerical' THEN 'Y' ELSE '' END
WHERE Focus_FocusAgentCd= @agency_entire_code
eND

</pre>
 
Share this answer
 
Comments
CHill60 4-Jun-14 14:21pm    
I disagree. The OP did not imply the ELSE. Your solution will overwrite the values in Focus_FarmFocus when @agency_lob does not equal 'Farm' and in Focus_CommercialFocus when @agency_lob does not equal Commercial. They may have been set to 'Y' or another value by other logic that the OP has not shared with us. Solution 1 by OriginalGriff is the better solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900