SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How 2 get comma seperated values frm multiple rows
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

nayanancha
Starting Member

27 Posts

Posted - 02/24/2012 :  09:11:37  Show Profile  Reply with Quote
I am using the below dynamic sql in my sp. I want the rolename for the user to be comma seperated value.

set @sql =
			'
			 select uc.UserName,
					r.RoleName
			from ' + @PortalDb30 + '.dbo.aspnet_Membership m
					inner join ' + @Portal + '.dbo.UserConfig uc
						on uc.userid = m.userid
					inner join ' + @Portal + '.dbo.Company c
						on c.CompanyCode = uc.CompanyCode
					inner join ' + @Portal + '.dbo.aspnet_UsersInRoles ur
						on ur.userid = uc.userid
					inner join ' + @Portal + '.dbo.aspnet_Roles r
						on r.RoleId = ur.RoleId
			where c.CompanyCode = ''' + @CompanyCode + ''''

		If @UserName is NOT NULL
			set @sql = @sql + ' and uc.UserName = ''' + @UserName + ''' order by uc.UserName '
		Else If @RoleName IS NOT NULL
			set @sql = @sql + ' and r.RoleName = ''' + @RoleName + ''' order by uc.UserName'
		Else
			set @sql = @sql + ' order by uc.UserName'		

		
		exec sp_executesql @sql


I am getting the output as below:

Username Rolename
user1 Admin
user1 Customer

But I want the output as

user1 Admin,Customer

How can I acheive this?

Jayam.cnu
Starting Member

India
40 Posts

Posted - 02/24/2012 :  10:03:32  Show Profile  Reply with Quote
can you follow this link.......... http://www.simple-talk.com/sql/database-administration/creating-csv-files-using-bcp-and-stored-procedures/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 02/24/2012 :  10:04:12  Show Profile  Reply with Quote
see scenario 3

http://visakhm.blogspot.com/2010/01/multipurpose-apply-operator.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

nayanancha
Starting Member

27 Posts

Posted - 02/24/2012 :  10:27:41  Show Profile  Reply with Quote
I couldn't follow that scenario. Could you explain it with my code?

Thanks,
Go to Top of Page

malachi151
Posting Yak Master

142 Posts

Posted - 02/27/2012 :  17:20:39  Show Profile  Visit malachi151's Homepage  Reply with Quote
My suggestion is to first replace the exec sp_executesql @sql with PRINT @sql, and then use the value of @sql to test with and first get the query working like you want it, then once its working correctly, figure out how you need to modify the dynamic SQL to achieve the same result.

In addition, if you can't figure it out, at least paste in the results of the PRINT statement here so it will be easier to figure out what the query is doing.
Go to Top of Page

Bustaz Kool
Flowing Fount of Yak Knowledge

USA
1430 Posts

Posted - 02/27/2012 :  19:25:20  Show Profile  Reply with Quote
Would this approach work for you?
declare @tbl table (
	pkey	int identity(1, 1),
	fruit	varchar(20) not null
	)

insert into @tbl(fruit)
select 'Apples' union all
select 'Bananas' union all
select 'Cherries'

select *
from @tbl

declare @csv_list varchar(max) = ''

update @tbl
set	@csv_list += fruit + ','

set @csv_list = LEFT(@csv_list, len(@csv_list) - 1)  -- Remove trailing comma

select @csv_list csv
It's kind of a wierd use of an UPDATE but I think it gets you what you want.

=================================================
Men shout to avoid listening to one another. -Miguel de Unamuno
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000