Solved

Update Case SQL Statement

Posted on 2011-09-27
6
448 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 45

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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 45

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 125 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

728 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