USE [Kbase]
GO
/****** Object:  View [dbo].[v_kb_cat]    Script Date: 03/14/2021 11:17:39 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[v_kb_cat]'))
DROP VIEW [dbo].[v_kb_cat]
GO
/****** Object:  StoredProcedure [dbo].[sp_kb_insertcategory]    Script Date: 03/14/2021 11:17:39 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_kb_insertcategory]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_kb_insertcategory]
GO
/****** Object:  StoredProcedure [dbo].[sp_kb_movecategory]    Script Date: 03/14/2021 11:17:39 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_kb_movecategory]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_kb_movecategory]
GO
/****** Object:  Table [dbo].[kb_cat]    Script Date: 03/14/2021 11:17:37 ******/
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_portal_kbcategory_parentid]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[kb_cat] DROP CONSTRAINT [DF_portal_kbcategory_parentid]
END
GO
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_portal_kbcategory_orderid]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[kb_cat] DROP CONSTRAINT [DF_portal_kbcategory_orderid]
END
GO
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_portal_kbcategory_depth]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[kb_cat] DROP CONSTRAINT [DF_portal_kbcategory_depth]
END
GO
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_hr2_kbcsscategory_path]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[kb_cat] DROP CONSTRAINT [DF_hr2_kbcsscategory_path]
END
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[kb_cat]') AND type in (N'U'))
DROP TABLE [dbo].[kb_cat]
GO
/****** Object:  Table [dbo].[kb_news]    Script Date: 03/14/2021 11:17:37 ******/
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_kb_news_orderid]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[kb_news] DROP CONSTRAINT [DF_kb_news_orderid]
END
GO
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_kb_news_click]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[kb_news] DROP CONSTRAINT [DF_kb_news_click]
END
GO
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_kb_news_adddate]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[kb_news] DROP CONSTRAINT [DF_kb_news_adddate]
END
GO
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_kb_news_ispass]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[kb_news] DROP CONSTRAINT [DF_kb_news_ispass]
END
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[kb_news]') AND type in (N'U'))
DROP TABLE [dbo].[kb_news]
GO
/****** Object:  Table [dbo].[users_dept]    Script Date: 03/14/2021 11:17:37 ******/
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_portal_category_parentid]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[users_dept] DROP CONSTRAINT [DF_portal_category_parentid]
END
GO
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_portal_category_orderid]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[users_dept] DROP CONSTRAINT [DF_portal_category_orderid]
END
GO
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_portal_category_depth]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[users_dept] DROP CONSTRAINT [DF_portal_category_depth]
END
GO
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_hr_csscategory_path]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[users_dept] DROP CONSTRAINT [DF_hr_csscategory_path]
END
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[users_dept]') AND type in (N'U'))
DROP TABLE [dbo].[users_dept]
GO
/****** Object:  Table [dbo].[users_getpwd]    Script Date: 03/14/2021 11:17:37 ******/
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_users_getpwd_createdate]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[users_getpwd] DROP CONSTRAINT [DF_users_getpwd_createdate]
END
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[users_getpwd]') AND type in (N'U'))
DROP TABLE [dbo].[users_getpwd]
GO
/****** Object:  Table [dbo].[users_releation]    Script Date: 03/14/2021 11:17:37 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[users_releation]') AND type in (N'U'))
DROP TABLE [dbo].[users_releation]
GO
/****** Object:  Table [dbo].[users_roles]    Script Date: 03/14/2021 11:17:37 ******/
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_users_roles_issystem]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[users_roles] DROP CONSTRAINT [DF_users_roles_issystem]
END
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[users_roles]') AND type in (N'U'))
DROP TABLE [dbo].[users_roles]
GO
/****** Object:  Table [dbo].[users_users]    Script Date: 03/14/2021 11:17:37 ******/
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_portal_users_createdate]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[users_users] DROP CONSTRAINT [DF_portal_users_createdate]
END
GO
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_portal_users_lastlogin]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[users_users] DROP CONSTRAINT [DF_portal_users_lastlogin]
END
GO
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_users_users_avtor]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[users_users] DROP CONSTRAINT [DF_users_users_avtor]
END
GO
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_users_users_logintime]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[users_users] DROP CONSTRAINT [DF_users_users_logintime]
END
GO
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_users_users_logintime_1]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[users_users] DROP CONSTRAINT [DF_users_users_logintime_1]
END
GO
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_users_users_ismgr]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[users_users] DROP CONSTRAINT [DF_users_users_ismgr]
END
GO
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_users_users_issec]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[users_users] DROP CONSTRAINT [DF_users_users_issec]
END
GO
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_users_users_isstaff]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[users_users] DROP CONSTRAINT [DF_users_users_isstaff]
END
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[users_users]') AND type in (N'U'))
DROP TABLE [dbo].[users_users]
GO
/****** Object:  Table [dbo].[users_usersinroles]    Script Date: 03/14/2021 11:17:37 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[users_usersinroles]') AND type in (N'U'))
DROP TABLE [dbo].[users_usersinroles]
GO
/****** Object:  Table [dbo].[users_usersinroles]    Script Date: 03/14/2021 11:17:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[users_usersinroles]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[users_usersinroles](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[uid] [int] NULL,
	[rolename] [nvarchar](50) NULL,
 CONSTRAINT [PK_portal_usersinroles] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET IDENTITY_INSERT [dbo].[users_usersinroles] ON
INSERT [dbo].[users_usersinroles] ([id], [uid], [rolename]) VALUES (1, 1, N'administrators')
SET IDENTITY_INSERT [dbo].[users_usersinroles] OFF
/****** Object:  Table [dbo].[users_users]    Script Date: 03/14/2021 11:17:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[users_users]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[users_users](
	[uid] [int] IDENTITY(1,1) NOT NULL,
	[username] [nvarchar](256) NOT NULL,
	[adpath] [nvarchar](1024) NULL,
	[firstname] [nvarchar](50) NULL,
	[lastname] [nvarchar](50) NULL,
	[city] [nvarchar](256) NULL,
	[Office] [nvarchar](256) NULL,
	[Description] [nvarchar](1024) NULL,
	[Tel] [nvarchar](128) NULL,
	[fax] [nvarchar](128) NULL,
	[email] [nvarchar](256) NULL,
	[MailType] [nvarchar](128) NULL,
	[Title] [nvarchar](128) NULL,
	[Department] [nvarchar](128) NULL,
	[Company] [nvarchar](128) NULL,
	[Password] [nvarchar](128) NULL,
	[displayname] [nvarchar](128) NULL,
	[StaffCode] [nvarchar](128) NULL,
	[IsAccountActive] [int] NULL,
	[createdate] [datetime] NULL CONSTRAINT [DF_portal_users_createdate]  DEFAULT (getdate()),
	[lastlogin] [datetime] NULL CONSTRAINT [DF_portal_users_lastlogin]  DEFAULT (getdate()),
	[sex] [nvarchar](50) NULL,
	[avtor] [nvarchar](300) NULL CONSTRAINT [DF_users_users_avtor]  DEFAULT ('avtor.jpg'),
	[logintimes] [int] NULL CONSTRAINT [DF_users_users_logintime]  DEFAULT ((0)),
	[ip] [nvarchar](50) NULL,
	[logintime] [int] NULL CONSTRAINT [DF_users_users_logintime_1]  DEFAULT ((0)),
	[intro] [nvarchar](500) NULL,
	[ismgr] [int] NULL CONSTRAINT [DF_users_users_ismgr]  DEFAULT ((0)),
	[issec] [int] NULL CONSTRAINT [DF_users_users_issec]  DEFAULT ((0)),
	[isstaff] [int] NULL CONSTRAINT [DF_users_users_isstaff]  DEFAULT ((1)),
	[aid] [int] NULL,
	[mobile] [nvarchar](50) NULL,
	[pinyin] [nvarchar](50) NULL,
 CONSTRAINT [PK_portal_users_1] PRIMARY KEY CLUSTERED 
(
	[uid] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET IDENTITY_INSERT [dbo].[users_users] ON
INSERT [dbo].[users_users] ([uid], [username], [adpath], [firstname], [lastname], [city], [Office], [Description], [Tel], [fax], [email], [MailType], [Title], [Department], [Company], [Password], [displayname], [StaffCode], [IsAccountActive], [createdate], [lastlogin], [sex], [avtor], [logintimes], [ip], [logintime], [intro], [ismgr], [issec], [isstaff], [aid], [mobile], [pinyin]) VALUES (1, N'admin', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N'CE-0B-FD-15-05-9B-68-D6-76-88-88-4D-7A-3D-3E-8C', N'Admin', NULL, NULL, CAST(0x0000ACE800E8DC3D AS DateTime), CAST(0x0000ACE800E8DC3D AS DateTime), NULL, N'avtor.jpg', 0, NULL, 0, NULL, 0, 0, 1, NULL, NULL, NULL)
SET IDENTITY_INSERT [dbo].[users_users] OFF
/****** Object:  Table [dbo].[users_roles]    Script Date: 03/14/2021 11:17:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[users_roles]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[users_roles](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[rolename] [nvarchar](50) NULL,
	[issystem] [int] NULL CONSTRAINT [DF_users_roles_issystem]  DEFAULT ((0)),
	[roletype] [nvarchar](50) NULL,
 CONSTRAINT [PK_users_roles] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET IDENTITY_INSERT [dbo].[users_roles] ON
INSERT [dbo].[users_roles] ([id], [rolename], [issystem], [roletype]) VALUES (1, N'administrators', 1, NULL)
SET IDENTITY_INSERT [dbo].[users_roles] OFF
/****** Object:  Table [dbo].[users_releation]    Script Date: 03/14/2021 11:17:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[users_releation]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[users_releation](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[mgrid] [int] NULL,
	[secid] [int] NULL,
 CONSTRAINT [PK_users_releation] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[users_getpwd]    Script Date: 03/14/2021 11:17:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[users_getpwd]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[users_getpwd](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[uid] [int] NOT NULL,
	[display] [nvarchar](50) NULL,
	[createdate] [datetime] NULL CONSTRAINT [DF_users_getpwd_createdate]  DEFAULT (getdate()),
 CONSTRAINT [PK_users_getpwd] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[users_dept]    Script Date: 03/14/2021 11:17:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[users_dept]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[users_dept](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[catname] [nvarchar](50) NULL,
	[parentid] [int] NULL CONSTRAINT [DF_portal_category_parentid]  DEFAULT ((0)),
	[orderid] [int] NULL CONSTRAINT [DF_portal_category_orderid]  DEFAULT ((0)),
	[depth] [int] NULL CONSTRAINT [DF_portal_category_depth]  DEFAULT ((1)),
	[note] [nvarchar](200) NULL,
	[path] [nvarchar](1000) NULL CONSTRAINT [DF_hr_csscategory_path]  DEFAULT (''),
	[types] [nvarchar](50) NULL,
 CONSTRAINT [PK_users_dept] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[kb_news]    Script Date: 03/14/2021 11:17:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[kb_news]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[kb_news](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[title] [nvarchar](400) NULL,
	[contents] [nvarchar](max) NULL,
	[catid] [int] NULL,
	[orderid] [decimal](18, 8) NULL CONSTRAINT [DF_kb_news_orderid]  DEFAULT ((0)),
	[click] [int] NULL CONSTRAINT [DF_kb_news_click]  DEFAULT ((0)),
	[adddate] [datetime] NULL CONSTRAINT [DF_kb_news_adddate]  DEFAULT (getdate()),
	[opname] [nvarchar](50) NULL,
	[opuid] [int] NULL,
	[ispass] [int] NULL CONSTRAINT [DF_kb_news_ispass]  DEFAULT ((1)),
 CONSTRAINT [PK_kb_news] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
END
GO
SET IDENTITY_INSERT [dbo].[kb_news] ON
INSERT [dbo].[kb_news] ([id], [title], [contents], [catid], [orderid], [click], [adddate], [opname], [opuid], [ispass]) VALUES (2, N'Window7与Window10', N'本文介绍如何安装 book 系统。 
book 系统采用ASP.NET(C#)+MSSQL+IIS开发, 它可以运行在windows操作系统上（包括Windows7或Windows2008）。
本文将使用Microsoft Windows 2008来演示如何安装book系统 。 要查看windows7安装指南请点击此处
### 1. 安装 IIS
打开Window 2008, 点击Server Manage,然后点击 Add Role
在向导里，选择 Web Server
确保 ASP.NET 和 Security 里相关组件被选中，然后点击下一步完成安装。

### 2. 安装 MSSQL 2008 Express
MSSQL 2008 Express 是微软提供的一款免费数据库软件，Express比MSSQL专业版更简单易用。
如果您公司已经有安装MSSQL数据库，可以跳过此步。

下载 MSSQL 2008 Express SQLEXPRWT_x64_CHS.exe 文件， 此文件来自 MSSQL 2008 EXPRESS微软官网站点
双击运行，点击左边 "Installation"，然后点击 "New Installation or add features to an existing installation"', 3, CAST(2.00000000 AS Decimal(18, 8)), 7, CAST(0x0000ACE900B1C82D AS DateTime), NULL, NULL, 1)
INSERT [dbo].[kb_news] ([id], [title], [contents], [catid], [orderid], [click], [adddate], [opname], [opuid], [ispass]) VALUES (3, N'Windows2012和Windows2016', N'本文介绍如何安装 book 系统。 
book 系统采用ASP.NET(C#)+MSSQL+IIS开发, 它可以运行在windows操作系统上（包括Windows7或Windows2008）。
本文将使用Microsoft Windows 2008来演示如何安装book系统 ', 3, CAST(3.00000000 AS Decimal(18, 8)), 6, CAST(0x0000ACE900B1EFA4 AS DateTime), NULL, NULL, 1)
INSERT [dbo].[kb_news] ([id], [title], [contents], [catid], [orderid], [click], [adddate], [opname], [opuid], [ispass]) VALUES (4, N'Helpdesk入门', N'### 1.登录
请使用Admin，密码123456 登录系统。', 4, CAST(4.00000000 AS Decimal(18, 8)), 4, CAST(0x0000ACE900B21562 AS DateTime), NULL, NULL, 1)
SET IDENTITY_INSERT [dbo].[kb_news] OFF
/****** Object:  Table [dbo].[kb_cat]    Script Date: 03/14/2021 11:17:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[kb_cat]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[kb_cat](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[catname] [nvarchar](200) NULL,
	[pid] [int] NULL CONSTRAINT [DF_portal_kbcategory_parentid]  DEFAULT ((0)),
	[orderid] [decimal](18, 8) NULL CONSTRAINT [DF_portal_kbcategory_orderid]  DEFAULT ((0)),
	[depth] [int] NULL CONSTRAINT [DF_portal_kbcategory_depth]  DEFAULT ((1)),
	[note] [nvarchar](200) NULL,
	[path] [nvarchar](1000) NULL CONSTRAINT [DF_hr2_kbcsscategory_path]  DEFAULT (''),
	[fullcatname] [nvarchar](200) NULL,
 CONSTRAINT [PK_kb_category] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET IDENTITY_INSERT [dbo].[kb_cat] ON
INSERT [dbo].[kb_cat] ([id], [catname], [pid], [orderid], [depth], [note], [path], [fullcatname]) VALUES (1, N'会议系统', 0, CAST(12.00000000 AS Decimal(18, 8)), 1, NULL, N'.1.', N'/会议系统')
INSERT [dbo].[kb_cat] ([id], [catname], [pid], [orderid], [depth], [note], [path], [fullcatname]) VALUES (2, N'帮助台', 0, CAST(13.00000000 AS Decimal(18, 8)), 1, NULL, N'.2.', N'/帮助台')
INSERT [dbo].[kb_cat] ([id], [catname], [pid], [orderid], [depth], [note], [path], [fullcatname]) VALUES (3, N'安装指南', 1, CAST(0.00000000 AS Decimal(18, 8)), 1, NULL, N'.1.3.', N'/会议系统/安装指南')
INSERT [dbo].[kb_cat] ([id], [catname], [pid], [orderid], [depth], [note], [path], [fullcatname]) VALUES (4, N'使用指南', 2, CAST(12.00000000 AS Decimal(18, 8)), 1, NULL, N'.2.4.', N'/帮助台/使用指南')
INSERT [dbo].[kb_cat] ([id], [catname], [pid], [orderid], [depth], [note], [path], [fullcatname]) VALUES (5, N'快速入门', 2, CAST(11.00000000 AS Decimal(18, 8)), 1, NULL, N'.2.5.', N'/帮助台/快速入门')
INSERT [dbo].[kb_cat] ([id], [catname], [pid], [orderid], [depth], [note], [path], [fullcatname]) VALUES (6, N'高级入门', 2, CAST(13.00000000 AS Decimal(18, 8)), 1, NULL, N'.2.6.', N'/帮助台/高级入门')
SET IDENTITY_INSERT [dbo].[kb_cat] OFF
/****** Object:  StoredProcedure [dbo].[sp_kb_movecategory]    Script Date: 03/14/2021 11:17:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_kb_movecategory]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create    PROCEDURE  [dbo].[sp_kb_movecategory] 
(
    @ParentID int,--新父节点
    @CataID int,--要移动的节点编号
    @CataPath varchar(20) output,--新移动后的节点路径
    @orderid int,
    @note nvarchar(500),
    @types nvarchar(50),
    @catname nvarchar(50) 

)
AS

if (not exists(select 1 from helpdesk_kbcategory where ID = @ParentID))
    set @CataPath = '''' -- 如果新父节点不存在，则设置错误信息
else 
    begin

        declare @oldParentID int -- 原来的父节点
        select @oldParentID = ParentID from helpdesk_kbcategory where ID = @CataID
      


  if @oldParentID = @ParentID -- 没有移动，只更新内容即可
             begin
                update helpdesk_kbcategory set orderid=@orderid, note=@note,types=@types,catname=@catname  where id=@cataid
                select @CataPath = path from helpdesk_kbcategory where ID = @CataID
            end 
       
       else
			begin
	            
				declare @pathLength  int
				declare @path varchar(20)
				select @path = max(Path) from helpdesk_kbcategory  where parentID =@parentId --order by Path desc
				
            select @pathLength = len(@path) 
				 
			
   	declare @newpath varchar(20)-- 计算移动到新分类后的节点路径
				if @pathLength > 0 -- 已经存在该父类
						select @newpath = substring(@path,1,@pathLength -1)+CHAR(ASCII(REVERSE(@path))+1) 
				else
						select @newpath = path + ''A'' from helpdesk_kbcategory where ID = @parentID
	        
				if len(@newpath) > 0
					begin
						declare @oldPath varchar(20)-- 记录原来的节点路径
						select @oldPath = Path from helpdesk_kbcategory where ID = @CataID
						set @CataPath = @newpath;
						update helpdesk_kbcategory set orderid=@orderid,   ParentID = @ParentID,Path = @newpath, types=@types,catname=@catname,note=@note where ID = @CataID
						-- 替换子类的Path
						update helpdesk_kbcategory set Path = stuff(path,1,len(@oldPath),@CataPath) where Path like @oldPath+''%'' AND types=@types
					end
				else
					set @CataPath = ''''
			end
     end



' 
END
GO
/****** Object:  StoredProcedure [dbo].[sp_kb_insertcategory]    Script Date: 03/14/2021 11:17:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_kb_insertcategory]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'


CREATE   PROCEDURE [dbo].[sp_kb_insertcategory] 
(
    @ParentID int, 
    @CatName varchar(20), 
    @Note nvarchar(200),
    @OrderId int,
    @Types nvarchar(50),
    @ID int output  
)
AS

if (@ParentID=0)
  begin
    insert into 
    helpdesk_kbcategory(catName,ParentID,Path,Note,OrderId,types) 
    values(@CatName,@ParentID,'''',@Note,@OrderId,@types)
    select @ID = scope_identity();
 end


else
    begin
        declare @pathLength  int --路径长度
        declare @path varchar(20) --该父节点下最后一个节点的路径
        select @path = max(Path) from helpdesk_kbcategory  where parentID = @parentId  
        select @pathLength = len(@path) 
        print @pathLength
        declare @newpath varchar(20)--新加节点的路径
        if @pathLength > 0 -- 该父类节点下已经存在有子类
                select @newpath = substring(@path,1,@pathLength -1)+CHAR(ASCII(REVERSE(@path))+1) 
        else
                select @newpath = path + ''A'' from helpdesk_kbcategory where ID = @parentID
        
        if len(@newpath) > 0
            begin
            insert into helpdesk_kbcategory(catName,ParentID,Path,note,OrderId,types) values(@CatName,@ParentID,@newpath,@Note,@OrderId,@types)
            select @ID = scope_identity();
            end
        else
            set @ID = -1
    end


 





' 
END
GO
/****** Object:  View [dbo].[v_kb_cat]    Script Date: 03/14/2021 11:17:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[v_kb_cat]'))
EXEC dbo.sp_executesql @statement = N'
CREATE view [dbo].[v_kb_cat] as
select top 100 percent 0-id as id, catid as pId, title as name from kb_news order by id desc
'
GO
