# Error while using FOR XML PATH statement

**URL:** <https://www.epiusers.help/t/error-while-using-for-xml-path-statement/102053>\
**Category:** Kinetic ERP\
**Tags:** baq\
**Created:** [April 25, 2023, 3:38pm UTC](https://www.epiusers.help/t/error-while-using-for-xml-path-statement/102053 "2023-04-25T15:38:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DGodfrey](https://www.epiusers.help/user_avatar/www.epiusers.help/dgodfrey/32/5177_2.png) [@DGodfrey](https://www.epiusers.help/u/DGodfrey)\
**Post date:** [April 25, 2023, 3:38pm UTC](https://www.epiusers.help/t/error-while-using-for-xml-path-statement/102053/1 "2023-04-25T15:38:38Z")

</div>

I created a BAQ that **should** concatenate the CommentText field of multiple records into a single column using the FOR XML PATH statement. I am receiving a **“Severity: Error, Text: SQL Statement”** error when I test the BAQ. I have tried to simplify the query from what I originally had, trying to identify the issue. I also removed the UDField, at one time, and that had no impact. I can not find where my mistake is.

I am hoping that someone can see my mistake since I have been staring at this for a while now.  
[DG\_TEST3.baq](https://www.epiusers.help/uploads/short-url/dW5XCYo4ZhROj50wMD8bUD2iKlZ.baq) (34.3 KB)  
Help help!! Thank you

The query phrase text is

 ![QueryPhraseText](https://www.epiusers.help/uploads/default/original/3X/5/1/51cefdd851efa7f40a206d58aa42728b492d55e1.jpeg)

The Main Query info

 ![MainQueryCalcFields](https://www.epiusers.help/uploads/default/original/3X/7/5/75188b71e6b3c7aa316e362f0d23d9e5ccb6a052.jpeg)  
 ![MainQueryFields](https://www.epiusers.help/uploads/default/original/3X/c/1/c1a8c0098f991414a7fd5c92d7b2e5a17f00ae97.jpeg)

Subquery Info

 ![Subquery_SubQueryCriteria](https://www.epiusers.help/uploads/default/original/3X/e/3/e3e16c522f3a030a6ef841cf08fa7f8f97b32dc9.jpeg)  
 ![SubQueryCalcFields](https://www.epiusers.help/uploads/default/original/3X/7/6/76ece8e828c6557715fbe487d25db5f9e5899c5c.jpeg)  
 ![SubQueryFields](https://www.epiusers.help/uploads/default/original/3X/9/6/9645b781c02b71cb955640fa3c8b3f3c94548bdc.jpeg)

---

<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:** [April 25, 2023, 3:44pm UTC](https://www.epiusers.help/t/error-while-using-for-xml-path-statement/102053/2 "2023-04-25T15:44:01Z")

</div>

XML Path For was a hack used until SQL Server provided the String\_agg command. Lots of posts here by @Banderson explaining the better way.

> [@BAQ Concatenate Inventory Bin information error](https://www.epiusers.help/t/baq-concatenate-inventory-bin-information-error/49613/14):
>
> You’re missing the whole subquery to concatenate the rows. if your SQL version is 2019+ then try this as a calculated field string\_agg(PartBin.BinNum, ' ,'); and group by the other fields in the query. [image]

---

<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:** [April 25, 2023, 3:44pm UTC](https://www.epiusers.help/t/error-while-using-for-xml-path-statement/102053/3 "2023-04-25T15:44:57Z")

</div>

I can’t wait to use that function.

---

<div class="post-metadata">

**Author:** ![Banderson](https://www.epiusers.help/user_avatar/www.epiusers.help/banderson/32/50875_2.png) [@Banderson](https://www.epiusers.help/u/Banderson)\
**Post date:** [April 25, 2023, 3:49pm UTC](https://www.epiusers.help/t/error-while-using-for-xml-path-statement/102053/4 "2023-04-25T15:49:09Z")

</div>

~~First, get rid of the replace stuff and just look at the field without the string manipulation stuff. Then work the replace stuff into it piece by piece.~~

never mind, if you’re on a modern version, use String\_Agg()

---

<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:** [April 25, 2023, 3:52pm UTC](https://www.epiusers.help/t/error-while-using-for-xml-path-statement/102053/5 "2023-04-25T15:52:24Z")

</div>

I think your new name should be Brandon Aggregate. Or at least make your EpiUsers name @baggregate. It’s kinda a windowing function to your soul. 🤷

---

<div class="post-metadata">

**Author:** ![DGodfrey](https://www.epiusers.help/user_avatar/www.epiusers.help/dgodfrey/32/5177_2.png) [@DGodfrey](https://www.epiusers.help/u/DGodfrey)\
**Post date:** [April 25, 2023, 4:06pm UTC](https://www.epiusers.help/t/error-while-using-for-xml-path-statement/102053/6 "2023-04-25T16:06:05Z")

</div>

Thank you @Mark_Wonsil & @Banderson ! I had not done this since upgrading.
