Our Cookie Policy

Echofavor and carefully selected third parties use cookies on this site to improve performance, for analytics and to show you offers tailored to your interests on our site and third party sites. By continuing to use our site, you consent to our use of cookies. Privacy Policy

Life is memorable. Share your experience. Help others. Return the favor. Terms Of Use    Privacy Policy    About    FAQ    Help   Contact Us  
As an Amazon Associate and affiliate with other third parties, Echofavor earn from qualifying purchases and display advertisements.
0.1241964
Research >> Programming >> T-SQL >> subquery returned more than 1 value - return multiple results as comma delimited

subquery returned more than 1 value - return multiple results as comma delimited

You may want to do a subquery to get data from another table, but you realize there could be more than one record. If you do a normal subquery in your SELECT statement, you will get the following error.

Error

subquery returned more than 1 value


Solution

You can use STUFF ... FOR xml path to return multiple records as a comma delimited single result. 


SELECT [EmployeeUid]

      ,[FirstName]

      ,[LastName]

      ,[Email]

      ,[Phone]


  ,STUFF(

(  

SELECT distinct ',' + convert(varchar(10),LocationId)

FROM [dbo].[Offices] a where a.EmployeeUid = g.EmployeeUid

FOR xml path('')

)

                        , 1

                        , 1

                        , '') as Locations

  FROM [dbo].[Employees] g


Thanks for subscribing to Echofavor
Loading...
  

Copyright © Echofavor 2021. All Rights Reserved. Powered by Echofavor
Copyright © Echofavor 2021. All Rights Reserved.
Powered by Echofavor