BAQ difference in Pilot compared to LIVE

We are a SaaS customer and we copied LIVE over Pilot less than 1 month ago. We have Query that has been running fine for years, but now users are not seeing all the records. The query runs correctly in Pilot, but in LIVE we get the following error:
Severity: Error, Text: Bad SQL statement.
Review the server event logs for details.
Query returned 10 row(s).
Query has no more records to return.
Query execution total time: 482.1792 ms.

For some reason we are getting some results (but not as many as we should). I am using parameters from BEFORE we copied LIVE to Pilot (so the data in that timeframe should be the same).

I am struggling to understand why the query works perfectly in Pilot but has a Bad SQL statement in LIVE. The queries are identical. I have even exported the working query in Pilot and Imported it into live (and get the error in LIVE with the copied Query).

Epicor gave me the server event logs (because we are SaaS). The message that stands out in the Server Log is “Conversion failed when converting the nvarchar value ‘UserID’ to data type int.” The UserID in the previous sentence is my UserID in Epicor. I cannot find anywhere in the query where we are using the BAQ Special Constant of UserID.

This Query was created by a predecessor and I am struggling to figure out what the issue is.

Can anyone point me in the right direction? What areas should I be checking?

Thank you in advance.

Divide by zero?

this means that some field contains ‘UserID’ string instead of integer

1 Like

One thing to try is to limit your results. There may be one specific row in Live that’s causing all the fuss that isn’t in Pilot. So maybe try to set some criteria to divide the results into smaller chunks to see where the issue is… the other way you can do it is to remove calculated fields by commenting things out… and then one by one add them back in and run it and see when it fails.

That will effectively give you a row (or rows) and a column to be looking at.

1 Like

My calculated fields are not using division. Most are lookups, but the only ones using math are:

max(Task.TaskSeqNum) + 10

MAX(LastTask.TaskSeqNum)

convert(int,LastTask.Key1 )

The rest are primary if statements.

1 Like

search what values you have there

Usually when I see a BAQ return some results and then error out it means it is choking on one specific record. Typically this winds up being a divide by zero, or as @Olga is suggesting, some kind of conversion issue.

Like @dr_dan is saying the record most likely only exists in your live environment.

1 Like

The values we have for Last Task.Key1 are quote numbers from 100001 to 170012 with no blanks and no letters.

then it may happen in some join, where SQL server decided that fields are int, but another side contains nvarchar value.

In Pilot I return 12 rows and Live I get 10 rows (for the date Oct 6) This date is well before we copied Live over Pilot. When I filter for another parameter (Salesperson), I get 1644 rows in Pilot and only 361 rows in Live (and we have almost 1 month of more data i LIVE than we do in Pilot). So I don’t think this is a single record.

The joins are tricky ones. Especially if you’re joining a UD table or something that is nvarchar to a table that is keyed by an integer, say a QuoteHed or something. Then you get a convert statement in the join.
Totally agree with @Olga

Olga, I originally thought this too, which is why I added the calculated field:
convert(int,LastTask.Key1 ).

There is a relationship between Erp.QuoteHed and a subquery called LastTask.
The link is QuoteNum in QuoteHed equal to LastTask_Key1 in (my subquery called LastTask that is only Erp.Task table).

Key1 in Task table is nvarchar:
image

QuoteNum in Erp.QuoteHed is type int
image

But this works in Pilot! Why?

Because the problem is in your data, they must be different between pilot and prod.
Or maybe just retrieval order is different, but I doubt so.

1 Like

one thing you could try (may or may not work) is changing the table order so that it sees the character field first so it sets the type to character instead of int.

SQL server chooses the order itself :woman_shrugging:. Unless you add hint to enforce it

Ah, didn’t know that. So it’s possible the plans are just different then. Would a option(recompile) possibly fix it?

The other (probably right thing) to do then would be to cast your type in the join. You can type in those boxes and force the int field to be character.

It will add more randomness for sure :rofl:
Hint I meant is FORCE ORDER

(Note: I dont’ tell we need to use it here, just show an example)

The data is different we have about 1 month more data in LIVE compared to Pilot. But when using parameters limiting the data to a time before the copy over, we still get the error and we are still missing lines in LIVE.

Parameter limiting does not mean SQL server won’t process your data. It means it won’t return data you don’t want.
You need to sanitize your data. First, find out what give the error - remove join, then remove calc field, (one step at a time) and check when error dissappears

Can you paste in your SQL @BKen ? We’re kind of flying blind here.