if condition in sql server update query

前端 未结 5 868
日久生厌
日久生厌 2021-02-01 16:29

I have a SQL server table in which there are 2 columns that I want to update either of their values according to a flag sent to the stored procedure along with the new value, so

5条回答
  •  Happy的楠姐
    2021-02-01 17:11

    DECLARE @JCnt int=null
    SEt @JCnt=(SELECT COUNT( ISNUll(EmpCode,0)) FROM tbl_Employees WHERE EmpCode=1  )
    
    UPDATE #TempCode
    SET janCA= CASE WHEN @JCnt>0 THEN (SELECT SUM (ISNUll(Amount,0)) FROM tbl_Salary WHERE Code=1 )ELSE 0 END
    WHERE code=1
    

提交回复
热议问题