如何使计算列在SQL Server中为空 [英] How to make a computed column nullable in SQL Server

查看:162
本文介绍了如何使计算列在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中为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆