# Possible SQL injection

**URL:** <https://www.epiusers.help/t/possible-sql-injection/120280>\
**Category:** Kinetic ERP\
**Tags:** baq, e10, epicor\
**Created:** [October 25, 2024, 11:57am UTC](https://www.epiusers.help/t/possible-sql-injection/120280 "2024-10-25T11:57:06Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kshitij](https://www.epiusers.help/letter_avatar_proxy/v4/letter/k/a698b9/32.png) [@Kshitij](https://www.epiusers.help/u/Kshitij)\
**Post date:** [October 25, 2024, 11:57am UTC](https://www.epiusers.help/t/possible-sql-injection/120280/1 "2024-10-25T11:57:06Z")

</div>

Hello Epicor Community,

I’m running into an issue while trying to execute a BAQ query, and I could use some assistance to troubleshoot and resolve it.  
I’ve created a BAQ to retrieve order details, including a concatenated list of part numbers for each order. However, when I attempt to run or save the query, I encounter an error that says:  
Possible SQL injection.  
Review the server event logs for details.

My query:  
select  
[OrderHed].[Company] as [OrderHed\_Company],  
[OrderHed].[OrderNum] as [OrderHed\_OrderNum],  
[OrderHed].[OrderDate] as [OrderHed\_OrderDate],  
[OrderHed].[PONum] as [OrderHed\_PONum],  
[OrderHed].[VoidOrder] as [OrderHed\_VoidOrder],  
[OrderHed].[OpenOrder] as [OrderHed\_OpenOrder],  
[OrderHed].[OrderHeld] as [OrderHed\_OrderHeld],  
[Customer].[CustID] as [Customer\_CustID],  
[Customer].[Name] as [Customer\_Name],  
[Customer].[CreditHold] as [Customer\_CreditHold],  
[SubQuery2].[Calculated\_PartNum] as [Calculated\_PartNum]  
from Erp.OrderHed as OrderHed  
inner join Erp.Customer as Customer on  
OrderHed.Company = Customer.Company  
and OrderHed.BTCustNum = Customer.CustNum  
inner join (select  
[OrderDtl1].[OrderNum] as [OrderDtl1\_OrderNum],  
(CASE  
WHEN COUNT(PartNum) = 1  
THEN MAX(PartNum)  
WHEN COUNT(PartNum) = 2  
THEN (  
SELECT STUFF(  
(SELECT ’ | ’ + PartNum  
FROM OrderDtl AS OD  
WHERE OD.OrderNum = OrderDtl.OrderNum  
FOR XML PATH(‘’)),  
1, 3, ‘’  
)  
)  
ELSE (  
SELECT STUFF(  
(SELECT ’ | ’ + PartNum  
FROM OrderDtl AS OD  
WHERE OD.OrderNum = OrderDtl.OrderNum  
FOR XML PATH(‘’)),  
1, 3, ‘’  
) + ’ | —’  
)  
END) as [Calculated\_PartNum]  
from Erp.OrderDtl as OrderDtl1  
group by [OrderDtl1].[OrderNum]) as SubQuery2 on  
OrderHed.OrderNum = SubQuery2.OrderDtl1\_OrderNum  
where (OrderHed.OrderNum = 30265)

Has anyone encountered a similar issue with BAQ errors related to SQL injection, especially when using string concatenation? Any workarounds or best practices would be greatly appreciated. I am currently on Epicor 10.2.

Thanks in advance for any help or insights you can provide!

---

<div class="post-metadata">

**Author:** ![tsmith](https://www.epiusers.help/user_avatar/www.epiusers.help/tsmith/32/64774_2.png) [@tsmith](https://www.epiusers.help/u/tsmith)\
**Post date:** [October 25, 2024, 12:03pm UTC](https://www.epiusers.help/t/possible-sql-injection/120280/2 "2024-10-25T12:03:09Z")

</div>

What version of SQL Server are you on? If possible, you should start using the String\_agg() function available starting from SQL Server 2017.

FOR XML triggers Epicor’s SQL Injection protection logic and is a workaround that should be replaced with String\_agg when possible.

> **[STRING\_AGG (Transact-SQL) - SQL Server](https://learn.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql?view=sql-server-ver16)**
>
> STRING\_AGG (Transact-SQL)

---

<div class="post-metadata">

**Author:** ![Mark\_Wonsil](https://www.epiusers.help/user_avatar/www.epiusers.help/mark_wonsil/32/51594_2.png) [@Mark\_Wonsil](https://www.epiusers.help/u/Mark_Wonsil)\
**Post date:** [October 25, 2024, 12:03pm UTC](https://www.epiusers.help/t/possible-sql-injection/120280/3 "2024-10-25T12:03:22Z")

</div>

SQL Version and compatibility level?

---

<div class="post-metadata">

**Author:** ![Kshitij](https://www.epiusers.help/letter_avatar_proxy/v4/letter/k/a698b9/32.png) [@Kshitij](https://www.epiusers.help/u/Kshitij)\
**Post date:** [October 25, 2024, 12:22pm UTC](https://www.epiusers.help/t/possible-sql-injection/120280/4 "2024-10-25T12:22:34Z")

</div>

i am not sure but currently we are working on sql 2019

---

<div class="post-metadata">

**Author:** ![Mark\_Wonsil](https://www.epiusers.help/user_avatar/www.epiusers.help/mark_wonsil/32/51594_2.png) [@Mark\_Wonsil](https://www.epiusers.help/u/Mark_Wonsil)\
**Post date:** [October 25, 2024, 12:25pm UTC](https://www.epiusers.help/t/possible-sql-injection/120280/5 "2024-10-25T12:25:29Z")

</div>

As long as the compatibility level is 14, use @tsmith’s advice and move to STRING\_AGG instead of the FOR XML syntax.

[View or change the compatibility level of a database - SQL Server | Microsoft Learn](https://learn.microsoft.com/en-us/sql/relational-databases/databases/view-or-change-the-compatibility-level-of-a-database?view=sql-server-ver16)

---

<div class="post-metadata">

**Author:** ![tsmith](https://www.epiusers.help/user_avatar/www.epiusers.help/tsmith/32/64774_2.png) [@tsmith](https://www.epiusers.help/u/tsmith)\
**Post date:** [October 25, 2024, 12:30pm UTC](https://www.epiusers.help/t/possible-sql-injection/120280/6 "2024-10-25T12:30:00Z")

</div>

If you don’t have access to view the compatibility level of the server, you can create a simple BAQ that uses string\_agg in a calculated field. If the BAQ runs then you are on Compatibility 14+, if not you will receive an error.

---

<div class="post-metadata">

**Author:** ![Kshitij](https://www.epiusers.help/letter_avatar_proxy/v4/letter/k/a698b9/32.png) [@Kshitij](https://www.epiusers.help/u/Kshitij)\
**Post date:** [October 25, 2024, 12:37pm UTC](https://www.epiusers.help/t/possible-sql-injection/120280/7 "2024-10-25T12:37:57Z")

</div>

![image](https://www.epiusers.help/uploads/default/original/3X/7/d/7d6fb06e3d96e7d73c7ea7631ec11fdde615090d.png)

still gives error

---

<div class="post-metadata">

**Author:** ![utaylor](https://www.epiusers.help/user_avatar/www.epiusers.help/utaylor/32/64664_2.png) [@utaylor](https://www.epiusers.help/u/utaylor)\
**Post date:** [October 25, 2024, 12:39pm UTC](https://www.epiusers.help/t/possible-sql-injection/120280/8 "2024-10-25T12:39:06Z")

</div>

Y’all, I’m pretty sure you can’t write new SQL queries in calculated fields, can you???

---

<div class="post-metadata">

**Author:** ![utaylor](https://www.epiusers.help/user_avatar/www.epiusers.help/utaylor/32/64664_2.png) [@utaylor](https://www.epiusers.help/u/utaylor)\
**Post date:** [October 25, 2024, 12:42pm UTC](https://www.epiusers.help/t/possible-sql-injection/120280/9 "2024-10-25T12:42:01Z")

</div>

@Kshitij is trying to write a whole different query in the calculated field.

@Kshitij, you need to use subqueries and CTE functionality within the BAQ framework to do what you’re doing, not write it out as SQL.

---

<div class="post-metadata">

**Author:** ![hkeric.wci](https://www.epiusers.help/user_avatar/www.epiusers.help/hkeric.wci/32/52019_2.png) [@hkeric.wci](https://www.epiusers.help/u/hkeric.wci)\
**Post date:** [October 25, 2024, 12:47pm UTC](https://www.epiusers.help/t/possible-sql-injection/120280/10 "2024-10-25T12:47:18Z")

</div>

You have to make a Sub-Query, then supply in the Sub-Query SubQuery Criteria, then instead of using it in your Main Query as a JOIN, you can use it in a Calculated field with `{SubQueryNameHere}`

 ![image](https://www.epiusers.help/uploads/default/original/3X/1/1/1102f137771e54b8be42ccd581a1c151c7a16f22.png)  
 ![image](https://www.epiusers.help/uploads/default/original/3X/c/c/cc41d7376dbf12a850f7d7a8770951879570f450.png)

Epicor will then insert that Sub-Query as a Sub-Select into that field wherever `{SubQueryNameHere}` is used, you can even use it in Expressions on JOINs etc…

 ![image](https://www.epiusers.help/uploads/default/original/3X/e/8/e8624e5bf64607590ad2bb7e866ea6bd17b9afc4.png)

* * *

> [@Using Count() in BAQ - Incorrect Syntax](https://www.epiusers.help/t/using-count-in-baq-incorrect-syntax/63385/13):
>
> @tmcmullen you can also do Sub-Select SubQuery. Then you wouldnt need to do GROUP BY on your Top-Query. In a Nutshell: Make a Sub-Query but DO NOT Use it on the Designer On your TopLevel Query add the Sub-Query to a Calculated Field On the Sub-Query under SubQuery Criteria you can add WHERE Clause. Example: SubQuery1: SubQuery2: Result: [image] Basically in a simpler view it does SELECT (SELECT count(\*) FROM table WHERe...) as YourCalculate…

---

<div class="post-metadata">

**Author:** ![utaylor](https://www.epiusers.help/user_avatar/www.epiusers.help/utaylor/32/64664_2.png) [@utaylor](https://www.epiusers.help/u/utaylor)\
**Post date:** [October 25, 2024, 12:47pm UTC](https://www.epiusers.help/t/possible-sql-injection/120280/11 "2024-10-25T12:47:42Z")

</div>

Haso, thank you as always for doing the hard work and documenting it for the forum.

---

<div class="post-metadata">

**Author:** ![utaylor](https://www.epiusers.help/user_avatar/www.epiusers.help/utaylor/32/64664_2.png) [@utaylor](https://www.epiusers.help/u/utaylor)\
**Post date:** [October 25, 2024, 12:48pm UTC](https://www.epiusers.help/t/possible-sql-injection/120280/12 "2024-10-25T12:48:46Z")

</div>

@Kshitij do you have access to any of the Epicor Documentation? I think they go through a CTE BAQ or subqueries if I am not mistaken.

You can search it in your kinetic help

![image](https://www.epiusers.help/uploads/default/original/3X/1/7/17d478db2ab8fbbf25cb4f1df26c96e46270ac34.png)

---

<div class="post-metadata">

**Author:** ![amitn](https://www.epiusers.help/user_avatar/www.epiusers.help/amitn/32/86661_2.png) [@amitn](https://www.epiusers.help/u/amitn)\
**Post date:** [October 25, 2024, 12:52pm UTC](https://www.epiusers.help/t/possible-sql-injection/120280/13 "2024-10-25T12:52:10Z")

</div>

In Epicor, SQL injection checks can sometimes be triggered by certain patterns in BAQ subqueries, particularly when using string manipulation functions like `STUFF` or complex conditional logic.

Try updating the inner subquery by removing `FOR XML PATH` or using a simpler aggregation if feasible:

---

<div class="post-metadata">

**Author:** ![Kshitij](https://www.epiusers.help/letter_avatar_proxy/v4/letter/k/a698b9/32.png) [@Kshitij](https://www.epiusers.help/u/Kshitij)\
**Post date:** [October 25, 2024, 12:53pm UTC](https://www.epiusers.help/t/possible-sql-injection/120280/14 "2024-10-25T12:53:22Z")

</div>

ooh! my bad 😅

I am trying to achieve some thing like this:

 ![image](https://www.epiusers.help/uploads/default/original/3X/1/0/10568daf1b0ccd96b7a5d41468292fbb29d1217c.png)

where i can concatenate OrderNums together in one column.  
below is the query:  
select  
[Customer].[CustID] as [Customer\_CustID],  
[Customer].[Name] as [Customer\_Name],  
(REPLACE(REPLACE(((select  
(CAST(OrderHed.OrderNum AS VARCHAR) + ‘,’) as [Calculated\_OrderNum]  
from Erp.OrderHed as OrderHed  
where (OrderHed.CustNum = Customer.CustNum FOR XML PATH(‘’)))) , ‘\</Calculated\_OrderNum\>’,‘’),‘\<Calculated\_OrderNum\>’,‘’)) as [Calculated\_Orders]  
from Erp.Customer as Customer

it is also giving bad sql error. but there is no syntax error.

---

<div class="post-metadata">

**Author:** ![utaylor](https://www.epiusers.help/user_avatar/www.epiusers.help/utaylor/32/64664_2.png) [@utaylor](https://www.epiusers.help/u/utaylor)\
**Post date:** [October 25, 2024, 12:56pm UTC](https://www.epiusers.help/t/possible-sql-injection/120280/15 "2024-10-25T12:56:02Z")

</div>

so if you have that BAQ working, then replace the STUFF function you’re using with string agg in _that_ BAQ

---

<div class="post-metadata">

**Author:** ![utaylor](https://www.epiusers.help/user_avatar/www.epiusers.help/utaylor/32/64664_2.png) [@utaylor](https://www.epiusers.help/u/utaylor)\
**Post date:** [October 25, 2024, 12:58pm UTC](https://www.epiusers.help/t/possible-sql-injection/120280/16 "2024-10-25T12:58:56Z")

</div>

> [@Kshitij](#):
>
> (REPLACE(REPLACE(((select  
> (CAST(OrderHed.OrderNum AS VARCHAR) + ‘,’) as [Calculated\_OrderNum]  
> from Erp.OrderHed as OrderHed  
> where (OrderHed.CustNum = Customer.CustNum FOR XML PATH(‘’)))) , ‘\</Calculated\_OrderNum\>’,‘’),‘\<Calculated\_OrderNum\>’,‘’)) as [Calculated\_Orders]

Make this a string\_agg function not FOR XML.

You can ask an LLM (chatGPT, whatever you want) to take that and make it a string\_agg function instead.

---

<div class="post-metadata">

**Author:** ![hkeric.wci](https://www.epiusers.help/user_avatar/www.epiusers.help/hkeric.wci/32/52019_2.png) [@hkeric.wci](https://www.epiusers.help/u/hkeric.wci)\
**Post date:** [October 25, 2024, 1:01pm UTC](https://www.epiusers.help/t/possible-sql-injection/120280/17 "2024-10-25T13:01:18Z")

</div>

Example

 ![undefined](https://www.epiusers.help/uploads/default/original/3X/6/8/68a85462b063bd096fbe6878e2c59f4820c594eb.png)

---

<div class="post-metadata">

**Author:** ![utaylor](https://www.epiusers.help/user_avatar/www.epiusers.help/utaylor/32/64664_2.png) [@utaylor](https://www.epiusers.help/u/utaylor)\
**Post date:** [October 25, 2024, 1:02pm UTC](https://www.epiusers.help/t/possible-sql-injection/120280/18 "2024-10-25T13:02:39Z")

</div>

Thanks Haso.
