MS Access 2007中NVL功能的替代方案是什么

时间:2013-05-30 05:05:07

标签: sql ms-access

我在MS Access中编写了一个SQL查询

select NVL(count(re.rule_status),0) from validation_result re, validation_rules ru where re.cycle_nbr="+cycle_nbr+" and re.rule_response=ru.rule_desc and re.rule_status='FAIL' and ru.rule_category='NAMING_CONVENTION' group by re.rule_status"

但输出为Null。我想将其转换为Zero。如果我使用NVL函数,则MS Access不接受它。我也尝试了NZ函数,但也提供了相同的输出,即NULL而不是Zero。

1 个答案:

答案 0 :(得分:10)

Nz()绝对是您正在寻找的功能。你说你尝试了它并返回Null,但我觉得很难相信,因为Nz()的重点是 返回Null。供参考:

x = Nz(Null, 0)返回0(VbVarType.vbInteger

x = Nz(Null, "")返回一个空字符串(VbVarType.vbString

x = Nz(Null)返回一个空变量(VbVarType.vbEmpty VbVarType.vbNull

修改

进一步调查表明,在您的特定情况下,问题是您在查询COUNT(re.rule_status)中执行了GROUP BY re.rule_status。如果查询的WHERE子句导致空集(没有返回行),则整个查询只返回没有行而不是单行,值为0或Null。

可以使用以下测试代码验证...

Sub NzTest()
Dim rst As DAO.Recordset, strSQL As String
strSQL = "SELECT Nz(COUNT(LastName), 0) FROM Members WHERE False GROUP BY LastName"
Debug.Print strSQL
Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
If rst.EOF Then
    Debug.Print "No rows were returned."
Else
    Debug.Print "Count = " & rst(0).Value
End If
rst.Close
Set rst = Nothing
End Sub

...产生结果

SELECT Nz(COUNT(LastName), 0) FROM Members WHERE False GROUP BY LastName
No rows were returned.

删除GROUP BY后,我们会......

SELECT Nz(COUNT(LastName), 0) FROM Members WHERE False
Count = 0

......事实上Nz()在这种情况下甚至不需要:

SELECT COUNT(LastName) FROM Members WHERE False
Count = 0