首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >表中具有已知值的Microsoft Access查找字段

表中具有已知值的Microsoft Access查找字段
EN

Stack Overflow用户
提问于 2012-08-08 01:31:11
回答 2查看 2.1K关注 0票数 1

我们有一个相当大的Oracle数据库,可以通过Microsoft access和具有只读访问权限的ODBC进行连接。我们使用的前端系统与幕后的结构不匹配,我经常需要通过Microsoft Access查询系统。问题是,我们没有得到任何关于结构的文档,结构需要认真关注。搜索我需要的字段非常耗时。

使用我们的前端,我能够查看我想要查询的值,并且我知道关键字段,但我需要找到包含已知值的字段。

如果我有一条记录,其中我知道字段"A“的值,并且有字段"X”的值,那么可以查询字段"X“吗?

前端展示

代码语言:javascript
运行
复制
Student ID: 12345678
Payments: 23456

后端

代码语言:javascript
运行
复制
TechID: 12345678
???: 23456

我可以查询“?”

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-08 02:39:54

您可以通过迭代表的集合和每个表的字段集合来实现这一点。

代码语言:javascript
运行
复制
Open Database
Get all Tables
For Each Table
   Get all Fields
   For Each Field
       If Field type is text ... and
       If Field size is not TOO Long ...
           Search for string
           If found, write to a results bucket
   Next
Next

下面是编目表的代码示例(源here)

代码语言:javascript
运行
复制
Public Function GenerateDataDictionary(aDataDictionaryTable As String)
'***       Usage: GenerateDataDictionary("MyDataDictionaryTable")
'*** Extracts the information about the tables for the data dictionary 
'*** and inserts it to a table named aDataDictionaryTable

    Dim tdf As TableDef, fldCur As Field, colTdf As TableDefs
    Dim rstDatadict As Recordset
    Dim i As Integer, j As Integer, k As Integer
    Set rstDatadict = CurrentDb.OpenRecordset(aDataDictionaryTable)
    Set colTdf = CurrentDb.TableDefs

    'Go through the database and get a tablename   
    For Each tdf In CurrentDb.TableDefs
    'Do what you want with the table names here.
    rstDatadict.AddNew
    rstDatadict.Update   
rstDatadict.AddNew
    rstDatadict![Table] = tdf.NAME
    rstDatadict![Field] = "----------------------------"
    rstDatadict![Display] = "----------------------------"
    rstDatadict![Type] = ""
    rstDatadict.Update
    rstDatadict.AddNew
    rstDatadict![Table] = "Table Description:"
    For j = 0 To tdf.Properties.Count - 1
          If tdf.Properties(j).NAME = "Description" Then
              rstDatadict![Field] = tdf.Properties(j).Value
          End If
    Next j

    rstDatadict.Update 
    rstDatadict.AddNew
    rstDatadict.Update

For i = 0 To tdf.Fields.Count - 1
          Set fldCur = tdf.Fields(i)          
          rstDatadict.AddNew
          rstDatadict![Table] = tdf.NAME
          rstDatadict![Field] = fldCur.NAME
          rstDatadict![Size] = fldCur.Size

          Select Case fldCur.Type
            Case 1
              FieldDataType = "Yes/No"
            Case 4
              FieldDataType = "Number"
            Case 8
              FieldDataType = "Date"
            Case 10
              FieldDataType = "String"
            Case 11
              FieldDataType = "OLE Object"
            Case 12
              FieldDataType = "Memo"
            Case Else    ' Other values.
              FieldDataType = fldCur.Type
          End Select

          rstDatadict![Type] = FieldDataType                                
                For j = 0 To tdf.Fields(i).Properties.Count - 1
                    If fldCur.Properties(j).NAME = "Description" Then
                        rstDatadict![DESCRIPTION] = fldCur.Properties(j).Value
                    End If

                    If fldCur.Properties(j).NAME = "Caption" Then
                        rstDatadict![Display] = fldCur.Properties(j).Value
                    End If

                    If fldCur.Properties(j).NAME = "Rowsource" Then
                        rstDatadict![LookupSQL] = fldCur.Properties(j).Value
                    End If
                Next j

            rstDatadict.Update

    Next i
    Debug.Print "  " & tdf.NAME
    Next tdf

End Function

您可以在Access中编目您的查找结果,方法是制作一个字段名表,该表连接到一个表名称表。那么您的搜索将基于目录而不是原始集合。

我以这种方式对MAS90的模式进行了反向工程(使用JobOps插件)。没有映射,但我有一个只读的ODBC连接,我完全按照您的建议使用了它。采购会计会给我一个独特的产品编号,我会通过这个综合引擎来运行它。随着时间的推移,我成功地提取了700个表,包括18k个字段,减少到20个表和几百个字段。这使我们可以导出数据。

票数 2
EN

Stack Overflow用户

发布于 2012-08-08 02:20:58

你的问题的答案很简单。不,你不能这么做。

我能想到两种解决方案。第一种方法是手动将所有值连接在一起,然后查找包含该值的行。这是不完美的,但可能会起作用:

代码语言:javascript
运行
复制
select *
from (select t.*, ('|'""col1||'|'||col2+'|' . . .||'|') as allcols
      from t
     ) t
where instr('|23456|', allcols) > 0

这将查找列中包含该值的任何行。可能已经足够接近你想要的了。

第二种方法是使用UNPIVOT来做本质上相同的事情。

我强烈建议您花一点时间找到字段之间的映射,然后在Oracle中创建一个具有应用程序中看到的字段名称的视图。从中期来看,这听起来会帮你节省很多精力。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11851114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档