Create a query in the db containing the data required, then use that query as the data source for your doc. You shouldn't have to run anything from within the db.

HTH