I have this scenario ---- where a stored procedure may be given parameter values optionally. If the values are empty/default I would like to retain the existing values.
Is the following way of handling it with CASE statements correct? This works for me but is there a better way to do this?
CREATE PROCEDURE [UpdateUser]
(
@UserID int,
@UserKey VARCHAR(32),
@UserName varchar(50),
@CategoryID INT = 0,
)
AS
BEGIN
SET NOCOUNT ON
UPDATE [Users]
SET
[UserKey] = (CASE WHEN (LEN(RTRIM(LTRIM(@UserKey)))>0) THEN @UserKey ELSE UserKey END )
,[UserName] = (CASE WHEN (LEN(RTRIM(LTRIM(@UserName)))>0) THEN @UserName ELSE UserName END )
,[CategoryID] = (CASE WHEN (@CategoryID>0) THEN @CategoryID ELSE CategoryID END )
WHERE
[UserID] = @UserID
END