patternModerate
update stored proc ony fields that are passed to the proc and leaving others alone
Viewed 0 times
storedonytheupdatealonearefieldsleavingpassedothers
Problem
i have a stored proc that does a simple update (see below).
i want to update only fields passed and not the ones that are null. that is to say if i dont get a parm value for somethign i dont want to update the table data to null, but rather just leave what the current value is.
can this be done in a sql update statement such as this?
i want to update only fields passed and not the ones that are null. that is to say if i dont get a parm value for somethign i dont want to update the table data to null, but rather just leave what the current value is.
can this be done in a sql update statement such as this?
ALTER PROCEDURE [dbo].[upd_MessageDetail]
@MessageId INT,
@IsDraft INT,
@IsPreviewed INT,
@IsRead INT,
@IsFlagged INT,
@IsDeleted INT,
@IsArchived INT
AS
BEGIN
UPDATE MessageDetail
SET
IsDraft = @IsDraft,
IsPreviewed = @IsPreviewed,
IsRead = @IsRead,
IsFlagged = @IsFlagged,
IsDeleted = @IsDeleted,
IsArchived = @IsArchived
WHERE MessageId = @MessageIdSolution
Something like below if I understand you correctly.
UPDATE MessageDetail
SET
IsDraft = ISNULL (@IsDraft, IsDraft),
IsPreviewed = ISNULL (@IsPreviewed, IsPreviewed),
IsRead = ISNULL (@IsRead, IsRead),
IsFlagged = ISNULL (@IsFlagged, IsFlagged),
IsDeleted = ISNULL(@IsDeleted, IsDeleted)
IsArchived = ISNULL(@IsArchived, IsArchived)
WHERE MessageId = @MessageIdCode Snippets
UPDATE MessageDetail
SET
IsDraft = ISNULL (@IsDraft, IsDraft),
IsPreviewed = ISNULL (@IsPreviewed, IsPreviewed),
IsRead = ISNULL (@IsRead, IsRead),
IsFlagged = ISNULL (@IsFlagged, IsFlagged),
IsDeleted = ISNULL(@IsDeleted, IsDeleted)
IsArchived = ISNULL(@IsArchived, IsArchived)
WHERE MessageId = @MessageIdContext
StackExchange Database Administrators Q#2940, answer score: 10
Revisions (0)
No revisions yet.