CREATE PROCEDURE dbo.ShowHierarchy
(
@Root int,
@idList varchar(100) OUTPUT
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @IndID int, @IndName varchar(30)
SET @IndName = (SELECT ID FROM dbo.forum_industries WHERE ID = @Root)
PRINT REPLICATE('-', @@NESTLEVEL * 4) + @IndName
SET @idList = @idList + ',' + CAST(@IndName AS VARCHAR(100))
SET @IndID = (SELECT MIN(ID) FROM dbo.forum_industries WHERE ParentID = @Root)
WHILE @IndID IS NOT NULL
BEGIN
EXEC dbo.ShowHierarchy @IndID, @idList--, @finalIdList
SET @IndID = (SELECT MIN(ID) FROM dbo.forum_industries WHERE ParentID = @Root AND ID > @IndID)
END
END
BEGIN
DECLARE @IndID int
DECLARE @idList varchar
-- Find out whether we check the inserted or deleted table, depending on whether a record is getting deleted or inserted or updated.
if (select count(*) from inserted) <> 0 and (select count(*) from deleted) = 0 --insert
begin
SELECT @IndID = forum_industriesID FROM inserted
end
if (select count(*) from inserted) <> 0 and (select count(*) from deleted) <> 0 --update
begin
SELECT @IndID = forum_industriesID FROM inserted
end
if (select count(*) from inserted) = 0 and (select count(*) from deleted) <> 0 --delete
begin
SELECT @IndID = forum_industriesID FROM deleted
end
SET @idList = 0
EXECUTE ShowHierarchy @IndID, @idList
-- Query code to store the IDList or select based on the list
END
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE