SQL subquery and stuff functions (convert multiple roles and multiple departments into strings), sqlstuff

Source: Internet
Author: User

SQL subquery and stuff functions (convert multiple roles and multiple departments into strings), sqlstuff

USE [erp2015] GO/****** Object: StoredProcedure [dbo]. [GetUser] Script Date: 03/14/2015 13:27:04 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO -- ================ =================================-- Author: wangyanling -- Create date: 205-03-12 -- Description: obtain customer information -- ================================================== ========= alter procedure [dbo]. [GetUser] -- Add the parameters for the stored procedure here @ UName varchar (200) ASBEGIN -- 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 uleft join User_Group ug on u. UId = ug. uIdleft joinDb_Group db on ug. gId = db. gIdinsert into # temp select u. UId, g. GName from u_user uleft join ug_User_Group ug on u. UId = ug. UIdleft 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 # temp2END -- exec getuser''

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.