Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Please help in writing a function


Please help in writing a function

Author
Message
Prakash-485822
Prakash-485822
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 124
Dear All

I have table with following data
Slno   ParnetId   Leg   Orign   Dest
1   1    1    V   X
2   1    2    X   Y
3   1    3    Y   Z

I have to write a fucntion which will take PraentId as parameter and return the following output

select fn_GetTripDetails(1)
output:V-X-Y-Z
Dave Ballantyne
Dave Ballantyne
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1818 Visits: 8370
Hi ,

start with this article http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/.

I would advise against using a scalar function and create a inline table function for perfomance reasons



Clear Sky SQL
My Blog
wim.buyens
wim.buyens
SSC Veteran
SSC Veteran (277 reputation)SSC Veteran (277 reputation)SSC Veteran (277 reputation)SSC Veteran (277 reputation)SSC Veteran (277 reputation)SSC Veteran (277 reputation)SSC Veteran (277 reputation)SSC Veteran (277 reputation)

Group: General Forum Members
Points: 277 Visits: 188
if object_id('dbo.tmpTable') is not null
   drop table dbo.tmpTable
go
create table dbo.tmpTable(Slno int, ParentId int, Leg int, Orign char(1), Dest char(1))
insert into dbo.tmpTable
select 1, 1, 1, 'V', 'X'
union all select 2, 1, 2, 'X','Y'
union all select 3, 1, 3, 'Y','Z'
go

--when you can use Leg as the order
declare @concat nvarchar(max)
select @concat=coalesce(@concat,Orign)+'-'+Dest
from dbo.tmpTable where Parentid=1 order by Leg
select @concat

--or order is not known:

if object_id('dbo.fn_GetTripDetails') is not null
   drop function dbo.fn_GetTripDetails
go
create function dbo.fn_GetTripDetails(@ParentID int)
returns nvarchar(max)
as
begin
   declare @concat nvarchar(max)

;with Tab ([Concat],[Last],lvl)
as
(select convert(nvarchar(max),Orign+'-'+Dest),Dest,0 as lvl
   from dbo.tmpTable t1 where ParentId=@ParentID and not exists
   (select * from dbo.tmpTable t2 where t1.Orign=t2.Dest and t1.ParentiD=t2.ParentID)
union all
select [Concat]+'-'+t2.Dest,t2.dest,t.lvl+1
from Tab t
inner join dbo.TmpTable t2
   on t.Last=t2.Orign
   and t2.ParentiD=@ParentID
)
select top 1 @concat=[Concat] from Tab order by lvl desc;
RETURN @concat
end
go

select dbo.fn_GetTripDetails(1)
Prakash-485822
Prakash-485822
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 124
Thanks for your solution.

It is working fine in Sql server 2005.

But problem is we are using Sql server 2000

Can provide me the soultion in Sql server 2000

regards
Prakash
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44993 Visits: 39879
Lookup "Expanding Hierarchies" in SQL Server 2000 Books Online.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search