It might have something to do with the (version of) the library that PDO uses to connect to MSSQL?
Or some server setting that is incompatible? If you google for "Error: 17832 Severity: 20" you get quite a lot of hits, also from non-PHP environments.
When you run that code in a controller, did you already have had a DB connection by then? Like for example a session? In other words, maybe it's some PDO initialisation in our PDO driver that conflicts?