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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SlNo Generation

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 REC1
2 2 REC2
2.1 4 REC3
2.2 5 REC4
2.2.1 10 REC5

Is 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 TName
1 1 REC1
2 2 REC2
4.2 4 REC3
5.2 5 REC4
10.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 REC1
2 2 REC2
2.1 4 REC3
2.2 5 REC4
2.2.1 10 REC5

Is 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 table
INSERT @test ([ID], [tName], [parentId])
SELECT 1, 'REC1', NULL
UNION SELECT 2, 'REC2', NULL
UNION SELECT 4, 'REC3', 2
UNION SELECT 5, 'REC4', 2
UNION SELECT 10, 'REC5', 5

-- Raw Dump of Table
SELECT * 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 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

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)
Go to Top of Page

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 thing
slNo for Id 4,5 Which is under 1
and after 2
then id 10

Generate row on that order
Go to Top of Page

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 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -