0

您好我想知道我们如何将列的值分配给 SQL Server 2012 中的变量

例如,考虑以下场景,其中我检索记录 id 和电子邮件(多个电子邮件作为逗号分隔在单行中)以获取表单中多人字段中提到的人员列表 spd 如下所示(包含许多连接)整个spd的样本结构如下。

BEGIN
DECLARE @VAR1 VARCHAR(50)
DECLARE @VAR2 VARCHAR(50)
DECLARE @FINAL NVARCHAR(MAX)
DECLARE @DEFAULT VARCHAR(50)
SET @DEFAULTE = 'defautemail@xyz.com'

select  @var1 = isnull( email,@DEFAULTE) from usertable inner join userdet on 
                 usertable .recid = userdet.recid and usertable.uid=1 

SET @var2 = isnull (email,@DEFAULTE)  from usertable inner join userdet on 
            usertable .recid = userdet.recid and usertable.uid=1  
SET @FINAL= @var1 + "," +@var2


SELECT FORM.RECORD ID AS [RECORD ID],
       PRODUCT.DESCRIPTION AS [DESCRIPTION],
       SUP.NAME AS [SUPPLIER],
    --  @FINAL AS [FINALEMAILS]
       ABC AS [RESULTEMAILS]
     
     FROM DB1.FORM F INNER JOIN
          DB1.PROJTYPE P
          ON F.RECORDID= P.RECORDID
      LEFT OUTER JOIN
       (select Sup.formid,Supdet.name
       From DB1.Formdet fdet
       inner join DB1.Form Fo on Fo.recordid= Sup.formid)SupRec
       On RecSup.formid = form.recordid
       LEFT OUTER JOIN 
       ( Select FData.Formid,FDDesc.description 
       From DB1.FormDetails FData , DB1.FormDesc F
       on Fdata.formid= F.Formid) Desc on Desc.Formid= form.recordid
     
LEFT OUTER JOIN
(SELECT distinct formid , abc= STUFF((Select ',' + a.email from Db1.User b, 
Db1.formfields c
DB1.Userdetails a, DB1.FormRecordmapping d
where
where b.UserAddressid= a.id
and d.formfieldid= c.id
and b.Userid= d.FormValue
and c.id='3000'
and d.formid = ep.id
FOR XML PATH (''),TYPE).value('text()[1]','nvarchar(max)'),1,1,'') 
FROM [DB1.FORMRecordmapping ep) OPS
on OPS.formid= formid
WHERE  F.NOTIFREQ ='yes'
AND P= 300
END

示例输出显示为:

Record Id     Descripton  Supplier  ResultEmails
---------------------------------------------------------------------------------
22222          DescrTest   Sup1     abc@xyz.com,yrc@hotmail.com,h@xyz.com
33333          DescTest2   Sup2     abc@gmail.com
-------------------------------------------------------------

我的要求是:如何实现将上述代码中的 stuff 函数返回的值(逗号分隔的电子邮件列表)分配给字符串变量(varchar 或 nvarchar 类型)进行进一步处理,而不需要对上述代码进行过多修改?任何帮助,将不胜感激。谢谢

4

0 回答 0