troubleshooting Question

SQL Syntax - Dynamic Update Statement

Avatar of WaldaInc
WaldaInc asked on
Microsoft SQL ServerSQL
14 Comments1 Solution658 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
regsworld

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 14 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 14 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros