无法在sql数据库中检查空值

时间:2013-01-29 13:28:19

标签: sql sql-server f#

使用F#查询语法我试图获取某个字段不为空的所有记录,但我似乎无法找到一种方法。

首先,我尝试了:

query {
    for h in dc.Table do
    where (h.SectorId <> null)
    select h
}

但是错误说明The type 'Nullable<Guid>' does not have 'null' as a proper value. To create a null value for a Nullable type use 'System.Nullable()'.因此,按照建议将null替换为Nullable(),我使用了:

query {
    for h in dc.Table do
    where (h.SectorId <> Nullable())
    select h
}

当我在LINQPad中使用上述查询时,它不会检索任何值,即使我知道它们存在。问题似乎出现在SQL中:

-- Region Parameters
DECLARE @p0 UniqueIdentifier = null
-- EndRegion
SELECT [t0].[Id], [t0].[Name], [t0].[SectorId], [t0].[Blah], [t0].[Meh], [t0].[DisplayOrder]
FROM [Table] AS [t0]
WHERE [t0].[SectorId] <> @p0

当然这不起作用,因为SQL中的NULL <> NULL总是假的;应该在哪里阅读WHERE [t0].[SectorId] is not null。如何在F#查询中检查null?

1 个答案:

答案 0 :(得分:6)

尝试

query {
    for h in dc.Table do
    where h.SectorId.HasValue
    select h
}
相关问题