0

I want to loop CustomerType column in CustomerDetails table. then use the condition where CustomerType = 'CorporateCustomer' to update IsCorporateCustomer column to 1 and else update others to 0

This is what I did but It only executes the first statement and added 1 to all the IsCorporateCustomer column.

How can I fix this? Thanks

IF EXISTS (SELECT * FROM CustomerDetails WHERE CustomerType =  'CorporateCustomer')
BEGIN
UPDATE CustomerDetails set IsCorporateCustomer = 1;
END
ELSE
BEGIN
UPDATE CustomerDetails set IsCorporateCustomer=0;
END

3
  • Why not UPDATE CustomerDetails SET IsCorporateCustomer=1 WHERE CustomerType = 'CorporateCustomer' Commented Jan 25, 2021 at 14:51
  • Please provide sample data and desired results. Commented Jan 25, 2021 at 14:51
  • I think @Sergey suggestion will also work Commented Jan 25, 2021 at 15:12

1 Answer 1

2

you can use case statement, so your whole statement above will be as simple as below query:

UPDATE CustomerDetails 
set IsCorporateCustomer = case when CustomerType =  'CorporateCustomer' then 1 else 0 end;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.