如何将查询结果转换为字符串并将其插入表行

Posted

技术标签:

【中文标题】如何将查询结果转换为字符串并将其插入表行【英文标题】:How can I cast a query result to string and insert it into a table row 【发布时间】:2019-04-05 06:59:27 【问题描述】:

我收到一个错误,即子查询返回多个值,我知道它会这样做,现在我想将该子查询结果转换为一个字符串,然后将该字符串插入到同一位置的表中

我已经尝试过使用循环,但由于除了查询之外我不能在其他任何地方使用循环,它并没有多大帮助。

这是我正在处理的查询

SELECT DISTINCT LiveCampaign_SubscriberList_Email.Email, Firstname, Lastname, dbo.spValueToString(Subscribed) AS Subscribed,

dbo.spValueToString((SELECT COUNT(*) 
    FROM tblEmailBlackList
    WHERE tblEmailBlackList.Email = LiveCampaign_SubscriberList_Email.Email and tblEmailBlackList.PortalID = -2)) As GeneralBlockList,

(SELECT DISTINCT PortalLocalization.PortalName
    FROM tblEmailBlackList
    LEFT JOIN tblLiveCampaignSettings ON tblLiveCampaignSettings.ModuleID = tblEmailBlackList.ModuleID
    LEFT JOIN PortalLocalization ON PortalLocalization.PortalID = tblEmailBlackList.PortalID AND PortalLocalization.CultureCode = 'sl-SI'
    LEFT JOIN #tmp ON tblEmailBlackList.Email = #tmp.Email COLLATE Slovenian_CI_AS
    WHERE LiveCampaign_SubscriberList_Email.Email = #tmp.Email COLLATE Slovenian_CI_AS
    AND tblEmailBlackList.PortalID >= 0 OR tblEmailBlackList.PortalID =- 1)
    AS LocalBlockList

FROM LiveCampaign_SubscriberList_Email

这是我需要转换成字符串的部分:

(SELECT DISTINCT PortalLocalization.PortalName
    FROM tblEmailBlackList
    LEFT JOIN tblLiveCampaignSettings ON tblLiveCampaignSettings.ModuleID = tblEmailBlackList.ModuleID
    LEFT JOIN PortalLocalization ON PortalLocalization.PortalID = tblEmailBlackList.PortalID AND PortalLocalization.CultureCode = 'sl-SI'
    LEFT JOIN #tmp ON tblEmailBlackList.Email = #tmp.Email COLLATE Slovenian_CI_AS
    WHERE LiveCampaign_SubscriberList_Email.Email = #tmp.Email COLLATE Slovenian_CI_AS
    AND tblEmailBlackList.PortalID >= 0 OR tblEmailBlackList.PortalID =- 1)

Result 将是一个字符串,然后将其插入到表中的同一行中,因为该子查询的结果应该是

【问题讨论】:

那么你需要给我们一个逻辑,通过它我们可以限制这个查询只返回一行。 @TimBiegeleisen 你这是什么意思? 所以你想聚合从你给出的查询返回的所有PortalName,用一些东西分隔(比如说,如果它返回两行 - 'A'和'B',你希望它是' A, B'?) 然后插入到哪里? @MarcinJ 是的,可以返回多个 PortalName,我希望所有的都在一行中,并且插入的位置与子查询的结果相同。我将在其中添加更多代码 哪个 SQL Server 版本?能否请您添加 sql-server- 标签? 【参考方案1】:

因此,您需要将返回的行聚合为一个。由于您使用的是 SQL Server 2014,因此 STRING_AGG 不可用,因此我们将使用 XML aggregation trick。试试这个:

SELECT DISTINCT LiveCampaign_SubscriberList_Email.Email, Firstname, Lastname, dbo.spValueToString(Subscribed) AS Subscribed,

dbo.spValueToString((SELECT COUNT(*) 
    FROM tblEmailBlackList
    WHERE tblEmailBlackList.Email = LiveCampaign_SubscriberList_Email.Email and tblEmailBlackList.PortalID = -2)) As GeneralBlockList,
STUFF((
    SELECT DISTINCT ',' + PortalLocalization.PortalName
    FROM tblEmailBlackList
    LEFT JOIN tblLiveCampaignSettings ON tblLiveCampaignSettings.ModuleID = tblEmailBlackList.ModuleID
    LEFT JOIN PortalLocalization ON PortalLocalization.PortalID = tblEmailBlackList.PortalID AND PortalLocalization.CultureCode = 'sl-SI'
    LEFT JOIN #tmp ON tblEmailBlackList.Email = #tmp.Email COLLATE Slovenian_CI_AS
    WHERE LiveCampaign_SubscriberList_Email.Email = #tmp.Email COLLATE Slovenian_CI_AS
    AND tblEmailBlackList.PortalID >= 0 OR tblEmailBlackList.PortalID =- 1
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
    AS LocalBlockList

FROM LiveCampaign_SubscriberList_Email

【讨论】:

解决了,谢谢!还刚刚注意到我将不得不使用 UNION,我认为这不适用于 FOR XML,但我会尝试解决它

以上是关于如何将查询结果转换为字符串并将其插入表行的主要内容,如果未能解决你的问题,请参考以下文章

将日期时间转换为字符串格式并将其传递给查询

如何分离从表行中获取的值并将其存储在雪花中的数组中

如何将 Json 转换为 CSV 并将其发送到大查询或谷歌云存储桶

如何将PageRequest对象转换为查询字符串?

我们如何将字符串转换为日期时间并将其存储在 varchar 中

如何在Javascript中从数组中提取值