stylish
Replies to this thread:

More by stylish
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 SQL help
[VIEWED 7008 TIMES]
SAVE! for ease of future access.
Posted on 02-24-08 8:42 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

How do I transform a table into a comma separated values?? If I have an "employeeid"  as 1 that has taken different orders, I want to display in the result set like this

Employeeid           orderid

1                            1024,1025,1028,1029

I know only to display as column:

CREATE function dbo.fn_inonerow(@P_empid int)
returns @v_table table(employeeid int,orderid int)
As

Begin

Insert into @v_table(employeeid,orderid)

  Select               a.employeeid,a.orderid 
  From                orders a
  inner join           employees b
  on                       b.employeeid=a.employeeid
  Where                b.employeeid=@p_empid


Return
End

GO

 


 
Posted on 02-24-08 10:25 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Stylish, i do not know you are working on ORACLE or SQL Server. If you are working on SQL, there is a tool called DTS  in 2000 and SSIS in 2005. You are use these tools to generate the CSV file as  that u want to.  Please let me know further and i might be able to assist you.

 

Thanks


 
Posted on 02-24-08 12:22 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

create function [dbo].fn_inonerow(@p_empid int)
returns nvarchar(4000)
as
begin
 declare @orderids nvarchar(4000)
 select @orderids = coalesce(@orderids + ', ', '') +  rtrim(orderid)
 from                orders a
 inner join           employees b
 on                       b.employeeid=a.employeeid
 where                b.employeeid=@p_empid

 return @orderids
end
go

--Then call the function like this:
select [dbo].fn_inonerow(1) as orderids


 
Posted on 02-24-08 1:10 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Yaar echoes bhayena ta. Its still displaying as column. Yesari display bhari rakhya cha

Employeeid                         orderid

1                                         10248

1                                          10234

1                                          10343

1                                          10345

 

I want to display in a single row

1                              10248,10234,10343,10345

 

But You gave me the idea of using string function.

Thanks buddy I will try

 


 
Posted on 02-24-08 1:14 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Fewata I am using SQL Server 2000
 
Posted on 02-24-08 1:26 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Did you drop your old function? Drop your function and run my code. It should work and produce the result you're looking for. Your output suggests that it's still running the old code. My function is scalar, and should in no way return multiple rows.

To drop your old function run this code: 

drop function [dbo].fn_inonerow


 
Posted on 02-24-08 1:47 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Echoes, still around? hmmm... nice to see u again. Where r u these days?
 
Posted on 02-24-08 2:06 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

stylish, PM me with your number.

 

Thanks

 


 
Posted on 02-24-08 2:09 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Bravo, superb echoes thank you... U r gr8
 
Posted on 02-24-08 2:39 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Credit goes to Echoes. I modified to code so that now its returning value to the table variable and displaying two columns employee id and the related orderid  separated by commas.

Thanks once again echoes.

 

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

Alter function [dbo].fn_inonerow(@p_empid int)
Returns @v_table table(employeeid int,orderid nvarchar(4000))
As

Begin

Declare @orderids nvarchar(4000)
Select  @orderids=coalesce(@orderids + ',','') +  rtrim(orderid)
From                orders a
inner join          employees b
on                  b.employeeid=a.employeeid
where               b.employeeid=@p_empid


Insert into @v_table(employeeid,orderid)
Select Distinct    a.employeeid, @orderids
From                orders a
inner join          employees b
on                  b.employeeid=a.employeeid
Where               b.employeeid=@p_empid


Return
End

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

--Select * from [dbo].fn_inonerow(3)

 


 
Posted on 02-24-08 6:06 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

stylish - no problem. glad it worked.

>>Echoes, still around? hmmm... nice to see u again. Where r u these days?

Rusty, yes, evidently ;-). Good to hear from you. Same place. Have you moved?

 


 
Posted on 02-24-08 6:13 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

OMG echoes uncle you're ALIVE??? 


 
Posted on 02-24-08 7:02 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Yes, niece. Hope it was not unexpected? ;-) How's school?

Who else is still here from the old days?


 
Posted on 02-24-08 7:03 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

OMG nailu le echoes lai uncle re?

Echoes guruji how's everything? No time for trivia these days?

 
Posted on 02-24-08 7:37 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

daZ DAI how's ashfield?? ;)


 
Posted on 02-25-08 6:00 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

nailu a'field is great. Neps everywhere now.

 
Posted on 02-25-08 8:05 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

really?? i am coming there REAL REAL soon.....;)
 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 7 days
Recommended Popular Threads Controvertial Threads
I hope all the fake Nepali refugee get deported
Those who are in TPS, what’s your backup plan?
Travel Document for TPS (approved)
MAGA and all how do you feel about Trumps cabinet pick?
MAGA मार्का कुरा पढेर दिमाग नखपाउनुस !
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters