Menu đa cấp bằng SQL SEVER

Theo: nguyenhaidang.name.vn | 10/05/2017 - 08:59

 

CREATE TABLE [dbo].[NewsCategories](

[CategoryID] [int] IDENTITY(1,1) NOT NULL,

[CategoryName] [nvarchar](100) NULL,

[ZOrder] [int] NULL,

[ParentID] [int] NULL,

[IsChecked] [bit] NULL,

[LangID] [int] NULL,

[IsChild] [varchar](50) NULL,

[Position] [int] NOT NULL,

 CONSTRAINT [PK_EQ_NewsCategories] PRIMARY KEY CLUSTERED 

(

[CategoryID] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

) ON [PRIMARY]

 

GO

 

 

 ALTER PROC SP_GetAllCategory

 AS

 DECLARE @Categories TABLE (

 ID int PRIMARY KEY ,Title nvarchar(256) ,ParentID int )  

 INSERT INTO @Categories select CategoryID,CategoryName,ParentID from NewsCategories;

 WITH  CategoryTree         AS (SELECT  r.ID, r.Title, 0 Level, r.ParentID,                     

 CAST(r.Title AS nVARCHAR(1000)) AS ''Path''            FROM    @Categories r            

  WHERE   r.ParentID = 0             UNION ALL             

  SELECT  c.ID, c.Title, p.Level + 1 AS Level, c.ParentID,                     

  CAST((p.path + ''/'' + c.Title) AS nVARCHAR(1000)) AS ''Path''           

   FROM    @Categories c                     INNER JOIN CategoryTree p                       

   ON p.ID = c.ParentID            )   

   SELECT  ID AS CategoryID, REPLICATE('' --'', Level) + Title AS CategoryName, [Path]   FROM    CategoryTree   ORDER BY [Path]

Back Head Print
Tin khác

Search GridView with Paging on TextBox KeyPress using jQuery in ASP.Net    (28/07/2010)

Bootstrap AutoComplete TextBox example using jQuery TypeAhead plugin in ASP.Net with C# and VB.Net    (28/07/2010)

Disable Button and Submit button after one click using JavaScript and jQuery    (29/07/2010)

Split and convert Comma Separated (Delimited) String to Table in SQL Server    (01/09/2010)

Select Column values as Comma Separated (Delimited) string in SQL Server using COALESCE    (01/09/2010)