Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
rakhigulati
Starting Member
1 Post |
Posted - 2006-07-14 : 04:59:41
|
| Hi ,I have the following structure:Item table----------ItemIdItemNameItem Transaction Table----------------------TransactionIdGiverItemIdSenderItemIdthe data is somewhat like this:Item table__________1 abc 2 xyz 3 pnr4 rew5 dds6 djs7 dsfItem Transaction table---------------------1 1 2 2 2 43 4 34 3 7Now i have a reauirement to build a stored proc in which all the transactions starting from one transaction like, if i want to know the chain for item no 2 it shall give the following result:2 44 33 7if i want to know the chain for item no 3 then it shall give following3 7if i want to know the chain for item no 1 then it shall give following1 22 44 33 7Please help.. Its urgent..... Thanks |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-07-14 : 06:05:56
|
[code]declare @Item table( ItemId int, ItemName varchar(10))declare @ItemTrans table( TransactionId int, GiverItemId int, SenderItemId int)insert into @Itemselect 1, 'abc' union allselect 2, 'xyz' union allselect 3, 'pnr' union allselect 4, 'rew' union allselect 5, 'dds' union allselect 6, 'djs' union allselect 7, 'dsf'insert into @ItemTransselect 1, 1, 2 union allselect 2, 2, 4 union allselect 3, 4, 3 union allselect 4, 3, 7declare @itemid intdeclare @result table( seq int identity(1,1), GiverItemId int, SenderItemId int)select @itemid = 1while (@itemid is not null)begin print @itemid insert into @result select GiverItemId, SenderItemId from @ItemTrans where GiverItemId = @itemid select @itemid = SenderItemId from @ItemTrans where GiverItemId = @itemid if @@rowcount = 0 select @itemid = null print @itemidendselect GiverItemId, SenderItemId from @resultorder by seq[/code] KH |
 |
|
|
|
|
|
|
|