SQL 如果传递的字段为空,则不调用辅助函数

SQL helper function not called if a field passed is empty

我有一个辅助函数,应该连接 Notes 名字Notes 中间名Notes 姓氏 来自名为 IBM Notes.

的 Access table 的字段

助手本身工作正常,但是如果我尝试在上述字段之一的条目为空的行上调用它,则不会调用助手。

如何修改用于调用助手的 SQL 以确保无论用作参数的字段是否为空,它 returns 都是一个结果?

这里是用来调用helper的SQL(DB设置为CurrentDb)-

DB.Execute "UPDATE [IBM Notes] SET [Notes Full Name] = NotesFullName([Notes First Name], [Notes Middle Name], [Notes Surname])"

这是助手本身 -

Function NotesFullName(Optional ByVal firstName As String = "", _
                       Optional ByVal middleName As String = "", _
                       Optional ByVal surname As String = "") As String

    NotesFullName = Trim( _
        IIf(Not firstName = "", firstName & " ", "") & _
        IIf(Not middleName = "", middleName & " ", "") & _
        IIf(Not surname = "", surname, "") _
    )

End Function

那是因为空字段是 Null。试试这个:

Function NotesFullName(Optional ByVal firstName As Variant = Null, _
                       Optional ByVal middleName As Variant = Null, _
                       Optional ByVal surName As Variant = Null) As Variant

    If IsNull(firstName & middleName & surName) Then
        NotesFullName = Null
    Else
        NotesFullName = Trim( _
            IIf(Not IsNull(firstName), firstName & " ", "") & _
            IIf(Not IsNull(middleName), middleName & " ", "") & _
            IIf(Not IsNull(surName), surName, "") _
        )
    End If

End Function

这个查询是否可以在不需要辅助函数的情况下做同样的事情?

UPDATE [IBM Notes] 
SET [IBM Notes].[Notes Full Name] = 
    TRIM(Replace(Nz([Notes First Name],"") & " " & 
    Nz([Notes Middle Name],"") & " " & 
    Nz([Notes Surname],""),"  "," "))

注意: Replace 函数正在用单数替换双数 space。

编辑:在这种情况下,NZ 正在用空字符串替换 Null 字段。