📜  在SQL Server中将行合并为字符串

📅  最后修改于: 2021-08-29 10:56:28             🧑  作者: Mango

假设我们需要从任何给定列表中选择所有数据。我们可以使用多个查询来组合SQL Server中的行以形成字符串。

示例1:
让我们假设下面有一个名为“ geek_demo ”的表–

FirstName LastName Salary City
Ankit Gupta 24500 Delhi
Babita Dutta 23600 Noida
Chetan Jain 25600 Noida
Deepak Saini 24300 Delhi
Isha Sharma 25900 Delhi
Khushi Singh 24600 Noida
Megha Goyal 25500 Noida
Parul Kumari 23900 Noida

方法1:
在下面的示例中,我们将使用COALESCE函数合并行。

查询以串联SQL Server中的行–

DECLARE @Names VARCHAR(MAX)  
SELECT @Names = COALESCE(@Names + ', ', '') + [FirstName] 
FROM [geek_demo]
SELECT @Names AS [List of All Names]

输出 :

List of All Names
Ankit  , Babita    , Chetan    , Deepka    , Isha      , Khushi    , Megha     , Parul    

方法2:
在下面的示例中,我们还将合并姓氏行。

查询以串联SQL Server中的行–

DECLARE @FirstNames VARCHAR(MAX)
DECLARE @LastNames VARCHAR(MAX)
SELECT @FirstNames = COALESCE(@FirstNames + ', ', '') + [FirstName] 
FROM [geek_demo]   
SELECT @LastNames = COALESCE(@LastNames + ', ', '') + [LastName] 
FROM [geek_demo] 
SELECT @FirstNames AS [List of All First Names],
            @LastNames AS [List of All Last Names]

输出 :

List of All First Names List of All Last Names
Ankit  , Babita    , Chetan    , Deepka    , Isha      , Khushi    , Megha     , Parul  Gupta     , Dutta     , Jain      , Saini     , Sharma    , Singh     , Goyal     , Kumari 

方法3:
在下面的示例中,我们还将串联姓氏行。

查询以串联SQL Server中的行–

DECLARE @FirstNames VARCHAR(MAX)
DECLARE @LastNames VARCHAR(MAX)
SELECT @FirstNames = CONCAT(@FirstNames + ', ', '') + [FirstName] 
FROM [geek_demo]  
SELECT @LastNames = CONCAT(@LastNames + ', ', '') + [LastName] 
FROM [geek_demo]
SELECT @FirstNames AS [List of First All Names],
   @LastNames AS [List of All Last Names]

输出 :

List of First All Names List of All Last Names
Ankit  , Babita    , Chetan    , Deepka    , Isha      , Khushi    , Megha     , Parul  Gupta     , Dutta     , Jain      , Saini     , Sharma    , Singh     , Goyal     , Kumari 

方法4:
在下面的示例中,我们将使用填充函数并针对XML路径合并两列(名字和姓氏)中的行。

查询以串联SQL Server中的行–

SELECT STUFF((
   SELECT ',' + SPACE(1) + [FirstName],
   ' ' + SPACE(1) + [LastName]
 FROM [geek_demo]
  FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'), 1, 1, '')
AS [List Of All Names]

输出 :

List Of Names
Ankit   Gupta     , Babita     Dutta     , Chetan     Jain      , Deepka     Saini     , Isha       Sharma    , Khushi     Singh     , Megha      Goyal     , Parul      Kumari   

示例2:
让我们假设下面有一个名为“ geek_demo1”的表–

Name JoiningDate Email
Ankit 04 May 2018 ankit@gfg.org
Babita 09 July 2019 babita@gfg.org
Chetan 14 June 2017 chetan@gfg.org
Deepak 26 January 2019 deepak@gfg.org
Isha 24 May 2018 isha@gfg.org
Khushi 17 July 2018 khushi@gfg.org
Megha 03 March 2017 megha@gfg.org
Parul 20 December 2018 parul@gfg.org

方法1:
在下面的示例中,我们将使用SQL Server中的CONCAT函数将行组合为字符串。

查询以串联SQL Server中的行–

DECLARE @Names VARCHAR(MAX)
DECLARE @Emails VARCHAR(MAX)
SELECT @Names = CONCAT(@Names + ', ', '') + [Name] 
FROM [geek_demo1]  
SELECT @Emails = CONCAT(@Emails + ', ', '') + [Email] 
FROM [geek_demo]  
SELECT @Names AS [List of All Names],
            @Emails AS [List of All Emails]

输出 :

List of All Names List of All Emails
Ankit  , Babita    , Chetan    , Deepka    , Isha      , Khushi    , Megha     , Parul   ankit@gfg.org, babita@gfg.org, chetan@gfg.org, deepak@gfg.org, isha@gfg.org, khushi@gfg.org, megha@gfg.org, parul@gfg.org

方法2:
在下面的示例中,我们将使用SPACE和XML路径合并SQL Server中的行。

查询以串联SQL Server中的行–

SELECT STUFF((
  SELECT ',' + SPACE(1) + [Email]
  FROM [geek_demo]
  FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'), 1, 1, '')
AS [List Of All Emails]

输出 :

List Of All Emails
 ankit@gfg.org, babita@gfg.org, chetan@gfg.org, deepak@gfg.org, isha@gfg.org, khushi@gfg.org, megha@gfg.org, parul@gfg.org