Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

SQL update/set more than one column

this question is related to the question (SQL update logic) that i posted before.

is it possible to setMORE than one column with different values if condition is true?

set table1.ColumnA = 0  and table1.ColumnB = abc1 and table 1.ColumnC  = xyz1
     if table1.columnB=table2.columnX and table1.columnC = table2.columnY
set table1.ColumnA = 1  and table1.ColumnB = abc2 and table 1.ColumnC  = xyz2
0
jgordin
Asked:
jgordin
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
i guess you are looking for a syntax like this:

set table1.ColumnA = 0
   , table1.ColumnB = abc1  
   , table 1.ColumnC  = xyz1
   , table1.ColumnA = case when table1.columnB = table2.columnX and table1.columnC = table2.columnY then 1 else  table1.ColumnA end
   , table1.ColumnB = case when table1.columnB = table2.columnX and table1.columnC = table2.columnY then abc2  else   table1.ColumnB  end
   , table1.ColumnC  = case when table1.columnB = table2.columnX and table1.columnC = table2.columnY then xyz2 else   table1.ColumnC  end


0
 
jgordinAuthor Commented:
i am looking for the syntax that will allow me to update multiple columns of the table based on the condition:

if  t1.columnB=t2.columnX and t1.columnC = t2.columnY = True
  set t1.columnA = 1, t1.columnB = t2.columnZ1
if  t1.columnB=t3.columnX and t1.columnC = t3.columnY = True
 set t1.columnA = 2, t1.columnB = t2.columnZ2

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
without being more "clear" on the circumstances (ie how are the tables joined etc), it will be impossible to get clearer on the "response" without guessing...
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now