我有2个表(table_A,Table_B)和
需要帮助查询子列TABLE_A.DESCRIPString
TABLE_A:
ID PARTNUM DESCRIPString
1 4456 121~134~111
2 4457 122~111
3 4458 122~134
4 4459 111
5 4460 121~134~111
与TABLE_B.DESCRIPID
TABLE_B:
ID DESCRIPID DECSRIPLong
1 121 Silver
2 122 Black
3 111 Mask
4 134 Pickle
最后展示以下内容:
Table_AB
ID PARTNUM DESCRIPString PARTDESCRIP
1 4456 121~134~111 Silver~Pickle~Mask
2 4457 122~111 Black~Mask
3 4458 122~134 Black~Pickle
4 4459 111 Mask
5 4460 121~134~111 Silver~Pickle~Mask
据我所知,大多数人会建议将数据库重做为每列的单个变量,但是在这种情况下不能这样做。
答案 0 :(得分:0)
将此函数放入模块并运行此查询。它应该给你所要求的结果。你没有指定数据类型,所以我选择了函数中的变体。
Public Function fnParseDescr(strWork As Variant) As String
Dim vEnd
Dim strTemp As String
Dim strLook As String
Dim strResult As String
strResult = ""
strWork = Trim(CStr(strWork))
Do While Len(strWork) > 0
If InStr(strWork, "~") = 0 Then
strLook = Trim(DLookup("[DESCRIPLong]", "TABLE_B", "[DESCRIPID] = CVar('" & strWork & "')"))
strResult = strResult & "~" & strLook
strWork = ""
Else
vEnd = InStr(strWork, "~") - 1
strTemp = Left(strWork, vEnd)
strLook = Trim(DLookup("[DESCRIPLong]", "TABLE_B", "[DESCRIPID] = CVar('" & strTemp & "')"))
strResult = strResult & "~" & strLook
strWork = Right(strWork, (Len(strWork) - (vEnd + 1)))
strTemp = ""
strLook = ""
End If
Loop
If Left(strResult, 1) = "~" Then strResult = Right(strResult, (Len(strResult) - 1))
fnParseDescr = strResult
Exit Function
End Function
SELECT ID,
PARTNUM,
DESCRIPString,
fnParseDescr([DESCRIPString]) AS PARTDESCRIP
FROM TABLE_A;