Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-04-01
7
Medium Priority
?
194 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
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

885 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