ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 254.8k

How to write update query to check exist column by value 1 where it ex

Mar 2 2021 7:51 AM

How to update check exist column by value 1 where partid exist on table FeaturesvalueA or FeaturesvalueB ?

I work on sql server 2012 I face issue I need to update column checkexist on table temp

where partid exist at least one time on table #FeaturesvalueA or table #FeaturesvalueB

But if partid exist on both tables then not update check exist by 1

i need only update check exist by 1 in case of exist on only one table from both

FeaturesvalueA or FeaturesvalueB

case of not update

IF partid exist on both tables #FeaturesvalueA and #FeaturesvalueB then no need to update column check exist by 1

IF partid not exist on both tables #FeaturesvalueA and #FeaturesvalueB then no need to update column check exist by 1

case of update

IF partid exist on only one tables from #FeaturesvalueA or #FeaturesvalueB then update check exist by 1
  1. create table #temp  
  2.  (  
  3.  PartId int ,  
  4.  checkexist int,  
  5.  )  
  6.  insert into #temp(PartId,checkexist)  
  7.  values  
  8.  (555,0),  
  9.  (999,0),  
  10.  (1200,0),  
  11.  (1300,0),  
  12.  (1010,0),  
  13.  (1500,0)  
  14.       
  15.       
  16.  create table #FeaturesvalueA  
  17.  (  
  18.  PartId int,  
  19.  FeatureName nvarchar(50),  
  20.  FeatureValue  nvarchar(50),  
  21.  updatedStuffDiff nvarchar(500)  
  22.  )  
  23.  insert into #FeaturesvalueA(PartId,FeatureName,FeatureValue)  
  24.  values  
  25.  (555,'Temperature','5c'),  
  26.  (555,'resistance','10c'),  
  27.  (1200,'Temperature','20c'),  
  28.  (1200,'resistance','30c'),  
  29.  (1010,'cold','40c'),  
  30.  (1010,'air','7c')  
  31.       
  32.       
  33.  create table #FeaturesvalueB  
  34.  (  
  35.  PartId int,  
  36.  FeatureName nvarchar(50),  
  37.  FeatureValue  nvarchar(50),  
  38.  updatedStuffDiff nvarchar(500)  
  39.  )  
  40.  insert into #FeaturesvalueB(PartId,FeatureName,FeatureValue)  
  41.  values  
  42.  (555,'Temperature','5c'),  
  43.  (555,'resistance','10c'),  
  44.  (999,'Temperature','20c'),  
  45.  (1300,'resistance','30c'),  
  46.  (1010,'cold','40c'),  
  47.  (1010,'air','7c')  
  48.   
  49. Expected result  
  50.   
  51.  PartId    checkexist  
  52.  555            0  
  53.  999            1  
  54.  1200            1  
  55.  1300            1  
  56.  1010            0  
  57.  1500            0  

Answers (2)