SQL:行转列、合并行
Sql split函数: /*分割一定格式的数据源 以数据表的形式返回 */ CREATE function f_splitstr(@SourceSql varchar(8000),@StrSeprate varchar(10)) returns @temp table(F1 varchar(100)) as begin declare @i int set @SourceSql=rtrim(ltrim(@SourceSql)) set @i=charindex(@StrSeprate,@SourceSql) while @i>=1 begin insert @temp values(left(@SourceSql,@i-1)) set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i) set @i=charindex(@StrSeprate,@SourceSql) end if @SourceSql<>'' insert @temp values(@SourceSql) return end 行转列: select identity(int ,1,1) as rownum1 ,F1 as F1 into #tmp1 from dbo.f_splitstr('1,2,3,4',',') declare @sql