Horrible, but maybe unavoidable
The sample code that laboriously build a where clause is truly horrible. But it's quite possible that it isn't that way because of the developer's addiction to copy-n-paste.
It looks like it's using the stored procedure language from a database. In their early incarnations these languages frequently didn't support any kind of array or collection, and they were often syntactically feeble, making it hard to do the kind of abstraction that is usual in proper programming languages.