多行SQL语句拼成一条数据

Posted on 2014-12-25 15:02  极冰  阅读(607)  评论(0编辑  收藏  举报

将多条数据组合到一行

 
  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)


效果如图:

 

Copyright © 2024 极冰
Powered by .NET 9.0 on Kubernetes