SAP接口编程-RFC系列08 : 获取SAP DDIC的数据字段

转载出处:http://blog.csdn.net/stone0823 https://blog.csdn.net/stone0823/article/details/50637137

获取DDIC的数据字段

如果想获取数据表的字段,使用RFC_READ_TABLE,设置NO_DATA为”X”。也可以使用SAPFunctions.CreateStructure()方法。这个方法更具通用性,能够从DDIC的数据类型中获取相关信息。
以下是示例代码。注意返回的结构是一个复合型结构,每一列包含了诸如列名,数据类型,数据长度,小数位等信息。所以为了直观和调试,使用数组解析和输出。
logon()和logoff()的代码请参考以前博文。

Option Explicit

Public Sub TestGetTableStructure()
    Call Logon
    Call GetTableStructure("SKA1")
    Call logoff
End Sub

Private Sub GetTableStructure(tableName As String)
    If sapConnection Is Nothing Or sapConnection.IsConnected <> tloRfcConnected Then
        Debug.Print "Please connect."
        Exit Sub
    End If

    Dim functions As New SAPFunctionsOCX.SAPFunctions
    Set functions.Connection = sapConnection

    Dim ddicFields As SAPFunctionsOCX.Structure ' data dictionary fields
    Set ddicFields = functions.CreateStructure(tableName) ' get fields from tableName

    Dim arr() As Variant
    arr = StructToArray(ddicFields)

    ' Print structure
    Dim row As Integer
    Dim col As Integer
    For row = 1 To UBound(arr, 1)
        For col = 1 To UBound(arr, 2)
            Debug.Print arr(row, col),
        Next
        Debug.Print  ' new line
    Next
End Sub


Private Function StructToArray(struct As SAPFunctionsOCX.Structure) As Variant
    Dim arr() As Variant
    Dim cols As Integer
    cols = struct.ColumnCount
    ReDim arr(1 To cols, 1 To 3) As Variant ' rowcount is cols ,column count is 3

    ' 可以把strcut理解为一个复合结构,每一个元素包括多项内容
    ' 使用二维的array还原成行列格式
    Dim i As Integer
    For i = 1 To struct.ColumnCount
        arr(i, 1) = struct.ColumnName(i)         '第一列为列名
        arr(i, 2) = Str(struct.ColumnSAPType(i)) ' 第二列为类型
        arr(i, 3) = struct.ColumnLength(i)       '第三列为长度
    Next

    StructToArray = arr
End Function


发表回复