I've got an Update SP that updates fields in a table. The table has 30+ fields so we came up with a SP that would only update whatever parameters were passed into it:
This works great with 1 exception.....for fields that can go from having a value to being null. In that case, this SP ignores it and the field doesn't get updated. For instance, if a user opens an issue and puts a "Due Date" in of 9/22/2011 and saves it...then later realizes he didn't want to do that and clears the field out. The program should update the table and set the Due Date to NULL but again, it doesn't because the new value is NULL.
One thing to note is that table has a field called LastUpdatedDate and we would like that to reflect when the row was truely "lastUpdated"...so if I make the field not default and then it's always updated to matter what, then the LastUpdated field isn't an accurate record.
Any suggestions on how to make a SP to do what I want to do?
Code:
ALTER PROCEDURE [dbo].[UpdateIssue]
@IssueID int,
@ReceivedDate datetime = NULL,
@DueDate datetime = NULL,
@ResolvedDate datetime = NULL,
@ResolvedByUserID int = NULL,
@AssignedGroupID int = NULL,
@AssignedUserID int = NULL
--The rest have been omitted.
AS
UPDATE Issues
SET ReceivedDate = ISNULL(@ReceivedDate, ReceivedDate),
DueDate = ISNULL(@DueDate, DueDate),
--ResolvedDate = ISNULL(@ResolvedDate, ResolvedDate),
ResolvedDate = @ResolvedDate,
--ResolvedByUserID = ISNULL(@ResolvedByUserID, ResolvedByUserID),
ResolvedByUserID = @ResolvedByUserID,
AssignedGroupID = ISNULL(@AssignedGroupID, AssignedGroupID),
AssignedUserID = ISNULL(@AssignedUserID, AssignedUserID)
WHERE IssueID = @IssueID
SELECT @@ROWCOUNT AS RecordsAffected
This works great with 1 exception.....for fields that can go from having a value to being null. In that case, this SP ignores it and the field doesn't get updated. For instance, if a user opens an issue and puts a "Due Date" in of 9/22/2011 and saves it...then later realizes he didn't want to do that and clears the field out. The program should update the table and set the Due Date to NULL but again, it doesn't because the new value is NULL.
One thing to note is that table has a field called LastUpdatedDate and we would like that to reflect when the row was truely "lastUpdated"...so if I make the field not default and then it's always updated to matter what, then the LastUpdated field isn't an accurate record.
Any suggestions on how to make a SP to do what I want to do?