Using variables

A variable "var" in a SQLUnit test specification is denoted by ${var}. The value of a single variable can be set explicitly using the scalar form of the set tag:


<set name="${var}" value="41">
    

You can also set the value for a group of variables using the non-scalar form of the set tag, shown below, which will set the value of ${myquery.col1} to the value returned from the SELECT statement.


<set name="${myquery}">
  <sql><stmt>select col1 from mytable where col2=45</stmt></sql>
  <result>
    <resultset id="1">
      <row id="1">
        <col id="1" name="c1" type="INTEGER">${col1}</col>
      </row>
    </resultset>
  </result>
</set>
    

Variables can also be set implicitly when it is defined as a variable in a result tag and when it has not been previously declared (ie it does not have a value). The example below sets the value of the variable ${var} from the value returned from the stored procedure AddDept.


<test name="Adding department HR">
  <sql>
    <stmt>select AddDept(?)</stmt>
    <param id="1" type="VARCHAR">Human Resources</param>
  </sql>
  <result>
    <resultset id="1">
      <row id="1">
        <col id="1" name="adddept" type="INTEGER">${var}</col>
      </row>
    </resultset>
  </result>
</test>
    

In all the above cases, the variable ${var} functions like an lvalue in an assignment expression. Once the variable is set, the variable functions like an rvalue in an assignment expression. So if we had specified the scalar set tag above to appear in the setup tag for the test specification or the prepare tag for the test, then the test would compare the value that AddDept returned with the value for the variable ${var}.

The scope of the variable is an individual test specification file. So a variable that has been declared and assigned a value is available for the life of the test suite defined by a single XML file.

The only way to reset the value of a variable that has already been declared and assigned a value is to invoke the set tag for that variable again within the test specification.