Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to conditionally update key/value pair table in MS SQL?

Posted on 2009-04-01
7
Medium Priority
?
193 Views
Last Modified: 2012-05-06
I have a table with a key/value pair that I want to update. My table of ID's and their key/value pairs is below.

What I want to do is set "SettingEnable" and "SettingTracking" to "True" ONLY IF one or both of them are already "True". Can anyone help with this SQL statement?

ID      Name      Value
12007      SettingEnable      FALSE
12007      SettingTracking      FALSE
12228      SettingEnable      TRUE
12228      SettingTracking      TRUE
12245      SettingEnable      TRUE
12245      SettingTracking      FALSE
12249      SettingEnable      TRUE
12249      SettingTracking      FALSE
12315      SettingEnable      TRUE
12315      SettingTracking      TRUE
12350      SettingEnable      FALSE
12350      SettingTracking      FALSE
12362      SettingEnable      TRUE
12362      SettingTracking      TRUE
12367      SettingEnable      TRUE
12367      SettingTracking      TRUE
12368      SettingEnable      TRUE
12368      SettingTracking      TRUE
12369      SettingEnable      TRUE
12369      SettingTracking      TRUE
12382      SettingEnable      FALSE
12382      SettingTracking      FALSE
12383      SettingEnable      FALSE
12383      SettingTracking      FALSE
12384      SettingEnable      TRUE
12384      SettingTracking      FALSE
12385      SettingEnable      FALSE
12385      SettingTracking      TRUE
12386      SettingEnable      TRUE
12386      SettingTracking      TRUE
12387      SettingEnable      TRUE
12387      SettingTracking      TRUE
12388      SettingEnable      FALSE
12388      SettingTracking      TRUE
12389      SettingEnable      TRUE
12389      SettingTracking      FALSE
12391      SettingEnable      TRUE
12391      SettingTracking      FALSE
12392      SettingEnable      FALSE
12392      SettingTracking      FALSE
12393      SettingEnable      FALSE
12393      SettingTracking      FALSE
12402      SettingEnable      TRUE
12402      SettingTracking      TRUE
12403      SettingEnable      TRUE
12403      SettingTracking      TRUE
12404      SettingEnable      FALSE
12404      SettingTracking      FALSE
12405      SettingEnable      FALSE
12405      SettingTracking      FALSE
12406      SettingEnable      FALSE
12406      SettingTracking      FALSE
12408      SettingEnable      TRUE
12408      SettingTracking      TRUE
12409      SettingEnable      TRUE
12409      SettingTracking      TRUE
12410      SettingEnable      FALSE
12410      SettingTracking      FALSE
12411      SettingEnable      FALSE
12411      SettingTracking      FALSE
0
Comment
Question by:bemara57
[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
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24043781
this will do
update t
  set value = 'TRUE'
 from yourtable t
 WHERE t.value <> 'TRUE'
   and exists ( SELECT NULL FROM yourtable o WHERE o.ID = t.ID and o.value = 'TRUE' )

Open in new window

0
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 24043814
UPDATE Table SET Value = 1
WHERE Name IN ('SettingEnable', 'SettingTracking')
AND ID IN (
      SELECT ID
      FROM Table
      WHERE (Name = 'SettingEnable' AND Value = 1)
            OR (Name = 'SettingTracking' AND Value = 1)
      )
0
 
LVL 27

Expert Comment

by:Chris Luttrell
ID: 24043840
update Table
set Value = 'True'
from Table inner join
(select Id
from Table where Name = 'SettingEnable' and Value = 'TRUE'
union
select Id
from Table where Name = 'SettingTracking'  and Value = 'TRUE') as TrueIds
on Table.Id = TrueIds.Id
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:bemara57
ID: 24043856
Thanks but where is it checking for the Name being SettingTracking and SettingEnable? I have other key/value pairs in my table that are not related.
0
 
LVL 18

Accepted Solution

by:
UnifiedIS earned 2000 total points
ID: 24043898
Mine and CGLuttrell's check the value of Name.  I assumed a 1 or 0 value for your boolean but if it needs to be spelled out then:
UPDATE Table SET Value = 'TRUE'
WHERE Name IN ('SettingEnable', 'SettingTracking')
AND ID IN (
      SELECT ID
      FROM Table
      WHERE (Name = 'SettingEnable' AND Value = 'TRUE')
            OR (Name = 'SettingTracking' AND Value = 'TRUE')
      )
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24043934
this il do:
update t
  set value = 'TRUE'
 from yourtable t
 WHERE t.value <> 'TRUE'
   and t.Name IN ('SettingEnable', 'SettingTracking')
   and exists ( SELECT NULL FROM yourtable o WHERE o.ID = t.ID and o.value = 'TRUE' and i.Name IN ('SettingEnable', 'SettingTracking')
 )

Open in new window

0
 
LVL 22

Expert Comment

by:dportas
ID: 24044163
UPDATE tbl
SET value =
 (SELECT MAX(value)
  FROM tbl t
  WHERE t.id = tbl.id
   AND t.Name IN ('SettingEnable', 'SettingTracking') )
WHERE Name IN ('SettingEnable', 'SettingTracking') ;
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

670 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