将多条数据组合到一行
declare @sql varchar(5000) set @sql='select ''roleinfo:''' declare @column varchar(20) declare cursor1 cursor for select id from Userrole where 登录名='tl' open cursor1 fetch next from cursor1 into @column while @@FETCH_STATUS=0 begin set @sql=@sql+','+@column+'as id,(select 角色 from UserRole as s where s.id='+@column+') as 角色名称' fetch next from cursor1 into @column end close cursor1 deallocate cursor1 exec (@sql)
效果如图: