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
 Sql query urgently needed

Author  Topic 

arunsaibk
Starting Member

1 Post

Posted - 2009-11-17 : 09:03:05
There are basically 4 levels

Level0 - Head(NoParent)
Level1 - Child of Level0(Parent)
Level2 - Child of Level1(Parent)
Level3 - Child of Level2(Parent)

Id Name ParentId
--------------------
1 A0 Null
2 A1 1
3 A2 2
4 B2 2
5 A3 3
6 B3 3
7 C3 3


if I am passing id as 2,then i need to fetch A2,B2,A3,B3,C3 including other cols of these rows

please try to give me answer in query and not to use temp table

Thanks in Advance

pls reply back to arunsaibk@gmail.com
91-9901436451

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-11-17 : 09:14:29
Are you using SQL 2000 ? 2005 ? 2008 ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

peterkirubakaran
Starting Member

12 Posts

Posted - 2009-11-18 : 01:17:28
hi...
check this out...
GIVE UR TABLE NAME Instead of tblname inside cursor



CREATE OR ALTER PROC prc_arun @givenid int
AS
BEGIN
DECLARE @id int,@name varchar(max),@parentid int
DECLARE cur CURSOR
FOR
SELECT id,name,parentid FROM tblname
OPEN cur
FETCH NEXT FROM cur INTO @id,@name,@parentid
WHILE @@FETCH_STATUS=0
BEGIN
if(@id>@givenid)
BEGIN
PRINT @name+str(@parentid)

END
FETCH NEXT FROM cur INTO @id,@name,@parentid
END
CLOSE CUR
DEALLOCATE CUR
END

EXEC prc_arun 2
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-11-18 : 06:29:06
Or -- if you are on sql server 2005 or greater then you can do this:

/*
Level0 - Head(NoParent)
Level1 - Child of Level0(Parent)
Level2 - Child of Level1(Parent)
Level3 - Child of Level2(Parent)

Id Name ParentId
--------------------
1 A0 Null
2 A1 1
3 A2 2
4 B2 2
5 A3 3
6 B3 3
7 C3 3
*/

DECLARE @startId INT
SET @startID = NULL

DECLARE @data TABLE (
[ID] INT PRIMARY KEY
, [Name] VARCHAR(255)
, [PARENTID] INT
)

INSERT @data ([ID], [Name], [PARENTID])
SELECT 1, 'A0', NULL
UNION SELECT 2, 'A1', 1
UNION SELECT 3, 'A2', 2
UNION SELECT 4, 'B2', 2
UNION SELECT 5, 'A3', 3
UNION SELECT 6, 'B3', 3
UNION SELECT 7, 'C3', 3

-- Show Base Data
SELECT * FROM @data

-- CTE (This works on sql server 2005 and greater)
; WITH tree (
[startID]
, [nodeID]
, [startName]
, [endName]
, [nodePath]
)
AS (
SELECT
[ID]
, [ID]
, [name]
, CAST('' AS VARCHAR(255))
, CAST([name] AS VARCHAR(MAX))
FROM
@data
WHERE
[ID] = @startId OR @startId IS NULL

UNION ALL SELECT
p.[startId]
, l.[Id]
, p.[startName]
, l.[name]
, p.[nodePath] + ' -> ' + CAST(l.[name] AS VARCHAR(MAX))
FROM
tree p
JOIN @data l ON l.[parentID] = p.[nodeID]
)
SELECT
t.[startID]
, t.[startName]
, LEFT(t.[children], LEN(t.[children]) - 1) AS [children]
FROM
(
SELECT
[startId] AS [startId]
, [startName] AS [startName]
, (SELECT l.[endName] + ', ' FROM tree l WHERE l.[endName] <> '' AND l.[startId] = p.[startID] FOR XML PATH ('')) AS [Children]
FROM
tree p
WHERE
[endName] = ''
)
t

and then you don't need to use a cursor.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -