SQL help - Sajha Mobile
SAJHA MOBILE
SQL help
Posts 17 · Viewed 5884 · Go to Last Post
stylish
· Snapshot
Like · Likedby · 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

 

fewatal
· Snapshot
Like · Liked by · 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

Echoes
· Snapshot
Like · Liked by · 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

stylish
· Snapshot
Like · Liked by · 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

 

stylish
· Snapshot
Like · Liked by · 0
Fewata I am using SQL Server 2000
Echoes
· Snapshot
Like · Liked by · 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

Rusty
· Snapshot
Like · Liked by · 0
Echoes, still around? hmmm... nice to see u again. Where r u these days?
fewatal
· Snapshot
Like · Liked by · 0

stylish, PM me with your number.

 

Thanks

 

stylish
· Snapshot
Like · Liked by · 0
Bravo, superb echoes thank you... U r gr8
stylish
· Snapshot
Like · Liked by · 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)

 

Echoes
· Snapshot
Like · Liked by · 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?

 

nails
· Snapshot
Like · Liked by · 0

OMG echoes uncle you're ALIVE??? 

Echoes
· Snapshot
Like · Liked by · 0

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

Who else is still here from the old days?

daZ
· Snapshot
Like · Liked by · 0
OMG nailu le echoes lai uncle re?

Echoes guruji how's everything? No time for trivia these days?
nails
· Snapshot
Like · Liked by · 0

daZ DAI how's ashfield?? ;)

daZ
· Snapshot
Like · Liked by · 0
nailu a'field is great. Neps everywhere now.
nails
· Snapshot
Like · Liked by · 0
really?? i am coming there REAL REAL soon.....;)
Please log in to reply to this post

You can also log in using your Facebook
View in Desktop
What people are reading
You might like these other discussions...
· Posts 4 · Viewed 399
· Posts 69 · Viewed 13438 · Likes 15
· Posts 9 · Viewed 2021
· Posts 39 · Viewed 9337 · Likes 2
· Posts 1 · Viewed 111
· Posts 1 · Viewed 115
· Posts 5 · Viewed 927
· Posts 1 · Viewed 187
· Posts 1 · Viewed 165
· Posts 5 · Viewed 1270 · Likes 1



Your Banner Here
Travel Partners
Travel House Nepal