3 回答
TA贡献1851条经验 获得超4个赞
如果值被传递为 as NULL
,那么您可以简单地执行以下操作:
SET AboutMe = COALESCE(@AboutMe, About_me)
如果它们可以是NULL
或空白,那么您可以使用显式CASE
:
SET AboutMe = (CASE WHEN @AboutMe IS NULL OR LTRIM(@AboutMe) = '' THEN Aboutme ELSE @AboutMe END)
TA贡献1827条经验 获得超8个赞
CASE如果变量IS NULL或空字符串或只是空格,您可以使用自身替换值。否则使用变量的值。这是可能的例如通过使用的组合ltrim(),isnull()并为空字符串的比较。
UPDATE [Users_Table]
SET [User_name] = CASE
WHEN ltrim(isnull(@User_name, '')) = '' THEN
[User_name]
ELSE
@User_name
END,
[About_me] = CASE
WHEN ltrim(isnull(@AboutMe, '')) = '' THEN
[About_me]
ELSE
@AboutMe
END,
[City] = CASE
WHEN ltrim(isnull(@City, '')) = '' THEN
[City]
ELSE
@City
END
WHERE [UserID] = @ID;
TA贡献1864条经验 获得超2个赞
我认为您也想“忽略”,''但问题是这不是NULL.
尝试这个:
CREATE PROC UpdateMy
@User_name nvarchar(15),
@AboutMe nvarchar(150),
@City nvarchar(21),
@ID int
AS
BEGIN
UPDATE Users_Table
SET
[User_name] = CASE WHEN ISNULL(@User_name, N'') = N'' THEN [User_name] ELSE @User_name END,
[About_me] = CASE WHEN ISNULL(@AboutMe, N'') = N'' THEN [About_me] ELSE @AboutMe END,
[City] = CASE WHEN ISNULL(@City, N'') = N'' THEN [City] ELSE @City END
WHERE [UserID] = @ID;
SELECT * FROM Users_Table WHERE [UserID] = @ID;
END
GO
- 3 回答
- 0 关注
- 215 浏览
添加回答
举报