标题响了半天还是觉得词不达意。
实际上有这么条数据
要变成如下的多条
代码:
;WITH roy AS (SELECT [DataRowStartIndex] , [DataRowEndIndex] , [DataColumnStartIndex] , [DataColumnEndIndex] , [Separator], [SheetName]=CAST(left([SheetName],CHARINDEX(',',[SheetName]+',')-1) AS nvarchar(100)), Split=CAST(STUFF([SheetName]+',',1,CHARINDEX(',',[SheetName]+','),'') AS NVARCHAR(100)) FROM AccessTypeForExcel UNION ALL SELECT [DataRowStartIndex] , [DataRowEndIndex] , [DataColumnStartIndex] , [DataColumnEndIndex] , [Separator], [SheetName]=CAST(left(Split,CHARINDEX(',',Split)-1) AS NVARCHAR(100)), Split= CAST(STUFF(Split,1,CHARINDEX(',',Split),'') AS NVARCHAR(100)) FROM Roy WHERE split>'') SELECT [DataRowStartIndex] , [DataRowEndIndex] , [DataColumnStartIndex] , [DataColumnEndIndex] , [Separator], [SheetName] FROM roy OPTION (MAXRECURSION 0)
这个就是按照","分割字符串,
每次分成第一个逗号前([sheetname])的和之后的部分(split),
然后自联,
拿上次余下的部分(split)继续重复前次的分割操作,
直到不满足split>''这个条件时,一条数据就算分割好了