sql 子查询stuff功能(同一个人的多任务,多领域成为字符串)

USE [erp2015]
GO
/****** Object:  StoredProcedure [dbo].[GetUser]    Script Date: 03/14/2015 13:27:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		wangyanling
-- Create date: 205-03-12
-- Description:	获取客户信息
-- =============================================
ALTER PROCEDURE [dbo].[GetUser] 
	-- Add the parameters for the stored procedure here
	@UName varchar(200)
AS
BEGIN 
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	
	SET NOCOUNT ON;
	Create table #temp
	(
	    USerID int,
	   GName  VARCHAR(100)
	)
	create table #temp2
	(
	USerID int,
	GName  VARCHAR(100)
	)
	insert into #temp2 SELECT u.UId,db.gName FROM u_user u
	left join User_Group ug on u.UId=ug.uId
	left join
	Db_Group db on ug.gId=db.gId

	insert into #temp  select u.UId,g.GName from u_user u
		left join ug_User_Group ug on u.UId=ug.UId
		left join g_group g on ug  .GId=g.GId 
    declare @count int
   
    begin
      select distinct u_user.UId, LName,WorkNum,UName,UQQ,UType,ToTime,
      Remark=STUFF((select ','+rtrim(#temp.GName) from #temp where t.USerID=#temp.USerID 
      order by #temp.USerID for xml path('')),1,1,'') ,
      probation=STUFF((select ','+rtrim(#temp2.GName) from #temp2 where t2.USerID=#temp2.USerID 
      order by #temp2.USerID for xml path('')),1,1,'')
      from u_user 
      left join
       #temp t on u_user.UId=t.USerID
       left join 
       #temp2 t2 on u_user.UId=t2.USerID
     
       
       where UName like '%'+@UName+'%'
      
    end
    drop table #temp
    drop table #temp2
END
--exec GetUser ''

版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值