如何更新sql server存储过程中可为空的列
我有一个存储过程负责更新我的人员表。我并不总是更新每一列,因此我为某些过程参数提供了默认值(空)。但是,如果我真的想将 person.middleinitial 设置为 null 该怎么办?程序如何知道我的意图?
实际上,我的人员表有很多列,并且许多列可以为空,因此根据解决方案,这可能会成为负担。
ALTER PROCEDURE [dbo].[uspPerson_update]
@id uniqueidentifier,
@fname nvarchar(20) = NULL,
@minitial nvarchar(1) = NULL,
@lname nvarchar(20) = NULL
AS BEGIN
SET NOCOUNT ON;
UPDATE Person
SET
fname=ISNULL(@fname,fname),
minitial=ISNULL(@minitial,minitial),
lname=ISNULL(@lname, lname)
WHERE id=@id
END
I have a stored procedure that takes care of updates to my person table. I don't always update every column so I've provided default values (null) for some of the procedure parameters. But, what if I really want to set the person.middleinitial to null? How does the procedure know my intent?
In reality my person table has LOTS of columns and many are nullable so this can become burdensom depending on the solution.
ALTER PROCEDURE [dbo].[uspPerson_update]
@id uniqueidentifier,
@fname nvarchar(20) = NULL,
@minitial nvarchar(1) = NULL,
@lname nvarchar(20) = NULL
AS BEGIN
SET NOCOUNT ON;
UPDATE Person
SET
fname=ISNULL(@fname,fname),
minitial=ISNULL(@minitial,minitial),
lname=ISNULL(@lname, lname)
WHERE id=@id
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要将首字母设置为
NULL
,将其设置为零长度/空字符串''
。然后您就可以区分“未指定”(
NULL
) 和“无首字母”(''
)。Don't set initials to
NULL
, set them to a zero length/empty string''
.Then you can tell the difference between "not specified" (
NULL
) and "no initial" (''
).