I was looking for some guidance on the best and most efficient way to achieve the follwing:I have an application that imports contact data from multiple sources, some contacts may exist in more than one dataset and each dataset may have varying levels of detail for a contact.I have the SP below that performs an insert only if the contact doesn't already exist otherwise performs an update. The problem is that during an update some existing values could be overwritten with nulls so what i am looking to do is have the query only update a field if the current value is null otherwise ignore it.Not sure if this is even possible using SQL along but I guessed you gues would be the best people to answer this:CREATE PROCEDURE dbo.pn_InsertUpdateContact( @FirstName nvarchar(50) = null, @LastName nvarchar(50) = null, @Address1 nvarchar(50) = null, @Address2 nvarchar(50) = null, @Address3 nvarchar(50) = null, @City nvarchar(50) = null, @County nvarchar(50) = null, @Postcode nvarchar(50) = null, @Country int = null, @Telephone nvarchar(30) = null, @Email nvarchar(50) = null, @Gender char(1) = null, @DateOfBirth smalldatetime = null, @CreatedBy uniqueidentifier, @UpdatedDate smalldatetime = null, @UpdatedBy uniqueidentifier = null)ASDECLARE @ContactId intSELECT @ContactId = (SELECT ContactId FROM Contacts WHERE Email = @Email)IF @ContactID IS NULLBEGININSERT INTO Contacts ( FirstName, LastName, Address1, Address2, Address3, City, County, Postcode, Country, Telephone, Email, Gender, DateOfBirth, CreatedBy, UpdatedDate, UpdatedBy)VALUES ( @FirstName, @LastName, @Address1, @Address2, @Address3, @City, @County, @Postcode, @Country, @Telephone, @Email, @Gender, @DateOfBirth, @CreatedBy, @UpdatedDate, @UpdatedBy) ENDELSEBEGINUPDATE Contacts SET FirstName = @FirstName, LastName = @LastName, Address1 = @Address1, Address2 = @Address2, Address3 = @Address3, City = @City, County = @County, Postcode = @Postcode, Country = @Country, Telephone = @Telephone, Email = @Email, Gender = @Gender, DateOfBirth = @DateOfBirth, UpdatedDate = getDate(), UpdatedBy = @UpdatedBy WHERE Email = @EmailENDGO
www.creativenrg.co.uk