首页 > 代码库 > 行变列 拼接字符串 MSSQL 一个超级搞的问题
行变列 拼接字符串 MSSQL 一个超级搞的问题
由数据ThreeLevelSortID KeyWordID KeyWordName2 142 key12 142 key23 143 key33 144 key4得到数据大ThreeLevelSortID KeyWordName2 key1,key23 key3,key4declare @tblTmp table (id int,ThreeLevelSortID int,KeyWordName nvarchar(max))declare @tblTmpCount intinsert @tblTmp(id,ThreeLevelSortID)select ROW_NUMBER() over (order by t.ThreeLevelSortID), t.ThreeLevelSortID from ( select Distinct ThreeLevelSortID from tbl_ThreeLevelSortKeyWordsDetail) tselect @tblTmpCount = count(1) from @tblTmpdeclare @index intset @index = 0While(@index<)begindeclare @currentThreeLevelSortID intselect @currentThreeLevelSortID = ThreeLevelSortIDfrom @tblTmp where id = @index--拼接关键词字符串declare @keywords nvarchar(max)select @keywords=ISNULL(@keywords+‘;‘,‘‘)+k.KeyWordName from (SELECT h.KeyWordID,h.ThreeLevelSortID,j.KeyWordName FROM dbo.tbl_ThreeLevelSortKeyWordsDetail h,dbo.tbl_KeyWords jWHERE ThreeLevelSortID = @currentThreeLevelSortIDAND h.KeyWordID = j.KeyWordID) kupdate @tblTmp set KeyWordName = @keywords where ThreeLevelSortID = @currentThreeLevelSortIDset @keywords = nullset @index = @index +1endselect * from @tblTmp求简单方法
行变列 拼接字符串 MSSQL 一个超级搞的问题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。