Hello
I have an SQL spd (SQLServer 2012) in which I have a concatenated list of email addresses as follows
SET @emailAddr = @Email1 + "," +@Email2
@Email1 and @Email are stored with email addresses (values such as abc@gmail.com etc) which was fetched directly from the user details table and each of them contains only single email address values.
Email1 and Email2 are declared as VARCHAR
and emailAddr is declared as NVARCHAR
I have another sql query in the same spd which joins multiple tables to fetch another set of email addresses for multiple persons mentioned in a multi person data field of a form . Query looks like follows (Edited query to remove actual references)
***********************************************
select a.email from Db1.User b, Db1.formfields c, DB1.Userdetails a, DB1.FormRecordmapping d
where b.UserAddressid= a.id
and d.formfieldid= c.id
and b.Userid= d.FormValue
and c.id='3000'
and d.Recordid= xxxx
*************************************************
From the above query we get multiple email addresses depending on the persons selected for the data field
Sample output looks like
----------------------------
abc@gmail.com
dd&ee@hotmail.com
chd123@yahoo.com
----------------------------
My requirement is to format the above 3 email results as comma separated values and concatenate it to the existing list of above variable emailAddr as shown in the beginning.
How can this be achieved in a simpler way?. What changes can I make in the above query to achieve this?
Thanks for your help.
What I have tried:
I have tried using listagg and string_agg functions but it is not supported in SQL Server 2012