原始结构:

SQL Server 中多行多列连接成为单行单列Column1     Column2                                                                                              
SQL Server 中多行多列连接成为单行单列----------- ----------
SQL Server 中多行多列连接成为单行单列1           A
SQL Server 中多行多列连接成为单行单列1           B
SQL Server 中多行多列连接成为单行单列2           C
SQL Server 中多行多列连接成为单行单列2           D
SQL Server 中多行多列连接成为单行单列2           E
SQL Server 中多行多列连接成为单行单列3           F

查询效果:

 

SQL Server 中多行多列连接成为单行单列Column1     Column2                                                                 
SQL Server 中多行多列连接成为单行单列----------- ------------------
SQL Server 中多行多列连接成为单行单列1           A,B
SQL Server 中多行多列连接成为单行单列2           C,D,E
SQL Server 中多行多列连接成为单行单列3           F

即将 Column1 相同的行的 Column2 连成一列。
不知如何描述此种用法,是否具有像交叉表相关的 Cross-Table 和 Pivot  之类的约定成熟的专业称谓?
是否也可以称为另一种 Cross-Table ?
此需求应该是常见的,网上也有许多DEMO,只是 CSDN 中频繁有新手提问,现简单实现一个DEMO,以便参考。

SQL Server 中多行多列连接成为单行单列-- 多行多列连接成为单行单列示例:需要一个自定义函数
SQL Server 中多行多列连接成为单行单列--
 http://community.csdn.net/Expert/TopicView3.asp?id=5603231
SQL Server 中多行多列连接成为单行单列

SQL Server 中多行多列连接成为单行单列
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[VertToHorzSample]'and OBJECTPROPERTY(id, N'IsUserTable'= 1)
SQL Server 中多行多列连接成为单行单列
drop table [dbo].[VertToHorzSample]
SQL Server 中多行多列连接成为单行单列
GO
SQL Server 中多行多列连接成为单行单列
SQL Server 中多行多列连接成为单行单列
-- 建立测试数据
SQL Server 中多行多列连接成为单行单列
CREATE TABLE VertToHorzSample(
SQL Server 中多行多列连接成为单行单列    Column1 
int,
SQL Server 中多行多列连接成为单行单列    Column2 
varchar(100)
SQL Server 中多行多列连接成为单行单列)
SQL Server 中多行多列连接成为单行单列
SQL Server 中多行多列连接成为单行单列
GO
SQL Server 中多行多列连接成为单行单列
SQL Server 中多行多列连接成为单行单列
INSERT INTO VertToHorzSample(Column1, Column2)
SQL Server 中多行多列连接成为单行单列
SELECT 1'A'
SQL Server 中多行多列连接成为单行单列
UNION ALL
SQL Server 中多行多列连接成为单行单列
SELECT 1'B'
SQL Server 中多行多列连接成为单行单列
UNION ALL
SQL Server 中多行多列连接成为单行单列
SELECT 2'C'
SQL Server 中多行多列连接成为单行单列
UNION ALL
SQL Server 中多行多列连接成为单行单列
SELECT 2'D'
SQL Server 中多行多列连接成为单行单列
UNION ALL
SQL Server 中多行多列连接成为单行单列
SELECT 2'E'
SQL Server 中多行多列连接成为单行单列
UNION ALL
SQL Server 中多行多列连接成为单行单列
SELECT 3'F'
SQL Server 中多行多列连接成为单行单列
SQL Server 中多行多列连接成为单行单列
GO
SQL Server 中多行多列连接成为单行单列
SQL Server 中多行多列连接成为单行单列
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ConvertVertToHorz]'and xtype in (N'FN', N'IF', N'TF'))
SQL Server 中多行多列连接成为单行单列
drop function [dbo].[ConvertVertToHorz]
SQL Server 中多行多列连接成为单行单列
GO
SQL Server 中多行多列连接成为单行单列
SQL Server 中多行多列连接成为单行单列
-- 建立辅助函数
SQL Server 中多行多列连接成为单行单列
CREATE FUNCTION ConvertVertToHorz(@Col1Val int)
SQL Server 中多行多列连接成为单行单列
RETURNS VARCHAR(8000)
SQL Server 中多行多列连接成为单行单列
AS
SQL Server 中多行多列连接成为单行单列    
BEGIN 
SQL Server 中多行多列连接成为单行单列        
-- 实际项目中,应该考虑 @RetVal 是否会超过 8000 个字符
SQL Server 中多行多列连接成为单行单列
        DECLARE @RetVal varchar(8000)
SQL Server 中多行多列连接成为单行单列        
SET @RetVal = ''
SQL Server 中多行多列连接成为单行单列        
-- 通过递归 SELECT 连接指定列存储到临时变量中
SQL Server 中多行多列连接成为单行单列
        SELECT @RetVal = Column2 + ',' + @RetVal FROM VertToHorzSample WHERE Column1 = @Col1Val
SQL Server 中多行多列连接成为单行单列        
-- 连接多列
SQL Server 中多行多列连接成为单行单列
        -- SELECT @RetVal = Column2 + ',' + Column3 + ',' + Column4 + ',' + @RetVal FROM VertToHorzSample WHERE Column1 = @Col1Val
SQL Server 中多行多列连接成为单行单列
        
SQL Server 中多行多列连接成为单行单列        
-- 去掉尾巴的 , (逗号)
SQL Server 中多行多列连接成为单行单列
        IF LEN(@RetVal> 0
SQL Server 中多行多列连接成为单行单列          
SET @RetVal = LEFT(@RetValLEN(@RetVal- 1)    
SQL Server 中多行多列连接成为单行单列        
--PRINT @RetVal
SQL Server 中多行多列连接成为单行单列
        
SQL Server 中多行多列连接成为单行单列        
RETURN @RetVal
SQL Server 中多行多列连接成为单行单列    
END
SQL Server 中多行多列连接成为单行单列
SQL Server 中多行多列连接成为单行单列
GO
SQL Server 中多行多列连接成为单行单列
SQL Server 中多行多列连接成为单行单列
-- 测试
SQL Server 中多行多列连接成为单行单列
SELECT Column1, dbo.ConvertVertToHorz(Column1) Column2 FROM (SELECT DISTINCT Column1 FROM VertToHorzSample) t
*/

下载

相关文章: