We help IT Professionals succeed at work.
Get Started

SQL Syntax - Dynamic Update Statement

WaldaInc
WaldaInc asked
on
657 Views
Last Modified: 2021-04-21
Hi.

I am processing a very large text file, trying to update columns for exising members. However, somtimes the text file contains blank values and I don't want to overwrite values that exist with blank values. So, if the values (from text file) are blank, we don't update that column.

The code snippet below shows a partial update (there will be more columns, but if I can get an example with these, I can do the rest).

Notice the SET portion. Any of those values (@CityID, @AddressLine1, @ZipCode) could be blank and if it is, I don't want to include that line in the SET.

How do i write an UPDATE that looks at each variable (@CityID etc..) and determines if we should put it in the UPDATE statement.

I hope I am explaiing myself clearly.

Thanks in advance for any and all comments.

UPDATE Contact
SET
	PostalAddressLine1 = @AddressLine1,
	CityID = @CityID,
	TerritoryID = @StateID,
	PostalZipCode = @ZipCode
WHERE EntityID = 1
AND EntityValueID = @PersonID

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 14 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE