-1
SELECT distinct STUFF(
   (SELECT distinct ',' + hg.CODE
    FROM SP_HD hg
    Inner join Purchase.SP_DT on GOODS_RECEIPT_ID = hg.ID
    WHERE PO_DT_ID = d.PO_DT_ID
    FOR XML PATH(''), TYPE
   ).value('.', 'NVARCHAR(MAX)') ,1,1,'') GRN_CODE,

The for XML usage is causing me performance issues . Is there any fast alternatives for this ?

3
  • 2
    Let's check out the execution plan and other basic things first.
    – vonPryz
    Oct 27, 2022 at 10:26
  • 4
    Not one, but /two/ 'select distinct's in there - Big Red Flag (for me) performance-wise.
    – Phill W.
    Oct 27, 2022 at 10:54
  • What is this code doing?...string concatenation (aggregation)? What version of SQL Server are you using?
    – J.D.
    Oct 27, 2022 at 12:00

1 Answer 1

0

Is there any fast alternatives for this ?

SQL Server now has a native function for string concatenation across rows: STRING_AGG

Not the answer you're looking for? Browse other questions tagged or ask your own question.