I have the following script in the "Value Changed" under Value Events for one of my tags and it doesn't work but works fine when I run it on the script console.
The designer script console runs in designer scope (a variation of Vision Client Scope).
I would recommend putting this in a Gateway Tag Change Event, as query scripts of this type tend to have longer running times and many of them used in tag events will come back to bite you. Get in the habit of doing as little as possible in Tag Value Change events.