Output:
CREATE
TABLE [dbo].[tblSkillSet]([SkillId] [int]
IDENTITY(1,1) NOT NULL,[SkillName] [varchar]
(250) NOT NULL,[ParentSkillId] [int]
NOT NULL CONSTRAINT [DF_tblSkillSet_ParentSkillId] DEFAULT (0),[Depth] [int] NULL,
[Lineage] [varchar]
(100) NULL,CONSTRAINT [PK_tblSkillSet] PRIMARY KEY CLUSTERED(
[SkillId]
ASC)
WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY])
ON [PRIMARY]--------------------------------------------------------------------------------------------------------------------------
SET
ANSI_NULLS ONSET
QUOTED_IDENTIFIER ONgo
CREATE
PROCEDURE [dbo].[SelALLParentsAndChilds]AS
SET
NOCOUNT ONCreate
table #tblSkills(SkillId INT, SkillName VARCHAR(8000))CREATE
TABLE #stack (Parent_Id INT, SkillName VARCHAR(8000) , [level] INT)CREATE
TABLE #stack1 (SkillId INT ,Parent_Id INT, SkillName VARCHAR(8000) , [level] INT)DECLARE
@maxId INTDECLARE
@strskill VARCHAR(8000)DECLARE
@colskill VARCHAR(8000)DECLARE
@parentId INTDECLARE
@level INT, @line char(20)Declare
@SKillName VARCHAR(50)SET
@strskill=''DECLARE
@PSID INTDECLARE
@OuterPSID INTDECLARE
@PSName VARCHAR(100)DECLARE
cur_skill1 cursor for SELECT SkillId from tblSkillSet WHERE ParentSkillId = 0OPEN
cur_skill1FETCH
next from cur_skill1 into @PSIDSET
@OuterPSID = @PSIDWHILE
@@FETCH_STATUS=0BEGIN
DELETE FROM #stack1SELECT @SkillName =SkillName from tblSkillSet WHERE ParentSkillId =@PSIDINSERT INTO #stack VALUES (@PSID, @SkillName, 1)SELECT @level = 1WHILE @level > 0BEGINIF EXISTS (SELECT * FROM #stack WHERE [level] = @level)BEGINSELECT @PSID = Parent_Id FROM #stack WHERE [level] = @levelSELECT @line = space(@level - 1) + @PSIDPRINT @lineDELETE FROM #stack WHERE [level] = @level AND parent_id = @PSID INSERT #stackSELECT SkillId , SkillName , @level + 1 FROM tblSkillSet WHERE ParentSkillId = @PSIDDECLARE @strinnerSkillName VARCHAR(100)SET @strinnerSkillName = ''IF exists (SELECT * FROM #stack1 WHERE SkillId = @PSID)BEGINSET @strinnerSkillName = (SELECT SkillName from #stack1 WHERE SkillId=@PSID) + '>>'INSERT #stack1 SELECT SkillId, ParentSkillId , @strinnerSkillName + SkillName as SkillName, @level + 1FROM tblSkillSet WHERE ParentSkillId = @PSIDENDELSEBEGINSET @strinnerSkillName = (SELECT max(SkillName) from tblSkillSet WHERE SkillId=@PSID) + '>>'INSERT #stack1 SELECT SkillId, ParentSkillId , @strinnerSkillName + SkillName as SkillName , @level + 1FROM tblSkillSet WHERE ParentSkillId = @PSIDENDIF @@ROWCOUNT > 0BEGINSELECT @level = @level + 1END IF exists ( SELECT 'sometext' from #stack WHERE parent_id=@PSID)SELECT @level=0ENDELSESELECT @level = @level - 1END
-- WHILESET @PSName = (SELECT skillName from tblSkillSet WHERE SkillId=@OuterPSID)INSERT INTO #tblSkills VALUES(@OuterPSID,@PSName)INSERT INTO #tblSkills SELECT skillId,skillName from #stack1FETCH next FROM cur_skill1 INTO @PSIDSET @OuterPSID = @PSIDEND
--for cursorCLOSE
cur_skill1DEALLOCATE
cur_skill1SELECT
* FROM #tblSkills--------------------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment