简体   繁体   中英

How to send column data as a comma separated string from stored procedure?

TWO PART QUESTION

I'll try to explain this as best I can.

PART 1

I have a table that looks like this:

在此处输入图片说明

the two last rows are identical exept for the types and UrlId.

I have a SP that I call that executes the following query to get me the data above:

SELECT Urlid, DomainName, OrgId, DomainId, s.TypeId AS TypeId
FROM DomainData d
JOIN SystemUrls s ON s.DomainId = d.Id
WHERE @OrgId IS NULL OR  OrgId = @OrgId 

How can I make so that the data looks like? :

Name(varchar)   OrgId(bigint)        DomainId(bigint)  TypeId(int)
Three            556548-4499             71              2,1

PART 2

Assuming that I dont change the above result and decide to change it on the server with a linq-query(C#)

How would a linq-query look like to give me the desiered result?

For part 1 - then look at this question . Someone answered before me so I won't repeat :)

For part 2 - Split it into 2 parts - the first is to select the joint data and then after having it in memory ( ToList() ) you can use string.Join to concatenate all the TypeId s

var jointData = (from d in db.DomainData
                 join s in db.SystemUrls on d.Id equals s.DomainId into sg
                 new { m.DomainName, m.OrgId, DomainId = m.Id, s.TypeId }).ToList();

var result = jointData.GroupBy(item => new { item.DomainName, item.OrgId, item.DomainId })
             .Select(g => new { 
                 g.Key.DomainName, 
                 g.Key.OrgId, 
                 g.Key.DomainId, 
                 TypeId = string.Join(", ", g.Select(item => item.TypeId))
             });

See if this works for you:
Note: I'm considering Urlid, DomainName, OrgId, DomainId belongs to "DomainData " and Id , TypeId belongs to "SystemUrls" table.

    SELECT  DomainName, OrgId, Id, 
    STUFF(( SELECT  ', ' + cast(s2.TypeId as varchar(5))
                    FROM    SystemUrls AS s2 
                    WHERE   s2.DomainId = d.Id
                    FOR XML PATH('')
                ), 1, 2, '') as TypeId

    FROM DomainData d
    WHERE @OrgId IS NULL OR  OrgId = @OrgId 
    GROUP BY DomainName, OrgId, Id

first create temp table, and declaration with expected range when you want it all in long string format datatype and size may effect so make sure to that.

then,

  insert into #temp(
  Urlid, DomainName, OrgId, DomainId, s.TypeId)
  SELECT Urlid, DomainName, OrgId, DomainId, s.TypeId
  FROM DomainData d
  JOIN SystemUrls s ON s.DomainId = d.Id
  WHERE @OrgId IS NULL OR  OrgId = @OrgId 

 declare @Column1 varchar(350),
 select @Column1= coalesce(@Column1+',','')+convert(varchar(30),TypeId )
 from #temp

update #temp
set Typeid=@column1
from #temp

select * from #temp

by the way this question has been answered previously try not to create duplicate entry this is the link: click here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM