I have a target and a source table where I want to update the target from the source. I have one non-nullable key field, and 3 data fields UDF1,UDF2, and UDF3.I get updates provided to me which list the key field and will have one or more of the 3 UDF fields populated. I need to update the main table from these updates.I'm pretty new to SQL so I chose the MERGE command to do this. My problem is in the code below in where I wrote "PROBLEM HERE". I need to be able to update the fields UDF1,2, or 3 ONLY if the source is not null. I gather there is no opposite to ISNULL() like I have in that line. My input may have an update for UDF1, but the input may be NULL for UDF2. I don't want the source UDF2 to get overwritten with NULL. Is that possible?[code="sql"]MERGE UDFTest --target tableUSING UDFIn --source tableON UDFTest.JobNumber = UDFIn.JobNumberWHEN MATCHED AND -- if the incomign record is all blanks, then delete original (UDFIn.UDF1 is null) and (UDFIn.UDF2 is null) and (UDFIn.UDF3 is null) THEN DELETEWHEN MATCHED THEN -- if the incoming record is a match, then udpate the fields to match (if they are not null) UPDATE SET -- PROBLEM HERE UDFTest.UDF1 = ISNULL(UDFIn.UDF1,UDFTest.UDF1), -- Isnull(checkexpression, value if true) UDFtest.UDF2 = UDFIn.UDF2, UDFtest.UDF3 = UDFIn.UDF3WHEN NOT MATCHED BY TARGET -- If it's a matched record, AND there is corresponding daa in any UDF field, then insert new record AND (UDFIn.UDF1 is not null) or (UDFIn.UDF2 is not null) or (UDFIn.UDF3 is not null) THEN INSERT (JobNumber, UDF1, UDF2, UDF3) VALUES ( UDFIn.JobNumber, UDFIn.UDF1, UDFIn.UDF2, UDFIn.UDF3 );[/code]
↧