子查询注意点

56 阅读1分钟

写子查询时要注意外面一层和里面的一层要一一对应,子查询如果出现多行的情况,可以如果转成单行,分号分隔 image.png

(SELECT distinct STUFF((SELECT ';' + t.MaterialLotName from (select hd.MaterialLotName from A_ConsumeMaterialsHistory h
                                            inner join HistoryMainline hm on hm.HistoryMainlineId=h.HistoryMainlineId
                                            inner join A_ConsumeMaterialsHistoryDetai hd on hd.ConsumeMaterialsHistoryId=h.ConsumeMaterialsHistoryId
                                            where hm.ContainerName='LOT20231227001')t FOR XML PATH('')) ,1,1,''))