想象一下,我们需要从任何给定列表中选择所有数据。我们可以使用多个查询来组合 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 |
方法一:
在下面的示例中,我们将使用 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:
在下面的示例中,我们还将合并 LastName 行。
在 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:
在下面的示例中,我们将使用 stuff函数和 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 | |
---|---|---|
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 |
方法一:
在下面的示例中,我们将使用 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 |