在T-SQL中,您可以这样做:
SELECT ProductId, COALESCE(Price, 0)
FROM Products
Run Code Online (Sandbox Code Playgroud)
你如何在Access SQL中做同样的事情?我看到在VBA中使用Nz进行的示例,但我正在寻找SQL等价物.
谢谢.
pip*_*eek 21
Access支持Nz功能,允许您在查询中使用它.请注意,Nz与T-SQL ISNULL函数相同.它不能像COALESCE那样采用任意数量的参数.
Cod*_*rks 17
如果它在Access查询中,您可以尝试这样做:
"Price = IIf([Price] Is Null,0,[Price])"
Run Code Online (Sandbox Code Playgroud)
看起来我可以使用:
SELECT ProductId, Nz(Price, 0)
FROM Products
Run Code Online (Sandbox Code Playgroud)
似乎工作得很好.
使用Iif(Price is null, 0, Price)应该可以为您带来最佳性能(请参见Allen Browne的性能提示)。但是SQL服务器Coalesce()具有很大的优势Iif()和Nz()它可以在级联处理几个参数。因此,我创建了这个等效的快速VBA:
Function Coalesce(ParamArray varValues()) As Variant
'returns the first non null value, similar to SQL Server Coalesce() function
'Patrick Honorez --- www.idevlop.com
Dim i As Integer
Coalesce = Null
For i = LBound(varValues) To UBound(varValues)
If Not IsNull(varValues(i)) Then
Coalesce = varValues(i)
Exit Function
End If
Next
End Function
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
60349 次 |
| 最近记录: |