?
Solved

Update Case SQL Statement

Posted on 2011-09-27
6
Medium Priority
?
449 Views
Last Modified: 2012-05-12
Experts,
I am trying to "set" a variable to either 'Resolved' or 'Active'.  I tried using an update case stmt but it would only return 'Resolved' when clearly there should have been some 'Active'.

This is the stmt I am using.

declare @status varchar(20)

Update mytable
Set @Status =
Case
when field > 1 Then 'Resolved'
else 'Active'
End

The field has a number from 1 to 10 in each of the records but @status just returns 'Resolved'.

And suggestions?
Thanks,
Kelly
0
Comment
Question by:mschmidt14
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 36712648
Hi Kelly,

Close.  :)

You just need to SELECT the result....


 Set @Status =
SELECT
Case
when field > 1 Then 'Resolved'
else 'Active'
End
FROM mytable


Good Luck,
Kent
0
 
LVL 23

Expert Comment

by:nemws1
ID: 36712690
Is this in a stored procedure?  Your statement doesn't make much sense... since you're not UPDATE()ing anything (even then, you'd still just use SET and not UPDATE).  This would work for a SELECT.  Note: not using SET and using ":=" instead:
SELECT field,
@Status :=
CASE
WHEN (field > 1) THEN 'Resolved'
ELSE 'Active'
END AS `Status`
FROM mytable
;

Open in new window

0
 

Author Comment

by:mschmidt14
ID: 36712790
I tried both suggestions and they didn't work.  I may have been putting the code in the wrong place though.  This is the whole code so you can see what's going on.
declare @status varchar(20)

Update filteredjxe_csisurvey
Set @Status =
Case
when jxe_recommend > 1 Then 'Resolved'
else 'Active'
End

select distinct
c.jxe_recommend,
@status as status,
c.jxe_branchname,
c.jxe_deptname,
c.jxe_execidname,
c.jxe_companyidname,
c.jxe_csisurveyid,
c.jxe_caseid,
c.jxe_companyid,
Datediff(day,c.createdon, i.modifiedon) as jxe_daysopen,
CONVERT(varchar(20),c.createdon,101) as createdon,
CONVERT(varchar(20),c.modifiedon,101) as modifiedon

From filteredjxe_csisurvey c
left outer join filteredincident as i on c.jxe_caseid = i.incidentid
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 46

Expert Comment

by:Kent Olsen
ID: 36712818
Update filteredjxe_csisurvey
Set @Status =
Case
when jxe_recommend > 1 Then 'Resolved'
else 'Active'
End

That's a problem.  :(  

The Set clause of the UPDATE statement needs a column name within the table that you're updating.  @Status is a local variable.


Are you trying to update a table or set a local variable?

0
 
LVL 23

Accepted Solution

by:
nemws1 earned 500 total points
ID: 36712858
It almost looks like you want @Status set for each row, but its a single variable, and gets overwritten.  Even if what you had worked, @Status would just be set according to the last row in your table.

Why don't you just put the CASE statement in your SELECT?
select distinct
c.jxe_recommend,
  CASE
  WHEN c.jxe_recommend > 1 THEN 'Resolved'
  ELSE 'Active'
  END AS Status,
c.jxe_branchname,
c.jxe_deptname,
c.jxe_execidname,
c.jxe_companyidname,
c.jxe_csisurveyid,
c.jxe_caseid,
c.jxe_companyid,
Datediff(day,c.createdon, i.modifiedon) as jxe_daysopen,
CONVERT(varchar(20),c.createdon,101) as createdon,
CONVERT(varchar(20),c.modifiedon,101) as modifiedon
From filteredjxe_csisurvey c
left outer join filteredincident as i on c.jxe_caseid = i.incidentid

Open in new window

0
 

Author Closing Comment

by:mschmidt14
ID: 36713058
I didn't realize you could do that...I learn more and more with this language everyday!

Thanks so much!!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

770 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