Skip to main content

Transformer - Check if column exists and add if not.

In Scott Currie's walkthrough: BimlScript Transformers Primer there is a section that shows how to use a transformer to add columns to a table in BimlStudio. There is a brief mention of 'duplicate checking' (the transformer should first see if the column names exist before adding them). This code snippet shows a way to achieve this.

Using the GetColumnList will return the columns that already exist in the Table, then "!" in the code [if (!TargetNode.GetColumnList().Contains("ColumnToBeAdded"))] determines whether the column already exists, if it does, no change is made, if it does not, then the column is added.

<#@ target type="Table" mergemode="LocalMerge" #>
<Node>
<# if (TargetNode.Name == "TargetTableName") { #>
<Columns>
<# if (!TargetNode.GetColumnList().Contains("ColumnToBeAdded")) { #>
<Column Name="ColumnToBeAdded" DataType="int32" />
<#} #>

</Columns>
<# } #>
</Node>