从查询结果而不是行中获取字符串

时间:2016-12-28 06:56:17

标签: sql-server tsql sql-server-2012

我正在使用SQL Server 2012.我需要将下面查询的结果连接成一个长字符串。所以,我不需要获得行结果,而只需要一个字符串。我想用结果填充@TEMPS_MEASURE。

目前,结果如下:

enter image description here

而不是这个,我希望在列之间只返回一个带有<br/>的字符串,如下所示:

H 1 topla kuhinja (2 - 6 °C):<br/>Temperatura: 5.00°C<br/>H 2 hladna kuhinja (2 - 6 °C):<br/>Temperatura: 4.00°C<br/>

我该如何更改查询?

查询:

DECLARE @TEMPS_MEASURE NVARCHAR(500) = NULL
SELECT
 CASE WHEN HATE.HATE_TE1 IS NULL AND HATE.HATE_AC1 IS NULL AND HATE.HATE_TE2 IS NULL THEN NULL ELSE COLI.COLI_NME + ':<br/>' END AS EQ_NAME 
, CASE HATE.HATE_TE1 WHEN NULL THEN NULL ELSE 'Temperatura: ' + CONVERT(VARCHAR(20), CAST(ROUND(HATE.HATE_TE1,2,0) AS NUMERIC(10,2))) + '°C<br/>' END AS TEMP
, CASE HATE.HATE_AC1 
        WHEN NULL THEN NULL 
        WHEN '1' THEN 'Ukrep: Zavržemo jed / živilo<br/>'
        WHEN '2' THEN 'Ukrep: Izmerimo središčno temperaturo jedi / živila (TS*)<br/>'
        WHEN '3' THEN 'Ukrep: Dodatno ogrejemo<br/>'
  END ACTION_UPON
, CASE HATE.HATE_TE2 WHEN NULL THEN NULL ELSE 'Temperatura središča: ' + CONVERT(VARCHAR(20), CAST(ROUND(HATE.HATE_TE2,2,0) AS NUMERIC(10,2))) + '°C<br/>' END AS TEMP_CENTER   
FROM CAHATE HATE
INNER JOIN CACOLI COLI ON COLI.COLI_KEY = HATE.COLI_KEY
WHERE HACC_KEY = 160001417

SOLUTION:

我用FOR XML PATH:

创建了函数
ALTER FUNCTION dbo.f_CAHATE_Get_Temperatures (@iHACC_KEY INTEGER)
RETURNS NVARCHAR(500)
AS BEGIN



DECLARE @TEMPS_MEASURE NVARCHAR(500) = NULL
SELECT @TEMPS_MEASURE = (SELECT
    CONCAT(
         (CASE WHEN HATE.HATE_TE1 IS NULL AND HATE.HATE_AC1 IS NULL AND HATE.HATE_TE2 IS NULL THEN '' ELSE COLI.COLI_NME + ':' END)
        , (CASE HATE.HATE_TE1 WHEN NULL THEN '' ELSE 'Temperatura: ' + CONVERT(VARCHAR(20), CAST(ROUND(HATE.HATE_TE1,2,0) AS NUMERIC(10,2))) + '°C;' END)
        , (CASE HATE.HATE_AC1 
                WHEN NULL THEN '' 
                WHEN '1' THEN 'Ukrep: Zavržemo jed / živilo;'
                WHEN '2' THEN 'Ukrep: Izmerimo središčno temperaturo jedi / živila (TS*);'
                WHEN '3' THEN 'Ukrep: Dodatno ogrejemo;'        
          END)
        , (CASE HATE.HATE_TE2 WHEN NULL THEN '' ELSE 'Temperatura središča: ' + CONVERT(VARCHAR(20), CAST(ROUND(HATE.HATE_TE2,2,0) AS NUMERIC(10,2))) + '°C;' END)
    )
FROM 
    CAHATE HATE
    INNER JOIN CACOLI COLI ON COLI.COLI_KEY = HATE.COLI_KEY
WHERE 
    HACC_KEY = @iHACC_KEY
    for xml path('') ) 


    RETURN @TEMPS_MEASURE
END

5 个答案:

答案 0 :(得分:2)

您还可以使用SQL Server 2012中引入的 CONCAT 函数将不同的字符串值合并为一个。它还处理不同的数据类型以转换为字符串类型。所以,你不会有类型不匹配的例外。

SELECT
    CONCAT(
         (CASE WHEN HATE.HATE_TE1 IS NULL AND HATE.HATE_AC1 IS NULL AND HATE.HATE_TE2 IS NULL THEN '' ELSE COLI.COLI_NME + ':<br/>' END)
        , (CASE HATE.HATE_TE1 WHEN NULL THEN '' ELSE 'Temperatura: ' + CONVERT(VARCHAR(20), CAST(ROUND(HATE.HATE_TE1,2,0) AS NUMERIC(10,2))) + '°C<br/>' END)
        , (CASE HATE.HATE_AC1 
                WHEN NULL THEN '' 
                WHEN '1' THEN 'Ukrep: Zavržemo jed / živilo<br/>'
                WHEN '2' THEN 'Ukrep: Izmerimo središčno temperaturo jedi / živila (TS*)<br/>'
                WHEN '3' THEN 'Ukrep: Dodatno ogrejemo<br/>'        
          END)
        , (CASE HATE.HATE_TE2 WHEN NULL THEN '' ELSE 'Temperatura središča: ' + CONVERT(VARCHAR(20), CAST(ROUND(HATE.HATE_TE2,2,0) AS NUMERIC(10,2))) + '°C<br/>' END)
    )
FROM 
    CAHATE HATE
    INNER JOIN CACOLI COLI ON COLI.COLI_KEY = HATE.COLI_KEY
WHERE 
    HACC_KEY = 160001417

答案 1 :(得分:1)

您需要将4个列组合在一起

SELECT
 (CASE WHEN HATE.HATE_TE1 IS NULL AND HATE.HATE_AC1 IS NULL AND HATE.HATE_TE2 IS NULL THEN '' ELSE COLI.COLI_NME + ':<br/>' END)
+ (CASE HATE.HATE_TE1 WHEN NULL THEN '' ELSE 'Temperatura: ' + CONVERT(VARCHAR(20), CAST(ROUND(HATE.HATE_TE1,2,0) AS NUMERIC(10,2))) + '°C<br/>' END)
+ (CASE HATE.HATE_AC1 
        WHEN NULL THEN '' 
        WHEN '1' THEN 'Ukrep: Zavržemo jed / živilo<br/>'
        WHEN '2' THEN 'Ukrep: Izmerimo središčno temperaturo jedi / živila (TS*)<br/>'
        WHEN '3' THEN 'Ukrep: Dodatno ogrejemo<br/>'        
  END)
+ (CASE HATE.HATE_TE2 WHEN NULL THEN '' ELSE 'Temperatura središča: ' + CONVERT(VARCHAR(20), CAST(ROUND(HATE.HATE_TE2,2,0) AS NUMERIC(10,2))) + '°C<br/>' END)
FROM CAHATE HATE
INNER JOIN CACOLI COLI ON COLI.COLI_KEY = HATE.COLI_KEY
WHERE HACC_KEY = 160001417

注意:我将When NULL then NULL更改为When NULL then ''以确保“+”有效。

答案 2 :(得分:0)

请尝试以下查询。 COALESCE 是SQL提供的功能,可以解决您的问题。

我首先逐行加入所有列,然后使用COALESCE将行转换为一行。

select * into #Temp 
from 
(
  select (isnull(EQ_Name,'')+isnull(Temp,'')+isnull(Action_Upon,'')+isnull(TempCenter,'') )as data 
  from CAHATE 
) as TempData

DECLARE @result nVARCHAR(MAX)

SELECT @result = COALESCE(@result + ', ' + [data], [data]) 
FROM #Temp

PRINT @result

Drop Table #Temp

答案 3 :(得分:0)

以下是一个例子:

;WITH T AS 
(   
    SELECT      
        1 AS Id,
        CONCAT(
             (CASE WHEN HATE.HATE_TE1 IS NULL AND HATE.HATE_AC1 IS NULL AND HATE.HATE_TE2 IS NULL THEN '' ELSE COLI.COLI_NME + ':<br/>' END)
            , (CASE HATE.HATE_TE1 WHEN NULL THEN '' ELSE 'Temperatura: ' + CONVERT(VARCHAR(20), CAST(ROUND(HATE.HATE_TE1,2,0) AS NUMERIC(10,2))) + '°C<br/>' END)
            , (CASE HATE.HATE_AC1 
                    WHEN NULL THEN '' 
                    WHEN '1' THEN 'Ukrep: Zavržemo jed / živilo<br/>'
                    WHEN '2' THEN 'Ukrep: Izmerimo središčno temperaturo jedi / živila (TS*)<br/>'
                    WHEN '3' THEN 'Ukrep: Dodatno ogrejemo<br/>'        
              END)
            , (CASE HATE.HATE_TE2 WHEN NULL THEN '' ELSE 'Temperatura središča: ' + CONVERT(VARCHAR(20), CAST(ROUND(HATE.HATE_TE2,2,0) AS NUMERIC(10,2))) + '°C<br/>' END)
        ) AS String
    FROM 
        CAHATE HATE
        INNER JOIN CACOLI COLI ON COLI.COLI_KEY = HATE.COLI_KEY
    WHERE 
        HACC_KEY = 160001417
)

SELECT 
   STUFF((SELECT '; ' + US.String 
          FROM T US
          WHERE US.Id = SS.Id
          FOR XML PATH('')), 1, 1, '') [FinalString]
FROM T SS
GROUP BY SS.Id, SS.String
ORDER BY 1

如果不需要,您可以删除分隔符;

答案 4 :(得分:0)

如上所述,这是解决问题的方法。 我已使用FOR XML PATH:

创建了功能
ALTER FUNCTION dbo.f_CAHATE_Get_Temperatures (@iHACC_KEY INTEGER)
RETURNS NVARCHAR(500)
AS BEGIN



DECLARE @TEMPS_MEASURE NVARCHAR(500) = NULL
SELECT @TEMPS_MEASURE = (SELECT
    CONCAT(
         (CASE WHEN HATE.HATE_TE1 IS NULL AND HATE.HATE_AC1 IS NULL AND HATE.HATE_TE2 IS NULL THEN '' ELSE COLI.COLI_NME + ':' END)
        , (CASE HATE.HATE_TE1 WHEN NULL THEN '' ELSE 'Temperatura: ' + CONVERT(VARCHAR(20), CAST(ROUND(HATE.HATE_TE1,2,0) AS NUMERIC(10,2))) + '°C;' END)
        , (CASE HATE.HATE_AC1 
                WHEN NULL THEN '' 
                WHEN '1' THEN 'Ukrep: Zavržemo jed / živilo;'
                WHEN '2' THEN 'Ukrep: Izmerimo središčno temperaturo jedi / živila (TS*);'
                WHEN '3' THEN 'Ukrep: Dodatno ogrejemo;'        
          END)
        , (CASE HATE.HATE_TE2 WHEN NULL THEN '' ELSE 'Temperatura središča: ' + CONVERT(VARCHAR(20), CAST(ROUND(HATE.HATE_TE2,2,0) AS NUMERIC(10,2))) + '°C;' END)
    )
FROM 
    CAHATE HATE
    INNER JOIN CACOLI COLI ON COLI.COLI_KEY = HATE.COLI_KEY
WHERE 
    HACC_KEY = @iHACC_KEY
    for xml path('') ) 


    RETURN @TEMPS_MEASURE
END