| Author |
Topic |
|
aoriju
Posting Yak Master
156 Posts |
Posted - 2009-10-20 : 08:20:13
|
| Declare @tbl_Test as Table(Id INt,TName VARCHAR(10) , ParentId INT)INSERT INTO @tbl_Test(Id ,TName,ParentId) VALUES(1,'REC1',NULL)INSERT INTO @tbl_Test(Id ,TName,ParentId) VALUES(2,'REC2',NULL)INSERT INTO @tbl_Test(Id ,TName,ParentId) VALUES(4,'REC3',2)INSERT INTO @tbl_Test(Id ,TName,ParentId) VALUES(5,'REC4',2)INSERT INTO @tbl_Test(Id ,TName,ParentId) VALUES(10,'REC5',5)For this Records I want to get SLNo Like this wtr ParentId values (Gnerate one after another when ParentId isNull)SlNo Id TName 1 1 REC12 2 REC22.1 4 REC32.2 5 REC42.2.1 10 REC5Is it posible with Sql server Programming(2,2.1,2.1.2 like that ..Then How |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-10-20 : 09:06:07
|
A few questions:1) Are you using SQL server 2005 or later?2) I'm not sure how you are working out your SlNo column. Did you maybe mean results like this:SlNo Id TName1 1 REC12 2 REC24.2 4 REC35.2 5 REC410.5.2 10 REC5 If so then the following sql will recursively navigate your tree. However, you need sql server 2005 or newer./*Declare @tbl_Test as Table(Id INt,TName VARCHAR(10) , ParentId INT)INSERT INTO @tbl_Test(Id ,TName,ParentId) VALUES(1,'REC1',NULL)INSERT INTO @tbl_Test(Id ,TName,ParentId) VALUES(2,'REC2',NULL)INSERT INTO @tbl_Test(Id ,TName,ParentId) VALUES(4,'REC3',2)INSERT INTO @tbl_Test(Id ,TName,ParentId) VALUES(5,'REC4',2)INSERT INTO @tbl_Test(Id ,TName,ParentId) VALUES(10,'REC5',5)For this Records I want to get SLNo Like this wtr ParentId values (Gnerate one after another when ParentId isNull)SlNo Id TName 1 1 REC12 2 REC22.1 4 REC32.2 5 REC42.2.1 10 REC5Is it posible with Sql server Programming(2,2.1,2.1.2 like that ..Then How*/DECLARE @test TABLE ( [ID] INT PRIMARY KEY , [TName] VARCHAR(10) , [parentID] INT )-- Populate test tableINSERT @test ([ID], [tName], [parentId]) SELECT 1, 'REC1', NULLUNION SELECT 2, 'REC2', NULLUNION SELECT 4, 'REC3', 2UNION SELECT 5, 'REC4', 2UNION SELECT 10, 'REC5', 5-- Raw Dump of TableSELECT * FROM @test-- Recursive Tree;WITH cte ( [SlNo] , [Id] , [TName] )AS ( -- Anchor Definition SELECT CAST([Id] AS VARCHAR(MAX)) , [Id] , [tName] FROM @test WHERE parentId IS NULL -- Recursive Definition UNION ALL SELECT CAST(t.[Id] AS VARCHAR(MAX)) + '.' + p.[SlNo] , t.[Id] , t.[tName] FROM @test t JOIN cte p ON p.[Id] = t.[parentId] )SELECT *FROM cte Best of luck,Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
aoriju
Posting Yak Master
156 Posts |
Posted - 2009-10-20 : 09:09:29
|
| Can generate Sl No as 1,2 when ParentId IS NULL otherwise as No SlNo(Null) |
 |
|
|
aoriju
Posting Yak Master
156 Posts |
Posted - 2009-10-20 : 09:12:08
|
| Can generate Sl No as 1,2 when ParentId IS NULL otherwise as No SlNo(Null)One more thingslNo for Id 4,5 Which is under 1and after 2then id 10Generate row on that order |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-10-20 : 09:16:04
|
I'm sorry - I don't understand what you are telling me.quote: Can generate Sl No as 1,2 when ParentId IS NULL otherwise as No SlNo(Null)
Can you explain how you would (manually) work out the SlNo for REC5?Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
|
|
|