1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER FUNCTION [dbo].[fct_StructureEnfant]
()
RETURNS @t table
(
[id] int,
[libelle] varchar(100) COLLATE French_CI_AS
)
AS
BEGIN
DECLARE @libelle varchar(100)
DECLARE @id int
DECLARE move_Line_Item_Cursor CURSOR
FOR
SELECT id,indent + structure
from StructureHierarchy as s
--------------------------------------------------------------------
WITH StructureHierarchy (id, idStructure, structure, indent)
AS
(
-- Anchor member definition
SELECT e.id, e.idStructure, e.structure, '' AS indent
FROM Structure AS e
WHERE e.idStructure IS NULL
UNION ALL
-- Recursive member definition
SELECT e.id, e.idStructure, e.structure, indent + ' '
FROM Structure AS e
INNER JOIN DirectReports AS d
ON d.id = e.idStructure
)
--------------------------------------------------------------------
OPEN move_Line_Item_Cursor
FETCH NEXT FROM move_Line_Item_Cursor INTO @id, @libelle
--Fetch next record
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO @t(id,libelle) VALUES (@id,@libelle)
-- now move the cursor
FETCH NEXT FROM move_Line_Item_Cursor INTO @id, @libelle
END
CLOSE move_Line_Item_Cursor --Close cursor
DEALLOCATE move_Line_Item_Cursor --Deallocate cursor
return
END |
Partager