Transformer - Check if column exists and add if not.

gravatar

Darryl W

Using a transformer to check if a column exists and add if not.

published 02.21.15

last updated 02.21.15


Share

Tags

  • Tranformer
                            


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

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 Mist. There is a brief mention of 'duplicate checking' i.e. for the transformer to 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.

You are not authorized to comment. A verification email has been sent to your email address. Please verify your account.

Comments

gravatar

Dietrich

6:24am 10.29.16

I needed to replace <Node> with <Table> Do I miss something here or is that just a typo?

gravatar

Johan445

5:41am 10.18.17

I know this is about a year ago that you posted your comment. Did you get an answer somewhere yet? I also want to know this but I suspect this piece of code is meant for MIST.

Thanks in advance. Johan