[ Good or Bad ] BAQ - SQL Procedure / Function Helpers by Martin Horton

Martin Horton posted an example on his Blog how to execute a SQL Procedure from a BAQ. I think this is promising, as long as you keep it on the dbo.* object and not Epicor!

You can also do Cross-Database SELECTs and create a few nifty Procedures that you think you will re-use! How many times do you have to repeat in a BAQ “Get Default Plant Cost ID”

But I am curious what @Bart_Elia and others alike @danbedwards @josecgomez @timshuwy @Edge @aidacra etc think of this? Bad, Good if Used Properly?


Martin Writes:

BAQ’s have always been very useful things, especially now with the sub query function allowing better use of aggregates. Calculated fields, in BAQ’s, have always been a very important part of the creation of BAQ’s and ultimately Dashboards. But in the past the function set available has been quite limiting - especially in the older versions of Epicor ERP. External BAQ’s in ERP E9 would be used to enable the more useful aspects of SQL - like it’s excellent function set. An External BAQ’s would also give you access to user defined functions. Sometimes you need a custom calculation that SQL doesn’t do as standard.

Something that I’ve not seen commented on or suggested (which is why I thought I’d share it here) is the ability to use more than just the standard set of Epicor built in functions. All the basic ones are there for conversion and string manipulation but what if you wanted something like DateAdd or DateDiff - two very useful built in SQL functions but not in the Epicor basic set? Well the good news is you can use them! The Calculated Field screen will allow the use of any SQL statement that would work in SQL Server Management Studio (SSMS). Think of the Calculated Field screen as a smaller version of the query window in SSMS. This ability isn’t advertised in the screen but it is there.

To demonstrate, I created a little function in my SQL DB. It’s a little pointless but it’s just to demonstrate it can be done. It takes a gross order amount and returns the value without tax on it.

BAQ1
With that added, I’ve gone into Epicor ERP and created a new BAQ, added the Erp.OrderHed table to the query and then gone to add my calculated field.

I usually keep my functions and stored procedures away from Epicor’s schema for tidiness so I always leave them in the dbo schema as default. So in the above screenshot, I’ve fully qualified the name with the correct schema for it but I’m using my custom function which obviously isn’t in Epicor’s palette of functions.

If you save that, I’ve added the order amount field on to the BAQ as well just to show it’s calculating correctly. So the BAQ will return 2 columns of data.

Now if we run the BAQ by on to the Analyze tab and pressing analyze you will get the results.

@Chris_Conn any thoughts sir?

I like turtles.

Pretty cool, especially for those unwieldy beasts of calc fields. But, unless it’s just something you use more than a handful of times, is it worth it?

Probably not, unless you need to write some complex Query that goes through your GL Books and use CURSOR and Variables to get a fancy Total.

I would only use it for those “repeatable things I do”… I think I have this LOGIC Here atleast 18 times repeated throughout BAQs to get a single column, sometimes even Sub-Select Query:

2018-06-28_1836

Even for ParCost going to the Plant figuring out my default Part Cost ID over and over again :slight_smile:

Can it be useful? Sure… but just because you can doesn’t mean you should.

I say #NoThanks it doesn’t “smell” right. At first glance it would violate all security within Epicor which is one of the big selling points of BAQ’s. Poorly written SQL functions and procedures can wreck havoc on your DB… and in my experience the great majority of “BAQ” type users should not be entrusted with this power.

Maybe I’m old fashioned, I am also 100% against adding ANYTHING to the Epicor DB Stored Procs, Views, Functions etc they cause nothing but pain at upgrade time (been there done that) though Epicor has gotten better about it. There was a time where a wrong table or stored proc with the wrong name or naming convention would crash and burn your upgrade.

Again maybe its because I’m always thinking about the future and the next time the customer needs something. Can someone easily pick this up and figure out what I was doing and change it?

You can do something similar in the BPM Code (I won’t share it, you can figure it out on your own and if you break it don’t call me) but with the right call/object you can run straight SQL from BPMs… again a big ole NOPE to that one.

4 Likes

True I’ve seen Upgrades Remove Indexes and Views. We just need a better way for the ICE SDK Folks; not sure what they have been up-to for so many years to add some Re-Usability things… Especially for BPMs and BAQs without going External and digging a big hole which makes Upgrades painful.

Not sure when I would use this, perhaps some SQL Funcs that I repeat alot alot alot.

This makes me fairly uncomfortable… mainly because it MIGHT work today, but next upgrade it might be locked down and secured.
Due to the world becoming more and more SaaS compliant, you will probably see more and more blocks to access data directly. I know that our Multi-tenant SaaS customers are already blocked from writing certain types of C# code that runs on server side… Anytime you allow someone to write something on the server, and then access that from the client, you leave a hole.
WAY back in the olden days, there was a certain hole that some of us knew about that allowed us to directly edit nearly any file on the server. These holes were plugged… I believe that direct calls to Sql functions will also be eventually blocked (my opinion… not based on being a sql expert).

1 Like

Good Point, they could restrict it to tighten security.

Disclaimer: I am NOT a programmer.

I AM, however, the guy who comes in after the fact and has to figure it all out. I’m the detective in the crime movie, but not only am I not the murderer, there is no body, and the clues are written in hieroglyphics. Sort of like National Treasure but I’m not as smart as Nick Cage.

I was recently at a client on E9 who desperately needs (and wants!) to upgrade, but what seemed like a good idea at the time is making that virtually impossible. They probably can’t even do an upgrade, but will have to re-implement. This is admittedly a worst case, as the folks who did those queries/views/customizations/external DLLs are no longer in business and we can’t find them, but the thought holds.

1 Like