Checking for common column names among tables

Using <loop> makes it easy to perform multiple identical operations. Instead of previewing multiple tables to decide which column to link on, you can determine which tables contain columns with matching names by looping through a directory folder.



You want to determine all the tables in one or more folders that contain the same column. For instance, you may want to combine all of the tables within a folder, but you do not want to open every table to check if it contains the specific column you want to link on.


  <loop with_="all_tabs" mode_="merge">
      <directory folder="" depth="0"/>
      <merge table2="default.lonely">
        <directory folder="pub.doc.retail.altseg" depth="0"/>
        <sel value="ttype='table'"/>
      <sel value="(type='tab')"/>
      <base table="{@all_tabs.path}"/>
      <willbe name="path" value="'{@all_tabs.path}'"/>
      <sel value="contains(name;'div' 'id')"/>
      <colord cols="name,path"/>
      <col name="name" format="width:20"/>
      <col name="path" format="width:45"/>
  <sort cols="name,path"/>


Creating a block that can check for common column names within tables can be particularly useful if you have folders that contain a large number of tables. This recipe determines if there are any tables in the folders and pub.demo.retail that have column names containing the substrings "div" or "id".

The solution uses a <loop> where the <outer> creates a package of table paths and the <inner> merges the column names of each table. A column is created to hold the table path for each column, and a selection statement using the contains(X;L) function selects only the columns that contain the specified substrings in their names.

A <sort> operation is performed at the end so that the common columns are grouped together and the table paths in each group are sorted in ascending order.

Further reading

If you would like to learn more about the functions and operations discussed in this recipe, click on the links below: