문제

I am working with an organization that does not have AD distribution groups. SQL is 2012. One table has rows of an identifier and then a second row has the corresponding email addresses. I wanted to run a script to grab the email addresses based off the identifier and create one string separated by semi colons.

So:

SELECT [Email Address] FROM MYTABLE WHERE ID="ABC" 

Result:
123@email.com
345@email.com
567@email.com

I want result of 123@email.com;345@email.com;567@email.com

What would work best, recursive, pivot, SQL Concatenation?

I'd like something fairly straight forward and am just not sure what the best method is. Any tips or advice is greatly appreciated. Thank you!

도움이 되었습니까?

해결책

You can try the following :

DECLARE @email VARCHAR(8000)  
SELECT @email = COALESCE(@email + ';', '') + [Email Address] FROM MYTABLE WHERE ID='ABC' 
SELECT @email
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top