Update multiple lines with CASE statement

I have a table with, say 1000 clients in it. I need to update a field in my table for each client with a different flag based on a mix of field values from a view that is in another database. The value will be different for each client in the table.

I am using the syntax below but I am missing something - any help greatly appreciated.

 USE [DB2]
GO

	DECLARE @TV bit
	DECLARE @EV bit
	
	    SET @TV = (SELECT [Test1] FROM [DB1].[dbo].[vwClientTable] WHERE [DB1].[dbo].[vwClientTable].[MainID1] = [DB2].[dbo].[ClientTable].[MainID])
        SET @EV = (SELECT [Test2] FROM [DB1].[dbo].[vwClientTable] WHERE [DB1].[dbo].[vwClientTable].[MainID1] = [DB2].[dbo].[ClientTable].[MainID])

UPDATE

        [DB2].[dbo].[Client]

    SET

        [FIELD1] =

        (

            CASE

                WHEN @TV = 1 AND @EV = 1 THEN -1003

                WHEN @TV = 1 AND @EV = 0 THEN -1001

				ELSE -1002

            END

        )

Open in new window

alexkingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Arifhusen AnsariBusiness Intelligence Developer and AnalystCommented:
Hello,

I am not sure about your script whether it was executed or not. You might get error in the first line it self.

You can go with below approach.

While updating multiple records in table.
I would suggest not to execute update query directly rather you can execute select query first and just verify whether you are getting data as per expectation or not. If everything goes well, you can covert that select query to update query.

Now let's come to your case. You need to update column in Client table based on data in two columns test1 and test2 in vwClientTable.

We will first build select query based on requirement and then convert it to update.

Using below query you can find whether you are getting correct value for your case statement or not.
Verify the same for some samples.

SELECT
	t1.*
	,t2.[Test1],t2.[Test2]
	,CASE 
		WHEN t2.[Test1]=1 AND t2.Test2=1 THEN -1003
		WHEN t2.[Test1]=1 AND t2.Test2=0 THEN -1001
		ELSE -1002
	END as ValueTOUpdate

FROM [DB2].[dbo].[Client] t1 
INNER JOIN [DB1].[dbo].[vwClientTable] t2
ON t1.[MainID1]=t2.MainID

Open in new window




If it looks great just convert select query to update one as below.

UPDATE t1
SET t1.FIELD1= CASE 
		WHEN t2.[Test1]=1 AND t2.Test2=1 THEN -1003
		WHEN t2.[Test1]=1 AND t2.Test2=0 THEN -1001
		ELSE -1002
	END 
FROM [DB2].[dbo].[Client] t1 
INNER JOIN [DB1].[dbo].[vwClientTable] t2
ON t1.[MainID1]=t2.MainID

Open in new window



Hope it will help you and guide you when you do multiple updates.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alexkingAuthor Commented:
That makes much more sense now - thank you so much :)
0
alexkingAuthor Commented:
Thank you very much - that was really helpful and I get it now :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.