Click here to Skip to main content
15,886,639 members
Home / Discussions / Database
   

Database

 
AnswerRe: Does PRINT work in sql server function Pin
Eddy Vluggen19-Aug-10 12:32
professionalEddy Vluggen19-Aug-10 12:32 
QuestionGet hirarchy query Pin
SatyaKeerthi1516-Aug-10 21:30
SatyaKeerthi1516-Aug-10 21:30 
AnswerRe: Get hirarchy query Pin
Blue_Boy16-Aug-10 22:19
Blue_Boy16-Aug-10 22:19 
GeneralRe: Get hirarchy query Pin
SatyaKeerthi1516-Aug-10 22:32
SatyaKeerthi1516-Aug-10 22:32 
AnswerRe: Get hirarchy query Pin
J4amieC16-Aug-10 22:35
J4amieC16-Aug-10 22:35 
GeneralRe: Get hirarchy query Pin
SatyaKeerthi1518-Aug-10 0:52
SatyaKeerthi1518-Aug-10 0:52 
GeneralRe: Get hirarchy query Pin
J4amieC18-Aug-10 4:52
J4amieC18-Aug-10 4:52 
AnswerRe: Get hirarchy query Pin
jayantbramhankar19-Aug-10 20:27
jayantbramhankar19-Aug-10 20:27 
To get result, you need to write recursive function

A function to get Parent
SQL
CREATE FUNCTION dbo.FindRoot(@id int)
RETURNS int
AS  
BEGIN 
  DECLARE @Id_parent int
  SELECT @Id_parent= Id_parent
  FROM TableName
  WHERE id = @id
  WHILE @Id_parent <> NULL
    BEGIN
      SELECT @id = @Id_parent
      SELECT @Id_parent = Id_parent
      FROM PrimeInfo
      WHERE id = @id
    END
  RETURN @id
END


and then store procedure to extract your data by providing id value

SQL
CREATE PROCEDURE BuildTree(@id int)
AS 
SET NOCOUNT ON
CREATE TABLE #results(level int, id int, id_parent int)
DECLARE @id_parent int
DECLARE @level int
SELECT @level = 1
DECLARE @root int
SELECT @root = dbo.FindRoots(@id)
CREATE TABLE #stack (id int, level smallint)
INSERT INTO #stack VALUES (@root, @level)
WHILE @level > 0
BEGIN
IF EXISTS (SELECT * FROM #stack WHERE level = @level)
  BEGIN
    SELECT @id = s.id, @id_parent= IsNull(t.id_parent, 0)
    FROM #stack s INNER JOIN TableName t
    ON t.id = s.id
    WHERE level = @level
    INSERT INTO #results VALUES (@level, @id, @id_parent)
    DELETE FROM #stack
    WHERE level = @level
    AND id = @id
    INSERT #stack
    SELECT id, @level + 1
    FROM TableName
    WHERE id_parent = @id
    IF @@ROWCOUNT > 0 
      BEGIN
        SELECT @level = @level + 1
    END
 END--IF EXISTS
 ELSE
 BEGIN
   SELECT @level = @level - 1
 END
END -- WHILE
SELECT id, id_parent, level FROM #results

AnswerRe: Get hirarchy query Pin
ScottM118-Aug-10 1:58
ScottM118-Aug-10 1:58 
GeneralRe: Get hirarchy query Pin
SatyaKeerthi1518-Aug-10 2:03
SatyaKeerthi1518-Aug-10 2:03 
GeneralRe: Get hirarchy query Pin
ScottM118-Aug-10 2:09
ScottM118-Aug-10 2:09 
GeneralRe: Get hirarchy query Pin
ScottM118-Aug-10 2:10
ScottM118-Aug-10 2:10 
GeneralRe: Get hirarchy query Pin
SatyaKeerthi1518-Aug-10 18:52
SatyaKeerthi1518-Aug-10 18:52 
GeneralRe: Get hirarchy query Pin
ScottM118-Aug-10 20:56
ScottM118-Aug-10 20:56 
GeneralRe: Get hirarchy query Pin
SatyaKeerthi1518-Aug-10 21:11
SatyaKeerthi1518-Aug-10 21:11 
GeneralRe: Get hirarchy query Pin
ScottM118-Aug-10 21:17
ScottM118-Aug-10 21:17 
GeneralRe: Get hirarchy query Pin
SatyaKeerthi1518-Aug-10 19:01
SatyaKeerthi1518-Aug-10 19:01 
QuestionGet hirarchy query Pin
SatyaKeerthi1518-Aug-10 19:02
SatyaKeerthi1518-Aug-10 19:02 
AnswerRe: Get hirarchy query Pin
ps_prakash0219-Aug-10 21:31
ps_prakash0219-Aug-10 21:31 
QuestionHow to query exclusion? SQL Help needed Pin
Kyudos16-Aug-10 17:36
Kyudos16-Aug-10 17:36 
AnswerRe: How to query exclusion? SQL Help needed Pin
Kyudos16-Aug-10 17:44
Kyudos16-Aug-10 17:44 
GeneralRe: How to query exclusion? SQL Help needed Pin
Mycroft Holmes16-Aug-10 18:22
professionalMycroft Holmes16-Aug-10 18:22 
GeneralRe: How to query exclusion? SQL Help needed Pin
Kyudos17-Aug-10 10:46
Kyudos17-Aug-10 10:46 
GeneralRe: How to query exclusion? SQL Help needed Pin
Kyudos17-Aug-10 13:18
Kyudos17-Aug-10 13:18 
GeneralRe: How to query exclusion? SQL Help needed Pin
Mycroft Holmes17-Aug-10 13:31
professionalMycroft Holmes17-Aug-10 13:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.