如何使计算列在SQL Server中为空

编程入门 行业动态 更新时间:2024-10-22 16:45:19
本文介绍了如何使计算列在SQL Server中为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我使用SQL Server 2008和Entity Framework 4.1数据库的第一种方法。

有没有办法可以创建无效的计算列?

我不能像这样创建:

添加全名可以为LastName +''+ FirstName

如果答案为否,那么有什么办法我可以在EF中映射到计算列的EDMX文件中进行适当的标量属性 Nullable

解决方案

默认情况下计算列为空可表达式涉及任何其他可空列,表达式或任何函数。如果你想要NON-NULL计算列,你应该使用ISNULL函数包装列的表达式。

更改表员工将FullName添加为LastName +''+ FirstName

在这种情况下,只有在列之一(FirstName,LastName )将为空可以

如果要使用非空值表达式列组成的可空可计算列,则使用用户定义函数作为表达式:

CREATE FUNCTION dbo.FullName(@LastName nvarchar(100),@FirstName nvarchar(100)) RETURNS nvarchar(201) AS BEGIN 返回@LastName + N''+ @ FirstName END GO 更改表员工将FullName添加为dbo.FullName(LastName, FirstName)

I use SQL Server 2008 and Entity framework 4.1 database first approach .

Is there any way I could create null-able Computed column ?

I can't create like this :

Alter Table Employee Add FullName nullable as LastName + ' ' +FirstName

If the answer is NO , then Is there any way I could make the appropriate Scalar Property Nullable in EDMX File of EF Which map to Computed column ?

解决方案

Computed columns by default nullable is the expression involves any other nullable column, expression or any function. If you want NON-NULL computed column, you should wrap the column's expression with ISNULL function.

Alter Table Employee Add FullName as LastName + ' ' +FirstName

In this case FullName will be nullable only if one of columns (FirstName, LastName) will be nullable too

If you want nullable computed column composed witn non-nullable expression columns, then use the User Defined function as expression:

CREATE FUNCTION dbo.FullName(@LastName nvarchar(100), @FirstName nvarchar(100)) RETURNS nvarchar(201) AS BEGIN RETURN @LastName + N' ' +@FirstName END GO Alter Table Employee Add FullName as dbo.FullName(LastName, FirstName)

更多推荐

如何使计算列在SQL Server中为空

本文发布于:2023-10-07 14:19:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469624.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为空   SQL   Server

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!