Turns out the problem was a combination of the nest of the with
and my choice of parameter names.
TSQLQuery has a property called schemaname
and I think that the compiler was getting confused with my parameter of the same name.
Catching this wasn't helped by the debugger correctly reporting the value of the parameter in the inspector...