sql server - TSQL: Strange behavior using case statement -
i have scenario wherein have update customer table’s billing_start_month column based on start_month.
i have been given start_month value in excel sheet along customer name. so, have created temp table , inserted customername , start_month values. in excel sheet start_month values contain other values eg: (if start_month month january, february etc update billing_start_month column 1, 2 recpectively, if start_month values contains values 1,3,4,5,7 need leave records without updating billing_start_month column of customer table). have 5 records in excel sheet has start_month values “1,3,4,5,7”.
now updating customer table’s billing_start_month column using following query:
update customer set billing_start_month = case when tmp.startmonth = 'january' 1 when tmp.startmonth = ‘february’ 2 end #temp tmp inner join customer c on tmp.customer = c.acc_name
but on executing query getting check constraint fail error. , when have deleted records of start_month values containing “1,3,4,5,7” temp table, above query executed successfully.
simillarly, in case not deleting records of start_month values containing “1,3,4,5,7” temp table have include when part in case this,
case when tmp.startmonth = 'january' 1 when tmp.startmonth = ‘february’ 2 when tmp.startmonth = ‘1,3,4,5,7’ 1235 –- random value , not null end
please me understanding behavior of case statement. assuming that, when not including case ‘1,3,4,5,7’ in case statement 5 records not considered in update statement.
your update
statement tries update records. description, sounds there scenarios none of case
conditions yield anything, , have no else. else billing_start_month
yield currenct valye records don't match cases.
alternatively, add where
update, update rows start_month
january
or february
.
Comments
Post a Comment