Whenever the root statement returns some number of rows, any child statements of the root statement are executed for every row returned. This allows other tables to be referenced for further details related to the basic information in the first table.
These child statements can also have child statements which will be executed for every row returned by its direct parent statement, allowing any number of related tables to be queried. All these child statements can contain parameters in them of the form @column_name
where column_name
must be the name of one of the columns returned by the parent statement. Then, before the statement is executed for each row, the value of the column for that row is used in place of the parameter.
For example, a child statement had the form:
SELECT firstname, secondname FROM patientDetails WHERE patientID = @patientID
And the parent statement had returned a result set including:
PatientID
---------
12345
54321
62943
The child statement would be executed three times, once for each row returned, the first time with @patientID
replaced by 12345
, the second time replaced with 54321
and the third time replaced with 62943
.
Once all the statements have been executed and all the information gathered from the database, an XML message is built which contains all the gathered data.