Back in November 2015, I had a brief post about running a SQL statement against every Dynamics GP company database. In that case, I needed to run a SQL update statement, so it was relatively simple.
But what if you want to retrieve some information from every single Dynamics GP company database?
The process is similar in that it uses a cursor, but if you want to have a nice clean result set, rather than a separate result for every database, it takes a little more creativity.
In this case, I needed to check the first GL account segment in every company database as part of a custom integration. The customer said that each company database has a separate value for the first segment of every GL account, and that I can use that value to map external data to the appropriate Dynamics GP database. So if the GL account in the source data is "15", I can find the company database with GL accounts of 15-XXXX-XXX, and that will be the database into which the data should be imported.
In addition to needing to map that first segment value to each database, I wanted the query to count how many different distinct segment 1 values existed in the company. Each company should only have one distinct value, so if we find that there is more than one, then there is a problem and this technique won't work.
So here's what I came up with, which is a modified version of my November cursor example.
This time, since I'm not running an update statement, I needed to use a Union to combine the multiple select statement results into a single result set.
As always, there are probably several ways to handle this requirement, but it was pretty easy to modify my existing cursor based query and get it done quickly.