Purpose
Demonstrate various table variable techniques

1. Create a table variable
Choose a suitable name, e.g. "ctab". Use CreateTable and specify the column names. Example:

CreateTable V[ctab] account name street city country

In many cases you will also need a "structured variable" for the handling of the table rows. We suggest you first define the structured variable and then include this structure in the table definition:

CreateStructure V[customeraddress] account name street city country
CreateTable V[ctab] include=V[customeraddress]

If you work with an SAP database table in conjunction with the table variable you may use the SAP field names of the corresponding SAP tables as table column names, but this is not required.

Please note: All names (table name and column names) are case sensitive.

To  display the full content of a table variable in the debugger, click the [...] icon:

This will open up the text editor with a temporary file containing all cell values as TAB-delimited text:

 


2. Delete a table variable
When you no longer need a table variable it is good practice to delete it, freeing the memory:
 
DeleteTable V[ctab]

You may also delete a structured variabe although this is less important since a structured variable normally does not occupy much memory:

Please note: To delete the table content without deleting the table definition, use
Clear V[ctab]


3. Write into table cells
You can set a value into any of the tables cells; the specified table rows between 1 and the row that you name in the Set statement are allocated automatically (empty celll values).

Example:

Set V[ctab.account.5000] "K1032"

This writes the value "K1032" into row 5000, column "account" All rows 1 to 5000 are automatically allocated if not yet present.

If you use a very high index e.g.

Set V[ctab.account.1000000000] "K1032"
you may run out of memory:

Line x: "Not enough memory for table variable"

Typically this occurs due to a programming error in an infinite loop.


4. Internal memory requirement
For each empty cell, i.e. a cell filled with "" or an initial cell, GuiXT needs 4 bytes of memory. This means that when we allocate 100 000
rows by:

Set V[ctab.account.100000] "K1032"

or

Set V[ctab.rowcount] "100000"

the memory of the SAP GUI process increases by
100 000*4*5 Bytes, approximately 2MB.

When there are values in some cells the additional memory needed is around 24 + value length in bytes for each cell. In our example, if all 500 000 cells are filled and the mean value length of the cell content is 12 this adds up to approximately 18 MB memory.

If you want to use GuiXT for mass data processing with several million data records, it may be necessary to use temporary files instead of table variables, because the entire address space of the SAP GUI process is limited to about 2 000 MB due to the 32-Bit process address space restriction.


5. CPU time to set a cell value
GuiXT needs about 5 seconds to fill in 1 million cell values (200 000 rows, 5 columns), using a loop in an InputScript. Not terribly fast but sufficient for most UI applications.


6. Fill cells via a structured variable
Instead of setting each cell value separately you may fill a structured variable and use AppendRow to append the structured variable to the table.
Example: The following scripts achieve the same result:

  • We fill each cell separately

    Set V[k] 0
    label next_row
    if V[k<100]
       Set V[k] &V[k] + 1

     
    Set V[ctab.account.&V[k]] "..."
       Set V[ctab.name.&V[k]] "..."
      Set V[ctab.street.&V[k]] "..."
     
    Set V[ctab.city.&V[k]] "..."
     
    Set V[ctab.country.&V[k]] "..."

      goto next_row
    endif

  • We fill a structured variable and append it to the table

    Set V[k] 0
    label next_row
    if V[k<100]
       Set V[k] &V[k] + 1
        
      Set
    V[customeraddress.account]
    "..."
      Set V[customeraddress.name] "..."
      Set V[customeraddressb.street] "..."
      Set V[customeraddress.city] "..."
      Set V[customeraddress.country] "..."

      AppendRow V[customeraddress] table=V[ctab]

      goto next_row

Please note: In AppendRow you can use a structured variable whose components represent only a subset of the table columns; AppendRow then leaves the remaining table cells initial.


7. Change V[tab.rowcount]
The variable V[tab.rowcount] contains the current number of rows in the table.
 You may use
  Set
V[tab.rowcount] "n"
in your script, increasing or decreasing the row count:

  • n=0
    Same as Clear V[tab], i.e. all rows are removed.
  • n higher than current rowcount
    Appends empty rows until rowcount equals n
  • n below current rowcount
    Removes rows at the end of the table until rowcount equals n


8. Copy a table V[tab] into an equally structured table V[tab2]
Use CopyText to/from a temporary text:

// copy table V[tab] into V[tab2]
CopyText fromTable=V[tab] toText="temp"
CopyText fromText="temp" toTable=V[tab2]

// clear temporary text
Clear text[temp]

Please note:

  • Do not copy table variables with

    Set V[tab2] "&V[tab]"    // !!! does not work for large tables !!!

    This works well for small tables, but not for larger ones. The reason is that the expression "&V[tab]" copies the table content into an internal command parameter area using JSON format (JavaScript Object Notation)  and then the Set command is interpreted. This parameter area is of limited size (8000 characters) without dynamic re-allocation for large values. So for a large table the JSON value of the table is truncated and the Set command aborts with the error message



    instead of copying the table.
  • The CopyText command uses one text line for each cell value; It does not store or interpret the column names of the two table variables. The column names can actually be different; they are mapped by position, not by name. See 9. for how to copy differently structured tables.

Similarly you can combine two tables into a third one:

// combine tables V[tab1] and V[tab2] into V[tab3]
CopyText fromTable=V[tab1] toText="temp"
CopyText fromTable=V[tab2] toText="temp"  -appendline

CopyText fromText="temp" toTable=V[tab3]

// clear temporary text
Clear text[temp]


9. Copy a table V[tab] into a table V[tab2] with possibly different structure
We want to copy each column in V[tab] to the column with same name in V[tab2] and leave all other V[tab2] columns empty.
For this purpose we apply the -JSON option of the CopyText command:

// copy table V[tab] into V[tab2] (matching column names)
CopyText fromTable=V[tab] toText="temp" -JSON
CopyText fromText="temp" toTable=VV[tab2] -J-JSON

// clear temporary text
Clear text[temp]

The JSON format (JavaScript Object Notation) is a commonly used data exchange format which includes the column names. Example:

[{"account": "K1032", "city": "Toronto", "toDelete": ""}, {"account": "K1033", "city": "Paris"3", "toDelete": "X"}, ....    ]


10. Delete table rows
WIth "DeleteRow" you can delete a single row. If the row or rows to be deleted are at the end of the table you decrease V[tab.rowcount] as explained in 7.

11. Save a table variable as .csv file
We use two CopyText commands. Set the appropriate delimiter ";" or ",":

CopyText fromTable=V[ctab] toText="temp" delimiter=";"
CopyText
fromText="temp" toFile="C:\temp\customers.csv"
Clear
text[temp]

You may start the .csv file with appropriate column headers; observe the -appendline option in the first CopyText:

// column headers
Set text[temp] "Account;Name;Street;City;Country"

CopyText fromTable=V[ctab] toText="temp" delimiter=";"  -appendline
CopyText
fromText="temp" toFile="C:\temp\customers.csv"
Clear
text[temp]


12. Read SAP database values into a table variable
We suggest you use the function module /guixt/dbselect, since it provides a flexible and efficient way to read data from an SAP table.

As an example, we read all customers from database table KNA1 whose name contains the string "computer"

  • Method 1
    Define the table variable so that the columns exactly match the result set of the select call. The column names can be different from the database column names.

    CreateTable V[ctab] account name street city country

    Clear text[r]
    Call /guixt/dbselect _
     
    in.table="KNA1" _
     
    in.condition="UPPER( NAME1 ) LIKE '%COMPUTER%' " _
     
    in.fields="KUNNR,NAME1,STRAS,ORT01,LAND1" _
     
    table.values="r"

    CopyText fromText="r" toTable=V[ctab]


  • Method 2
    Same as Method 1, but we use the GuiXT Open Call Interface. No intermediate text variable is necessary in this case.

    CreateTable V[ctab] account name street city country

    Set V[saptable] "KNA1"
    Set V[condition] "UPPER( NAME1 ) LIKE '%COMPUTER%' "
    Set V[fields] "KUNNR,NAME1,STRAS,ORT01,LAND1"

    Call /guixt/dbselect _
     
    export.table="saptable" _
     
    export.condition="condition" _
     
    export.fields="fields" _
     
    import.values="ctab"

Please note: The UPPER() function in the where-condition requires SAP kernel 7.10 and upwards and /guixt/dbselect 20200129 and upwards.

Components InputAssistant