2

我正在使用 MySQL 数据库从 ASP 运行查询,我想根据带有人名(全名)的结果创建一个变量(ssResult),如果记录不存在我想分配文本“N/A”对于变量,下面的代码,我目前为我的数据库连接使用了一个函数 getOther,它传递了列名“fullname”:

ssResult = getOtherElse("SELECT fullname FROM table WHERE id=" & inArr(j), "fullname")

下面是函数 getOtherElse 的代码,它仅在返回结果时有效,但在结果为空时无效:

Function getOtherElse(inSQL, getColumn)
    Dim conn, rstemp
    Set conn = Server.CreateObject("ADODB.Connection")
    conn.open myDSN
    Set Session("lp_conn") = conn
    Set rstemp = Server.CreateObject("ADODB.Recordset")
    rstemp.Open inSQL, conn 
    if not rstemp.eof then
        rstemp.movefirst
        getOtherElse=rstemp.fields(getColumn)
    else
        getOtherElse="N/A"
    end if
    rstemp.close
    set rstemp=nothing
    conn.close
    set conn=nothing
End Function

谢谢!

4

3 回答 3

1

你可以试试换行

if not rstemp.eof then

if rstemp.RecordCount > 0 then
于 2008-11-25T16:42:08.050 回答
0

为什么不将 SQL 更改为仅提取带有名字的结果,这样“N/A”将适用:

sResult = getOtherElse("SELECT fullname FROM table WHERE id=" & inArr(j), "fullname AND fullname<>''")
于 2008-11-25T14:11:51.957 回答
0

替换此代码块:

if not rstemp.eof then
    rstemp.movefirst
    getOtherElse=rstemp.fields(getColumn)
else
    getOtherElse="N/A"
end if

使用这段代码:

Dim output
output = "N/A"

If Not rstemp.eof Then
    rstemp.movefirst
    value = rstemp.fields(getColumn)

    If trim(value) = "" Then
        value = "N/A"
    End If
End If

getOtherElse = value

上面的代码始终假定没有返回任何内容,直到连接实际将其设置为返回。然后检查该值是否为空字符串并将该值也设置为“N/A”

于 2008-11-25T14:18:54.453 回答