1

我正在尝试找到一种方法来根据另一个表中的值动态确定/引用表名。

在概念上,这就是我想要的:

SELECT * FROM TTestCase 
WHERE TTestCase.ElementNo = (
    SELECT TControlTables.ControlTable from TControlTables 
WHERE TControlTables.MessageType in (
    SELECT TTestCaseParameter.MessageType 
    FROM TTestCaseParameter).ElementNo

SELECT TControlTables.ControlTable from TControlTables WHERE TControlTables.MessageType in (SELECT TTestCaseParameter.MessageType FROM TTestCaseParameter ”是确定哪个表的ElementNo应该与TestCase.ElementNo进行比较的查询。

4

1 回答 1

0

一些非常粗略的注释,你会在这里得到更好的代码http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand(v=vs.71).aspx。希望这会让你开始。

    Dim tablename As String
    Dim cn As New OleDbConnection("connection here")
    cn.Open()
    Dim query = "SELECT TControlTables.ControlTable " _
              & "FROM TControlTables " _
              & "WHERE TControlTables.MessageType in (  " _
              & "SELECT TTestCaseParameter.MessageType " _
              & "FROM TTestCaseParameter)"

    Dim dc = New OleDbCommand(query, cn)
    Dim tname As OleDb.OleDbDataReader
    tname = dc.ExecuteReader

    If tname.HasRows Then
        tname.Read()
        tablename = tname("ControlTable")
        tname.Close()
        query = "SELECT * FROM TTestCase WHERE TTestCase.ElementNo = ( " _
              & "SELECT ElementNo FROM [" & tablename & "])"
        dc.CommandText = query
        Dim rows As OleDb.OleDbDataReader
        rows = dc.ExecuteReader

    End If
于 2012-12-14T15:46:53.337 回答