
/****** 对象:  StoredProcedure [sp_Pts_GetProjectFieldsInUse]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectFieldsInUse]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'create procedure [sp_Pts_GetProjectFieldsInUse]
@ProjectID int
AS
Select * From Pts_DbDirectory Where ProjectID = @ProjectID AND (IsSystem=0 or InUse=1) Order by ListOrder' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProblemInfo]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemInfo]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_GetProblemInfo]
@ProblemID int
AS
Select * from v_Pts_Problems where ProblemID = @ProblemID



' 
END
GO
/****** 对象:  Table [Accounts_Roles]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Accounts_Roles]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Accounts_Roles](
	[RoleID] [int] IDENTITY(1,1) NOT NULL,
	[Description] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
 CONSTRAINT [PK_Accounts_Roles] PRIMARY KEY CLUSTERED 
(
	[RoleID] ASC
)
)
END
GO
SET IDENTITY_INSERT [Accounts_Roles] ON
INSERT [Accounts_Roles] ([RoleID], [Description]) VALUES (1, N'系统管理员')
INSERT [Accounts_Roles] ([RoleID], [Description]) VALUES (2, N'用户管理员')
INSERT [Accounts_Roles] ([RoleID], [Description]) VALUES (3, N'项目管理员')
INSERT [Accounts_Roles] ([RoleID], [Description]) VALUES (4, N'知识库管理员')
SET IDENTITY_INSERT [Accounts_Roles] OFF
/****** 对象:  StoredProcedure [sp_Pts_GetProblemList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





CREATE   Procedure [sp_Pts_GetProblemList]
@QueryString nvarchar(512)
AS
exec sp_executesql @QueryString





' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_CreateUser]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_CreateUser]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--添加用户，使用邮件作为唯一标识
CREATE PROCEDURE [sp_Accounts_CreateUser]
@Password 	BINARY(20),
@EmailAddress 	varchar(255),
@DisplayName 	nvarchar(50),
@UserState	int,
@Address	nvarchar(80),
@OfficePhone	nvarchar(50),
@MobilePhone	nvarchar(50),
@MSN		nvarchar(255),
@PasswordNote nvarchar(50),
@Remark	nvarchar(1024),
@DeptID 	int,
@LoginName nvarchar(255),
@ExpireDate datetime
AS
	Insert into Accounts_Users (Password, EmailAddress, DisplayName, UserState, 
		Address, OfficePhone, MobilePhone, MSN,PasswordNote, Remark,DeptID,LoginName, ExpireDate)
	Values (@Password, @EmailAddress, @DisplayName, @UserState, 
		@Address, @OfficePhone, @MobilePhone, @MSN,@PasswordNote, @Remark, @DeptID, @LoginName, @ExpireDate)
	
	RETURN @@IDENTITY
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Time_3]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_3]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_Filter_Time_3]
@ProjectID int,
@UserID int
AS
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	CreateTime < dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7,convert(varchar,getdate(),112))
AND
	CreateTime >= dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7-7,convert(varchar,getdate(),112))

' 
END
GO
/****** 对象:  Table [Pts_ProjectTempletDbDirectory]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProjectTempletDbDirectory]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProjectTempletDbDirectory](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectTempletID] [int] NOT NULL,
	[TableName] [varchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[ColumnName] [varchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[ColumnTitle] [varchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[ColumnType] [int] NOT NULL,
	[ColumnSizeLimit] [int] NOT NULL,
	[InputType] [int] NOT NULL,
	[InputHeight] [int] NOT NULL,
	[InputWidth] [int] NOT NULL,
	[ColumnDescription] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[DropDownID] [int] NULL,
	[Visible] [bit] NOT NULL,
	[Fixed] [bit] NOT NULL,
	[ListOrder] [int] NOT NULL
)
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProblemStateInfo]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemStateInfo]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [sp_Pts_GetProblemStateInfo]
@ProblemStateID int
AS
SELECT * FROM Pts_ProblemState WHERE ProblemStateID = @ProblemStateID' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_DeleteDepartment]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_DeleteDepartment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [sp_Accounts_DeleteDepartment]
@DeptID int
AS
Update Accounts_Users Set DeptID = 0 Where DeptID = @DeptID
Delete From Accounts_Department Where DeptID = @DeptID
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Time_4]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_4]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_Filter_Time_4]
@ProjectID int,
@UserID int
AS
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 1
AND
	CloseTime < dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7,convert(varchar,getdate(),112))
AND
	CloseTime >= dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7-7,convert(varchar,getdate(),112))

' 
END
GO
/****** 对象:  Table [Pts_ProjectTemplets]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProjectTemplets]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProjectTemplets](
	[ProjectTempletID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectTempletName] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[ProjectTempletDescription] [nvarchar](3000) COLLATE Chinese_PRC_CI_AS NULL,
	[ThreadTitle] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[EnableTrace] [bit] NOT NULL,
	[EnableSeverity] [bit] NOT NULL,
	[EnableProblemState] [bit] NOT NULL,
	[EnableEmailNotify] [bit] NOT NULL,
	[EnableProblemDeadline] [bit] NOT NULL,
	[EnableRecordType] [bit] NOT NULL,
 CONSTRAINT [PK_Pts_ProjectTypes] PRIMARY KEY CLUSTERED 
(
	[ProjectTempletID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_GlobalSelectValues]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_GlobalSelectValues]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_GlobalSelectValues](
	[SelectValueID] [int] IDENTITY(1,1) NOT NULL,
	[GlobalSelectID] [int] NOT NULL,
	[ValueString] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[ListOrder] [char](10) COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Pts_GlobalSelectValues] PRIMARY KEY CLUSTERED 
(
	[SelectValueID] ASC
)
)
END
GO
/****** 对象:  Table [Common_Events]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Common_Events]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Common_Events](
	[EventID] [int] IDENTITY(1,1) NOT NULL,
	[Time] [datetime] NULL,
	[UserID] [int] NULL,
	[UserName] [nvarchar](100) COLLATE Chinese_PRC_CI_AS NULL,
	[EventType] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[EventInfo] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Common_Events] PRIMARY KEY CLUSTERED 
(
	[EventID] ASC
)
)
END
GO
/****** 对象:  Table [Accounts_UserState]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Accounts_UserState]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Accounts_UserState](
	[UserStateID] [int] IDENTITY(1,1) NOT NULL,
	[UserStateName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Description] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Accounts_UserState] PRIMARY KEY CLUSTERED 
(
	[UserStateID] ASC
)
)
END
GO
SET IDENTITY_INSERT [Accounts_UserState] ON
INSERT [Accounts_UserState] ([UserStateID], [UserStateName], [Description]) VALUES (1, N'待审核', N'用户刚刚注册，未经过管理员审核')
INSERT [Accounts_UserState] ([UserStateID], [UserStateName], [Description]) VALUES (2, N'普通', N'正常状态用户')
INSERT [Accounts_UserState] ([UserStateID], [UserStateName], [Description]) VALUES (3, N'受限', N'用户可以登录，但无法进行操作')
INSERT [Accounts_UserState] ([UserStateID], [UserStateName], [Description]) VALUES (4, N'禁用', N'禁止用户登录')
SET IDENTITY_INSERT [Accounts_UserState] OFF
/****** 对象:  StoredProcedure [sp_Pts_GetProjectInfo]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectInfo]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_GetProjectInfo]
	(
		@ProjectID int
	)
AS
	Select * From v_Pts_Projects where ProjectID = @ProjectID
	RETURN 




' 
END
GO
/****** 对象:  Table [Pts_ProblemShiftHistory]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemShiftHistory]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemShiftHistory](
	[ShiftID] [int] IDENTITY(1,1) NOT NULL,
	[ProblemID] [int] NOT NULL,
	[PrevProjectID] [int] NOT NULL,
	[PrevUserID] [int] NOT NULL,
	[PrevStateID] [int] NOT NULL,
	[NextProjectID] [int] NOT NULL,
	[NextStateID] [int] NOT NULL,
	[NextUserID] [int] NOT NULL,
	[ShiftDate] [datetime] NOT NULL,
	[Description] [nvarchar](2000) COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Pts_ProblemShiftHistory] PRIMARY KEY CLUSTERED 
(
	[ShiftID] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProjectList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





CREATE   PROCEDURE [sp_Pts_GetProjectList]
@ProjectCatalogID int,
@ProjectStateID int
AS
--ProjectCatalogID <=0 代表返回所有catalog的记录
--ProjectStateID <=0 代表返回所有state的记录
if @ProjectCatalogID > 0 
BEGIN
	if @ProjectStateID > 0
		Select * From v_Pts_Projects where ProjectCatalogID = @ProjectCatalogID AND ProjectStateID = @ProjectStateID order by ProjectID asc
	else
	BEGIN
		if @ProjectStateID = 0
			Select * From v_Pts_Projects where ProjectCatalogID = @ProjectCatalogID order by ProjectID asc
		if @ProjectStateID = -1
			Select * From v_Pts_Projects where ProjectCatalogID = @ProjectCatalogID AND IsClosed=0 order by ProjectID asc
		if @ProjectStateID = -2
			Select * From v_Pts_Projects where ProjectCatalogID = @ProjectCatalogID AND IsClosed=1 order by ProjectID asc
	END
END
ELSE
BEGIN
	if @ProjectStateID > 0
		Select * From v_Pts_Projects where  ProjectStateID = @ProjectStateID order by ProjectID asc
	else
	BEGIN
		if @ProjectStateID = 0
			Select * From v_Pts_Projects  order by ProjectID asc
		if @ProjectStateID = -1
			Select * From v_Pts_Projects where  IsClosed=0 order by ProjectID asc
		if @ProjectStateID = -2
			Select * From v_Pts_Projects where  IsClosed=1 order by ProjectID asc
	END
END





' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProjectSeverity]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectSeverity]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





CREATE PROCEDURE [sp_Pts_GetProjectSeverity]
@ProjectSeverity int
AS
Select * from Pts_ProjectSeveritys where ProjectSeverityID = @ProjectSeverity




' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Time_5]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_5]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Pts_Filter_Time_5]
@ProjectID int,
@UserID int
AS
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	CreateTime >= dateadd(day,1-day(getdate()),convert(varchar,getdate(),112))

' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_GetDepartmentList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetDepartmentList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [sp_Accounts_GetDepartmentList]
AS
SELECT 
 *
FROM 
 Accounts_Department
ORDER BY 
 ListOrder ASC
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Time_6]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_6]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_Filter_Time_6]
@ProjectID int,
@UserID int
AS
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 1
AND
	CloseTime >= dateadd(day,1-day(getdate()),convert(varchar,getdate(),112))

' 
END
GO
/****** 对象:  Table [Pts_Workgroups]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_Workgroups]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_Workgroups](
	[WorkgroupID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectID] [int] NOT NULL,
	[Name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Description] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[AdminProject] [bit] NOT NULL,
	[AddProblem] [bit] NOT NULL,
	[ChangeProblemState] [bit] NOT NULL,
	[ShiftProblem] [bit] NOT NULL,
	[AddRecord] [bit] NOT NULL,
	[ListOrder] [int] NOT NULL,
	[OldID] [int] NULL,
	[ExtendedSettings] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Pts_Workgroups] PRIMARY KEY CLUSTERED 
(
	[WorkgroupID] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Time_7]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_7]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_Filter_Time_7]
@ProjectID int,
@UserID int
AS
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	CreateTime >= dateadd(month,-1,dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)))
AND
	CreateTime < dateadd(day,1-day(getdate()),convert(varchar,getdate(),112))

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Workflow_DeleteWorkflowStep]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Workflow_DeleteWorkflowStep]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_Workflow_DeleteWorkflowStep]
	@StepID int
AS

SET NOCOUNT ON

DELETE FROM [Pts_WorkflowSteps]
WHERE
	[StepID] = @StepID' 
END
GO
/****** 对象:  Table [Pts_SetFieldValueActions]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_SetFieldValueActions]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_SetFieldValueActions](
	[FieldActionID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectID] [int] NOT NULL,
	[FieldName] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[FieldLabel] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[FieldValue] [nvarchar](1000) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[FieldDisplayValue] [nvarchar](1000) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Note] [nvarchar](1000) COLLATE Chinese_PRC_CI_AS NOT NULL,
 CONSTRAINT [PK_Pts_SetFieldActions] PRIMARY KEY CLUSTERED 
(
	[FieldActionID] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Time_9]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_9]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Pts_Filter_Time_9]
@ProjectID int,
@UserID int
AS
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0
AND
	DateDiff(d, LastTime, GetDate()) > 7

' 
END
GO
/****** 对象:  Table [Pts_ProblemStateTimeLimitRoleItems]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemStateTimeLimitRoleItems]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemStateTimeLimitRoleItems](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[ProblemStateID] [int] NOT NULL,
	[ListOrder] [int] NOT NULL,
	[Note] [nvarchar](100) COLLATE Chinese_PRC_CI_AS NULL,
	[Settings] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[TimeSpan] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Pts_ProblemStateTimeLimitRoleItems] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProjectType]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'







CREATE PROCEDURE [sp_Pts_GetProjectType]
@ProjectType int
AS
Select * from Pts_ProjectTypes where ProjectTypeID = @ProjectType





' 
END
GO
/****** 对象:  Table [Pts_StepActions]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_StepActions]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_StepActions](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectID] [int] NOT NULL,
	[SrcStateID] [int] NOT NULL,
	[DstStateID] [int] NOT NULL,
	[SetFieldValueActionList] [nvarchar](1000) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Note] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Pts_StepActions] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Time_8]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_8]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_Filter_Time_8]
@ProjectID int,
@UserID int
AS
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 1
AND
	CloseTime >= dateadd(month,-1,dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)))
AND
	CloseTime < dateadd(day,1-day(getdate()),convert(varchar,getdate(),112))

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetRecord]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetRecord]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'


Create PROCEDURE [sp_Pts_GetRecord]
@RecordID int
AS
Select * From v_Pts_Records where RecordID = @RecordID



' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_GetUserDetailsByEmail]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetUserDetailsByEmail]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--根据Email获取某用户的详细信息
CREATE PROCEDURE [sp_Accounts_GetUserDetailsByEmail]
@EmailAddress varchar(255)
AS
	SELECT * FROM v_Accounts_Users  WHERE EmailAddress = @EmailAddress



' 
END
GO
/****** 对象:  Table [Pts_StateEditableFields]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_StateEditableFields]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_StateEditableFields](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectID] [int] NOT NULL,
	[ProblemStateID] [int] NOT NULL,
	[FieldName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
 CONSTRAINT [PK_Pts_ProblemStateField] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_GetUsers]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Accounts_GetUsers]  AS
Select * from v_Accounts_Users



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetRecordList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetRecordList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_GetRecordList]
@ProblemID int
AS
Select * From v_Pts_Records where ProblemID = @ProblemID



' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_GetUserDetails]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetUserDetails]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--返回用户详细信息
CREATE PROCEDURE [sp_Accounts_GetUserDetails]
@UserID int
AS
	SELECT * FROM v_Accounts_Users WHERE UserID = @UserID



' 
END
GO
/****** 对象:  Table [Kb_ArticleViewer]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Kb_ArticleViewer]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Kb_ArticleViewer](
	[ArticleID] [int] NOT NULL,
	[UserID] [int] NOT NULL
)
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Time_TodayUpdated]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_TodayUpdated]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE procedure [sp_Pts_Filter_Time_TodayUpdated]
@ProjectID int,
@UserID int
AS
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	LastTime >= Convert(varchar(10),getdate(),120)' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Time_Expired]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_Expired]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE procedure [sp_Pts_Filter_Time_Expired]
@ProjectID int,
@UserID int
AS
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0
AND
	Deadline is not null
AND
	Deadline < GetDate()' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetUserCanAddProblemProjects]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserCanAddProblemProjects]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'

CREATE  Procedure [sp_Pts_GetUserCanAddProblemProjects]
@UserID int
AS

SELECT * From 
	v_Pts_Projects
WHERE
	ProjectID 
IN
	(SELECT ProjectID FROM Pts_Workgroups WHERE 
		AddProblem = 1 AND
		WorkgroupID in 
		(Select WorkgroupID From Pts_WorkgroupUsers Where UserID = @UserID)
	)
AND
	IsClosed = 0	

Order By 
	CatalogOrder
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetUserNeedProcessProjects]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserNeedProcessProjects]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [sp_Pts_GetUserNeedProcessProjects]
@UserID int
AS

SELECT * From 
	v_Pts_Projects
WHERE
	ProjectID 
IN
	(SELECT distinct ProjectID FROM v_Pts_Problems WHERE 
		IsClosed = 0
		AND
		((AssignedTo > 0 AND AssignedTo = @UserID)
		OR (AssignedTo < 0 AND EXISTS (SELECT * FROM Pts_WorkgroupUsers WHERE UserID=@UserID AND WorkgroupID=-AssignedTo)
		))
		
		
	)
AND
	IsClosed = 0	

Order By 
	CatalogOrder' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_ResetDepartmentOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_ResetDepartmentOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Accounts_ResetDepartmentOrder]
AS
Declare @Order int
set @Order = 1
Declare @deptID int
Declare state_cursor CURSOR
FOR 
	Select DeptID From Accounts_Department
Open state_cursor
Fetch next from state_cursor into @deptID 
while (@@FETCH_STATUS <> -1)
BEGIN
	if (@@FETCH_STATUS <> -2)
	BEGIN
		Update Accounts_Department set ListOrder = @Order where DeptID = @deptID 
		Set @Order = @Order + 1		
	END
	Fetch next from state_cursor into @deptID 
END
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Time_StateExpired]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_StateExpired]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE procedure [sp_Pts_Filter_Time_StateExpired]
@ProjectID int,
@UserID int
AS
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0
AND
	StateDeadline is not null
AND
	StateDeadline < GetDate()' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetIssueCounts_ByFields]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetIssueCounts_ByFields]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE procedure [sp_Pts_GetIssueCounts_ByFields]
@ProjectID int
AS
--得到根据各系统字段统计的事务数量

declare @t table
(
	count_field nvarchar(20),
	count_type_name nvarchar(50),
	count_type_id int,
	count_value int
)

insert into @t select ''priority'', ProblemPriorityName,ProblemPriorityID,count(*) from v_Pts_Problems WHERE ProjectID=@ProjectID AND IsClosed=0 
GROUP BY ProblemPriorityID,ProblemPriorityName,ProblemPriorityOrder order by ProblemPriorityOrder


insert into @t select ''severity'',ProblemSeverityName,ProblemSeverityID,count(*) from v_Pts_Problems WHERE ProjectID=@ProjectID AND IsClosed=0 
GROUP BY ProblemSeverityID,ProblemSeverityName,ProblemSeverityOrder order by ProblemSeverityOrder


insert into @t select ''type'', ProblemTypeName,ProblemTypeID,count(*) from v_Pts_Problems WHERE ProjectID=@ProjectID AND IsClosed=0 
GROUP BY ProblemTypeID,ProblemTypeName,ProblemTypeOrder order by ProblemTypeOrder

select * from @t' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_TestPassword]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_TestPassword]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--试验密码是否正确
CREATE PROCEDURE [sp_Accounts_TestPassword]
@UserID int,
@EncryptedPassword binary(20)
AS
     DECLARE @TempID int
     SELECT @TempID = UserID FROM Accounts_Users WHERE UserID = @UserID AND
	Password = @EncryptedPassword

     IF @TempID IS NULL
	RETURN 0
     ELSE
	RETURN 1





' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_UpdateDepartment]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_UpdateDepartment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [sp_Accounts_UpdateDepartment]
@DeptID int,
@DeptName nvarchar(50)
AS
Update Accounts_Department Set DeptName = @DeptName Where DeptID = @DeptID
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetUserFields]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserFields]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_GetUserFields]
@ProjectID int
AS
Select * From Pts_DbDirectory where ProjectID = @ProjectID  Order by ListOrder
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProjectUsers]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'






--获取某个项目的所有用户
CREATE    Procedure [sp_Pts_GetProjectUsers]
@ProjectID int
AS

--Select u.UserID, u.DisplayName, u.EmailAddress , g.Name as WorkgroupName, g.WorkgroupID, 
--	("(" + g.Name + ")" + u.DisplayName) AS FullName
--From 
--	Accounts_Users u, Pts_Workgroups g, Pts_WorkgroupUsers gu
--Where
--	gu.WorkgroupID = g.WorkgroupID 
--	AND
--	gu.UserID = u.UserID 
--	AND
--	gu.WorkgroupID in
--		(Select WorkgroupID From Pts_Workgroups where ProjectID = @ProjectID)
--	
--	Order by FullName

select *
from v_Accounts_Users
Where UserState<>4 AND UserID in
(Select distinct UserID From Pts_WorkgroupUsers Where WorkgroupID in
 (Select WorkgroupID From Pts_Workgroups Where ProjectID = @ProjectID )
)

Order By DeptName, DisplayName
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_CreatedByMeNotClosed]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_CreatedByMeNotClosed]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
create Procedure [sp_Pts_Filter_CreatedByMeNotClosed]
@ProjectID int,
@UserID int,
@Count int
AS
--我创建的未关闭事务
SELECT top 500 ProblemID,ProblemCode,Title,AssignedTo,ProblemStateName,CreateUserName,LastTime,LastUser FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	CreateUserID = @UserID
AND 
	IsClosed = 0
ORDER BY CreateTime DESC


' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_UpdateUser]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_UpdateUser]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--更新用户信息
CREATE Procedure [sp_Accounts_UpdateUser]
@UserID 	int,
@Password 	BINARY(20),
@EmailAddress 	varchar(255),
@DisplayName 	nvarchar(50),
@UserState	int,
@Address	nvarchar(80) =null,
@OfficePhone	nvarchar(50) = null,
@MobilePhone	nvarchar(50) = null,
@MSN		nvarchar(255) = null,
@PasswordNote nvarchar(50) = null,
@Remark	nvarchar(1024) = null,
@DeptID 	int,
@LoginName nvarchar(255),
@ExpireDate datetime
AS
	Update Accounts_Users Set
		EmailAddress = @EmailAddress,
		Password = @Password,
		DisplayName = @DisplayName,
		UserState = @UserState,
		Address = @Address,
		OfficePhone = @OfficePhone,
		MobilePhone = @MobilePhone,
		MSN = @MSN,
		PasswordNote = @PasswordNote,
		Remark = @Remark,
		DeptID = @DeptID,
		LoginName = @LoginName,
		ExpireDate = @ExpireDate
	Where UserID = @UserID
' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_ValidateLogin]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_ValidateLogin]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





--验证密码合法性
--验证密码合法性
CREATE  Procedure [sp_Accounts_ValidateLogin]
@LoginName varchar(255),
@EncryptedPassword binary(20)
AS
	DECLARE @UserID int
	
	Select @UserID = UserID From Accounts_Users
		Where (LoginName = @LoginName OR  EmailAddress = @LoginName) 
		AND Password = @EncryptedPassword
		
	if @UserID != NULL
		return @UserID
	else
		return -1




' 
END
GO
/****** 对象:  StoredProcedure [sp_Common_GetRecordFromPage]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Common_GetRecordFromPage]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
/*
  函数名称: GetRecordFromPage
  函数功能: 获取指定页的数据
  参数说明:   @tblName        包含数据的表名
             @fldName        关键字段名
              @PageSize      每页记录数
             @PageIndex    要获取的页码
              @IsCount         是否要取得记录数
             @OrderType    排序类型, 0 - 升序, 1 - 降序
             @strWhere      查询条件 (注意: 不要加 where)
*/
CREATE  PROCEDURE [sp_Common_GetRecordFromPage]
    @tblName      varchar(255),       -- 表名
    @fldName      varchar(255),       -- 字段名
    @PageSize     int = 10,           -- 页尺寸
    @PageIndex    int = 1,            -- 页码
    @IsCount      bit = 0,            -- 返回记录总数, 非 0 值则返回
    @OrderType    bit = 0,            -- 设置排序类型, 非 0 值则降序
    @strWhere     varchar(1000) = ''''  -- 查询条件 (注意: 不要加 where)
AS

declare @strSQL   varchar(6000)       -- 主语句
declare @strTmp   varchar(500)        -- 临时变量
declare @strOrder varchar(400)        -- 排序类型

-- 如果是查询记录总数，直接使用Count(0)函数
if @IsCount != 0
 begin
  if @strWhere != ''''
   set @strSQL = ''select count(*) as Total from ['' + @tblName + ''] where '' + @strWhere
  else
   set @strSQL = ''select count(*) as Total from ['' + @tblName + ''] '' 
 end
--如果是想查询记录，则
else
 begin
  if @PageIndex = 1
   begin
        set @strTmp = ''''
        if @strWhere != ''''
             set @strTmp = '' where '' + @strWhere

        set @strSQL = ''select top '' + str(@PageSize) + '' * from [''
             + @tblName + '']'' + @strTmp + '' '' + @strOrder
   end
  else
   begin
    --如果是降序查询……
    if @OrderType != 0
     begin
          set @strTmp = ''<(select min''
          set @strOrder = '' order by ['' + @fldName +''] desc''
     end
    --如果是升序查询……
    else
     begin
          set @strTmp = ''>(select max''
          set @strOrder = '' order by ['' + @fldName +''] asc''
     end

    if @strWhere != ''''
         set @strSQL = ''select top '' + str(@PageSize) + '' * from [''
              + @tblName + ''] where ['' + @fldName + '']'' + @strTmp + ''([''
              + @fldName + '']) from (select top '' + str((@PageIndex-1)*@PageSize) + '' [''
              + @fldName + ''] from ['' + @tblName + ''] where '' + @strWhere + '' ''
              + @strOrder + '') as tblTmp) and '' + @strWhere + '' '' + @strOrder
    else
     set @strSQL = ''select top '' + str(@PageSize) + '' * from [''
          + @tblName + ''] where ['' + @fldName + '']'' + @strTmp + ''([''
          + @fldName + '']) from (select top '' + str((@PageIndex-1)*@PageSize) + '' [''
          + @fldName + ''] from ['' + @tblName + '']'' + @strOrder + '') as tblTmp)''
          + @strOrder  

  
   end
end

exec (@strSQL)

' 
END
GO
/****** 对象:  Table [Common_Version]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Common_Version]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Common_Version](
	[DbVersion] [int] NULL
)
END
GO
INSERT [Common_Version] ([DbVersion]) VALUES (240)
/****** 对象:  StoredProcedure [sp_Pts_GetUserRelateProblems]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserRelateProblems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'

CREATE procedure [sp_Pts_GetUserRelateProblems]
(
@UserID	int
)
AS
--用户处理过的事务
SELECT *
FROM	v_Pts_Problems
WHERE	IsClosed=0 AND ProblemID in
(
  select distinct(ProblemID) From v_Pts_Records Where CreateUser=@UserID)

' 
END
GO
/****** 对象:  StoredProcedure [sp_Common_GetTopicList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Common_GetTopicList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE procedure [sp_Common_GetTopicList] 
       @a_FieldList nVarchar(2000),
       @a_TableName nVarchar(300), 
       @a_SelectWhere nVarchar(4000),
       @a_SelectOrderId nVarchar(200),
       @a_SelectOrder nVarchar(500),
       @a_intPageNo int,
       @a_intPageSize int,
       @RecordCount int OUTPUT
as
   /*定义局部变量*/
   declare @intBeginID         int
   declare @intEndID           int
   declare @intRootRecordCount int
   declare @intRowCount        int
   declare @TmpSelect          NVarchar(4000)
   /*关闭计数*/
   set nocount on
   
   /*求总共根贴数*/

   select @TmpSelect = ''set nocount on;select @SPintRootRecordCount = count(*) from ''+@a_TableName+'' ''+@a_SelectWhere
   execute sp_executesql 
             @TmpSelect,
             N''@SPintRootRecordCount int OUTPUT'',
             @SPintRootRecordCount=@intRootRecordCount OUTPUT

select @RecordCount = @intRootRecordCount

   if (@intRootRecordCount = 0)    --如果没有贴子，则返回零
       return 0
       
   /*判断页数是否正确*/
   if (@a_intPageNo - 1) * @a_intPageSize > @intRootRecordCount
      return (-1)

   /*求开始rootID*/
   set @intRowCount = (@a_intPageNo - 1) * @a_intPageSize + 1
   /*限制条数*/

   select @TmpSelect = ''set nocount on;set rowcount @SPintRowCount;select @SPintBeginID = ''+@a_SelectOrderId+'' from ''+@a_TableName+'' ''+@a_SelectWhere+'' ''+@a_SelectOrder
   execute sp_executesql 
             @TmpSelect,
             N''@SPintRowCount int,@SPintBeginID int OUTPUT'',
             @SPintRowCount=@intRowCount,@SPintBeginID=@intBeginID OUTPUT


   /*结束rootID*/
   set @intRowCount = @a_intPageNo * @a_intPageSize
   /*限制条数*/

   select @TmpSelect = ''set nocount on;set rowcount @SPintRowCount;select @SPintEndID = ''+@a_SelectOrderId+'' from ''+@a_TableName+'' ''+@a_SelectWhere+'' ''+@a_SelectOrder
   execute sp_executesql 
             @TmpSelect,
             N''@SPintRowCount int,@SPintEndID int OUTPUT'',
             @SPintRowCount=@intRowCount,@SPintEndID=@intEndID OUTPUT


if @a_SelectWhere='''' or @a_SelectWhere IS NULL
   select @TmpSelect = ''set nocount off;set rowcount 0;select ''+@a_FieldList+'' from ''+@a_TableName+'' where ''+@a_SelectOrderId+'' between ''
else
   select @TmpSelect = ''set nocount off;set rowcount 0;select ''+@a_FieldList+'' from ''+@a_TableName+'' ''+@a_SelectWhere+'' and ''+@a_SelectOrderId+'' between ''

if @intEndID > @intBeginID
   select @TmpSelect = @TmpSelect+''@SPintBeginID and @SPintEndID''+'' ''+@a_SelectOrder
else
   select @TmpSelect = @TmpSelect+''@SPintEndID and @SPintBeginID''+'' ''+@a_SelectOrder

   execute sp_executesql 
             @TmpSelect,
             N''@SPintEndID int,@SPintBeginID int'',
             @SPintEndID=@intEndID,@SPintBeginID=@intBeginID

   return(@@rowcount)
   --select @@rowcount' 
END
GO
/****** 对象:  Table [Pts_ProblemTimers1]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemTimers1]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemTimers1](
	[TimerID] [int] IDENTITY(1,1) NOT NULL,
	[ProblemID] [int] NOT NULL,
	[SettingID] [int] NOT NULL,
	[CreateTime] [datetime] NOT NULL,
	[IsDeleteAfterIssueClose] [bit] NOT NULL,
	[NextExpireTime] [datetime] NOT NULL,
	[ExecuteCount] [int] NOT NULL,
	[LastExecuteTime] [datetime] NULL,
	[IsLastExecuteOK] [bit] NOT NULL,
	[LastExecuteMsg] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[ValueToCheck] [nvarchar](500) COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Pts_ProblemTimers1] PRIMARY KEY CLUSTERED 
(
	[TimerID] ASC
)
)
END
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_ProblemTimers1', N'COLUMN',N'ProblemID'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'对应的事务ID' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_ProblemTimers1', @level2type=N'COLUMN',@level2name=N'ProblemID'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_ProblemTimers1', N'COLUMN',N'SettingID'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'模板ID' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_ProblemTimers1', @level2type=N'COLUMN',@level2name=N'SettingID'
GO
/****** 对象:  StoredProcedure [sp_Kb_CreateCategory]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_CreateCategory]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create procedure [sp_Kb_CreateCategory]
@CategoryName NVARCHAR(1024),
@ParentID int
AS
INSERT INTO	Kb_Categories	(CategoryName, 	ParentID) Values (@CategoryName, @ParentID)

Declare @NewCategoryID as int
SELECT @NewCategoryID = @@IDENTITY

Declare @ParentPath as nvarchar(100)
Select @ParentPath=Path From Kb_Categories Where CategoryID = @ParentID

Declare @NewPath as nvarchar(100)
if @@ROWCOUNT > 0 
BEGIN
	SET @NewPath = @ParentPath + '','' + Cast(@NewCategoryID as NVarchar(10))
END
else
	SET @NewPath = Cast(@NewCategoryID as NVarchar(10))

Update Kb_Categories Set Path = @NewPath Where CategoryID = @NewCategoryID

Select @NewCategoryID

' 
END
GO
/****** 对象:  Table [Pts_Notify]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_Notify]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_Notify](
	[NotifyID] [int] IDENTITY(1,1) NOT NULL,
	[TargetType] [int] NOT NULL,
	[TargetID] [int] NOT NULL,
	[NotifyUsers] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[EventType] [int] NOT NULL,
	[CCMails] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[Sender] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[EventTime] [datetime] NOT NULL,
	[NotifyAssignee] [bit] NOT NULL,
 CONSTRAINT [PK_Pts_Notify] PRIMARY KEY CLUSTERED 
(
	[NotifyID] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Kb_GetCategoryList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetCategoryList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [sp_Kb_GetCategoryList]
@ParentID int
AS
SELECT * FROM Kb_Categories WHERE ParentID = @ParentID
' 
END
GO
/****** 对象:  Table [Pts_OutConnectorNotifys]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_OutConnectorNotifys]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_OutConnectorNotifys](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[TargetUserID] [int] NOT NULL,
	[MsgType] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[Message] [nvarchar](200) COLLATE Chinese_PRC_CI_AS NULL,
	[Link] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[AddTime] [datetime] NOT NULL,
 CONSTRAINT [PK_Pts_OutConnectorNotifys] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Kb_GetArticleNotAllowedViewer]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetArticleNotAllowedViewer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Kb_GetArticleNotAllowedViewer]
@ArticleID int
AS
SELECT * From v_Accounts_Users WHERE UserID Not in
(SELECT UserID From Kb_ArticleViewer WHere ArticleID = @ArticleID)
Order by DisplayName' 
END
GO
/****** 对象:  StoredProcedure [sp_Kb_GetArticleNotAllowedViewerByDept]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetArticleNotAllowedViewerByDept]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Kb_GetArticleNotAllowedViewerByDept]
@ArticleID int,
@DeptID int
AS
SELECT * From v_Accounts_Users WHERE
DeptID = @DeptID 
AND
UserID Not in
(SELECT UserID From Kb_ArticleViewer WHere ArticleID = @ArticleID)
Order by DisplayName' 
END
GO
/****** 对象:  Table [Common_PersonalConfig]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Common_PersonalConfig]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Common_PersonalConfig](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[UserID] [int] NOT NULL,
	[ProjectID] [int] NOT NULL,
	[Type] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[CreateDate] [datetime] NOT NULL,
	[Title] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Description] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[Data] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Common_PersonalConfig] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Kb_GetPathCategories]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetPathCategories]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'

--返回某个指定目录的父目录列表
CREATE  PROCEDURE [sp_Kb_GetPathCategories]
@CategoryID int
AS

Declare @Path NVARCHAR(100)
Select @Path=Path From Kb_Categories Where CategoryID = @CategoryID

Declare @Sql nvarchar(300)
Set @Sql = ''SELECT * FROM Kb_Categories WHERE CategoryID in ('' + @Path + '')''

exec sp_executesql @Sql


' 
END
GO
/****** 对象:  StoredProcedure [sp_Kb_GetArticleAllowedViewer]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetArticleAllowedViewer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Kb_GetArticleAllowedViewer]
@ArticleID int
AS
SELECT * FROM v_Accounts_Users 
WHERE UserID in
(Select UserID From Kb_ArticleViewer WHERE ArticleID = @ArticleID)
Order By DisplayName' 
END
GO
/****** 对象:  Table [Pts_ProblemSequenceRelation]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemSequenceRelation]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemSequenceRelation](
	[PrecedingProblemID] [int] NOT NULL,
	[SucceedingProblemID] [int] NOT NULL
)
END
GO
/****** 对象:  StoredProcedure [sp_Pts_MergeProjectCatalog]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_MergeProjectCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_MergeProjectCatalog]
@FromCatalogID int,
@ToCatalogID int
AS
Update Projects Set ProjectCatalogID=@ToCatalogID where ProjectCatalogID=@FromCatalogID



' 
END
GO
/****** 对象:  Table [Pts_OutSms]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_OutSms]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_OutSms](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[TargetPhone] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[Message] [nvarchar](200) COLLATE Chinese_PRC_CI_AS NULL,
	[IsSend] [bit] NOT NULL,
	[FailReason] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[FailTime] [datetime] NULL,
	[SubmitTime] [datetime] NOT NULL,
 CONSTRAINT [PK_HWSite_OutSms] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_GetUserAgent]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetUserAgent]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Accounts_GetUserAgent]
@UserID int
AS
--得到一个帐号的代理人用户ID
SELECT AgentUserID FROM Accounts_Users WHERE UserID = @UserID
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_MergeProjectState]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_MergeProjectState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_MergeProjectState]
@FromStateID int,
@ToStateID int
AS
Update Projects Set ProjectStateID=@ToStateID where ProjectStateID=@FromStateID



' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_GetRepresentedUsers]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetRepresentedUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Accounts_GetRepresentedUsers]
@UserID int
AS
--得到当前用户所代理的用户的列表
SELECT * FROM v_Accounts_Users WHERE UserID IN (SELECT UserID FROM Accounts_Users WHERE AgentUserID = @UserID)' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_RemoveRecordAttachment]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveRecordAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_RemoveRecordAttachment]
@AttachmentID int
AS
Delete from Pts_RecordAttachments where AttachmentID = @AttachmentID



' 
END
GO
/****** 对象:  Table [Pts_ProblemFieldHiddenState]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemFieldHiddenState]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemFieldHiddenState](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[ProblemStateID] [int] NOT NULL,
	[FieldID] [int] NOT NULL,
	[Workgroups] [nvarchar](100) COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Pts_ProblemFieldHiddenState] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_ProblemFieldHighlightDisplayState]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemFieldHighlightDisplayState]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemFieldHighlightDisplayState](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[FieldID] [int] NOT NULL,
	[FromStateID] [int] NOT NULL,
	[ToStateID] [int] NOT NULL,
 CONSTRAINT [PK_Pts_ProblemFieldHighlightDisplayState] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)
)
END
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_ProblemFieldHighlightDisplayState', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'哪些状态下需要突出显示字段' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_ProblemFieldHighlightDisplayState'
GO
/****** 对象:  StoredProcedure [sp_Pts_ChangeProblemStateOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProblemStateOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE   procedure [sp_Pts_ChangeProblemStateOrder]
(
     @ProblemStateID int,
     @MoveUp bit
)
AS
BEGIN
--获取ProjectID
Declare @ProjectID int
Select @ProjectID = ProjectID From Pts_ProblemState where ProblemStateID = @ProblemStateID
if @ProjectID is null
	RETURN
--修改顺序
DECLARE @currentSortValue int
DECLARE @replaceSortValue int

-- Get the current sort order
SELECT @currentSortValue = ListOrder FROM Pts_ProblemState WHERE ProblemStateID = @ProblemStateID

-- Move the item up or down?
IF (@MoveUp = 1)
  BEGIN
    IF (@currentSortValue != 1)
      BEGIN
        SET @replaceSortValue = @currentSortValue - 1

        UPDATE Pts_ProblemState SET ListOrder = @currentSortValue WHERE ProjectID = @ProjectID AND ListOrder = @replaceSortValue
        UPDATE Pts_ProblemState SET ListOrder = @replaceSortValue WHERE ProblemStateID= @ProblemStateID
      END
  END

ELSE
  BEGIN
    IF (@currentSortValue < (SELECT MAX(ListOrder) FROM Pts_ProblemState))
    BEGIN
      SET @replaceSortValue = @currentSortValue + 1

      UPDATE Pts_ProblemState SET ListOrder = @currentSortValue WHERE ProjectID = @ProjectID AND  ListOrder = @replaceSortValue
      UPDATE Pts_ProblemState SET ListOrder = @replaceSortValue WHERE ProblemStateID= @ProblemStateID
    END
  END
END



' 
END
GO
/****** 对象:  StoredProcedure [sp_GetRecordByPage]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_GetRecordByPage]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_GetRecordByPage]
(
@sTable nvarchar(100),  --表名
@sPkey nvarchar(50),            --主键(一定要有)
@sField nvarchar(1000)=''*'',     --字段
@iPageCurr int,                 --当前页数
@iPageSize int,                 --每页记录数
@sCondition nvarchar(1000),  --条件(不需要where)
@sOrder nvarchar(100) ,         --排序(不需要order by,需要asc和desc字符)
@Counts int=0 output,           --记录条数(已有值:外部赋值,0执行count)
@pageCount int=1 output     --查询结果分页后的总页数
)
AS
SET NOCOUNT ON

DECLARE @sC1 nvarchar(1000),@sC2 nvarchar(1000)
DECLARE @iAsc int,@iDesc int,@iType tinyint
DECLARE @sT1 nvarchar(1000),@sT2 nvarchar(1000),@sT3 nvarchar(1000),@sT4 nvarchar(1000),@sSQL nvarchar(4000)

/*----------------------判断where 条件是否空值-------------------*/
IF LEN(@sCondition)>2
 SELECT @sC1='' WHERE ''+@sCondition+'' '', @sC2='' WHERE ''+@sCondition+'' AND ''
 ELSE
 BEGIN
  SELECT @sC1='''', @sC2='' WHERE ''
 END


SELECT @sT1=UPPER(@sOrder), @sT2=@sOrder, @iType=0, @sOrder='''', @sT4=UPPER(@sPkey)

/*-----------------------获取查询的数据行数---------------------*/
IF LEN(@sT2)>2
BEGIN
 SELECT @iAsc=0, @iDesc=0
 IF @sT4=SUBSTRING(@sT1,0,LEN(@sT4)) --存在主建
     BEGIN
  SELECT @iAsc=CHARINDEX(''ASC'',@sT1), @iDesc=CHARINDEX(''DESC'',@sT1)
 END

 IF (@iAsc>0 and @iDesc=0) OR ((@iAsc>0 AND @iDesc>0) AND (@iAsc<@iDesc))
  SELECT @iType=1, @sT3=''>(SELECT MAX(''
 ELSE IF (@iAsc=0 and @iDesc>0) OR ((@iAsc>0 AND @iDesc>0) AND (@iAsc>@iDesc))
 BEGIN
  SELECT @iType=1, @sT3=''<(SELECT MIN(''
 END
 SET @sOrder='' ORDER BY ''+@sT2
END

/*-------------------------获取查询的数据行数----------------------*/

--IF (@Counts<1)
--BEGIN
 --SET @sSQL=''SELECT @Counts=Count(0) FROM ''+@sTable+@sC1
 --EXEC sp_executesql @sSQL,N''@Counts int OUT'',@Counts OUT 
--END

SET @sSQL=''SELECT @Counts=Count(0) FROM ''+@sTable+@sC1
EXEC sp_executesql @sSQL,N''@Counts int OUT'',@Counts OUT

SET @pageCount=(@Counts+@ipageSize-1)/@ipageSize

IF @iPageCurr>@pageCount
 SET @iPageCurr=@pageCount

SELECT @iPageCurr=(CASE WHEN @Counts<(@iPageCurr-1)*@iPageSize THEN CEILING(@Counts/@iPageSize) WHEN @iPageCurr<1 THEN 1 ELSE @iPageCurr END)

IF (@iPageCurr>1) AND (@iType=1)
 SET @sSQL=''SELECT TOP ''+CAST(@iPageSize AS nvarchar)+'' ''+@sField+'' FROM ''+@sTable+@sC2+@sPkey+@sT3+@sPkey+'') FROM (SELECT TOP ''+CAST((@iPageCurr-1)*@iPageSize AS nvarchar)+'' ''+@sPkey+'' FROM ''+@sTable+@sC1+@sOrder+'') AS tbTemp)''+@sOrder
ELSE IF (@iPageCurr>1) AND (@iType=0)
 SET @sSQL=''SELECT ''+@sField+'' FROM ''+@sTable+@sC2+@sPkey+'' IN (SELECT TOP ''+CAST(@iPageSize AS nvarchar)+'' ''+@sPkey+'' FROM ''+@sTable+@sC2+@sPkey+'' NOT IN(SELECT TOP ''+CAST((@iPageCurr-1)*@iPageSize AS nvarchar)+'' ''+@sPkey+'' FROM ''+@sTable+@sC1+@sOrder+'')''+@sOrder+'')''+@sOrder
ELSE
BEGIN
    SET @sSQL=''SELECT TOP ''+CAST(@iPageSize AS nvarchar)+'' ''+@sField+'' FROM ''+@sTable+@sC1+@sOrder
END

EXEC(@sSQL)

--print @sSQL
--print @Counts
--print @pageCount

SET NOCOUNT OFF 

' 
END
GO
/****** 对象:  Table [Pts_FavoriteLinks]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_FavoriteLinks]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_FavoriteLinks](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[UserID] [int] NOT NULL,
	[Title] [nvarchar](100) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Link] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Category] [int] NULL,
	[CreateTime] [datetime] NULL,
	[ListOrder] [int] NOT NULL,
 CONSTRAINT [PK_Pts_FavoriteLinks] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Pts_SearchProblem]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_SearchProblem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE   Procedure [sp_Pts_SearchProblem]
@KeyWord nvarchar(255),
@SearchType int,
@ProjectID int
AS

----提交的问题
if (@SearchType = 1)
BEGIN
	if (@ProjectID <= 0)
		select ProblemID,ProjectName,ProjectID,Title,CreateUserID,CreateUserName,CreateTime,IsClosed from v_Pts_Problems where CreateUserName like @KeyWord;
	else
		select ProblemID,ProjectName,ProjectID,Title,CreateUserID,CreateUserName,CreateTime,IsClosed from v_Pts_Problems where ProjectID = @ProjectID AND CreateUserName like @KeyWord
	
	RETURN	
END
----参与的问题
if (@SearchType = 2)
BEGIN
	if (@ProjectID <= 0)
		select distinct(ProblemID),ProjectName,ProjectID,Title,CreateUserID,CreateUserName,CreateTime,IsClosed from v_Pts_Problems where CreateUserName like @KeyWord or ProblemID in 
			(select distinct(ProblemID) From v_Pts_Records Where CreateUserName like @KeyWord)
	else
		select distinct(ProblemID),ProjectName,ProjectID,Title,CreateUserID,CreateUserName,CreateTime,IsClosed from v_Pts_Problems where  ProjectID = @ProjectID AND (CreateUserName like @KeyWord or ProblemID in 
			(select distinct(ProblemID) From v_Pts_Records Where CreateUserName like @KeyWord))
	
	RETURN	
END

----根据问题标题查询

if (@SearchType = 3)
BEGIN
	if (@ProjectID <= 0)
		select distinct(ProblemID),ProjectName,ProjectID,Title,CreateUserID,CreateUserName,CreateTime,IsClosed from v_Pts_Problems where Title like @KeyWord;
	else
		select distinct(ProblemID),ProjectName,ProjectID,Title,CreateUserID,CreateUserName,CreateTime,IsClosed from v_Pts_Problems where ProjectID = @ProjectID AND Title like @KeyWord
	
	RETURN
END



----问题所有字段关键字
if (@SearchType = 4)
BEGIN
	if (@ProjectID <= 0)
				select distinct(ProblemID),ProjectName,ProjectID,Title,CreateUserID,CreateUserName,CreateTime,IsClosed from v_Pts_Problems Where 
					Title like @KeyWord or 
					BigText1 like @KeyWord or
				BigText1 like @KeyWord or 
				BigText2 like @KeyWord or 
				BigText3 like @KeyWord or 
				BigText4 like @KeyWord or 
				BigText5 like @KeyWord or 
				Text1 like @KeyWord or 
				Text2 like @KeyWord or 
				Text3 like @KeyWord or 
				Text4 like @KeyWord or 
				Text5 like @KeyWord or 
				Text6 like @KeyWord or 
				Text7 like @KeyWord or 
				Text8 like @KeyWord or 
				Text9 like @KeyWord or 
				Text10 like @KeyWord or 
				Text11 like @KeyWord or 
				Text12 like @KeyWord or 
				Text13 like @KeyWord or 
				Text14 like @KeyWord or 
				Text15 like @KeyWord or 
				Text16 like @KeyWord or 
				Text17 like @KeyWord or 
				Text18 like @KeyWord or 
				Text19 like @KeyWord or 
				Text20 like @KeyWord or 
				Select1 like @KeyWord or 
				Select2 like @KeyWord or 
				Select3 like @KeyWord or 
				Select4 like @KeyWord or 
				Select5 like @KeyWord or 
				Select6 like @KeyWord or 
				Select7 like @KeyWord or 
				Select8 like @KeyWord or 
				Select9 like @KeyWord or 
				Select10 like @KeyWord or 
				Url1 like @KeyWord or 
				Url2 like @KeyWord or 
				Url3 like @KeyWord or 
				Url4 like @KeyWord or 
				Url5 like @KeyWord or 
				BigText6 like @KeyWord or 
				BigText7 like @KeyWord or 
				BigText8 like @KeyWord or 
				BigText9 like @KeyWord or 
				BigText10 like @KeyWord or 
				Select11 like @KeyWord or 
				Select13 like @KeyWord or 
				Select12 like @KeyWord or 
				Select14 like @KeyWord or 
				Select15 like @KeyWord or 
				Email1 like @KeyWord or 
				Email2 like @KeyWord or 
				Email3 like @KeyWord or 
				Email4 like @KeyWord or 
				Email5 like @KeyWord
	else
	select distinct(ProblemID),ProjectName,ProjectID,Title,CreateUserID,CreateUserName,CreateTime,IsClosed from v_Pts_Problems Where ProjectID = @ProjectID AND
			(
					Title like @KeyWord or 
					BigText1 like @KeyWord or
				BigText1 like @KeyWord or 
				BigText2 like @KeyWord or 
				BigText3 like @KeyWord or 
				BigText4 like @KeyWord or 
				BigText5 like @KeyWord or 
				Text1 like @KeyWord or 
				Text2 like @KeyWord or 
				Text3 like @KeyWord or 
				Text4 like @KeyWord or 
				Text5 like @KeyWord or 
				Text6 like @KeyWord or 
				Text7 like @KeyWord or 
				Text8 like @KeyWord or 
				Text9 like @KeyWord or 
				Text10 like @KeyWord or 
				Text11 like @KeyWord or 
				Text12 like @KeyWord or 
				Text13 like @KeyWord or 
				Text14 like @KeyWord or 
				Text15 like @KeyWord or 
				Text16 like @KeyWord or 
				Text17 like @KeyWord or 
				Text18 like @KeyWord or 
				Text19 like @KeyWord or 
				Text20 like @KeyWord or 
				Select1 like @KeyWord or 
				Select2 like @KeyWord or 
				Select3 like @KeyWord or 
				Select4 like @KeyWord or 
				Select5 like @KeyWord or 
				Select6 like @KeyWord or 
				Select7 like @KeyWord or 
				Select8 like @KeyWord or 
				Select9 like @KeyWord or 
				Select10 like @KeyWord or 
				Url1 like @KeyWord or 
				Url2 like @KeyWord or 
				Url3 like @KeyWord or 
				Url4 like @KeyWord or 
				Url5 like @KeyWord or 
				BigText6 like @KeyWord or 
				BigText7 like @KeyWord or 
				BigText8 like @KeyWord or 
				BigText9 like @KeyWord or 
				BigText10 like @KeyWord or 
				Select11 like @KeyWord or 
				Select13 like @KeyWord or 
				Select12 like @KeyWord or 
				Select14 like @KeyWord or 
				Select15 like @KeyWord or 
				Email1 like @KeyWord or 
				Email2 like @KeyWord or 
				Email3 like @KeyWord or 
				Email4 like @KeyWord or 
				Email5 like @KeyWord)
	return 
	
	end





----问题所有字段和处理记录关键字

if (@SearchType = 5)
BEGIN
	if (@ProjectID <= 0)
			select distinct(ProblemID),ProjectName,ProjectID,Title,CreateUserID,CreateUserName,CreateTime,IsClosed from v_Pts_Problems where
				Title like @KeyWord or 
				ProblemID in (Select ProblemID From Pts_Records where Title like @KeyWord or Content like @KeyWord) or
				BigText1 like @KeyWord or
			BigText1 like @KeyWord or 
			BigText2 like @KeyWord or 
			BigText3 like @KeyWord or 
			BigText4 like @KeyWord or 
			BigText5 like @KeyWord or 
			Text1 like @KeyWord or 
			Text2 like @KeyWord or 
			Text3 like @KeyWord or 
			Text4 like @KeyWord or 
			Text5 like @KeyWord or 
			Text6 like @KeyWord or 
			Text7 like @KeyWord or 
			Text8 like @KeyWord or 
			Text9 like @KeyWord or 
			Text10 like @KeyWord or 
			Text11 like @KeyWord or 
			Text12 like @KeyWord or 
			Text13 like @KeyWord or 
			Text14 like @KeyWord or 
			Text15 like @KeyWord or 
			Text16 like @KeyWord or 
			Text17 like @KeyWord or 
			Text18 like @KeyWord or 
			Text19 like @KeyWord or 
			Text20 like @KeyWord or 
			Select1 like @KeyWord or 
			Select2 like @KeyWord or 
			Select3 like @KeyWord or 
			Select4 like @KeyWord or 
			Select5 like @KeyWord or 
			Select6 like @KeyWord or 
			Select7 like @KeyWord or 
			Select8 like @KeyWord or 
			Select9 like @KeyWord or 
			Select10 like @KeyWord or 
			Url1 like @KeyWord or 
			Url2 like @KeyWord or 
			Url3 like @KeyWord or 
			Url4 like @KeyWord or 
			Url5 like @KeyWord or 
			BigText6 like @KeyWord or 
			BigText7 like @KeyWord or 
			BigText8 like @KeyWord or 
			BigText9 like @KeyWord or 
			BigText10 like @KeyWord or 
			Select11 like @KeyWord or 
			Select13 like @KeyWord or 
			Select12 like @KeyWord or 
			Select14 like @KeyWord or 
			Select15 like @KeyWord or 
			Email1 like @KeyWord or 
			Email2 like @KeyWord or 
			Email3 like @KeyWord or 
			Email4 like @KeyWord or 
			Email5 like @KeyWord
	else
		select distinct(ProblemID),ProjectName,ProjectID,Title,CreateUserID,CreateUserName,CreateTime,IsClosed from v_Pts_Problems where ProjectID = @ProjectID AND
		(
				Title like @KeyWord or 
				ProblemID in (Select ProblemID From Pts_Records where Title like @KeyWord or Content like @KeyWord) or
				BigText1 like @KeyWord or
			BigText1 like @KeyWord or 
			BigText2 like @KeyWord or 
			BigText3 like @KeyWord or 
			BigText4 like @KeyWord or 
			BigText5 like @KeyWord or 
			Text1 like @KeyWord or 
			Text2 like @KeyWord or 
			Text3 like @KeyWord or 
			Text4 like @KeyWord or 
			Text5 like @KeyWord or 
			Text6 like @KeyWord or 
			Text7 like @KeyWord or 
			Text8 like @KeyWord or 
			Text9 like @KeyWord or 
			Text10 like @KeyWord or 
			Text11 like @KeyWord or 
			Text12 like @KeyWord or 
			Text13 like @KeyWord or 
			Text14 like @KeyWord or 
			Text15 like @KeyWord or 
			Text16 like @KeyWord or 
			Text17 like @KeyWord or 
			Text18 like @KeyWord or 
			Text19 like @KeyWord or 
			Text20 like @KeyWord or 
			Select1 like @KeyWord or 
			Select2 like @KeyWord or 
			Select3 like @KeyWord or 
			Select4 like @KeyWord or 
			Select5 like @KeyWord or 
			Select6 like @KeyWord or 
			Select7 like @KeyWord or 
			Select8 like @KeyWord or 
			Select9 like @KeyWord or 
			Select10 like @KeyWord or 
			Url1 like @KeyWord or 
			Url2 like @KeyWord or 
			Url3 like @KeyWord or 
			Url4 like @KeyWord or 
			Url5 like @KeyWord or 
			BigText6 like @KeyWord or 
			BigText7 like @KeyWord or 
			BigText8 like @KeyWord or 
			BigText9 like @KeyWord or 
			BigText10 like @KeyWord or 
			Select11 like @KeyWord or 
			Select13 like @KeyWord or 
			Select12 like @KeyWord or 
			Select14 like @KeyWord or 
			Select15 like @KeyWord or 
			Email1 like @KeyWord or 
			Email2 like @KeyWord or 
			Email3 like @KeyWord or 
			Email4 like @KeyWord or 
			Email5 like @KeyWord)
		
		return
		
end




' 
END
GO
/****** 对象:  Table [Pts_ProblemVisitHistory]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemVisitHistory]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemVisitHistory](
	[VisitID] [int] IDENTITY(1,1) NOT NULL,
	[UserID] [int] NOT NULL,
	[ProblemID] [int] NOT NULL,
	[VisitIndex] [int] NOT NULL,
 CONSTRAINT [PK_Pts_ProblemVisitHistory] PRIMARY KEY CLUSTERED 
(
	[VisitID] ASC
)
)
END
GO
/****** 对象:  Table [Kb_Categories]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Kb_Categories]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Kb_Categories](
	[CategoryID] [int] IDENTITY(1,1) NOT NULL,
	[CategoryName] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[ParentID] [int] NOT NULL,
	[CreateDate] [datetime] NOT NULL,
	[ArticleCount] [int] NOT NULL,
	[LastUpdate] [datetime] NULL,
	[Path] [varchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[AllowedUserGroups] [nvarchar](1000) COLLATE Chinese_PRC_CI_AS NULL,
	[IsPublic] [bit] NOT NULL,
	[RequireReview] [bit] NOT NULL,
 CONSTRAINT [PK_Kb_Catalogs] PRIMARY KEY CLUSTERED 
(
	[CategoryID] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Stat_RunQueryString]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Stat_RunQueryString]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE  PROCEDURE [sp_Pts_Stat_RunQueryString]
@QueryString ntext
AS
exec sp_executesql @QueryString




' 
END
GO
/****** 对象:  Table [Pts_RelatedUsers]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_RelatedUsers]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_RelatedUsers](
	[ProblemID] [int] NOT NULL,
	[UserID] [int] NOT NULL,
 CONSTRAINT [PK_Pts_RelatedUsers] PRIMARY KEY CLUSTERED 
(
	[ProblemID] ASC,
	[UserID] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Pts_CreateNewUserField]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateNewUserField]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE  Procedure [sp_Pts_CreateNewUserField]
@ProjectID int,
@FieldName varchar(60),
@Label	nvarchar(25),
@FieldType varchar(20),
@ListOrder int,
@Required bit,
@DefaultValue nvarchar(255) = null,
@SelectItems nvarchar(2000) = null,
@FieldHelp nvarchar(255) = null,
@FormatString nvarchar(255) = null,
@HtmlEdit bit,
@MaxLength int = null,
@Width	varchar(50) = null,
@Height varchar(50) = null,
@GlobalSelectID int = null,
@UseSelectList	bit = false,
@UseGlobalSelect bit = false,
@IsSystem bit = false,
@VisibleWhenNew bit = true,
@VisibleWhenEdit bit = true,
@VisibleInList bit = false
AS
Insert into Pts_DbDirectory
	(
	ProjectID,
	FieldName,
	Label,
	FieldType,
	InUse,
	ListOrder,
	Required,
	DefaultValue,
	SelectItems,
	FieldHelp,
	FormatString,
	HtmlEdit,
	MaxLength,
	Width,
	Height,
	GlobalSelectID,
	UseSelectList,
	UseGlobalSelect,
	[ReadableToAll],
	[EditableToAll],
	[IsSystem],
	[VisibleWhenNew],
	[VisibleWhenEdit],
	[VisibleInList]
	)
values
	(
	@ProjectID,
	@FieldName,
	@Label,
	@FieldType,
	1,
	@ListOrder,
	@Required,
	@DefaultValue,
	@SelectItems,
	@FieldHelp,
	@FormatString,
	@HtmlEdit,
	@MaxLength,
	@Width,
	@Height,
	@GlobalSelectID,
	@UseSelectList,
	@UseGlobalSelect,
	1,
	1,
	0,
	@VisibleWhenNew,
	@VisibleWhenEdit,
	@VisibleInList
	)

SELECT @@IDENTITY
' 
END
GO
/****** 对象:  Table [Pts_Subscription]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_Subscription]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_Subscription](
	[SubscribeID] [int] IDENTITY(1,1) NOT NULL,
	[UserID] [int] NOT NULL,
	[TargetType] [int] NOT NULL,
	[TargetID] [int] NOT NULL,
	[EventType] [int] NOT NULL,
	[Params] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Pts_Subscribe] PRIMARY KEY CLUSTERED 
(
	[SubscribeID] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Pts_UpdateProblemOriginInfoHistoryID]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemOriginInfoHistoryID]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [sp_Pts_UpdateProblemOriginInfoHistoryID]
@ProblemID int,
@HistoryID int
AS
Update Pts_Problems Set HistoryID=@HistoryID Where ProblemID=@ProblemID
' 
END
GO
/****** 对象:  Table [Pts_IncomingSms]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_IncomingSms]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_IncomingSms](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[RecvTime] [datetime] NULL,
	[SrcPhone] [nvarchar](20) COLLATE Chinese_PRC_CI_AS NULL,
	[Content] [nvarchar](100) COLLATE Chinese_PRC_CI_AS NULL,
	[SmsTime] [nvarchar](20) COLLATE Chinese_PRC_CI_AS NULL,
	[IsAutoProcessed] [bit] NOT NULL,
	[IsReviewed] [bit] NOT NULL,
	[ReviewedBy] [nvarchar](15) COLLATE Chinese_PRC_CI_AS NULL,
	[Mark] [nvarchar](15) COLLATE Chinese_PRC_CI_AS NULL,
	[ReviewTime] [datetime] NULL,
	[ProblemID] [int] NULL,
 CONSTRAINT [PK_Pts_IncomingSms] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)
)
END
GO
/****** 对象:  Table [Common_Config]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Common_Config]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Common_Config](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[AllowSelfRegister] [bit] NOT NULL,
	[AutoSetNewUserNormal] [bit] NOT NULL,
	[RequireSmtpAuthentication] [bit] NOT NULL,
	[MailServer] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[MailUsername] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[MailPassword] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[SiteUrl] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[CompanyName] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[CompanyUrl] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[AdminMail] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[SenderMail] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[ExtendedSettings] [ntext] COLLATE Chinese_PRC_CI_AS NOT NULL
)
END
GO
/****** 对象:  Table [Accounts_AccessLists]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Accounts_AccessLists]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Accounts_AccessLists](
	[AccessListID] [int] IDENTITY(1,1) NOT NULL,
	[AccessListName] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NOT NULL,
 CONSTRAINT [PK_Pts_AccessList] PRIMARY KEY CLUSTERED 
(
	[AccessListID] ASC
)
)
END
GO
/****** 对象:  Table [Kb_Articles]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Kb_Articles]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Kb_Articles](
	[ArticleID] [int] IDENTITY(1,1) NOT NULL,
	[CategoryID] [int] NOT NULL,
	[Title] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[KeyWords] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[Summary] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[Content] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[CreateDate] [datetime] NULL,
	[UpdateDate] [datetime] NULL,
	[CreateUser] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[UpdateUser] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[NumRead] [int] NULL,
	[IsRepost] [bit] NULL,
	[NumRatings] [int] NOT NULL,
	[RateValue] [float] NOT NULL,
	[ArticleStatus] [int] NULL,
	[Source] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[SourceUrl] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[ValueLevel] [tinyint] NULL,
	[Difficulty] [tinyint] NULL,
	[RelateProblemID] [int] NULL,
	[OriginAuthor] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[CreateUserID] [int] NOT NULL,
	[NumComment] [int] NULL,
	[IsReviewed] [bit] NOT NULL,
	[ReviewedBy] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[IsPublic] [bit] NOT NULL,
	[Note1] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[Note2] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[Note3] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[Note4] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[Note5] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Kb_Articles] PRIMARY KEY CLUSTERED 
(
	[ArticleID] ASC
)
)
END
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Kb_Articles', N'COLUMN',N'CreateUserID'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'文章作者的ID' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Kb_Articles', @level2type=N'COLUMN',@level2name=N'CreateUserID'
GO
/****** 对象:  StoredProcedure [sp_Pts_UpdateRecord]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateRecord]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'


CREATE Procedure [sp_Pts_UpdateRecord]
	@RecordID int,
	@IsKeyRecord bit,
	@RecordType int,
	@AssignTo int,
	@Title nvarchar(255),
	@Content text
AS
	Update
		Pts_Records
	Set
		IsKeyRecord = @IsKeyRecord,
		RecordType = @RecordType,
		AssignTo = @AssignTo,
		Title = @Title,
		Content = @Content
	Where
		RecordID = @RecordID
	
	Declare @ProblemID int
	Select @ProblemID = ProblemID From Pts_Records Where RecordID = @RecordID

	Update Pts_Problems Set AssignedTo = @AssignTo Where ProblemID = @ProblemID


' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_UpdateRecordProperty]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateRecordProperty]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--修改属性
Create Procedure [sp_Pts_UpdateRecordProperty]
	@RecordID int,
	@IsKeyRecord bit,
	@RecordType int
AS
	Update Pts_Records Set
		IsKeyRecord = @IsKeyRecord,
		RecordType = @RecordType
	Where
		RecordID = @RecordID




' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_UpdateUserField]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateUserField]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE  Procedure [sp_Pts_UpdateUserField]
@ID int,
@Label	nvarchar(100),
@ListOrder int,
@InUse	bit,
@Required bit,
@DefaultValue ntext = null,
@SelectItems nvarchar(2000) = null,
@FieldHelp nvarchar(255) = null,
@FormatString nvarchar(255) = null,
@HtmlEdit bit,
@MaxLength int = null,
@Width	varchar(50) = null,
@Height varchar(50) = null,
@GlobalSelectID int = null,
@UseSelectList	bit = false,
@UseGlobalSelect bit = false,
@VisibleWhenNew bit = true,
@VisibleWhenEdit bit = true,
@VisibleInList bit = false
AS

Update Pts_DbDirectory
Set
	Label = @Label,
	ListOrder = @ListOrder,
	InUse = @InUse,
	Required = @Required,
	DefaultValue = @DefaultValue,
	SelectItems = @SelectItems,
	FieldHelp = @FieldHelp,
	FormatString = @FormatString,
	HtmlEdit = @HtmlEdit,
	MaxLength = @MaxLength,
	Width = @Width,
	Height = @Height,
	GlobalSelectID = @GlobalSelectID,
	UseSelectList = @UseSelectList,
	UseGlobalSelect = @UseGlobalSelect,
	[VisibleWhenNew] = @VisibleWhenNew,
	[VisibleWhenEdit] = @VisibleWhenEdit,
	[VisibleInList] = @VisibleInList
Where 
	[ID] = @ID' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Stat_ByAssignee]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Stat_ByAssignee]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_Stat_ByAssignee]
@ProjectID int
AS
if @ProjectID=0 
BEGIN
	Select count(*) as ProblemCount,AssignedTo
	from v_Pts_Problems where IsClosed=0
	group by AssignedTo
END
ELSE
BEGIN
	Select count(*) as ProblemCount,AssignedTo
	from v_Pts_Problems where IsClosed=0 and ProjectID=@ProjectID
	group by AssignedTo
END
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_stat_CurrentProjectStat]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_stat_CurrentProjectStat]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE procedure [sp_Pts_stat_CurrentProjectStat]
@ProjectID as int,
@TotalProblemNum int output,
@OpeningProblemNum int output,
@ClosedProblemNum int output,
@TotalRecordNum int output,
@AvgProcessTime int output	--as minute
AS
	--TotalProblemNum
	Select @TotalProblemNum=Count(ProblemID) From Pts_Problems Where ProjectID = @ProjectID
	--open
	Select @OpeningProblemNum = Count(ProblemID) From v_Pts_Problems Where ProjectID = @ProjectID AND IsClosed=0
	--closed
	Select @ClosedProblemNum = Count(ProblemID) From v_Pts_Problems Where ProjectID = @ProjectID AND IsClosed=1
	--total record num
	select @TotalRecordNum = Count(RecordID) From Pts_Records Where ProblemID in
		(Select ProblemID From Pts_Problems Where ProjectID = @ProjectID)
	--avage process time
	Select @AvgProcessTime = AVG(DateDiff(mi, CreateTime, CloseTime)) From v_Pts_Problems Where ProjectID = @ProjectID AND IsClosed=1



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_RelatedUsers_Get]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RelatedUsers_Get]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_RelatedUsers_Get]
@ProblemID int
AS
SELECT * FROM v_Accounts_Users Where UserID IN (Select UserID FROM Pts_RelatedUsers WHERE ProblemID=@ProblemID)
ORDER BY DisplayName' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetRecordAttachmentList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetRecordAttachmentList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
create procedure [sp_Pts_GetRecordAttachmentList]
@RecordID int
AS
Select * From v_Pts_RecordAttachments Where RecordID = @RecordID

' 
END
GO
/****** 对象:  Table [Pts_ProblemTimerSettings]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemTimerSettings]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemTimerSettings](
	[ProblemTimerSettingID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectID] [int] NOT NULL,
	[Title] [nvarchar](100) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Description] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[Config] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Pts_ProblemTimerSettings] PRIMARY KEY CLUSTERED 
(
	[ProblemTimerSettingID] ASC
)
)
END
GO
IF NOT EXISTS (SELECT * FROM dbo.sysindexes WHERE id = OBJECT_ID(N'[Pts_ProblemTimerSettings]') AND name = N'IX_Pts_ProblemTimerSettings')
CREATE NONCLUSTERED INDEX [IX_Pts_ProblemTimerSettings] ON [Pts_ProblemTimerSettings] 
(
	[ProjectID] ASC
)
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProblemRecordAttachmentList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemRecordAttachmentList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'create procedure [sp_Pts_GetProblemRecordAttachmentList]
@ProblemID int
AS
Select * From v_Pts_RecordAttachments Where RecordID in (SELECT RecordID FROM Pts_Records WHERE ProblemID = @ProblemID)' 
END
GO
/****** 对象:  Table [Accounts_Department]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Accounts_Department]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Accounts_Department](
	[DeptID] [int] IDENTITY(1,1) NOT NULL,
	[DeptName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[ListOrder] [int] NOT NULL,
 CONSTRAINT [PK_Accounts_Department] PRIMARY KEY CLUSTERED 
(
	[DeptID] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetFieldEditableStates]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetFieldEditableStates]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		CuiLiang
-- Create date: 20070614
-- Description:	获得有权限编辑指定字段的状态条件
-- =============================================
CREATE PROCEDURE [sp_Pts_GetFieldEditableStates]
	-- Add the parameters for the stored procedure here
	@FieldID int
AS
BEGIN
	
	SELECT * 
	FROM Pts_ProblemFieldEditableState 
	WHERE FieldID = @FieldID
END
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_DeleteRecord]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteRecord]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





--删除
CREATE  Procedure [sp_Pts_DeleteRecord]
	@RecordID int
AS
	Delete From Pts_RecordAttachments Where RecordID = @RecordID
	Delete from Pts_Records where RecordID = @RecordID





' 
END
GO
/****** 对象:  Table [Accounts_AccessListItems]    脚本日期: 03/18/2009 13:25:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Accounts_AccessListItems]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Accounts_AccessListItems](
	[AccessListItemID] [int] IDENTITY(1,1) NOT NULL,
	[AccessListID] [int] NOT NULL,
	[StartIP] [bigint] NOT NULL,
	[EndIP] [bigint] NOT NULL,
	[Description] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[StartIPString] [nvarchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[EndIPString] [nvarchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL,
 CONSTRAINT [PK_Pts_AccessListItem] PRIMARY KEY CLUSTERED 
(
	[AccessListItemID] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_LastWeekNew]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_LastWeekNew]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_Filter_LastWeekNew]
@ProjectID int
AS
SELECT 
	* 
FROM v_Pts_Problems 
Where 
	ProjectID=@ProjectID 
	AND
	((CreateTime >= dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7-7,convert(varchar,getdate(),112))) 
	AND 
	(CreateTime < dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7,convert(varchar,getdate(),112))))

' 
END
GO
/****** 对象:  Table [Pts_ProjectCatalogs]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProjectCatalogs]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProjectCatalogs](
	[ProjectCatalogID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Description] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[ListOrder] [int] NOT NULL,
	[ParentID] [int] NOT NULL,
 CONSTRAINT [PK_Pts_ProjectCatalogs] PRIMARY KEY CLUSTERED 
(
	[ProjectCatalogID] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_2]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_2]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_Filter_Relate_2]
@ProjectID int,
@UserID int
AS
--我创建的未关闭事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	CreateUserID = @UserID
AND 
	IsClosed = 0

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_3]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_3]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_Filter_Relate_3]
@ProjectID int,
@UserID int
AS
--我创建的所有事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	CreateUserID = @UserID


' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_4]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_4]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_Filter_Relate_4]
@ProjectID int,
@UserID int
AS
--我创建的所有事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	LastUser = @UserID
AND
	IsClosed = 0

' 
END
GO
/****** 对象:  Table [Common_SystemConfig]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Common_SystemConfig]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Common_SystemConfig](
	[ConfigItem] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[ConfigValue] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Common_SystemConfig] PRIMARY KEY CLUSTERED 
(
	[ConfigItem] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_6]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_6]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Pts_Filter_Relate_6]
@ProjectID int,
@UserID int
AS
--最后处理过
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 1
AND
	LastUser = @UserID

' 
END
GO
/****** 对象:  Table [Pts_ProjectStates]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProjectStates]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProjectStates](
	[ProjectStateID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Description] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[IsCloseState] [bit] NOT NULL,
	[ListOrder] [int] NOT NULL,
 CONSTRAINT [PK_Pts_ProjectStates] PRIMARY KEY CLUSTERED 
(
	[ProjectStateID] ASC
)
)
END
GO
SET IDENTITY_INSERT [Pts_ProjectStates] ON
INSERT [Pts_ProjectStates] ([ProjectStateID], [Name], [Description], [IsCloseState], [ListOrder]) VALUES (1, N'正在跟踪', N'项目正在进行中', 0, 1)
INSERT [Pts_ProjectStates] ([ProjectStateID], [Name], [Description], [IsCloseState], [ListOrder]) VALUES (2, N'已完成', N'项目已经完成', 1, 2)
INSERT [Pts_ProjectStates] ([ProjectStateID], [Name], [Description], [IsCloseState], [ListOrder]) VALUES (3, N'已取消', N'项目已经取消', 1, 4)
SET IDENTITY_INSERT [Pts_ProjectStates] OFF
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_8]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_8]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'Create Procedure [sp_Pts_Filter_Relate_8]
@ProjectID int,
@UserID int
AS
--提交给工作组的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0
AND
	AssignedTo < 0
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_State_1]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_State_1]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_Filter_State_1]
@ProjectID int,
@UserID int
AS
--我创建的所有事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0


' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Time_a1]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_a1]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Pts_Filter_Time_a1]
@ProjectID int,
@UserID int
AS
--今天创建
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	CreateTime >= Convert(varchar(10),getdate(),120)
	--CreateTime >= dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7,convert(varchar,getdate(),112))

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_State_2]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_State_2]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_Filter_State_2]
@ProjectID int,
@UserID int
AS
--我创建的所有事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 1


' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Time_a2]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_a2]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Pts_Filter_Time_a2]
@ProjectID int,
@UserID int
AS
--今天关闭
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 1
AND
	CloseTime >= Convert(varchar(10),getdate(),120)
	--CreateTime >= dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7,convert(varchar,getdate(),112))

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_State_3]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_State_3]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_Filter_State_3]
@ProjectID int,
@UserID int
AS
--我创建的所有事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID


' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_ThisMonthNew]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_ThisMonthNew]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_Filter_ThisMonthNew]
@ProjectID int
AS
SELECT 
	* 
FROM v_Pts_Problems 
Where 
	ProjectID=@ProjectID 
	AND
	(CreateTime >= dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)))

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Time_a4]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_a4]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Pts_Filter_Time_a4]
@ProjectID int,
@UserID int
AS
--今天关闭
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 1
AND
	DateDiff(day, CloseTime, GetDate()) = 1
	--CreateTime >= dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7,convert(varchar,getdate(),112))

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_ThisWeekNew]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_ThisWeekNew]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_Filter_ThisWeekNew]
@ProjectID int
AS
SELECT 
	* 
FROM v_Pts_Problems 
Where 
	ProjectID=@ProjectID 
	AND
	(CreateTime >= dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7,convert(varchar,getdate(),112))) 

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetStateAllowedUsers]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetStateAllowedUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE    Procedure [sp_Pts_GetStateAllowedUsers]
@StateID int
AS

Select *
 from v_Accounts_Users 
Where
 UserState <> 4 
 AND 
 UserID in
(Select UserID From Pts_WorkgroupUsers Where WorkgroupID in
	(Select distinct WorkgroupID From Pts_ProblemStateTransferWorkgroup Where TransferID in
		(Select TransferID From Pts_ProblemStateTransfer Where FromProblemStateID = @StateID)
	)
)
And
UserState <> 4
Order By DeptName,DisplayName

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Time_a3]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_a3]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Pts_Filter_Time_a3]
@ProjectID int,
@UserID int
AS
--昨天创建
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND	
	DateDiff(day, CreateTime, GetDate()) = 1
	--CreateTime >= dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7,convert(varchar,getdate(),112))

' 
END
GO
/****** 对象:  Table [Accounts_PermissionCategories]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Accounts_PermissionCategories]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Accounts_PermissionCategories](
	[CategoryID] [int] IDENTITY(1,1) NOT NULL,
	[Description] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
 CONSTRAINT [PK_Accounts_PermissionCategories] PRIMARY KEY CLUSTERED 
(
	[CategoryID] ASC
)
)
END
GO
SET IDENTITY_INSERT [Accounts_PermissionCategories] ON
INSERT [Accounts_PermissionCategories] ([CategoryID], [Description]) VALUES (1, N'系统管理')
INSERT [Accounts_PermissionCategories] ([CategoryID], [Description]) VALUES (2, N'用户管理')
INSERT [Accounts_PermissionCategories] ([CategoryID], [Description]) VALUES (3, N'项目管理')
INSERT [Accounts_PermissionCategories] ([CategoryID], [Description]) VALUES (4, N'知识库管理')
SET IDENTITY_INSERT [Accounts_PermissionCategories] OFF
/****** 对象:  StoredProcedure [sp_Pts_Filter_Time_1]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_1]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_Filter_Time_1]
@ProjectID int,
@UserID int
AS
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	CreateTime >= dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7,convert(varchar,getdate(),112))

' 
END
GO
/****** 对象:  Table [Pts_ProblemRelatedArticles]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemRelatedArticles]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemRelatedArticles](
	[ProblemID] [int] NOT NULL,
	[ArticleID] [int] NOT NULL
)
END
GO
IF NOT EXISTS (SELECT * FROM dbo.sysindexes WHERE id = OBJECT_ID(N'[Pts_ProblemRelatedArticles]') AND name = N'IX_Pts_ProblemRelateArticles')
CREATE NONCLUSTERED INDEX [IX_Pts_ProblemRelateArticles] ON [Pts_ProblemRelatedArticles] 
(
	[ProblemID] ASC
)
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_CreatedByMe_Closed]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_CreatedByMe_Closed]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_Filter_Relate_CreatedByMe_Closed]
@ProjectID int,
@UserID int
AS
--我创建的未关闭事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	CreateUserID = @UserID
AND 
	IsClosed = 1

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Time_10]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_10]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_Filter_Time_10]
@ProjectID int,
@UserID int
AS
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0
AND
	DateDiff(d, GetDate(), Deadline) < 3

' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_ChangeDeptOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_ChangeDeptOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE  procedure [sp_Accounts_ChangeDeptOrder]
(
     @DeptID int,
     @MoveUp bit
)
AS
BEGIN
DECLARE @currentSortValue int
DECLARE @replaceSortValue int

-- Get the current sort order
SELECT @currentSortValue = ListOrder FROM Accounts_Department WHERE DeptID = @DeptID 

-- Move the item up or down?
IF (@MoveUp = 1)
  BEGIN
    IF (@currentSortValue != 1)
      BEGIN
        SET @replaceSortValue = @currentSortValue - 1

        UPDATE Accounts_Department SET ListOrder = @currentSortValue WHERE ListOrder = @replaceSortValue
        UPDATE Accounts_Department SET ListOrder = @replaceSortValue WHERE DeptID = @DeptID 
      END
  END

ELSE
  BEGIN
    IF (@currentSortValue < (SELECT MAX(ListOrder) FROM Accounts_Department ))
    BEGIN
      SET @replaceSortValue = @currentSortValue + 1

      UPDATE Accounts_Department SET ListOrder = @currentSortValue WHERE ListOrder = @replaceSortValue
      UPDATE Accounts_Department SET ListOrder = @replaceSortValue WHERE DeptID = @DeptID 
    END
  END
END
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Time_2]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_2]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_Filter_Time_2]
@ProjectID int,
@UserID int
AS
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND	
	IsClosed = 1
AND
	CloseTime >= dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7,convert(varchar,getdate(),112))

' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_CreateDepartment]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_CreateDepartment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [sp_Accounts_CreateDepartment]
@DeptName nvarchar(50)
AS

Insert into Accounts_Department (DeptName) Values (@DeptName)
exec sp_Accounts_ResetDepartmentOrder

Declare @DeptID int
Select @DeptID = @@IDENTITY
' 
END
GO
/****** 对象:  Table [Pts_GlobalSelects]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_GlobalSelects]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_GlobalSelects](
	[GlobalSelectID] [int] IDENTITY(1,1) NOT NULL,
	[SelectName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[Description] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[DefaultValue] [int] NULL,
 CONSTRAINT [PK_Pts_GlobalSelects] PRIMARY KEY CLUSTERED 
(
	[GlobalSelectID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_Coworkers]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_Coworkers]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_Coworkers](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[ProblemID] [int] NOT NULL,
	[UserID] [int] NOT NULL,
	[CreateTime] [datetime] NOT NULL,
	[IsWorked] [bit] NOT NULL,
	[SubmitTime] [datetime] NULL,
	[WorkRecordID] [int] NOT NULL,
	[ProblemStateID] [int] NOT NULL,
	[CurrentAssignee] [int] NOT NULL,
 CONSTRAINT [PK_Pts_Coworkers] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_ProblemFields]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemFields]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemFields](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[FieldName] [varchar](60) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[FieldType] [varchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL
)
END
GO
SET IDENTITY_INSERT [Pts_ProblemFields] ON
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1761, convert(text, N'MultiSelect1' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1762, convert(text, N'MultiSelect2' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1763, convert(text, N'MultiSelect3' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1764, convert(text, N'MultiSelect4' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1765, convert(text, N'MultiSelect5' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1771, convert(text, N'Int6' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1772, convert(text, N'Int7' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1773, convert(text, N'Int8' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1774, convert(text, N'Int9' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1775, convert(text, N'Int10' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1776, convert(text, N'Int11' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1777, convert(text, N'Int12' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1778, convert(text, N'Int13' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1779, convert(text, N'Int14' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1780, convert(text, N'Int15' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1781, convert(text, N'Int16' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1782, convert(text, N'Int17' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1783, convert(text, N'Int18' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1784, convert(text, N'Int19' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1785, convert(text, N'Int20' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1786, convert(text, N'Float6' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1787, convert(text, N'Float7' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1788, convert(text, N'Float8' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1789, convert(text, N'Float9' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1790, convert(text, N'Float10' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1791, convert(text, N'Float11' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1792, convert(text, N'Float12' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1793, convert(text, N'Float13' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1794, convert(text, N'Float14' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1795, convert(text, N'Float15' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1796, convert(text, N'Float16' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1797, convert(text, N'Float17' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1798, convert(text, N'Float18' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1799, convert(text, N'Float19' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1800, convert(text, N'Float20' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1801, convert(text, N'Date11' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1802, convert(text, N'Date12' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1803, convert(text, N'Date13' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1804, convert(text, N'Date14' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1805, convert(text, N'Date15' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1806, convert(text, N'Date16' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1807, convert(text, N'Date17' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1808, convert(text, N'Date18' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1809, convert(text, N'Date19' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1810, convert(text, N'Date20' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1811, convert(text, N'BigText11' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1812, convert(text, N'BigText12' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1813, convert(text, N'BigText13' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1814, convert(text, N'BigText14' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1815, convert(text, N'BigText15' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1816, convert(text, N'BigText16' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1817, convert(text, N'BigText17' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1818, convert(text, N'BigText18' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1819, convert(text, N'BigText19' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1820, convert(text, N'BigText20' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1821, convert(text, N'Select16' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1822, convert(text, N'Select17' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1823, convert(text, N'Select18' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1824, convert(text, N'Select19' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1825, convert(text, N'Select20' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1826, convert(text, N'Email6' collate Chinese_PRC_CI_AS), convert(text, N'Email' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1827, convert(text, N'Email7' collate Chinese_PRC_CI_AS), convert(text, N'Email' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1828, convert(text, N'Email8' collate Chinese_PRC_CI_AS), convert(text, N'Email' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1829, convert(text, N'Email9' collate Chinese_PRC_CI_AS), convert(text, N'Email' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1830, convert(text, N'Email10' collate Chinese_PRC_CI_AS), convert(text, N'Email' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1831, convert(text, N'MultiSelect6' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1832, convert(text, N'MultiSelect7' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1833, convert(text, N'MultiSelect8' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1834, convert(text, N'MultiSelect9' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1835, convert(text, N'MultiSelect10' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1836, convert(text, N'MultiSelect11' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1837, convert(text, N'MultiSelect12' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1838, convert(text, N'MultiSelect13' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1839, convert(text, N'MultiSelect14' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1840, convert(text, N'MultiSelect15' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1841, convert(text, N'MultiSelect16' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1842, convert(text, N'MultiSelect17' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1843, convert(text, N'MultiSelect18' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1844, convert(text, N'MultiSelect19' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1845, convert(text, N'MultiSelect20' collate Chinese_PRC_CI_AS), convert(text, N'MultiSelect' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1846, convert(text, N'YesNo6' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1847, convert(text, N'YesNo7' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1848, convert(text, N'YesNo8' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1849, convert(text, N'YesNo9' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1850, convert(text, N'YesNo10' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1851, convert(text, N'YesNo11' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1852, convert(text, N'YesNo12' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1853, convert(text, N'YesNo13' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1854, convert(text, N'YesNo14' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1855, convert(text, N'YesNo15' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1856, convert(text, N'YesNo16' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1857, convert(text, N'YesNo17' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1858, convert(text, N'YesNo18' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1859, convert(text, N'YesNo19' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1860, convert(text, N'YesNo20' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1861, convert(text, N'Url6' collate Chinese_PRC_CI_AS), convert(text, N'Url' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1862, convert(text, N'Url7' collate Chinese_PRC_CI_AS), convert(text, N'Url' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1863, convert(text, N'Url8' collate Chinese_PRC_CI_AS), convert(text, N'Url' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1864, convert(text, N'Url9' collate Chinese_PRC_CI_AS), convert(text, N'Url' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1865, convert(text, N'Url10' collate Chinese_PRC_CI_AS), convert(text, N'Url' collate Chinese_PRC_CI_AS))
GO
print 'Processed 100 total records'
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (961, convert(text, N'BigText1' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (962, convert(text, N'Int1' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (963, convert(text, N'Int2' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (964, convert(text, N'Int3' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (965, convert(text, N'Int4' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (966, convert(text, N'Int5' collate Chinese_PRC_CI_AS), convert(text, N'Integer' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (967, convert(text, N'Float1' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (968, convert(text, N'Float2' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (969, convert(text, N'Float3' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (970, convert(text, N'Float4' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (971, convert(text, N'Float5' collate Chinese_PRC_CI_AS), convert(text, N'Float' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (972, convert(text, N'Date1' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (973, convert(text, N'Date2' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (974, convert(text, N'Date3' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (975, convert(text, N'Date4' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (976, convert(text, N'Date5' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (977, convert(text, N'Date6' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (978, convert(text, N'Date7' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (979, convert(text, N'Date8' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (980, convert(text, N'Date9' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (981, convert(text, N'Date10' collate Chinese_PRC_CI_AS), convert(text, N'DateTime' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (982, convert(text, N'BigText2' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (983, convert(text, N'BigText3' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (984, convert(text, N'BigText4' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (985, convert(text, N'BigText5' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (986, convert(text, N'Text1' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (987, convert(text, N'Text2' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (988, convert(text, N'Text3' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (989, convert(text, N'Text4' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (990, convert(text, N'Text5' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (991, convert(text, N'Text6' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (992, convert(text, N'Text7' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (993, convert(text, N'Text8' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (994, convert(text, N'Text9' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (995, convert(text, N'Text10' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (996, convert(text, N'Text11' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (997, convert(text, N'Text12' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (998, convert(text, N'Text13' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (999, convert(text, N'Text14' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1000, convert(text, N'Text15' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1001, convert(text, N'Text16' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1002, convert(text, N'Text17' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1003, convert(text, N'Text18' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1004, convert(text, N'Text19' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1005, convert(text, N'Text20' collate Chinese_PRC_CI_AS), convert(text, N'Text' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1006, convert(text, N'Select1' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1007, convert(text, N'Select2' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1008, convert(text, N'Select3' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1009, convert(text, N'Select4' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1010, convert(text, N'Select5' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1011, convert(text, N'Select6' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1012, convert(text, N'Select7' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1013, convert(text, N'Select8' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1014, convert(text, N'Select9' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1015, convert(text, N'Select10' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1016, convert(text, N'YesNo1' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1017, convert(text, N'YesNo2' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1018, convert(text, N'YesNo3' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1019, convert(text, N'YesNo4' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1020, convert(text, N'YesNo5' collate Chinese_PRC_CI_AS), convert(text, N'YesNo' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1021, convert(text, N'Url1' collate Chinese_PRC_CI_AS), convert(text, N'Url' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1022, convert(text, N'Url2' collate Chinese_PRC_CI_AS), convert(text, N'Url' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1023, convert(text, N'Url3' collate Chinese_PRC_CI_AS), convert(text, N'Url' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1024, convert(text, N'Url4' collate Chinese_PRC_CI_AS), convert(text, N'Url' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1025, convert(text, N'Url5' collate Chinese_PRC_CI_AS), convert(text, N'Url' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1026, convert(text, N'BigText6' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1027, convert(text, N'BigText7' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1028, convert(text, N'BigText8' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1029, convert(text, N'BigText9' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1030, convert(text, N'BigText10' collate Chinese_PRC_CI_AS), convert(text, N'BigText' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1031, convert(text, N'Select11' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1032, convert(text, N'Select13' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1033, convert(text, N'Select12' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1034, convert(text, N'Select14' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1035, convert(text, N'Select15' collate Chinese_PRC_CI_AS), convert(text, N'Select' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1036, convert(text, N'Email1' collate Chinese_PRC_CI_AS), convert(text, N'Email' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1037, convert(text, N'Email2' collate Chinese_PRC_CI_AS), convert(text, N'Email' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1038, convert(text, N'Email3' collate Chinese_PRC_CI_AS), convert(text, N'Email' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1039, convert(text, N'Email4' collate Chinese_PRC_CI_AS), convert(text, N'Email' collate Chinese_PRC_CI_AS))
INSERT [Pts_ProblemFields] ([ID], [FieldName], [FieldType]) VALUES (1040, convert(text, N'Email5' collate Chinese_PRC_CI_AS), convert(text, N'Email' collate Chinese_PRC_CI_AS))
SET IDENTITY_INSERT [Pts_ProblemFields] OFF
/****** 对象:  StoredProcedure [sp_Pts_GetProblemByCode]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemByCode]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Pts_GetProblemByCode]
@ProblemCode nvarchar(50)
AS
Declare @ProblemID int
Select @ProblemID = ProblemID From v_Pts_Problems Where ProblemCode = @ProblemCode

Select @ProblemID 

' 
END
GO
/****** 对象:  Table [Accounts_Permissions]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Accounts_Permissions]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Accounts_Permissions](
	[PermissionID] [int] NOT NULL,
	[Description] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[CategoryID] [int] NOT NULL,
 CONSTRAINT [PK_Accounts_Permissions] PRIMARY KEY CLUSTERED 
(
	[PermissionID] ASC
)
)
END
GO
INSERT [Accounts_Permissions] ([PermissionID], [Description], [CategoryID]) VALUES (1001, N'修改系统参数', 1)
INSERT [Accounts_Permissions] ([PermissionID], [Description], [CategoryID]) VALUES (1101, N'创建用户', 2)
INSERT [Accounts_Permissions] ([PermissionID], [Description], [CategoryID]) VALUES (1102, N'修改用户', 2)
INSERT [Accounts_Permissions] ([PermissionID], [Description], [CategoryID]) VALUES (1103, N'删除用户', 2)
INSERT [Accounts_Permissions] ([PermissionID], [Description], [CategoryID]) VALUES (1104, N'角色管理', 2)
INSERT [Accounts_Permissions] ([PermissionID], [Description], [CategoryID]) VALUES (1201, N'添加项目', 3)
INSERT [Accounts_Permissions] ([PermissionID], [Description], [CategoryID]) VALUES (1202, N'修改项目信息', 3)
INSERT [Accounts_Permissions] ([PermissionID], [Description], [CategoryID]) VALUES (1301, N'管理知识库', 4)
INSERT [Accounts_Permissions] ([PermissionID], [Description], [CategoryID]) VALUES (1302, N'文章审核', 4)
/****** 对象:  Table [Accounts_RolePermissions]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Accounts_RolePermissions]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Accounts_RolePermissions](
	[RoleID] [int] NOT NULL,
	[PermissionID] [int] NOT NULL,
 CONSTRAINT [PK_Accounts_RolePermissions] PRIMARY KEY CLUSTERED 
(
	[RoleID] ASC,
	[PermissionID] ASC
)
)
END
GO
INSERT [Accounts_RolePermissions] ([RoleID], [PermissionID]) VALUES (1, 1001)
INSERT [Accounts_RolePermissions] ([RoleID], [PermissionID]) VALUES (1, 1101)
INSERT [Accounts_RolePermissions] ([RoleID], [PermissionID]) VALUES (1, 1102)
INSERT [Accounts_RolePermissions] ([RoleID], [PermissionID]) VALUES (1, 1103)
INSERT [Accounts_RolePermissions] ([RoleID], [PermissionID]) VALUES (1, 1104)
INSERT [Accounts_RolePermissions] ([RoleID], [PermissionID]) VALUES (1, 1201)
INSERT [Accounts_RolePermissions] ([RoleID], [PermissionID]) VALUES (1, 1202)
INSERT [Accounts_RolePermissions] ([RoleID], [PermissionID]) VALUES (1, 1301)
INSERT [Accounts_RolePermissions] ([RoleID], [PermissionID]) VALUES (1, 1302)
INSERT [Accounts_RolePermissions] ([RoleID], [PermissionID]) VALUES (2, 1101)
INSERT [Accounts_RolePermissions] ([RoleID], [PermissionID]) VALUES (2, 1103)
INSERT [Accounts_RolePermissions] ([RoleID], [PermissionID]) VALUES (3, 1201)
INSERT [Accounts_RolePermissions] ([RoleID], [PermissionID]) VALUES (3, 1202)
INSERT [Accounts_RolePermissions] ([RoleID], [PermissionID]) VALUES (4, 1301)
INSERT [Accounts_RolePermissions] ([RoleID], [PermissionID]) VALUES (4, 1302)
/****** 对象:  Table [Accounts_Users]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Accounts_Users]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Accounts_Users](
	[UserID] [int] IDENTITY(1,1) NOT NULL,
	[Password] [binary](20) NOT NULL,
	[EmailAddress] [varchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[DisplayName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[UserState] [int] NOT NULL,
	[DeptID] [int] NOT NULL,
	[Address] [nvarchar](280) COLLATE Chinese_PRC_CI_AS NULL,
	[OfficePhone] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[MobilePhone] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[MSN] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[PasswordNote] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[Remark] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[AccessListID] [int] NULL,
	[BindIP] [bigint] NULL,
	[BindIPString] [nvarchar](20) COLLATE Chinese_PRC_CI_AS NULL,
	[AgentUserID] [int] NULL,
	[ExtendedSettings] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[LoginName] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[ExpireDate] [datetime] NULL,
	[ExtIntData1] [int] NULL,
	[ExtIntData2] [int] NULL,
	[ExtIntData3] [int] NULL,
	[ExtIntData4] [int] NULL,
	[ExtStrData1] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[ExtStrData2] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[Attributes] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Accounts_Users] PRIMARY KEY CLUSTERED 
(
	[UserID] ASC
),
 CONSTRAINT [IX_Accounts_Users] UNIQUE NONCLUSTERED 
(
	[EmailAddress] ASC
)
)
END
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Accounts_Users', N'COLUMN',N'BindIP'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'限制用户的访问IP' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Accounts_Users', @level2type=N'COLUMN',@level2name=N'BindIP'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Accounts_Users', N'COLUMN',N'BindIPString'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'限制IP所对应的数字值' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Accounts_Users', @level2type=N'COLUMN',@level2name=N'BindIPString'
GO
/****** 对象:  Table [Accounts_UserRoles]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Accounts_UserRoles]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Accounts_UserRoles](
	[RoleID] [int] NOT NULL,
	[UserID] [int] NOT NULL,
 CONSTRAINT [PK_Accounts_UserRoles] PRIMARY KEY CLUSTERED 
(
	[RoleID] ASC,
	[UserID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_WorkgroupUsers]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_WorkgroupUsers]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_WorkgroupUsers](
	[WorkgroupID] [int] NOT NULL,
	[UserID] [int] NOT NULL,
	[IsManager] [bit] NOT NULL
)
END
GO
/****** 对象:  Table [Pts_Projects]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_Projects]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_Projects](
	[ProjectID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectCatalogID] [int] NOT NULL,
	[ProjectCode] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[Name] [nvarchar](250) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[ShortName] [nvarchar](10) COLLATE Chinese_PRC_CI_AS NULL,
	[Description] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[ProjectStateID] [int] NOT NULL,
	[TotalProblems] [int] NOT NULL,
	[TotalThreads] [int] NOT NULL,
	[TotalRecords] [int] NOT NULL,
	[DateCreated] [smalldatetime] NOT NULL,
	[ProblemTitle] [nvarchar](10) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[ThreadTitle] [nvarchar](10) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[RecordTitle] [nvarchar](10) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[OnlyViewOwnProblem] [bit] NOT NULL,
	[EnableProblemState] [bit] NOT NULL,
	[EnableMultipleThread] [bit] NOT NULL,
	[EnableEmailNotify] [bit] NOT NULL,
	[EnableProblemPriority] [bit] NOT NULL,
	[EnableProblemSeverity] [bit] NOT NULL,
	[EnableProblemType] [bit] NOT NULL,
	[EnableProblemCatalog] [bit] NOT NULL,
	[EnableProblemDeadline] [bit] NOT NULL,
	[VisibleToAll] [bit] NOT NULL,
	[EnableStateTimer] [bit] NOT NULL,
	[AutoAssignedTo] [int] NULL,
	[OpenProblems] [int] NOT NULL,
	[NewProblemIndex] [int] NOT NULL,
	[ExtendedSettings] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[SecurityLevel] [int] NOT NULL,
	[KeyWords] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[ListOrder] [int] NOT NULL,
 CONSTRAINT [PK_Pts_Projects] PRIMARY KEY CLUSTERED 
(
	[ProjectID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_ProblemState]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemState]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemState](
	[ProblemStateID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectID] [int] NOT NULL,
	[StateName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Description] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[ListOrder] [int] NOT NULL,
	[IsFinalState] [bit] NOT NULL,
	[AssignType] [int] NOT NULL,
	[Assignee] [int] NULL,
	[SelectableAssigneeWorkgroupIDs] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[SelectableAssigneeUserIDs] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[OldID] [int] NULL,
	[ExtendedSettings] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[AssignInfoExtendedSettings] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Pts_ProblemState] PRIMARY KEY CLUSTERED 
(
	[ProblemStateID] ASC
)
)
END
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_ProblemState', N'COLUMN',N'AssignType'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'分配类型：0不分配给任何人' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_ProblemState', @level2type=N'COLUMN',@level2name=N'AssignType'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_ProblemState', N'COLUMN',N'Assignee'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'分配类型为指定处理人时，所指定的处理人' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_ProblemState', @level2type=N'COLUMN',@level2name=N'Assignee'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_ProblemState', N'COLUMN',N'SelectableAssigneeWorkgroupIDs'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'分配类型为选择待办人时，可选的处理人所在的工作组' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_ProblemState', @level2type=N'COLUMN',@level2name=N'SelectableAssigneeWorkgroupIDs'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_ProblemState', N'COLUMN',N'SelectableAssigneeUserIDs'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'分配类型为选择待办人时，可选待办人的列表；' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_ProblemState', @level2type=N'COLUMN',@level2name=N'SelectableAssigneeUserIDs'
GO
/****** 对象:  Table [Pts_ProblemInitStateWorkgroup]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemInitStateWorkgroup]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemInitStateWorkgroup](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[WorkgroupID] [int] NOT NULL,
	[ProblemStateID] [int] NOT NULL,
 CONSTRAINT [PK_Pts_ProblemInitStateWorkgroup] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_ProblemStateTransfer]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemStateTransfer]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemStateTransfer](
	[TransferID] [int] IDENTITY(1,1) NOT NULL,
	[FromProblemStateID] [int] NOT NULL,
	[ToProblemStateID] [int] NOT NULL,
	[OldID] [int] NULL,
 CONSTRAINT [PK_Pts_ProblemStateTransfer] PRIMARY KEY CLUSTERED 
(
	[TransferID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_ProblemStateTransferWorkgroup]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemStateTransferWorkgroup]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemStateTransferWorkgroup](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[TransferID] [int] NOT NULL,
	[WorkgroupID] [int] NOT NULL,
	[OldID] [int] NULL,
 CONSTRAINT [PK_Pts_ProblemStateTransferWorkgroup] PRIMARY KEY CLUSTERED 
(
	[TransferID] ASC,
	[WorkgroupID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_DbDirectory]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_DbDirectory]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_DbDirectory](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectID] [int] NOT NULL,
	[FieldName] [varchar](60) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Label] [nvarchar](100) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[FieldType] [varchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Hidden] [bit] NOT NULL,
	[InUse] [bit] NOT NULL,
	[ListOrder] [int] NOT NULL,
	[Required] [bit] NOT NULL,
	[DefaultValue] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[SelectItems] [nvarchar](2000) COLLATE Chinese_PRC_CI_AS NULL,
	[FieldHelp] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[FormatString] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[HtmlEdit] [bit] NULL,
	[MaxLength] [int] NULL,
	[Width] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[Height] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[GlobalSelectID] [int] NULL,
	[UseSelectList] [bit] NULL,
	[UseGlobalSelect] [bit] NOT NULL,
	[ReadableToAll] [bit] NOT NULL,
	[EditableToAll] [bit] NOT NULL,
	[IsSystem] [bit] NOT NULL,
	[VisibleWhenNew] [bit] NOT NULL,
	[VisibleWhenEdit] [bit] NOT NULL,
	[VisibleInList] [bit] NOT NULL,
	[ReadableToCreateUser] [bit] NOT NULL,
	[EditableToCreateUser] [bit] NOT NULL,
	[ExtFieldConfigData] [nvarchar](2000) COLLATE Chinese_PRC_CI_AS NULL,
	[ExtFieldType] [nvarchar](500) COLLATE Chinese_PRC_CI_AS NULL,
	[ValidationExpression] [nvarchar](2000) COLLATE Chinese_PRC_CI_AS NULL,
	[EnableFieldQuickSearch] [bit] NOT NULL,
	[EnableCollapseDisplay] [bit] NOT NULL,
	[ShowTime] [bit] NOT NULL,
	[QuickWords] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Pts_DbDirectory] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)
)
END
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_DbDirectory', N'COLUMN',N'GlobalSelectID'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'全局选择类型，所对应的全局选择项' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_DbDirectory', @level2type=N'COLUMN',@level2name=N'GlobalSelectID'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_DbDirectory', N'COLUMN',N'UseSelectList'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'使用选择列表' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_DbDirectory', @level2type=N'COLUMN',@level2name=N'UseSelectList'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_DbDirectory', N'COLUMN',N'IsSystem'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'是否是系统字段' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_DbDirectory', @level2type=N'COLUMN',@level2name=N'IsSystem'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_DbDirectory', N'COLUMN',N'VisibleWhenNew'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'创建时是否可见' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_DbDirectory', @level2type=N'COLUMN',@level2name=N'VisibleWhenNew'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_DbDirectory', N'COLUMN',N'VisibleWhenEdit'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'编辑时是否可见' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_DbDirectory', @level2type=N'COLUMN',@level2name=N'VisibleWhenEdit'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_DbDirectory', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_DbDirectory'
GO
/****** 对象:  Table [Pts_ProblemFieldReadableWorkgroup]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemFieldReadableWorkgroup]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemFieldReadableWorkgroup](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[FieldID] [int] NOT NULL,
	[WorkgroupID] [int] NOT NULL,
 CONSTRAINT [PK_Pts_ProblemFieldReadableGroup] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)
)
END
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_ProblemFieldReadableWorkgroup', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'对字段有读权限的工作组' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_ProblemFieldReadableWorkgroup'
GO
/****** 对象:  Table [Pts_ProblemFieldEditableWorkgroup]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemFieldEditableWorkgroup]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemFieldEditableWorkgroup](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[FieldID] [int] NOT NULL,
	[WorkgroupID] [int] NOT NULL,
 CONSTRAINT [PK_Pts_ProblemFieldEditableGroup] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)
)
END
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_ProblemFieldEditableWorkgroup', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'对字段具有编辑权限的工作组' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_ProblemFieldEditableWorkgroup'
GO
/****** 对象:  Table [Pts_SharedItems]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_SharedItems]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_SharedItems](
	[ItemID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectID] [int] NOT NULL,
	[Title] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Description] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[Type] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[KeyWords] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[CreateDate] [datetime] NULL,
	[CreateUser] [int] NULL,
	[UpdateDate] [datetime] NULL,
	[Options] [int] NULL,
 CONSTRAINT [PK_Pts_SharedItems] PRIMARY KEY CLUSTERED 
(
	[ItemID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_RecordType]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_RecordType]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_RecordType](
	[RecordTypeID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectID] [int] NOT NULL,
	[TypeName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Description] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[ListOrder] [int] NOT NULL,
 CONSTRAINT [PK_Pts_RecordType] PRIMARY KEY CLUSTERED 
(
	[RecordTypeID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_ProblemCatalogs]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemCatalogs]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemCatalogs](
	[ProblemCatalogID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectID] [int] NOT NULL,
	[ParentID] [int] NOT NULL,
	[Indent] [int] NOT NULL,
	[Name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Description] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[ListOrder] [int] NOT NULL,
	[GlobalOrder] [varchar](100) COLLATE Chinese_PRC_CI_AS NULL,
	[Path] [varchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[OldID] [int] NULL,
	[ResponseUser] [int] NULL,
 CONSTRAINT [PK_Pts_ProblemCatalogs] PRIMARY KEY CLUSTERED 
(
	[ProblemCatalogID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_Filters]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_Filters]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_Filters](
	[FilterID] [int] IDENTITY(1,1) NOT NULL,
	[FilterName] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[IsPublic] [bit] NOT NULL,
	[ProjectID] [int] NOT NULL,
	[MaxNodeID] [int] NOT NULL,
	[CreateDate] [datetime] NULL,
	[CreateUserID] [int] NULL,
	[Description] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Pts_Filters] PRIMARY KEY CLUSTERED 
(
	[FilterID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_ProblemCreateRoles]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemCreateRoles]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemCreateRoles](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectID] [int] NOT NULL,
	[DstStateID] [int] NOT NULL,
	[Description] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[AssignType] [int] NOT NULL,
	[Assignee] [int] NULL,
	[SelectableAssigneeWorkgroupIDs] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[SelectableAssigneeUserIDs] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[AssignInfoExtendedSettings] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Pts_ProblemCreateRoles] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)
)
END
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_ProblemCreateRoles', N'COLUMN',N'AssignType'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'分配类型：0不分配给任何人' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_ProblemCreateRoles', @level2type=N'COLUMN',@level2name=N'AssignType'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_ProblemCreateRoles', N'COLUMN',N'Assignee'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'分配类型为指定处理人时，所指定的处理人' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_ProblemCreateRoles', @level2type=N'COLUMN',@level2name=N'Assignee'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_ProblemCreateRoles', N'COLUMN',N'SelectableAssigneeWorkgroupIDs'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'分配类型为选择待办人时，可选的处理人所在的工作组' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_ProblemCreateRoles', @level2type=N'COLUMN',@level2name=N'SelectableAssigneeWorkgroupIDs'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_ProblemCreateRoles', N'COLUMN',N'SelectableAssigneeUserIDs'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'分配类型为选择待办人时，可选待办人的列表；' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_ProblemCreateRoles', @level2type=N'COLUMN',@level2name=N'SelectableAssigneeUserIDs'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_ProblemCreateRoles', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'创建事务的规则' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_ProblemCreateRoles'
GO
/****** 对象:  Table [Pts_WorkflowSteps]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_WorkflowSteps]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_WorkflowSteps](
	[StepID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectID] [int] NOT NULL,
	[SrcStateID] [int] NOT NULL,
	[DstStateID] [int] NOT NULL,
	[StepName] [nvarchar](100) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Description] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[AssignType] [int] NOT NULL,
	[Assignee] [int] NULL,
	[SelectableAssigneeWorkgroupIDs] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[SelectableAssigneeUserIDs] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[ListOrder] [int] NOT NULL,
	[RecordTitle] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[CommonMessages] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[AssignInfoExtendedSettings] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[StepExtendedSettings] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Pts_WorkflowSteps] PRIMARY KEY CLUSTERED 
(
	[StepID] ASC
)
)
END
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_WorkflowSteps', N'COLUMN',N'Assignee'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'分配类型为指定处理人时，所指定的处理人' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_WorkflowSteps', @level2type=N'COLUMN',@level2name=N'Assignee'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_WorkflowSteps', N'COLUMN',N'SelectableAssigneeWorkgroupIDs'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'分配类型为选择待办人时，可选的处理人所在的工作组' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_WorkflowSteps', @level2type=N'COLUMN',@level2name=N'SelectableAssigneeWorkgroupIDs'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_WorkflowSteps', N'COLUMN',N'SelectableAssigneeUserIDs'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'分配类型为选择待办人时，可选待办人的列表；' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_WorkflowSteps', @level2type=N'COLUMN',@level2name=N'SelectableAssigneeUserIDs'
GO
/****** 对象:  Table [Pts_ProblemPriority]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemPriority]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemPriority](
	[ProblemPriorityID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectID] [int] NOT NULL,
	[PriorityName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[DisplayColor] [varchar](15) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Description] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[ListOrder] [int] NOT NULL,
 CONSTRAINT [PK_Pts_ProblemPriority] PRIMARY KEY CLUSTERED 
(
	[ProblemPriorityID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_ProblemSeverity]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemSeverity]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemSeverity](
	[ProblemSeverityID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectID] [int] NOT NULL,
	[SeverityName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[DisplayColor] [varchar](15) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Description] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[ListOrder] [int] NOT NULL,
 CONSTRAINT [PK_Pts_ProblemSeverities] PRIMARY KEY CLUSTERED 
(
	[ProblemSeverityID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_ProblemType]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemType]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemType](
	[ProblemTypeID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectID] [int] NOT NULL,
	[TypeName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Description] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[ListOrder] [int] NOT NULL,
 CONSTRAINT [PK_Pts_ProblemType] PRIMARY KEY CLUSTERED 
(
	[ProblemTypeID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_Problems]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_Problems]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_Problems](
	[ProblemID] [int] IDENTITY(1,1) NOT NULL,
	[ProjectID] [int] NOT NULL,
	[OriginProjectID] [int] NOT NULL,
	[ProblemIndex] [int] NOT NULL,
	[ProblemCatalogID] [int] NULL,
	[ProblemTypeID] [int] NULL,
	[ProblemStateID] [int] NULL,
	[ProblemSeverityID] [int] NULL,
	[ProblemPriorityID] [int] NULL,
	[CreateTime] [datetime] NOT NULL,
	[CloseTime] [datetime] NULL,
	[CreateUserID] [int] NOT NULL,
	[Deadline] [datetime] NULL,
	[AssignedTo] [int] NULL,
	[Title] [nvarchar](250) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Description] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[RecordNum] [smallint] NOT NULL,
	[LastUser] [int] NULL,
	[LastTime] [datetime] NULL,
	[LastRecord] [int] NULL,
	[HistoryID] [int] NULL,
	[Int1] [int] NULL,
	[Int2] [int] NULL,
	[Int3] [int] NULL,
	[Int4] [int] NULL,
	[Int5] [int] NULL,
	[Float1] [float] NULL,
	[Float2] [float] NULL,
	[Float3] [float] NULL,
	[Float4] [float] NULL,
	[Float5] [float] NULL,
	[Date1] [datetime] NULL,
	[Date2] [datetime] NULL,
	[Date3] [datetime] NULL,
	[Date4] [datetime] NULL,
	[Date5] [datetime] NULL,
	[Date6] [datetime] NULL,
	[Date7] [datetime] NULL,
	[Date8] [datetime] NULL,
	[Date9] [datetime] NULL,
	[Date10] [datetime] NULL,
	[BigText1] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[BigText2] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[BigText3] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[BigText4] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[BigText5] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[Text1] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Text2] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Text3] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Text4] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Text5] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Select1] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Select2] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Select3] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Select4] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Select5] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Select6] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Select7] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Select8] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Select9] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Select10] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[YesNo1] [bit] NULL,
	[YesNo2] [bit] NULL,
	[YesNo3] [bit] NULL,
	[YesNo4] [bit] NULL,
	[YesNo5] [bit] NULL,
	[Url1] [varchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Url2] [varchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Url3] [varchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Url4] [varchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Url5] [varchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[BigText6] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[BigText7] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[BigText8] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[BigText9] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[BigText10] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[Text6] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Text7] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Text8] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Text9] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Text10] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Text11] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Text12] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Text13] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Text14] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Text15] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Text16] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Text17] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Text18] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Text19] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Text20] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Select11] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Select12] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Select13] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Select14] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Select15] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Email1] [varchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Email2] [varchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Email3] [varchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Email4] [varchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Email5] [varchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect1] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect2] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect3] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect4] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect5] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[ArticleID] [int] NULL,
	[StartTime] [datetime] NULL,
	[Int6] [int] NULL,
	[Int7] [int] NULL,
	[Int8] [int] NULL,
	[Int9] [int] NULL,
	[Int10] [int] NULL,
	[Int11] [int] NULL,
	[Int12] [int] NULL,
	[Int13] [int] NULL,
	[Int14] [int] NULL,
	[Int15] [int] NULL,
	[Int16] [int] NULL,
	[Int17] [int] NULL,
	[Int18] [int] NULL,
	[Int19] [int] NULL,
	[Int20] [int] NULL,
	[Float6] [float] NULL,
	[Float7] [float] NULL,
	[Float8] [float] NULL,
	[Float9] [float] NULL,
	[Float10] [float] NULL,
	[Float11] [float] NULL,
	[Float12] [float] NULL,
	[Float13] [float] NULL,
	[Float14] [float] NULL,
	[Float15] [float] NULL,
	[Float16] [float] NULL,
	[Float17] [float] NULL,
	[Float18] [float] NULL,
	[Float19] [float] NULL,
	[Float20] [float] NULL,
	[Date11] [datetime] NULL,
	[Date12] [datetime] NULL,
	[Date13] [datetime] NULL,
	[Date14] [datetime] NULL,
	[Date15] [datetime] NULL,
	[Date16] [datetime] NULL,
	[Date17] [datetime] NULL,
	[Date18] [datetime] NULL,
	[Date19] [datetime] NULL,
	[Date20] [datetime] NULL,
	[BigText11] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[BigText12] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[BigText13] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[BigText14] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[BigText15] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[BigText16] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[BigText17] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[BigText18] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[BigText19] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[BigText20] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[Select16] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Select17] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Select18] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Select19] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Select20] [nvarchar](80) COLLATE Chinese_PRC_CI_AS NULL,
	[Email6] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Email7] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Email8] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Email9] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Email10] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect6] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect7] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect8] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect9] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect10] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[YesNo6] [bit] NULL,
	[YesNo7] [bit] NULL,
	[YesNo8] [bit] NULL,
	[YesNo9] [bit] NULL,
	[YesNo10] [bit] NULL,
	[YesNo11] [bit] NULL,
	[YesNo12] [bit] NULL,
	[YesNo13] [bit] NULL,
	[YesNo14] [bit] NULL,
	[YesNo15] [bit] NULL,
	[YesNo16] [bit] NULL,
	[YesNo17] [bit] NULL,
	[YesNo18] [bit] NULL,
	[YesNo19] [bit] NULL,
	[YesNo20] [bit] NULL,
	[Url6] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Url7] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Url8] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Url9] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[Url10] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect11] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect12] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect13] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect14] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect15] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect16] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect17] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect18] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect19] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[MultiSelect20] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
	[ReadByAssignee] [bit] NOT NULL,
	[AssigneeReadTime] [datetime] NULL,
	[IsCoworking] [bit] NOT NULL,
	[CoworkInfo] [nvarchar](500) COLLATE Chinese_PRC_CI_AS NULL,
	[CustomInputField1] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[CustomInputField2] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[CustomInputField3] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[CustomInputField4] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[CustomInputField5] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[ExtField1] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[ExtField2] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[ExtField3] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[ExtField4] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[ExtField5] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[AttachNum] [int] NOT NULL,
	[StateDeadline] [datetime] NULL,
	[ParentID] [int] NOT NULL,
	[TotalChildNum] [int] NOT NULL,
	[ClosedChildNum] [int] NOT NULL,
	[IsActive] [bit] NOT NULL,
	[RealStartTime] [datetime] NULL,
	[RealEndTime] [datetime] NULL,
 CONSTRAINT [PK_Pts_Problems] PRIMARY KEY CLUSTERED 
(
	[ProblemID] ASC
)
)
END
GO
IF NOT EXISTS (SELECT * FROM dbo.sysindexes WHERE id = OBJECT_ID(N'[Pts_Problems]') AND name = N'IX_Pts_Problems')
CREATE NONCLUSTERED INDEX [IX_Pts_Problems] ON [Pts_Problems] 
(
	[ProjectID] ASC
)
GO
IF NOT EXISTS (SELECT * FROM dbo.sysindexes WHERE id = OBJECT_ID(N'[Pts_Problems]') AND name = N'IX_Pts_Problems_AssignedTo')
CREATE NONCLUSTERED INDEX [IX_Pts_Problems_AssignedTo] ON [Pts_Problems] 
(
	[AssignedTo] ASC
)
GO
IF NOT EXISTS (SELECT * FROM dbo.sysindexes WHERE id = OBJECT_ID(N'[Pts_Problems]') AND name = N'IX_Pts_Problems_CreateTime')
CREATE NONCLUSTERED INDEX [IX_Pts_Problems_CreateTime] ON [Pts_Problems] 
(
	[CreateTime] ASC
)
GO
IF NOT EXISTS (SELECT * FROM dbo.sysindexes WHERE id = OBJECT_ID(N'[Pts_Problems]') AND name = N'IX_Pts_Problems_CreateUser')
CREATE NONCLUSTERED INDEX [IX_Pts_Problems_CreateUser] ON [Pts_Problems] 
(
	[CreateUserID] ASC
)
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_Problems', N'COLUMN',N'ParentID'))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'父事务ID' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_Problems', @level2type=N'COLUMN',@level2name=N'ParentID'
GO
/****** 对象:  Table [Pts_ProblemStateTimeLimit]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemStateTimeLimit]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemStateTimeLimit](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[PriorityID] [int] NOT NULL,
	[StateID] [int] NOT NULL,
	[HoldingPeriod] [int] NOT NULL,
	[EndLimit] [int] NOT NULL,
 CONSTRAINT [PK_Pts_StateTimeLimit] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_ProblemStateRecord]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemStateRecord]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemStateRecord](
	[HistoryID] [int] IDENTITY(1,1) NOT NULL,
	[ProblemID] [int] NOT NULL,
	[StateID] [int] NOT NULL,
	[StartTime] [datetime] NOT NULL,
	[MaxEndTime] [datetime] NULL,
	[EndTime] [datetime] NULL,
	[KeepTime] [int] NULL,
	[WorkgroupID] [int] NULL,
	[UserID] [int] NULL,
	[BizKeepTime] [int] NULL,
 CONSTRAINT [PK_Pts_ProblemStateHistory] PRIMARY KEY CLUSTERED 
(
	[HistoryID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_ProblemRelations]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemRelations]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemRelations](
	[ProblemID_1] [int] NOT NULL,
	[ProblemID_2] [int] NOT NULL,
	[RelationType] [int] NOT NULL
)
END
GO
/****** 对象:  Table [Pts_Records]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_Records]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_Records](
	[RecordID] [int] IDENTITY(1,1) NOT NULL,
	[ProblemID] [int] NOT NULL,
	[CreateUser] [int] NOT NULL,
	[DateCreated] [datetime] NOT NULL,
	[IsKeyRecord] [bit] NOT NULL,
	[RecordType] [int] NULL,
	[AssignTo] [int] NULL,
	[Title] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[Content] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[StateID] [int] NOT NULL,
	[HistoryID] [int] NULL,
	[RecordClass] [int] NOT NULL,
	[SrcStateID] [int] NULL,
	[SrcUserID] [int] NULL,
	[ResponseWorkTime] [int] NULL,
	[ResponseTotalTime] [int] NULL,
	[RealTimeCost] [int] NULL,
	[ExtInt1] [int] NULL,
	[ExtInt2] [int] NULL,
	[ExtInt3] [int] NULL,
	[ExtString1] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[ExtString2] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[ExtString3] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[ExtNtext1] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[ExtNtext2] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[ParentRecordID] [int] NOT NULL,
	[RepresentingUserID] [int] NULL,
 CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED 
(
	[RecordID] ASC
)
)
END
GO
IF NOT EXISTS (SELECT * FROM dbo.sysindexes WHERE id = OBJECT_ID(N'[Pts_Records]') AND name = N'IX_Pts_Records')
CREATE NONCLUSTERED INDEX [IX_Pts_Records] ON [Pts_Records] 
(
	[ProblemID] ASC
)
GO
/****** 对象:  Table [Pts_ProblemHistory]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemHistory]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemHistory](
	[HistoryID] [int] IDENTITY(1,1) NOT NULL,
	[ProblemID] [int] NOT NULL,
	[ChangeDate] [datetime] NOT NULL,
	[UserID] [int] NULL,
	[Content] [ntext] COLLATE Chinese_PRC_CI_AS NOT NULL,
 CONSTRAINT [PK_Pts_ProblemHistory] PRIMARY KEY CLUSTERED 
(
	[HistoryID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_ProblemAttachments]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemAttachments]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemAttachments](
	[AttachmentID] [int] IDENTITY(1,1) NOT NULL,
	[ProblemID] [int] NOT NULL,
	[SaveName] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[OriginName] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[FileSize] [int] NULL,
	[Description] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[CreateDate] [datetime] NULL,
	[CreateUserID] [int] NULL,
 CONSTRAINT [PK_Pts_ProblemAttachments] PRIMARY KEY CLUSTERED 
(
	[AttachmentID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_FilterNodes]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_FilterNodes]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_FilterNodes](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[FilterID] [int] NOT NULL,
	[NodeID] [int] NOT NULL,
	[IsLeaf] [bit] NOT NULL,
	[SupportRuntimeValue] [bit] NOT NULL,
	[LeftChildID] [int] NOT NULL,
	[RightChildID] [int] NOT NULL,
	[ParentID] [int] NOT NULL,
	[IsLeftChild] [bit] NOT NULL,
	[OperatorString] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[IsNot] [bit] NOT NULL,
	[FieldTitle] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[FieldName] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[CompareString] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[FieldValue] [nvarchar](1024) COLLATE Chinese_PRC_CI_AS NULL,
	[FieldDisplayValue] [nvarchar](2048) COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Pts_FilterNodes] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_ProblemFieldEditableState]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_ProblemFieldEditableState]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_ProblemFieldEditableState](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[FieldID] [int] NOT NULL,
	[FromStateID] [int] NOT NULL,
	[ToStateID] [int] NOT NULL,
	[IsRequired] [bit] NOT NULL,
 CONSTRAINT [PK_Pts_ProblemFieldEditableState] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)
)
END
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'USER',N'dbo', N'TABLE',N'Pts_ProblemFieldEditableState', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_Description', @value=N'字段在哪些状态更改下可以编辑。可以同时指定源状态和目标状态，也可以只指定一个状态' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Pts_ProblemFieldEditableState'
GO
/****** 对象:  Table [Pts_RecordAttachments]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_RecordAttachments]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_RecordAttachments](
	[AttachmentID] [int] IDENTITY(1,1) NOT NULL,
	[RecordID] [int] NOT NULL,
	[SaveName] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
	[OriginName] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[FileSize] [int] NULL,
	[Description] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[CreateUserID] [int] NULL,
	[CreateDate] [datetime] NULL,
 CONSTRAINT [PK_Pts_RecordAttachments] PRIMARY KEY CLUSTERED 
(
	[AttachmentID] ASC
)
)
END
GO
/****** 对象:  Table [Pts_SharedItemAttachments]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Pts_SharedItemAttachments]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Pts_SharedItemAttachments](
	[AttachmentID] [int] IDENTITY(1,1) NOT NULL,
	[ItemID] [int] NOT NULL,
	[SaveName] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[OriginName] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[FileSize] [int] NULL,
	[Description] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[CreateDate] [datetime] NULL,
	[CreateUser] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Pts_SharedItemAttachments] PRIMARY KEY CLUSTERED 
(
	[AttachmentID] ASC
)
)
END
GO
/****** 对象:  Table [Kb_ArticleComment]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Kb_ArticleComment]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Kb_ArticleComment](
	[CommentID] [int] IDENTITY(1,1) NOT NULL,
	[ArticleID] [int] NOT NULL,
	[Content] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
	[AddDate] [datetime] NULL,
	[AddUser] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[Rate] [tinyint] NULL,
	[Attachment] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Kb_ArticleComment] PRIMARY KEY CLUSTERED 
(
	[CommentID] ASC
)
)
END
GO
/****** 对象:  Table [Kb_ArticleAttachments]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Kb_ArticleAttachments]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Kb_ArticleAttachments](
	[AttachmentID] [int] IDENTITY(1,1) NOT NULL,
	[ArticleID] [int] NOT NULL,
	[SaveName] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[OriginName] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[FileSize] [int] NULL,
	[Description] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
	[CreateDate] [datetime] NULL,
	[CreateUser] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Kb_ArticleAttachments] PRIMARY KEY CLUSTERED 
(
	[AttachmentID] ASC
)
)
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_DeleteRole]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_DeleteRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--删除角色
CREATE PROCEDURE [sp_Accounts_DeleteRole]
@RoleID int
AS
	BEGIN TRANSACTION
		DELETE Accounts_RolePermissions WHERE RoleID = @RoleID
		DELETE Accounts_UserRoles WHERE RoleID = @RoleID
		DELETE Accounts_Roles WHERE RoleID = @RoleID
	COMMIT TRANSACTION





' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_GetAllRoles]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetAllRoles]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--返回所有的角色
CREATE PROCEDURE [sp_Accounts_GetAllRoles]
AS
	SELECT RoleID, Description FROM Accounts_Roles ORDER BY RoleID ASC



' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_GetRoleDetails]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetRoleDetails]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--返回角色的详细信息
CREATE PROCEDURE [sp_Accounts_GetRoleDetails]
@RoleID int
AS
	SELECT RoleID, Description FROM Accounts_Roles WHERE RoleID = @RoleID





' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_GetUserRoles]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetUserRoles]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--返回某个用户所属于的角色
CREATE PROCEDURE [sp_Accounts_GetUserRoles]
@UserID int
AS
	SELECT ur.RoleID, r.Description FROM Accounts_UserRoles ur
		INNER JOIN Accounts_Roles r ON ur.RoleID = r.RoleID  WHERE ur.UserID = @UserID

	ORDER BY ur.RoleID



' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_CreateRole]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_CreateRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--创建角色
CREATE PROCEDURE [sp_Accounts_CreateRole]
@Description varchar(50)
AS
	INSERT INTO Accounts_Roles(Description) VALUES(@Description)
	RETURN @@IDENTITY





' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_UpdateRole]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_UpdateRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--更新角色信息
CREATE PROCEDURE [sp_Accounts_UpdateRole]
@RoleID int,
@Description varchar(50)
AS
	UPDATE Accounts_Roles SET Description = @Description WHERE RoleID = @RoleID





' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetSelectValues]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSelectValues]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetSelectValues]
@GlobalSelectID int
AS
Select * From Pts_GlobalSelectValues Where GlobalSelectID = @GlobalSelectID Order by ListOrder,ValueString' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_DeleteSelectValue]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteSelectValue]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_DeleteSelectValue]
@SelectValueID int
AS
Delete From Pts_GlobalSelectValues Where SelectValueID = @SelectValueID

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_AddSelectValue]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddSelectValue]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Pts_AddSelectValue]
@GlobalSelectID int,
@ValueString nvarchar(80)
AS
Insert into Pts_GlobalSelectValues (GlobalSelectID, ValueString) Values (@GlobalSelectID, @ValueString)
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_DeleteGlobalSelect]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteGlobalSelect]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_DeleteGlobalSelect]
@GlobalSelectID int
AS
Delete From Pts_GlobalSelectValues Where GlobalSelectID = @GlobalSelectID
Delete From Pts_GlobalSelects Where GlobalSelectID = @GlobalSelectID

' 
END
GO
/****** 对象:  StoredProcedure [sp_Common_AddEvent]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Common_AddEvent]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Common_AddEvent]
	@Time datetime,
	@UserID int,
	@UserName nvarchar(100),
	@EventType nvarchar(50),
	@EventInfo ntext
AS

SET NOCOUNT ON

INSERT INTO [Common_Events] (
	[Time],
	[UserID],
	[UserName],
	[EventType],
	[EventInfo]
) VALUES (
	@Time,
	@UserID,
	@UserName,
	@EventType,
	@EventInfo
)
' 
END
GO
/****** 对象:  View [v_Accounts_Users]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[v_Accounts_Users]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [v_Accounts_Users]
AS
SELECT     Accounts_Users.UserID, Accounts_Users.Password, Accounts_Users.EmailAddress, Accounts_Users.DisplayName, Accounts_Users.UserState, 
                      Accounts_Users.DeptID, Accounts_Users.Address, Accounts_Users.OfficePhone, Accounts_Users.MobilePhone, Accounts_Users.MSN, 
                      Accounts_Users.PasswordNote, Accounts_Users.Remark, Accounts_Users.AccessListID, Accounts_Users.BindIP, Accounts_Users.BindIPString, 
                      Accounts_Users.AgentUserID, Accounts_UserState.UserStateName, Accounts_Department.DeptName, (CASE WHEN DeptName IS NOT NULL 
                      THEN N''('' + DeptName + N'')'' WHEN DeptName IS NULL THEN '''' END) + Accounts_Users.DisplayName AS FullName, Accounts_Users.ExtendedSettings, 
                      Accounts_Users.LoginName, Accounts_Users.ExpireDate, Accounts_Users.ExtIntData1, Accounts_Users.ExtIntData2, 
                      Accounts_Users.ExtIntData4, Accounts_Users.ExtIntData3, Accounts_Users.ExtStrData1, Accounts_Users.ExtStrData2, 
                      Accounts_Users.Attributes
FROM         Accounts_Users INNER JOIN
                      Accounts_UserState ON Accounts_Users.UserState = Accounts_UserState.UserStateID LEFT OUTER JOIN
                      Accounts_Department ON Accounts_Users.DeptID = Accounts_Department.DeptID
'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPane1' , N'USER',N'dbo', N'VIEW',N'v_Accounts_Users', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[34] 4[28] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "Accounts_Users"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 271
               Right = 200
            End
            DisplayFlags = 280
            TopColumn = 12
         End
         Begin Table = "Accounts_UserState"
            Begin Extent = 
               Top = 6
               Left = 236
               Bottom = 108
               Right = 400
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "Accounts_Department"
            Begin Extent = 
               Top = 108
               Left = 236
               Bottom = 273
               Right = 405
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 10
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 2025
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Accounts_Users'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPaneCount' , N'USER',N'dbo', N'VIEW',N'v_Accounts_Users', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Accounts_Users'
GO
/****** 对象:  StoredProcedure [sp_Pts_ShiftProblem]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ShiftProblem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Pts_ShiftProblem]
@ProblemID int,
@PrevProjectID int,
@PrevStateID int,
@PrevUserID int,
@NextProjectID int,
@NextStateID int,
@NextUserID int,
@Description nvarchar(2000),
@ShiftDate DateTime
AS

--
Insert into 
 Pts_ProblemShiftHistory
 (
 ProblemID, 
 PrevProjectID,
 PrevStateID,
 PrevUserID,
 NextProjectID,
 NextStateID,
 NextUserID,
 Description,
 ShiftDate
 )
Values
 (
 @ProblemID, 
 @PrevProjectID,
 @PrevStateID,
 @PrevUserID,
 @NextProjectID,
 @NextStateID,
 @NextUserID,
 @Description,
 @ShiftDate
 )
--update pts_Problem table
Update Pts_Problems Set ProjectID = @NextProjectID,ProblemStateID=@NextStateID,AssignedTo=@NextUserID  Where ProblemID = @ProblemID
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetFieldEditableWorkgroups]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetFieldEditableWorkgroups]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		CuiLiang
-- Create date: 20070614
-- Description:	获得有权限编辑指定字段的工作组的列表
-- =============================================
CREATE PROCEDURE [sp_Pts_GetFieldEditableWorkgroups]
	-- Add the parameters for the stored procedure here
	@FieldID int
AS
BEGIN
	
	SELECT * 
	FROM Pts_Workgroups 
	WHERE WorkgroupID in
		(SELECT WorkgroupID FROM Pts_ProblemFieldEditableWorkgroup WHERE FieldID=@FieldID)
END
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetWorkgroupInfo]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetWorkgroupInfo]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_GetWorkgroupInfo]
@WorkgroupID int
AS
Select 
	workgroup.*, project.Name as [ProjectName]
From 
	Pts_Workgroups workgroup inner join Pts_Projects project
	on
	workgroup.ProjectID = project.ProjectID
Where
	workgroup.WorkgroupID = @WorkgroupID



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetFieldReadableWorkgroups]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetFieldReadableWorkgroups]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		CuiLiang
-- Create date: 20070614
-- Description:	获得有权限读取指定字段的工作组的列表
-- =============================================
CREATE PROCEDURE [sp_Pts_GetFieldReadableWorkgroups]
	-- Add the parameters for the stored procedure here
	@FieldID int
AS
BEGIN
	
	SELECT * 
	FROM Pts_Workgroups 
	WHERE WorkgroupID in
		(SELECT WorkgroupID FROM Pts_ProblemFieldReadableWorkgroup WHERE FieldID=@FieldID)
END
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_UpdateWorkgroup]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateWorkgroup]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE  PROCEDURE [sp_Pts_UpdateWorkgroup]
@WorkgroupID	int,
@Name nvarchar (50),
@Description nvarchar(255),
@AdminProject bit,
@AddProblem bit,
@ChangeProblemState bit,
@ShiftProblem bit,
@AddRecord bit
AS
Update Pts_Workgroups
  set 	[Name] = @Name, Description = @Description,AdminProject = @AdminProject,
	AddProblem = @AddProblem, ChangeProblemState = @ChangeProblemState, ShiftProblem = @ShiftProblem, AddRecord = @AddRecord
  Where WorkgroupID = @WorkgroupID




' 
END
GO
/****** 对象:  UserDefinedFunction [GetAssigneeName]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[GetAssigneeName]') AND xtype in (N'FN', N'IF', N'TF'))
BEGIN
execute dbo.sp_executesql @statement = N'-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [GetAssigneeName]
(
	-- Add the parameters for the function here
	@UserOrWorkgroupID int
)
RETURNS nvarchar(100)
AS
BEGIN
	-- Declare the return variable here
	DECLARE @AssigneeName nvarchar(100)

	-- Add the T-SQL statements to compute the return value here
	if (@UserOrWorkgroupID < 0)
	BEGIN
	SELECT @AssigneeName = Name FROM Pts_Workgroups WHERE WorkgroupID=-@UserOrWorkgroupID
	SET @AssigneeName = N''['' + @AssigneeName + N'']''
	END
	ELSE
	BEGIn
	SELECT @AssigneeName = DisplayName FROM Accounts_Users WHERE UserID=@UserOrWorkgroupID
	END

	-- Return the result of the function
	RETURN @AssigneeName

END
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetWorkgroupAllowedInitStates]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetWorkgroupAllowedInitStates]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetWorkgroupAllowedInitStates]
@ProjectID int
AS
----得到某个项目各个工作组所允许的初始状态的ID
SELECT WorkgroupID,ProblemStateID FROM Pts_ProblemInitStateWorkgroup 
WHERE WorkgroupID IN (SELECT WorkgroupID FROM Pts_Workgroups WHERE ProjectID=@ProjectID)' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetWorkgroupList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetWorkgroupList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE  PROCEDURE [sp_Pts_GetWorkgroupList]
@ProjectID int
AS
Select * from Pts_Workgroups where ProjectID = @ProjectID order by ListOrder




' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetSelectUsers]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSelectUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CReate Procedure [sp_Pts_GetSelectUsers]
--返回可选择的用户
@SelectID int,
@ProjectID int

AS

if (@SelectID = -1)
begin
  Select DisplayName as ValueString From Accounts_Users 
	Where UserState = 2 or UserState = 3
	Order By DisplayName
end
else if (@SelectID = -2)
begin
select DisplayName as ValueString
 from Accounts_Users
 Where UserState<>4 AND UserID in
 (Select distinct UserID From Pts_WorkgroupUsers Where WorkgroupID in
  (Select WorkgroupID From Pts_Workgroups Where ProjectID = @ProjectID )
  )
Order by DisplayName

end

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_IsUserInProject]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_IsUserInProject]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



--检查某个用户是否为项目成员，如果返回值大于0，则是，否则不是
Create Procedure [sp_Pts_IsUserInProject]
@ProjectID int,
@UserID int
AS
Select gu.UserID From 
	Pts_WorkgroupUsers gu, Pts_Workgroups g
Where
	gu.WorkgroupID = g.WorkgroupID
	AND g.ProjectID = @ProjectID 
	AND gu.UserID = @UserID

return @@ROWCOUNT 



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ResetWorkgroupOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetWorkgroupOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE  Procedure [sp_Pts_ResetWorkgroupOrder]
@ProjectID int
AS
Declare @Order int
set @Order = 1
Declare @groupID int
Declare group_cursor CURSOR
FOR 
	Select WorkgroupID From Pts_Workgroups Where ProjectID = @ProjectID Order by ListOrder
Open group_cursor 
Fetch next from group_cursor into @groupID
while (@@FETCH_STATUS <> -1)
BEGIN
	if (@@FETCH_STATUS <> -2)
	BEGIN
		Update Pts_Workgroups set ListOrder = @Order where WorkgroupID = @groupID
		Set @Order = @Order + 1		
	END
	Fetch next from group_cursor into @groupID
END




' 
END
GO
/****** 对象:  View [v_Pts_UserWorkgroups]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[v_Pts_UserWorkgroups]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N'



CREATE VIEW [v_Pts_UserWorkgroups]
AS
SELECT Pts_Workgroups.WorkgroupID, 
      Pts_Workgroups.Name AS WorkgroupName, Pts_Workgroups.Description, 
      Pts_Projects.Name AS ProjectName, Pts_WorkgroupUsers.IsManager, 
      Pts_WorkgroupUsers.UserID, Pts_Projects.ProjectID, 
      Pts_Workgroups.ListOrder
FROM Pts_Workgroups INNER JOIN
      Pts_Projects ON 
      Pts_Workgroups.ProjectID = Pts_Projects.ProjectID INNER JOIN
      Pts_WorkgroupUsers ON 
      Pts_Workgroups.WorkgroupID = Pts_WorkgroupUsers.WorkgroupID




'
GO
/****** 对象:  StoredProcedure [sp_Pts_DeleteProject]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProject]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'








CREATE      Procedure [sp_Pts_DeleteProject]
@ProjectID int
AS
Begin Transaction

Delete From Pts_ProblemStateRecord Where ProblemID in 
	(Select ProblemID From Pts_Problems Where ProjectID = @ProjectID )

Delete From Pts_RecordAttachments Where RecordID in 
	(Select RecordID From Pts_Records Where ProblemID in
		(Select ProblemID From Pts_Problems Where ProjectID = @ProjectID ))
Delete From Pts_Records  Where ProblemID in
	(Select ProblemID From Pts_Problems Where ProjectID = @ProjectID )

Delete From Pts_ProblemAttachments Where ProblemID in
	(Select ProblemID From Pts_Problems Where ProjectID = @ProjectID )

Delete From Pts_Problems Where ProjectID = @ProjectID


Delete From Pts_ProblemCatalogs Where ProjectID = @ProjectID
Delete From Pts_ProblemType Where ProjectID = @ProjectID
Delete From Pts_ProblemSeverity Where ProjectID = @ProjectID


--
Delete From Pts_ProblemStateTimeLimit Where PriorityID in
	(Select ProblemPriorityID From Pts_ProblemPriority Where ProjectID = @ProjectID)

--问题优先级
Delete From Pts_ProblemPriority Where ProjectID = @ProjectID

--
Delete From Pts_ProblemCreateRoles WHere ProjectID = @ProjectID

-- 
Delete FROM Pts_ProblemFieldEditableState WHERE FromStateID IN 
(Select ProblemStateID From Pts_ProblemState Where ProjectID = @ProjectID)

--旧的工作流
Delete From Pts_ProblemStateTransferWorkgroup Where TransferID in
	(Select TransferID From Pts_ProblemStateTransfer Where FromProblemStateID in
		(Select ProblemStateID From Pts_ProblemState Where ProjectID = @ProjectID))

--旧的工作流
Delete From Pts_ProblemStateTransfer Where FromProblemStateID in
	(Select ProblemStateID From Pts_ProblemState Where ProjectID = @ProjectID)

--初始状态
Delete From Pts_ProblemInitStateWorkgroup Where ProblemStateID in
	(Select ProblemStateID From Pts_ProblemState Where ProjectID = @ProjectID)

--各状态下的可编辑字段
Delete From Pts_StateEditableFields Where ProjectID = @ProjectID

--工作流
Delete From Pts_WorkflowSteps WHERE ProjectID = @ProjectID



--事务状态
Delete From Pts_ProblemState Where ProjectID = @ProjectID


--工作组
Delete From Pts_ProblemFieldEditableWorkgroup Where WorkgroupID IN 
   (Select WorkgroupID From Pts_Workgroups Where ProjectID = @ProjectID)

Delete From Pts_ProblemFieldReadableWorkgroup Where WorkgroupID IN
   (Select WorkgroupID From Pts_Workgroups Where ProjectID = @ProjectID)

Delete From Pts_WorkgroupUsers Where WorkgroupID in 
	(Select WorkgroupID From Pts_Workgroups Where ProjectID = @ProjectID)
Delete From Pts_Workgroups Where ProjectID = @ProjectID



--高级查询
Delete From Pts_FilterNodes Where FilterID in
	(Select FilterID From Pts_Filters Where ProjectID = @ProjectID)
Delete From Pts_Filters Where ProjectID = @ProjectID

--项目字段
Delete From Pts_DbDirectory Where ProjectID = @ProjectID

--记录类型
Delete From Pts_RecordType where ProjectID = @ProjectID

----SharedItem
--attachments
Delete FROM Pts_SharedItemAttachments WHERE ItemID in (SELECT ItemID FROM Pts_SharedItems WHERE ProjectID = @ProjectID)

--shared item 
DELETE FROM Pts_SharedItems WHERE ProjectID = @ProjectID


Delete From Pts_Projects Where ProjectID = @ProjectID


commit transaction







' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetUserAllowedInitStates]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserAllowedInitStates]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE  [sp_Pts_GetUserAllowedInitStates]
@ProjectID int,
@UserID int

AS
--返回用户可用的事务初始状态列表
--选择条件：针对该状态的事务创建规则已经定义；
--并且用户所在的工作组没有设置允许的初始状态列表，
--或者设置了允许的初始状态列表，并且当前状态存在于允许的状态列表中
Select * From Pts_ProblemState Where ProjectID=@ProjectID AND 
(
ProblemStateID IN 
(
	Select ProblemStateID From Pts_ProblemInitStateWorkgroup Where 
	WorkgroupID in 
		(Select WorkgroupID From Pts_Workgroups Where ProjectID = @ProjectID 
			AND WorkgroupID in 
			(Select WorkgroupID From Pts_WorkgroupUsers Where UserID = @UserID)
		)
	)
  OR
  NOT EXISTS(Select ProblemStateID From Pts_ProblemInitStateWorkgroup Where 
	WorkgroupID in 
		(Select WorkgroupID From Pts_Workgroups Where ProjectID = @ProjectID 
			AND WorkgroupID in 
			(Select WorkgroupID From Pts_WorkgroupUsers Where UserID = @UserID)
		)
	)
)

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetTransferWorkgroups]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetTransferWorkgroups]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE  PROCEDURE [sp_Pts_GetTransferWorkgroups]
@TransferID int
AS
Select * from Pts_Workgroups where
WorkgroupID in 
(Select WorkgroupID from Pts_ProblemStateTransferWorkgroup where TransferID = @TransferID)




' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetTransferCandidateWrokgroups]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetTransferCandidateWrokgroups]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE  Procedure [sp_Pts_GetTransferCandidateWrokgroups]
@ProjectID int,
@TransferID int
AS
Select * from Pts_Workgroups where 
	ProjectID = @ProjectID AND
	WorkgroupID not in 
		(Select WorkgroupID from Pts_ProblemStateTransferWorkgroup where TransferID = @TransferID)





' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProblemStateTimeLimitRoleItems]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemStateTimeLimitRoleItems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetProblemStateTimeLimitRoleItems]
@ProblemStateID int
AS
SELECT * FROM Pts_ProblemStateTimeLimitRoleItems WHERE ProblemStateID = @ProblemStateID Order by ListOrder' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetStepActions]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetStepActions]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetStepActions]
@ProjectID int,
@SrcStateID int,
@DstStateID int
AS
SELECT * FROM Pts_StepActions WHERE ProjectID=@ProjectID AND SrcStateID=@SrcStateID AND DstStateID=@DstStateID
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_UpdateProject]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProject]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE  procedure [sp_Pts_UpdateProject]
@ProjectID int,
@ProjectCatalogID int,
@ProjectCode varchar(50),
@Name nvarchar(50),
@ShortName nvarchar(15),
@Description nvarchar(255),
@ProjectStateID int,
@ProblemTitle nvarchar(10),
@ThreadTitle nvarchar(10),
@RecordTitle nvarchar(10),
@OnlyViewOwnProblem bit,
@EnableProblemState bit,
@EnableMultipleThread bit,
@EnableEmailNotify bit,
@EnableProblemPriority bit,
@EnableProblemSeverity bit,
@EnableProblemType bit,
@EnableProblemCatalog bit,
@EnableProblemDeadline bit,
@VisibleToAll bit,
@EnableStateTimer bit,
@AutoAssignedTo int,
@ExtendedSettings ntext,
@SecurityLevel int,
@KeyWords nvarchar(255)
AS
Update Pts_Projects
Set
	ProjectCatalogID = @ProjectCatalogID,
	ProjectCode = @ProjectCode,
	Name = @Name,
	ShortName = @ShortName,
	Description = @Description,
	ProjectStateID = @ProjectStateID,
	ProblemTitle = @ProblemTitle,
	ThreadTitle = @ThreadTitle,
	RecordTitle = @RecordTitle,
	OnlyViewOwnProblem = @OnlyViewOwnProblem,
	EnableProblemState = @EnableProblemState,
	EnableMultipleThread = @EnableMultipleThread,
	EnableEmailNotify = @EnableEmailNotify,
	EnableProblemPriority = @EnableProblemPriority,
	EnableProblemSeverity = @EnableProblemSeverity,
	EnableProblemType = @EnableProblemType,
	EnableProblemCatalog = @EnableProblemCatalog,
	EnableProblemDeadline = @EnableProblemDeadline,
	VisibleToAll = @VisibleToAll,
	EnableStateTimer = @EnableStateTimer,
	AutoAssignedTo = @AutoAssignedTo,
	ExtendedSettings = @ExtendedSettings,
	SecurityLevel = @SecurityLevel,
	KeyWords = @KeyWords
Where ProjectID = @ProjectID






' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetNewProblemIndex]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetNewProblemIndex]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Pts_GetNewProblemIndex]
@ProjectID int
AS
Declare @NewProblemIndex int

Select @NewProblemIndex = NewProblemIndex From Pts_Projects Where ProjectID = @ProjectID

Update Pts_Projects Set NewProblemIndex = NewProblemIndex + 1 Where ProjectID = @ProjectID

Select  @NewProblemIndex
' 
END
GO
/****** 对象:  View [v_Pts_Problems]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[v_Pts_Problems]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [v_Pts_Problems]
AS
SELECT     Accounts_Users.DisplayName AS CreateUserName, Pts_Problems.ProblemID, Pts_Problems.ProjectID, Pts_Problems.OriginProjectID, 
                      Pts_Problems.ProblemIndex, Pts_Problems.ProblemCatalogID, Pts_Problems.ProblemTypeID, Pts_Problems.ProblemStateID, 
                      Pts_Problems.ProblemSeverityID, Pts_Problems.ProblemPriorityID, Pts_Problems.CreateTime, Pts_Problems.CloseTime, 
                      Pts_Problems.CreateUserID, Pts_Problems.Deadline, Pts_Problems.AssignedTo, Pts_Problems.Title, Pts_Problems.Description, 
                      Pts_Problems.RecordNum, Pts_Problems.LastUser, Pts_Problems.LastTime, Pts_Problems.LastRecord, Pts_Problems.HistoryID, 
                      Pts_Problems.Int1, Pts_Problems.Int2, Pts_Problems.Int3, Pts_Problems.Int4, Pts_Problems.Int5, Pts_Problems.Float1, 
                      Pts_Problems.Float2, Pts_Problems.Float3, Pts_Problems.Float4, Pts_Problems.Float5, Pts_Problems.Date1, Pts_Problems.Date2, 
                      Pts_Problems.Date3, Pts_Problems.Date4, Pts_Problems.Date5, Pts_Problems.Date6, Pts_Problems.Date7, Pts_Problems.Date8, 
                      Pts_Problems.Date9, Pts_Problems.Date10, Pts_Problems.BigText1, Pts_Problems.BigText2, Pts_Problems.BigText3, 
                      Pts_Problems.BigText4, Pts_Problems.BigText5, Pts_Problems.Text1, Pts_Problems.Text2, Pts_Problems.Text3, Pts_Problems.Text4, 
                      Pts_Problems.Text5, Pts_Problems.Select1, Pts_Problems.Select2, Pts_Problems.Select3, Pts_Problems.Select4, Pts_Problems.Select5, 
                      Pts_Problems.Select6, Pts_Problems.Select7, Pts_Problems.Select8, Pts_Problems.Select9, Pts_Problems.Select10, 
                      Pts_Problems.YesNo1, Pts_Problems.YesNo2, Pts_Problems.YesNo3, Pts_Problems.YesNo4, Pts_Problems.YesNo5, Pts_Problems.Url1, 
                      Pts_Problems.Url2, Pts_Problems.Url3, Pts_Problems.Url4, Pts_Problems.Url5, Pts_Problems.BigText6, Pts_Problems.BigText7, 
                      Pts_Problems.BigText8, Pts_Problems.BigText9, Pts_Problems.BigText10, Pts_Problems.Text6, Pts_Problems.Text7, Pts_Problems.Text8, 
                      Pts_Problems.Text9, Pts_Problems.Text10, Pts_Problems.Text11, Pts_Problems.Text12, Pts_Problems.Text13, Pts_Problems.Text14, 
                      Pts_Problems.Text15, Pts_Problems.Text16, Pts_Problems.Text17, Pts_Problems.Text18, Pts_Problems.Text19, Pts_Problems.Text20, 
                      Pts_Problems.Select11, Pts_Problems.Select12, Pts_Problems.Select13, Pts_Problems.Select14, Pts_Problems.Select15, 
                      Pts_Problems.Email1, Pts_Problems.Email2, Pts_Problems.Email3, Pts_Problems.Email4, Pts_Problems.Email5, 
                      Pts_Problems.MultiSelect1, Pts_Problems.MultiSelect2, Pts_Problems.MultiSelect3, Pts_Problems.MultiSelect4, Pts_Problems.MultiSelect5, 
                      Pts_Problems.ArticleID, Pts_ProblemCatalogs.GlobalOrder + Pts_ProblemCatalogs.Name AS ProblemCatalogName, 
                      Pts_ProblemType.TypeName AS ProblemTypeName, Pts_ProblemState.StateName AS ProblemStateName, 
                      Pts_ProblemPriority.PriorityName AS ProblemPriorityName, Pts_ProblemSeverity.SeverityName AS ProblemSeverityName, 
                      Pts_ProblemSeverity.ListOrder AS ProblemSeverityOrder, Pts_ProblemState.ListOrder AS ProblemStateOrder, 
                      Pts_ProblemType.ListOrder AS ProblemTypeOrder, Pts_ProblemCatalogs.ListOrder AS ProblemCatalogOrder, 
                      Pts_ProblemPriority.ListOrder AS ProblemPriorityOrder, Pts_ProblemPriority.DisplayColor AS PriorityDisplayColor, 
                      Pts_ProblemSeverity.DisplayColor AS SeverityDisplayColor, Accounts_Users_1.DisplayName AS AssignedToUserName, Pts_Projects.Name AS ProjectName, 
                      Accounts_Users_1.EmailAddress AS AssignedToEmail, Pts_ProblemCatalogs.Path AS ProblemCatalogPath, Pts_ProblemState.IsFinalState AS IsClosed, 
                      Accounts_Users_2.DisplayName AS LastUserName, Pts_Projects.ProjectCode + N''-'' + CAST(Pts_Problems.ProblemIndex AS nvarchar(10)) AS ProblemCode, 
                      Pts_Projects.ProjectCode, Pts_Problems.StartTime, Pts_Problems.Url10, Pts_Problems.Url9, Pts_Problems.Url8, Pts_Problems.Url7, 
                      Pts_Problems.Url6, Pts_Problems.YesNo20, Pts_Problems.YesNo19, Pts_Problems.YesNo18, Pts_Problems.YesNo17, 
                      Pts_Problems.YesNo16, Pts_Problems.YesNo15, Pts_Problems.YesNo14, Pts_Problems.YesNo13, Pts_Problems.YesNo12, 
                      Pts_Problems.YesNo11, Pts_Problems.YesNo10, Pts_Problems.YesNo9, Pts_Problems.YesNo8, Pts_Problems.YesNo7, 
                      Pts_Problems.YesNo6, Pts_Problems.MultiSelect10, Pts_Problems.MultiSelect9, Pts_Problems.MultiSelect8, Pts_Problems.MultiSelect7, 
                      Pts_Problems.MultiSelect6, Pts_Problems.Email10, Pts_Problems.Email9, Pts_Problems.Email8, Pts_Problems.Email7, 
                      Pts_Problems.Email6, Pts_Problems.Select20, Pts_Problems.Select19, Pts_Problems.Select18, Pts_Problems.Select17, 
                      Pts_Problems.Select16, Pts_Problems.BigText20, Pts_Problems.BigText19, Pts_Problems.BigText18, Pts_Problems.BigText17, 
                      Pts_Problems.BigText16, Pts_Problems.BigText15, Pts_Problems.BigText14, Pts_Problems.BigText13, Pts_Problems.BigText12, 
                      Pts_Problems.BigText11, Pts_Problems.Date20, Pts_Problems.Date19, Pts_Problems.Date18, Pts_Problems.Date17, Pts_Problems.Date16, 
                      Pts_Problems.Date15, Pts_Problems.Date14, Pts_Problems.Date13, Pts_Problems.Date12, Pts_Problems.Date11, Pts_Problems.Float20, 
                      Pts_Problems.Float19, Pts_Problems.Float18, Pts_Problems.Float17, Pts_Problems.Float16, Pts_Problems.Float15, Pts_Problems.Float14, 
                      Pts_Problems.Float13, Pts_Problems.Float12, Pts_Problems.Float11, Pts_Problems.Float10, Pts_Problems.Float9, Pts_Problems.Float8, 
                      Pts_Problems.Float7, Pts_Problems.Float6, Pts_Problems.Int20, Pts_Problems.Int19, Pts_Problems.Int18, Pts_Problems.Int17, 
                      Pts_Problems.Int16, Pts_Problems.Int15, Pts_Problems.Int14, Pts_Problems.Int13, Pts_Problems.Int12, Pts_Problems.Int11, 
                      Pts_Problems.Int10, Pts_Problems.Int9, Pts_Problems.Int8, Pts_Problems.Int7, Pts_Problems.Int6, Pts_Problems.MultiSelect20, 
                      Pts_Problems.MultiSelect19, Pts_Problems.MultiSelect18, Pts_Problems.MultiSelect17, Pts_Problems.MultiSelect16, 
                      Pts_Problems.MultiSelect15, Pts_Problems.MultiSelect14, Pts_Problems.MultiSelect13, Pts_Problems.MultiSelect12, 
                      Pts_Problems.MultiSelect11, Pts_Problems.ReadByAssignee, Pts_Problems.AssigneeReadTime, Pts_Problems.IsCoworking, 
                      Pts_Problems.CoworkInfo, Pts_Problems.ExtField1, Pts_Problems.ExtField2, Pts_Problems.ExtField3, Pts_Problems.ExtField4, 
                      Pts_Problems.ExtField5, Pts_Problems.CustomInputField1, Pts_Problems.CustomInputField2, Pts_Problems.CustomInputField3, 
                      Pts_Problems.CustomInputField4, Pts_Problems.CustomInputField5, Pts_Problems.AttachNum, Pts_Problems.StateDeadline, 
                      Pts_Problems.ParentID, Pts_Problems.TotalChildNum, Pts_Problems.ClosedChildNum, Pts_Problems.IsActive, Pts_Problems.RealStartTime, 
                      Pts_Problems.RealEndTime
FROM         Pts_Problems LEFT OUTER JOIN
                      Accounts_Users AS Accounts_Users_2 ON Pts_Problems.LastUser = Accounts_Users_2.UserID LEFT OUTER JOIN
                      Pts_Projects ON Pts_Problems.ProjectID = Pts_Projects.ProjectID LEFT OUTER JOIN
                      Pts_ProblemSeverity ON Pts_Problems.ProblemSeverityID = Pts_ProblemSeverity.ProblemSeverityID LEFT OUTER JOIN
                      Pts_ProblemType ON Pts_Problems.ProblemTypeID = Pts_ProblemType.ProblemTypeID LEFT OUTER JOIN
                      Pts_ProblemPriority ON Pts_Problems.ProblemPriorityID = Pts_ProblemPriority.ProblemPriorityID LEFT OUTER JOIN
                      Pts_ProblemState ON Pts_Problems.ProblemStateID = Pts_ProblemState.ProblemStateID LEFT OUTER JOIN
                      Pts_ProblemCatalogs ON Pts_Problems.ProblemCatalogID = Pts_ProblemCatalogs.ProblemCatalogID LEFT OUTER JOIN
                      Accounts_Users ON Pts_Problems.CreateUserID = Accounts_Users.UserID LEFT OUTER JOIN
                      Accounts_Users AS Accounts_Users_1 ON Pts_Problems.AssignedTo = Accounts_Users_1.UserID
'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPane1' , N'USER',N'dbo', N'VIEW',N'v_Pts_Problems', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[39] 4[30] 2[14] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = -426
         Left = 0
      End
      Begin Tables = 
         Begin Table = "Pts_Problems"
            Begin Extent = 
               Top = 120
               Left = 672
               Bottom = 429
               Right = 913
            End
            DisplayFlags = 280
            TopColumn = 209
         End
         Begin Table = "Accounts_Users_2"
            Begin Extent = 
               Top = 6
               Left = 262
               Bottom = 133
               Right = 432
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "Pts_Projects"
            Begin Extent = 
               Top = 138
               Left = 38
               Bottom = 265
               Right = 253
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "Pts_ProblemSeverity"
            Begin Extent = 
               Top = 138
               Left = 291
               Bottom = 265
               Right = 477
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "Pts_ProblemType"
            Begin Extent = 
               Top = 270
               Left = 38
               Bottom = 397
               Right = 209
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "Pts_ProblemPriority"
            Begin Extent = 
               Top = 270
               Left = 247
               Bottom = 397
               Right = 430
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "Pts_ProblemState"
            Begin Extent = 
               Top = 402
               Left = 38
               Bottom = 529
         ' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_Problems'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPane2' , N'USER',N'dbo', N'VIEW',N'v_Pts_Problems', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPane2', @value=N'      Right = 209
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "Pts_ProblemCatalogs"
            Begin Extent = 
               Top = 402
               Left = 247
               Bottom = 529
               Right = 433
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "Accounts_Users"
            Begin Extent = 
               Top = 534
               Left = 38
               Bottom = 661
               Right = 208
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "Accounts_Users_1"
            Begin Extent = 
               Top = 534
               Left = 246
               Bottom = 661
               Right = 416
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 241
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
        ' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_Problems'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPane3' , N'USER',N'dbo', N'VIEW',N'v_Pts_Problems', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPane3', @value=N' Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 2490
         Alias = 2760
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_Problems'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPaneCount' , N'USER',N'dbo', N'VIEW',N'v_Pts_Problems', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=3 , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_Problems'
GO
/****** 对象:  StoredProcedure [sp_Pts_CreateProject]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProject]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'









CREATE       Procedure [sp_Pts_CreateProject]
@ProjectCatalogID int,
@Name nvarchar(50),
@ProjectCode nvarchar(50),
@Description nvarchar(255),
@ProjectStateID int,
@ProblemTitle nvarchar(10),
@ThreadTitle nvarchar(10),
@RecordTitle nvarchar(10),
@OnlyViewOwnProblem bit,
@EnableProblemState bit,
@EnableMultipleThread bit,
@EnableEmailNotify bit,
@EnableProblemPriority bit,
@EnableProblemSeverity bit,
@EnableProblemType bit,
@EnableProblemCatalog bit,
@EnableProblemDeadline bit,
@VisibleToAll bit,
@EnableStateTimer bit,
@AutoAssignedTo int,
@ProjectID int output,
@TemplateID int,
@ExtendedSettings ntext,
@SecurityLevel int,
@KeyWords nvarchar(255)
AS

SELECT ProjectID From Pts_Projects Where "Name" = @Name
if @@ROWCOUNT > 0
BEGIN
	Set @ProjectID = -1		--项目名称已经存在
	return
END

SELECT ProjectID From Pts_Projects Where ProjectCode=@ProjectCode
if @@ROWCOUNT>0 
BEGIN
	SET @ProjectID = -2		--项目编码已经存在
	return
END

DECLARE @ListOrder int
SELECT @ListOrder=MAX(ListOrder) FROM Pts_Projects WHERE ProjectCatalogID=@ProjectCatalogID
IF @ListOrder is NULL
BEGIN
	SET @ListOrder=0
END


Insert into Pts_Projects
	(
ProjectCatalogID,
Name,
ProjectCode,
Description,
ProjectStateID ,
ProblemTitle ,
ThreadTitle ,
RecordTitle ,
OnlyViewOwnProblem ,
EnableProblemState ,
EnableMultipleThread ,
EnableEmailNotify ,
EnableProblemPriority ,
EnableProblemSeverity ,
EnableProblemType ,
EnableProblemCatalog ,
EnableProblemDeadline ,
VisibleToAll ,
EnableStateTimer ,
AutoAssignedTo,
ExtendedSettings,
SecurityLevel,
KeyWords,
ListOrder
)
	
values
	(
@ProjectCatalogID,
@Name,
@ProjectCode,
@Description,
@ProjectStateID ,
@ProblemTitle ,
@ThreadTitle ,
@RecordTitle ,
@OnlyViewOwnProblem ,
@EnableProblemState ,
@EnableMultipleThread ,
@EnableEmailNotify ,
@EnableProblemPriority ,
@EnableProblemSeverity ,
@EnableProblemType ,
@EnableProblemCatalog ,
@EnableProblemDeadline ,
@VisibleToAll ,
@EnableStateTimer ,
@AutoAssignedTo,
@ExtendedSettings,
@SecurityLevel,
@KeyWords,
@ListOrder
 )




set @ProjectID = @@Identity
' 
END
GO
/****** 对象:  View [v_Pts_Projects]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[v_Pts_Projects]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [v_Pts_Projects]
AS
SELECT     Pts_Projects.ProjectID, Pts_Projects.ProjectCatalogID, Pts_Projects.ProjectCode, Pts_Projects.Name, Pts_Projects.ShortName, 
                      Pts_Projects.Description, Pts_Projects.ProjectStateID, Pts_Projects.TotalProblems, Pts_Projects.TotalThreads, Pts_Projects.TotalRecords, 
                      Pts_Projects.DateCreated, Pts_Projects.ProblemTitle, Pts_Projects.ThreadTitle, Pts_Projects.RecordTitle, Pts_Projects.OnlyViewOwnProblem, 
                      Pts_Projects.EnableProblemState, Pts_Projects.EnableMultipleThread, Pts_Projects.EnableEmailNotify, Pts_Projects.EnableProblemPriority, 
                      Pts_Projects.EnableProblemSeverity, Pts_Projects.EnableProblemType, Pts_Projects.EnableProblemCatalog, Pts_Projects.EnableProblemDeadline, 
                      Pts_Projects.VisibleToAll, Pts_Projects.EnableStateTimer, Pts_Projects.AutoAssignedTo, Pts_Projects.OpenProblems, 
                      Pts_Projects.NewProblemIndex, Pts_Projects.ExtendedSettings, Pts_ProjectStates.Name AS StateName, Pts_ProjectStates.IsCloseState AS IsClosed, 
                      Pts_Projects.SecurityLevel, Pts_Projects.KeyWords, Pts_Projects.ListOrder
FROM         Pts_Projects INNER JOIN
                      Pts_ProjectStates ON Pts_Projects.ProjectStateID = Pts_ProjectStates.ProjectStateID
'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPane1' , N'USER',N'dbo', N'VIEW',N'v_Pts_Projects', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[41] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "Pts_ProjectStates"
            Begin Extent = 
               Top = 9
               Left = 420
               Bottom = 126
               Right = 580
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "Pts_Projects"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 267
               Right = 238
            End
            DisplayFlags = 280
            TopColumn = 19
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 3420
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_Projects'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPaneCount' , N'USER',N'dbo', N'VIEW',N'v_Pts_Projects', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_Projects'
GO
/****** 对象:  StoredProcedure [sp_Pts_CreateRecord]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateRecord]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--创建
CREATE      Procedure [sp_Pts_CreateRecord]
	@ProblemID int,
	@CreateUser int,
	@IsKeyRecord bit,
	@RecordType int,
	@AssignTo int,
	@Title nvarchar(255),
	@Content ntext,
	@StateID int,
	@RecordClass int,
	@SrcStateID int,
	@SrcUserID int,
	@RecordID int output
AS
	Insert into Pts_Records
		(ProblemID,
		CreateUser,
		IsKeyRecord,
		RecordType,
		AssignTo,
		Title,
		Content,
		StateID,
		RecordClass,
		SrcStateID,
		SrcUserID)
	Values
		(@ProblemID,
		@CreateUser,
		@IsKeyRecord,
		@RecordType,
		@AssignTo,
		@Title,
		@Content,
		@StateID,
		@RecordClass,
		@SrcStateID,
		@SrcUserID)

	Set @RecordID = @@Identity

	if (@@ROWCOUNT = 1)
	BEGIN
	  	Update Pts_Problems Set  RecordNum=RecordNum+1,LastUser=@CreateUser,LastTime=GetDate(),LastRecord=@RecordID Where ProblemID = @ProblemID
		


		Declare @ProjectID int
		Select @ProjectID=ProjectID From Pts_Problems Where ProblemID = @ProblemID
		Update Pts_Projects Set TotalRecords=TotalRecords+1 Where ProjectID = @ProjectID
	END
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_CreateProblemPriority]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProblemPriority]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_CreateProblemPriority]
@ProjectID int,
@PriorityName nvarchar(50),
@DisplayColor varchar(15),
@Description nvarchar(255)
AS
Insert into Pts_ProblemPriority
	(ProjectID, PriorityName, DisplayColor, Description)
Values
	(@ProjectID, @PriorityName, @DisplayColor, @Description)

exec sp_Pts_ResetProblemPriorityOrder	@ProjectID



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_DeleteProblemPriority]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemPriority]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_DeleteProblemPriority]
@ProblemPriorityID int
AS

Declare @ProjectID int
Select @ProjectID = ProjectID From Pts_ProblemPriority where ProblemPriorityID = @ProblemPriorityID

Delete from Pts_ProblemPriority where ProblemPriorityID = @ProblemPriorityID

exec sp_Pts_ResetProblemPriorityOrder @ProjectID



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_UpdateProblemPriority]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemPriority]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





CREATE PROCEDURE [sp_Pts_UpdateProblemPriority]
@ProblemPriorityID int,
@PriorityName nvarchar(50),
@DisplayColor varchar(15),
@Description nvarchar(255)
AS
Update Pts_ProblemPriority 
Set
	PriorityName = @PriorityName,
	DisplayColor = @DisplayColor,
	 Description = @Description
where
	ProblemPriorityID = @ProblemPriorityID



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ResetProblemPriorityOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProblemPriorityOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





CREATE Procedure [sp_Pts_ResetProblemPriorityOrder]
@ProjectID int
AS
Declare @Order int
set @Order = 1
Declare @ProblemPriorityID int
Declare priority_cursor CURSOR
FOR 
	Select ProblemPriorityID From Pts_ProblemPriority Where ProjectID = @ProjectID Order by ListOrder
Open priority_cursor 
Fetch next from priority_cursor into @ProblemPriorityID
while (@@FETCH_STATUS <> -1)
BEGIN
	if (@@FETCH_STATUS <> -2)
	BEGIN
		Update Pts_ProblemPriority set ListOrder = @Order where ProblemPriorityID = @ProblemPriorityID
		Set @Order = @Order + 1		
	END
	Fetch next from priority_cursor into @ProblemPriorityID
END




' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProblemPriorityList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemPriorityList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'







CREATE PROCEDURE [sp_Pts_GetProblemPriorityList]
@ProjectID int
AS
Select * from Pts_ProblemPriority where ProjectID = @ProjectID order by ListOrder




' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ChangeProblemPriorityOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProblemPriorityOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE   procedure [sp_Pts_ChangeProblemPriorityOrder]
(
     @ProblemPriorityID int,
     @MoveUp bit
)
AS
BEGIN
--获取ProjectID
Declare @ProjectID int
Select @ProjectID = ProjectID From Pts_ProblemPriority where ProblemPriorityID = @ProblemPriorityID
if @ProjectID is null
	RETURN
--修改顺序
DECLARE @currentSortValue int
DECLARE @replaceSortValue int

-- Get the current sort order
SELECT @currentSortValue = ListOrder FROM Pts_ProblemPriority WHERE ProblemPriorityID = @ProblemPriorityID

-- Move the item up or down?
IF (@MoveUp = 1)
  BEGIN
    IF (@currentSortValue != 1)
      BEGIN
        SET @replaceSortValue = @currentSortValue - 1

        UPDATE Pts_ProblemPriority SET ListOrder = @currentSortValue WHERE ProjectID = @ProjectID AND ListOrder = @replaceSortValue
        UPDATE Pts_ProblemPriority SET ListOrder = @replaceSortValue WHERE ProblemPriorityID= @ProblemPriorityID
      END
  END

ELSE
  BEGIN
    IF (@currentSortValue < (SELECT MAX(ListOrder) FROM Pts_ProblemPriority))
    BEGIN
      SET @replaceSortValue = @currentSortValue + 1

      UPDATE Pts_ProblemPriority SET ListOrder = @currentSortValue WHERE ProjectID = @ProjectID AND  ListOrder = @replaceSortValue
      UPDATE Pts_ProblemPriority SET ListOrder = @replaceSortValue WHERE ProblemPriorityID= @ProblemPriorityID
    END
  END
END



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetStateEditableFields]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetStateEditableFields]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Pts_GetStateEditableFields]
@ProjectID int,
@ProblemStateID int
AS

Select FieldName 
From Pts_StateEditableFields 
Where ProjectID = @ProjectID AND ProblemStateID = @ProblemStateID

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ResetProblemSeverityOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProblemSeverityOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





CREATE Procedure [sp_Pts_ResetProblemSeverityOrder]
@ProjectID int
AS
Declare @Order int
set @Order = 1
Declare @ProblemSeverityID int
Declare Severity_cursor CURSOR
FOR 
	Select ProblemSeverityID From Pts_ProblemSeverity Where ProjectID = @ProjectID Order by ListOrder
Open Severity_cursor 
Fetch next from Severity_cursor into @ProblemSeverityID
while (@@FETCH_STATUS <> -1)
BEGIN
	if (@@FETCH_STATUS <> -2)
	BEGIN
		Update Pts_ProblemSeverity set ListOrder = @Order where ProblemSeverityID = @ProblemSeverityID
		Set @Order = @Order + 1		
	END
	Fetch next from Severity_cursor into @ProblemSeverityID
END




' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ChangeProblemSeverityOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProblemSeverityOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE   procedure [sp_Pts_ChangeProblemSeverityOrder]
(
     @ProblemSeverityID int,
     @MoveUp bit
)
AS
BEGIN
--获取ProjectID
Declare @ProjectID int
Select @ProjectID = ProjectID From Pts_ProblemSeverity where ProblemSeverityID = @ProblemSeverityID
if @ProjectID is null
	RETURN
--修改顺序
DECLARE @currentSortValue int
DECLARE @replaceSortValue int

-- Get the current sort order
SELECT @currentSortValue = ListOrder FROM Pts_ProblemSeverity WHERE ProblemSeverityID = @ProblemSeverityID

-- Move the item up or down?
IF (@MoveUp = 1)
  BEGIN
    IF (@currentSortValue != 1)
      BEGIN
        SET @replaceSortValue = @currentSortValue - 1

        UPDATE Pts_ProblemSeverity SET ListOrder = @currentSortValue WHERE ProjectID = @ProjectID AND ListOrder = @replaceSortValue
        UPDATE Pts_ProblemSeverity SET ListOrder = @replaceSortValue WHERE ProblemSeverityID= @ProblemSeverityID
      END
  END

ELSE
  BEGIN
    IF (@currentSortValue < (SELECT MAX(ListOrder) FROM Pts_ProblemSeverity))
    BEGIN
      SET @replaceSortValue = @currentSortValue + 1

      UPDATE Pts_ProblemSeverity SET ListOrder = @currentSortValue WHERE ProjectID = @ProjectID AND  ListOrder = @replaceSortValue
      UPDATE Pts_ProblemSeverity SET ListOrder = @replaceSortValue WHERE ProblemSeverityID= @ProblemSeverityID
    END
  END
END



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProblemSeverityList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemSeverityList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'







CREATE PROCEDURE [sp_Pts_GetProblemSeverityList]
@ProjectID int
AS
Select * from Pts_ProblemSeverity where ProjectID = @ProjectID order by ListOrder




' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_UpdateProblemSeverity]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemSeverity]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_UpdateProblemSeverity]
@ProblemSeverityID int,
@SeverityName nvarchar(50),
@DisplayColor varchar(15),
@Description nvarchar(255)
AS
Update Pts_ProblemSeverity 
Set
	SeverityName = @SeverityName, 
	DisplayColor = @DisplayColor,
	Description = @Description
where
	ProblemSeverityID = @ProblemSeverityID



' 
END
GO
/****** 对象:  View [v_Pts_WorkflowSteps]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[v_Pts_WorkflowSteps]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [v_Pts_WorkflowSteps]
AS
SELECT     Pts_WorkflowSteps.StepID, Pts_WorkflowSteps.ProjectID, Pts_WorkflowSteps.SrcStateID, Pts_WorkflowSteps.DstStateID, 
                      Pts_WorkflowSteps.StepName, Pts_WorkflowSteps.Description, Pts_WorkflowSteps.AssignType, Pts_WorkflowSteps.Assignee, 
                      Pts_WorkflowSteps.SelectableAssigneeWorkgroupIDs, Pts_WorkflowSteps.SelectableAssigneeUserIDs, Pts_WorkflowSteps.ListOrder, 
                      Pts_WorkflowSteps.RecordTitle, Pts_WorkflowSteps.CommonMessages, Pts_WorkflowSteps.AssignInfoExtendedSettings, 
                      Pts_WorkflowSteps.StepExtendedSettings, src.StateName AS SrcStateName, src.Description AS SrcStateDescription, src.ListOrder AS SrcStateListOrder, 
                      src.IsFinalState AS SrcStateIsFinalState, dst.StateName AS DstStateName, dst.Description AS DstStateDescription, dst.ListOrder AS DstStateListOrder, 
                      dst.IsFinalState AS DstStateIsFinalState
FROM         Pts_WorkflowSteps INNER JOIN
                      Pts_ProblemState AS src ON Pts_WorkflowSteps.SrcStateID = src.ProblemStateID INNER JOIN
                      Pts_ProblemState AS dst ON Pts_WorkflowSteps.DstStateID = dst.ProblemStateID'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPane1' , N'USER',N'dbo', N'VIEW',N'v_Pts_WorkflowSteps', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = -96
         Left = 0
      End
      Begin Tables = 
         Begin Table = "Pts_WorkflowSteps"
            Begin Extent = 
               Top = 21
               Left = 22
               Bottom = 265
               Right = 343
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "src"
            Begin Extent = 
               Top = 0
               Left = 398
               Bottom = 162
               Right = 562
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "dst"
            Begin Extent = 
               Top = 163
               Left = 401
               Bottom = 325
               Right = 565
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_WorkflowSteps'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPaneCount' , N'USER',N'dbo', N'VIEW',N'v_Pts_WorkflowSteps', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_WorkflowSteps'
GO
/****** 对象:  StoredProcedure [sp_Pts_Workflow_GetAvilibleNextStateList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Workflow_GetAvilibleNextStateList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'--获取问题状态列表中未被设置为某状态的可转化状态的列表
CREATE   PROCEDURE [sp_Pts_Workflow_GetAvilibleNextStateList]
@ProblemStateID int
AS
Declare @ProjectID int
select @ProjectID=ProjectID From Pts_ProblemState Where ProblemStateID = @ProblemStateID
Select * from Pts_ProblemState where 
	ProjectID = @ProjectID AND
	--ProblemStateID <> @ProblemStateID AND
	ProblemStateID not in
	(Select DstStateID as ProblemStateID from Pts_WorkflowSteps where SrcStateID = @ProblemStateID) 
Order by 
	ListOrder' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ResetProblemStateOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProblemStateOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE Procedure [sp_Pts_ResetProblemStateOrder]
@ProjectID int
AS
Declare @Order int
set @Order = 1
Declare @ProblemStateID int
Declare state_cursor CURSOR
FOR 
	Select ProblemStateID From Pts_ProblemState Where ProjectID = @ProjectID Order by ListOrder
Open state_cursor 
Fetch next from state_cursor into @ProblemStateID
while (@@FETCH_STATUS <> -1)
BEGIN
	if (@@FETCH_STATUS <> -2)
	BEGIN
		Update Pts_ProblemState set ListOrder = @Order where ProblemStateID = @ProblemStateID
		Set @Order = @Order + 1		
	END
	Fetch next from state_cursor into @ProblemStateID
END



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProblemStateList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemStateList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_GetProblemStateList]
@ProjectID int
AS
IF @ProjectID > 0 
BEGIN
Select * from Pts_ProblemState where ProjectID = @ProjectID order by ListOrder
END
ELSE
BEGIN
Select * from Pts_ProblemState order by ListOrder
END


' 
END
GO
/****** 对象:  View [v_Pts_Records]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[v_Pts_Records]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [v_Pts_Records]
AS
SELECT     Pts_Records.RecordID, Pts_Records.ProblemID, Pts_Records.CreateUser, Pts_Records.DateCreated, Pts_Records.IsKeyRecord, 
                      Pts_Records.RecordType, Pts_Records.AssignTo, Pts_Records.Title, Pts_Records.[Content], Pts_Records.StateID, Pts_Records.HistoryID, 
                      Accounts_Users.DisplayName AS CreateUserName, Accounts_Users_1.DisplayName AS AssignToUserName, 
                      Pts_ProblemState.StateName AS ProblemStateName, Pts_Records.RecordClass, Pts_Records.SrcStateID, Pts_Records.SrcUserID, 
                      Pts_Records.ResponseWorkTime, Pts_Records.ResponseTotalTime, Pts_Records.RealTimeCost, Pts_Records.ExtInt1, Pts_Records.ExtInt2, 
                      Pts_Records.ExtInt3, Pts_Records.ExtString1, Pts_Records.ExtString2, Pts_Records.ExtString3, Pts_Records.ExtNtext1, 
                      Pts_Records.ExtNtext2, Pts_Records.ParentRecordID, Pts_Records.RepresentingUserID
FROM         Pts_Records LEFT OUTER JOIN
                      Pts_ProblemState ON Pts_Records.StateID = Pts_ProblemState.ProblemStateID LEFT OUTER JOIN
                      Accounts_Users ON Pts_Records.CreateUser = Accounts_Users.UserID LEFT OUTER JOIN
                      Accounts_Users AS Accounts_Users_1 ON Pts_Records.AssignTo = Accounts_Users_1.UserID
'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPane1' , N'USER',N'dbo', N'VIEW',N'v_Pts_Records', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "Pts_Records"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 319
               Right = 210
            End
            DisplayFlags = 280
            TopColumn = 13
         End
         Begin Table = "Pts_ProblemState"
            Begin Extent = 
               Top = 126
               Left = 38
               Bottom = 243
               Right = 288
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "Accounts_Users"
            Begin Extent = 
               Top = 6
               Left = 236
               Bottom = 123
               Right = 396
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "Accounts_Users_1"
            Begin Extent = 
               Top = 246
               Left = 236
               Bottom = 363
               Right = 396
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 29
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = ' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_Records'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPane2' , N'USER',N'dbo', N'VIEW',N'v_Pts_Records', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPane2', @value=N'1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_Records'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPaneCount' , N'USER',N'dbo', N'VIEW',N'v_Pts_Records', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=2 , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_Records'
GO
/****** 对象:  StoredProcedure [sp_Pts_GetWorkgroupNotPermitInitStates]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetWorkgroupNotPermitInitStates]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create PROCEDURE [sp_Pts_GetWorkgroupNotPermitInitStates]
@WorkgroupID int,
@ProjectID int
AS
Select * From 
	Pts_ProblemState 
Where ProjectID = @ProjectID AND 
	ProblemStateID NOT in
	(Select ProblemStateID From Pts_ProblemInitStateWorkgroup Where WorkgroupID = @WorkgroupID)


' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetStatesHaveNoRelateStep]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetStatesHaveNoRelateStep]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'create procedure [sp_Pts_GetStatesHaveNoRelateStep]
@ProjectID int
AS
SELECT * FROM [Pts_ProblemState] 
WHERE ProjectID=@ProjectID 
	AND IsFinalState=0 
	AND ProblemStateID NOT IN (SELECT SrcStateID FROM Pts_WorkflowSteps WHERE ProjectID=@ProjectID)' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetWorkgroupPermitInitStates]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetWorkgroupPermitInitStates]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [sp_Pts_GetWorkgroupPermitInitStates]
@WorkgroupID int
AS
Select * From 
	Pts_ProblemState 
Where ProblemStateID in
	(Select ProblemStateID From Pts_ProblemInitStateWorkgroup Where WorkgroupID = @WorkgroupID)


' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetNextStateListByUser]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetNextStateListByUser]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE  PROCEDURE [sp_Pts_GetNextStateListByUser]
@UserID int,
@ProblemStateID int
AS
Select Pts_ProblemState.*, Pts_ProblemStateTransfer.TransferID From
	Pts_ProblemState inner join Pts_ProblemStateTransfer 
		on Pts_ProblemState.ProblemStateID = Pts_ProblemStateTransfer.ToProblemStateID
Where
	Pts_ProblemStateTransfer.FromProblemStateID = @ProblemStateID AND
	TransferID in
	(Select TransferID From Pts_ProblemStateTransferWorkgroup Where WorkgroupID in
		(Select WorkgroupID From Pts_WorkgroupUsers Where UserID = @UserID))
Order by  Pts_ProblemState.ListOrder





' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetNextStateList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetNextStateList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE  PROCEDURE [sp_Pts_GetNextStateList]
@ProblemStateID int
AS
Select Pts_ProblemState.*, Pts_ProblemStateTransfer.TransferID From
	Pts_ProblemState inner join Pts_ProblemStateTransfer 
		on Pts_ProblemState.ProblemStateID = Pts_ProblemStateTransfer.ToProblemStateID
Where
	Pts_ProblemStateTransfer.FromProblemStateID = @ProblemStateID
Order by  Pts_ProblemState.ListOrder




' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProjectStateTransferList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectStateTransferList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



----返回某个项目的所有问题状态切换类型列表
CREATE PROCEDURE [sp_Pts_GetProjectStateTransferList]
@ProjectID int
AS
Select a.TransferID as TransferID,
	a.FromProblemStateID as FromProblemStateID,
	a.ToProblemStateID as ToProblemStateID,
	b.StateName as FromProblemStateName,
	c.StateName as ToProblemStatename
From
	Pts_ProblemStateTransfer as a,
	Pts_ProblemState as b,
	Pts_ProblemState as c
Where 
	b.ProblemStateID = a.FromProblemStateID
	and c.ProblemStateID = a.ToProblemStateID
	and a.FromProblemStateID in
	(Select ProblemStateID from Pts_ProblemState where ProjectID = @ProjectID)



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetAvilibleNextStateList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetAvilibleNextStateList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





--获取问题状态列表中未被设置为某状态的可转化状态的列表
CREATE   PROCEDURE [sp_Pts_GetAvilibleNextStateList]
@ProblemStateID int
AS
Declare @ProjectID int
select @ProjectID=ProjectID From Pts_ProblemState Where ProblemStateID = @ProblemStateID
Select * from Pts_ProblemState where 
	ProjectID = @ProjectID AND
	ProblemStateID <> @ProblemStateID AND
	ProblemStateID not in
	(Select ToProblemStateID as ProblemStateID from Pts_ProblemStateTransfer where FromProblemStateID = @ProblemStateID) 
Order by 
	ListOrder





' 
END
GO
/****** 对象:  StoredProcedure [sp_Kb_RemoveUserFromArticleViewer]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_RemoveUserFromArticleViewer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Kb_RemoveUserFromArticleViewer]
@UserID int,
@ArticleID int
AS
DELETE FROM Kb_ArticleViewer WHERE ArticleID = @ArticleID AND UserID = @UserID' 
END
GO
/****** 对象:  StoredProcedure [sp_Kb_AddUserToArticleViewer]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_AddUserToArticleViewer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Kb_AddUserToArticleViewer]
@UserID int,
@ArticleID int
AS
IF not Exists(SELECT UserID FROM Kb_ArticleViewer WHERE UserID = @UserID AND ArticleID = @ArticleID)
BEGIN
  INSERT INTO Kb_ArticleViewer (ArticleID, UserID) VALUES(@ArticleID, @UserID)
END
' 
END
GO
/****** 对象:  StoredProcedure [sp_Kb_CanUserViewArticle]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_CanUserViewArticle]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Kb_CanUserViewArticle]
@UserID int,
@ArticleID int
AS
Declare @IsPublic as bit
SELECT @IsPublic=IsPublic FROM Kb_Articles WHERE ArticleID = @ArticleID

IF @IsPublic = 1 
BEGIN
	SELECT 1
	return
END
ELSE
BEGIN
	SELECT COUNT(*) 
	FROM Kb_ArticleViewer
	WHERE
	UserID = @UserID AND ArticleID = @ArticleID
END
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ResetProblemTypeOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProblemTypeOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE Procedure [sp_Pts_ResetProblemTypeOrder]
@ProjectID int
AS
Declare @Order int
set @Order = 1
Declare @ProblemTypeID int
Declare Type_cursor CURSOR
FOR 
	Select ProblemTypeID From Pts_ProblemType Where ProjectID = @ProjectID order by ListOrder
Open Type_cursor 
Fetch next from Type_cursor into @ProblemTypeID
while (@@FETCH_STATUS <> -1)
BEGIN
	if (@@FETCH_STATUS <> -2)
	BEGIN
		Update Pts_ProblemType set ListOrder = @Order where ProblemTypeID = @ProblemTypeID
		Set @Order = @Order + 1		
	END
	Fetch next from Type_cursor into @ProblemTypeID
END



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_UpdateProblemType]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'







CREATE PROCEDURE [sp_Pts_UpdateProblemType]
@ProblemTypeID int,
@TypeName nvarchar(50),
@Description nvarchar(255)
AS
Update Pts_ProblemType 
Set
	TypeName = @TypeName, Description = @Description
where
	ProblemTypeID = @ProblemTypeID





' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProblemTypeList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemTypeList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_GetProblemTypeList]
@ProjectID int
AS
Select * from Pts_ProblemType where ProjectID = @ProjectID order by ListOrder



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ChangeProblemTypeOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProblemTypeOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE   procedure [sp_Pts_ChangeProblemTypeOrder]
(
     @ProblemTypeID int,
     @MoveUp bit
)
AS
BEGIN
--获取ProjectID
Declare @ProjectID int
Select @ProjectID = ProjectID From Pts_ProblemType where ProblemTypeID = @ProblemTypeID
if @ProjectID is null
	RETURN
--修改顺序
DECLARE @currentSortValue int
DECLARE @replaceSortValue int

-- Get the current sort order
SELECT @currentSortValue = ListOrder FROM Pts_ProblemType WHERE ProblemTypeID = @ProblemTypeID

-- Move the item up or down?
IF (@MoveUp = 1)
  BEGIN
    IF (@currentSortValue != 1)
      BEGIN
        SET @replaceSortValue = @currentSortValue - 1

        UPDATE Pts_ProblemType SET ListOrder = @currentSortValue WHERE ProjectID = @ProjectID AND ListOrder = @replaceSortValue
        UPDATE Pts_ProblemType SET ListOrder = @replaceSortValue WHERE ProblemTypeID= @ProblemTypeID
      END
  END

ELSE
  BEGIN
    IF (@currentSortValue < (SELECT MAX(ListOrder) FROM Pts_ProblemType))
    BEGIN
      SET @replaceSortValue = @currentSortValue + 1

      UPDATE Pts_ProblemType SET ListOrder = @currentSortValue WHERE ProjectID = @ProjectID AND  ListOrder = @replaceSortValue
      UPDATE Pts_ProblemType SET ListOrder = @replaceSortValue WHERE ProblemTypeID= @ProblemTypeID
    END
  END
END




' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_DeleteAccessList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_DeleteAccessList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Accounts_DeleteAccessList]
@AccessListID int
AS

Delete From Accounts_AccessListItems Where AccessListID = @AccessListID
Update Accounts_Users Set AccessListID=null Where AccessListID = @AccessListID

Delete From Accounts_AccessLists Where AccessListID = @AccessListID

' 
END
GO
/****** 对象:  View [v_Pts_ProblemHistory]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[v_Pts_ProblemHistory]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N'
CREATE VIEW [v_Pts_ProblemHistory]
AS
SELECT Pts_ProblemHistory.*, 
      Accounts_Users.DisplayName AS UserName
FROM Accounts_Users RIGHT OUTER JOIN
      Pts_ProblemHistory ON 
      Accounts_Users.UserID = Pts_ProblemHistory.UserID

'
GO
/****** 对象:  View [v_Pts_SharedItems]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[v_Pts_SharedItems]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [v_Pts_SharedItems]
AS
SELECT     Pts_SharedItems.*, Accounts_Users.DisplayName AS CreateUserName, Accounts_Users.EmailAddress
FROM         Pts_SharedItems LEFT OUTER JOIN
                      Accounts_Users ON Pts_SharedItems.CreateUser = Accounts_Users.UserID
'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPane1' , N'USER',N'dbo', N'VIEW',N'v_Pts_SharedItems', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "Pts_SharedItems"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 290
               Right = 198
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "Accounts_Users"
            Begin Extent = 
               Top = 6
               Left = 236
               Bottom = 292
               Right = 436
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 13
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_SharedItems'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPaneCount' , N'USER',N'dbo', N'VIEW',N'v_Pts_SharedItems', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_SharedItems'
GO
/****** 对象:  View [v_Pts_SharedItemAttachments]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[v_Pts_SharedItemAttachments]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [v_Pts_SharedItemAttachments]
AS
SELECT     Pts_SharedItemAttachments.*, Accounts_Users.DisplayName AS CreateUserName
FROM         Pts_SharedItemAttachments LEFT OUTER JOIN
                      Accounts_Users ON Pts_SharedItemAttachments.CreateUser = Accounts_Users.UserID
'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPane1' , N'USER',N'dbo', N'VIEW',N'v_Pts_SharedItemAttachments', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "Pts_SharedItemAttachments"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 186
               Right = 198
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "Accounts_Users"
            Begin Extent = 
               Top = 6
               Left = 236
               Bottom = 286
               Right = 441
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_SharedItemAttachments'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPaneCount' , N'USER',N'dbo', N'VIEW',N'v_Pts_SharedItemAttachments', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_SharedItemAttachments'
GO
/****** 对象:  View [v_Pts_RecordAttachments]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[v_Pts_RecordAttachments]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N'
CREATE VIEW [v_Pts_RecordAttachments]
AS
SELECT Pts_RecordAttachments.*, 
      Accounts_Users.DisplayName AS CreateUserName
FROM Pts_RecordAttachments LEFT OUTER JOIN
      Accounts_Users ON 
      Pts_RecordAttachments.CreateUserID = Accounts_Users.UserID

'
GO
/****** 对象:  StoredProcedure [sp_Accounts_GetUsersNotInRole]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetUsersNotInRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE procedure [sp_Accounts_GetUsersNotInRole]
@RoleID int
AS
Select * From Accounts_Users where UserID not in
(Select UserID From Accounts_UserRoles Where RoleID = @RoleID)
Order By DisplayName



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetWorkgroupUsers]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetWorkgroupUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE  PROCEDURE [sp_Pts_GetWorkgroupUsers]
@WorkgroupID int
AS
Select u.*,r.IsManager 
from 
	Accounts_Users u inner join Pts_WorkgroupUsers r on u.UserID = r.UserID
Where
	r.WorkgroupID = @WorkgroupID
Order by u.DisplayName





' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_DeleteUser]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_DeleteUser]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





--删除用户
CREATE  PROCEDURE [sp_Accounts_DeleteUser]
@UserID int
AS
     --首先删除所有与用户相关的信息，然后删除用户
Begin Transaction
    Delete From Accounts_UserRoles Where UserID = @UserID
    Delete From Accounts_Users WHere UserID = @UserID
commit transaction

if @@Error <> 0
	Rollback transaction





' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_GetRoleUsers]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetRoleUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Accounts_GetRoleUsers]
@RoleID int
AS
Select u.* from 
	Accounts_Users  u inner join Accounts_UserRoles ur on u.UserID = ur.UserID 
Where
	ur.RoleID = @RoleID
ORDER BY 
	DisplayName



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetUsersNotInWorkgroup]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUsersNotInWorkgroup]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



Create Procedure [sp_Pts_GetUsersNotInWorkgroup]
@WorkgroupID int
AS
Select * from Accounts_Users where UserID not in
(
Select UserID from Pts_WorkgroupUsers where WorkgroupID = @WorkgroupID
)



' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_ValidateUserIP]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_ValidateUserIP]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'

CREATE  Procedure [sp_Accounts_ValidateUserIP]
@UserID int,
@ClientIP bigint
AS
--验证用户IP地址的合法性
--返回值：大于0表示成功，否则表示失败，绝对值表示验证到的步骤
DECLARE @UserAccessListID int--用户绑定的控制列表ID
DECLARE @UserBindIP bigint  --用户绑定的IP地址

--1.如果用户地址是127.0.0.1则通过验证
if (@ClientIP = 2130706433)
  begin
	select 1
	return 1
  end
else
 begin
  Select @UserAccessListID=AccessListID,@UserBindIP=BindIP From Accounts_Users Where UserID = @UserID

  --2.验证用户的IP绑定，如果没有配置，则继续其他验证。如果配置了，则执行该项检查，成功则通过，否则拒绝
  if (@UserBindIP is not null AND @UserBindIP <> 0)
    begin
      if (@ClientIP = @UserBindIP)
        begin
          select 2
          return 2
        end
      else
        begin
          select -2
          return -2
        end
    end

   --3.验证用户的控制列表，如果该项没有配置，则继续其他验证。如果该项配置，但所对应的控制列表中IP范围的数量为
   --  0，那么也继续其它验证，否则进行该项验证
    Declare @ItemCount int
    if (@UserAccessListID is not null)
    begin
      Select @ItemCount = Count(*) From Accounts_AccessListItems Where AccessListID = @UserAccessListID
      if (@ItemCount = 0)
        begin
          select 3
          return 3
        end
      else
        begin
	  Select @ItemCount = Count(*) From Accounts_AccessListItems Where AccessListID = @UserAccessListID AND StartIP <= @ClientIP and EndIP >= @ClientIP
          if (@ItemCount > 0)
            begin
              select 3
              return 3
            end
          else
            begin
              select -3
              return -3
            end
        end
     end

--4.验证默认控制列表
     Select @ItemCount = Count(*) From Accounts_AccessListItems Where AccessListID = 0
     if (@ItemCount = 0)
       begin
          select 4
          return 4
       end
     else
        begin
	  Select @ItemCount = Count(*) From Accounts_AccessListItems Where AccessListID = 0 AND StartIP <= @ClientIP and EndIP >= @ClientIP
          if (@ItemCount > 0)
            begin
              select 4
              return 4
            end
          else
            begin
              select -4
              return -4
            end
        end
  end


' 
END
GO
/****** 对象:  View [v_Pts_ProblemAttachments]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[v_Pts_ProblemAttachments]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N'
CREATE VIEW [v_Pts_ProblemAttachments]
AS
SELECT Pts_ProblemAttachments.*, 
      Accounts_Users.DisplayName AS CreateUserName
FROM Accounts_Users RIGHT OUTER JOIN
      Pts_ProblemAttachments ON 
      Accounts_Users.UserID = Pts_ProblemAttachments.CreateUserID

'
GO
/****** 对象:  StoredProcedure [sp_Kb_DeleteArticle]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_DeleteArticle]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'

CREATE  PROCEDURE [sp_Kb_DeleteArticle]
@ArticleID int
AS

DECLARE @ProblemID int
SELECT @ProblemID = RelateProblemID From Kb_Articles Where ArticleID = @ArticleID

DELETE FROM Kb_ArticleComment WHERE ArticleID = @ArticleID
DELETE FROM Kb_ArticleAttachments WHERE ArticleID = @ArticleID

DELETE FROm Kb_Articles WHERE ArticleID = @ArticleID

if (@ProblemID > 0)
  Update Pts_Problems Set ArticleID = null Where ProblemID = @ProblemID



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetUserViewableFields]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserViewableFields]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		CuiLiang
-- Create date: <Create Date,,>
-- Description:	Get all viewable fields 
-- 
-- =============================================
CREATE PROCEDURE [sp_Pts_GetUserViewableFields]
@ProjectID int,
@UserID int,
@ProblemID int
AS
BEGIN
--字段对所有人可见，
-- 或者字段对创建人可见，而且用户是事务的创建人;或者用户在所允许的工作组中
SELECT * FROM Pts_DbDirectory
WHERE	ProjectID = @ProjectID
	AND
		(IsSystem = 0 or (IsSystem = 1 AND InUse = 1))
	AND
	(
		ReadableToAll = 1 
		 OR 
		(
			(ReadableToCreateUser = 1 AND Exists(SELECT ProblemID FROM Pts_Problems WHERE ProblemID = @ProblemID AND CreateUserID=@UserID))
			OR
			ID IN (SELECT FieldID FROM Pts_ProblemFieldReadableWorkgroup WHERE 
				WorkgroupID IN (SELECT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID = @UserID)
					)
		)
	)
	AND 
	(
		ID NOT IN (SELECT FieldID FROM Pts_ProblemFieldHiddenState WHERE ProblemStateID = (SELECT ProblemStateID FROM Pts_Problems WHERE ProblemID = @ProblemID))
	)
		
ORDER BY 
	ListOrder ASC


END' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetEditableFieldsWhenProcessIssue]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetEditableFieldsWhenProcessIssue]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		CuiLiang
-- Create date: <Create Date,,>
-- Description:	Get all editable fields when processing issue
-- =============================================
CREATE PROCEDURE [sp_Pts_GetEditableFieldsWhenProcessIssue]
@ProjectID int,
@UserID int,
@ProblemID int,
@SrcStateID int,
@DestStateID int
AS
BEGIN

SELECT * FROM Pts_DbDirectory
WHERE	ProjectID = @ProjectID
	AND
		(IsSystem = 0 or (IsSystem = 1 AND InUse = 1))
	AND
		ID in (SELECT FieldID FROM Pts_ProblemFieldEditableState WHERE
			(FromStateID = @SrcStateID AND ToStateID = @DestStateID)
			OR
			(FromStateID = @SrcStateID AND ToStateID = 0)
			OR
			(FromStateID = 0 AND ToStateID = @DestStateID)	)		
	AND
		(
			EditableToAll = 1
			OR
			(EditableToCreateUser = 1 AND Exists(SELECT ProblemID FROM Pts_Problems WHERE ProblemID = @ProblemID AND CreateUserID=@UserID))
			OR
			ID in 
				(SELECT FieldID FROM Pts_ProblemFieldEditableWorkgroup 
				WHERE WorkgroupID in (SELECT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID = @UserID)
				)
		)
		
ORDER BY 
	ListOrder ASC


END' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetEditableFieldsWhenEditIssue]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetEditableFieldsWhenEditIssue]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		CuiLiang
-- Create date: <Create Date,,>
-- Description:	Get all editable fields when editing issue
-- =============================================
CREATE PROCEDURE [sp_Pts_GetEditableFieldsWhenEditIssue]
@ProjectID int,
@UserID int,
@ProblemID int,
@DestStateID int
AS
BEGIN

SELECT * FROM Pts_DbDirectory
WHERE	ProjectID = @ProjectID
	AND
		(IsSystem = 0 or (IsSystem = 1 AND InUse = 1))
	AND
		VisibleWhenEdit = 1
	AND
		(
			EditableToAll = 1
			OR
			(EditableToCreateUser = 1 AND Exists(SELECT ProblemID FROM Pts_Problems WHERE ProblemID = @ProblemID AND CreateUserID=@UserID))
			OR
			ID in 
				(SELECT FieldID FROM Pts_ProblemFieldEditableWorkgroup 
				WHERE WorkgroupID in (SELECT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID = @UserID)
				)
		)
		
ORDER BY 
	ListOrder ASC


END' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_UpdateProblemAttachNum]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemAttachNum]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'create procedure [sp_Pts_UpdateProblemAttachNum]
@ProblemID int
AS

DECLARE @AttachNum int
SELECT @AttachNum=COUNT(1) FROM Pts_ProblemAttachments  WHERE ProblemID=@ProblemID

UPDATE Pts_Problems SET AttachNum=@AttachNum WHERE ProblemID=@ProblemID
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_SetAssigneeReadFlag]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_SetAssigneeReadFlag]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'create procedure [sp_Pts_SetAssigneeReadFlag]
@ProblemID int
AS
Update Pts_Problems SET ReadByAssignee=1,AssigneeReadTime=GetDate() WHERE ProblemID=@ProblemID AND ReadByAssignee=0' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetStartTimeExpiredSuspendedIssues]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetStartTimeExpiredSuspendedIssues]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [sp_Pts_GetStartTimeExpiredSuspendedIssues]
--得到到达开始时间，并且设置为自动激活的被挂起的事务
@Projects nvarchar(1000)
AS
select ProblemID FROM Pts_Problems 
WHERE ProjectID in (@Projects) 
	AND IsActive=0 
	AND StartTime < GetDate()
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Utility_UpdateAllIssueAttachNum]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Utility_UpdateAllIssueAttachNum]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'create procedure [sp_Pts_Utility_UpdateAllIssueAttachNum]
AS
--更新所有事务的附件数量
declare rs CURSOR LOCAL SCROLL FOR
select problemID, COUNT(1) FROM Pts_ProblemAttachments GROUP BY ProblemID

DECLARE @ProblemID int
DECLARE @AttachNum int

OPEN rs
FETCH NEXT FROM rs INTO @ProblemID,@AttachNum

WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE Pts_Problems SET AttachNum=@AttachNum WHERE ProblemID=@ProblemID
FETCH NEXT FROM rs INTO @ProblemID,@AttachNum
END

CLOSE rs
' 
END
GO
/****** 对象:  StoredProcedure [sp_Kb_AddArticle]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_AddArticle]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--添加文章
CREATE     PROCEDURE [sp_Kb_AddArticle]
@CategoryID int,
@Title nvarchar(255),
@KeyWords nvarchar(1024),
@Summary nvarchar(2048),
@Content ntext,
@IsRepost bit,
@Source nvarchar(255),
@SourceUrl nvarchar(255),
@ValueLevel tinyint,
@Difficulty tinyint,
@CreateUser nvarchar(50),
@RelateProblemID int,
@OriginAuthor nvarchar(50),
@CreateUserID int,
@Note1 ntext,
@Note2 ntext,
@Note3 ntext,
@Note4 ntext,
@Note5 ntext
AS
Insert into Kb_Articles
(CategoryID,Title,  KeyWords, Summary, Content, IsRepost, 
Source, SourceUrl, ValueLevel, Difficulty,
CreateUser, RelateProblemID, OriginAuthor, CreateUserID,
Note1,Note2, Note3, Note4, Note5)
Values
(@CategoryID, @Title,  @KeyWords, @Summary, @Content, @IsRepost, 
@Source, @SourceUrl, @ValueLevel, @Difficulty,
@CreateUser, @RelateProblemID, @OriginAuthor, @CreateUserID,
@Note1, @Note2, @Note3, @Note4, @Note5)

DECLARE @ArticleID int

SELECT @ArticleID=@@IDENTITY

IF (@RelateProblemID > 0)
BEGIN
  Update Pts_Problems Set ArticleID = @ArticleID Where ProblemID = @RelateProblemID
END

Select @ArticleID' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetWorkflowStepBySrcAndDstStates]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetWorkflowStepBySrcAndDstStates]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetWorkflowStepBySrcAndDstStates]
@SrcStateID int,
@DstStateID int
AS
SELECT * FROM Pts_WorkflowSteps 
WHERE SrcStateID=@SrcStateID AND DstStateID=@DstStateID' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_RecordProblemEnd]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RecordProblemEnd]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE  Procedure [sp_Pts_RecordProblemEnd]
@ProblemID int
AS

Declare @OldHistoryID int
Select @OldHistoryID = HistoryID From Pts_ProblemStateRecord Where EndTime is null

if (@OldHistoryID is not null)
	Update Pts_ProblemStateRecord Set EndTime = GetDate(), KeepTime = DateDiff(mi, StartTime, GetDate()) Where HistoryID = @OldHistoryID





' 
END
GO
/****** 对象:  View [v_Pts_ProblemStateRecord]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[v_Pts_ProblemStateRecord]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N'



CREATE VIEW [v_Pts_ProblemStateRecord]
AS
SELECT Pts_ProblemStateRecord.*
FROM Pts_ProblemStateRecord
WHERE (KeepTime IS NOT NULL)




'
GO
/****** 对象:  StoredProcedure [sp_Pts_RecordStateStart]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RecordStateStart]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





--记录某个状态的开始
CREATE    Procedure [sp_Pts_RecordStateStart]
@ProblemID int,
@StateID int,
@UserID int
AS
--首先关闭以前的状态，然后记录一条新的状态开始

Declare @OldHistoryID int
Declare @CurrentTime datetime

set @CurrentTime = GetDate()

Select @OldHistoryID = HistoryID From Pts_ProblemStateRecord 
	Where ProblemID = @ProblemID AND EndTime is null

if (@OldHistoryID is not null)
	Update Pts_ProblemStateRecord Set EndTime = @CurrentTime, KeepTime=DateDiff(mi,StartTime, @CurrentTime)
	Where HistoryID = @OldHistoryID

Insert Into Pts_ProblemStateRecord (ProblemID, StateID, StartTime,  UserID)
	Values (@ProblemID, @StateID, @CurrentTime, @UserID)








' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_CreateProblemStateTransfer]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProblemStateTransfer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE  PROCEDURE [sp_Pts_CreateProblemStateTransfer] 
@FromProblemStateID int,
@ToProblemStateID int,
@TransferID int output
AS
Insert into Pts_ProblemStateTransfer
	(FromProblemStateID, ToProblemStateID)
Values
	(@FromProblemStateID, @ToProblemStateID)

Set @TransferID = @@Identity





' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_DeleteProblemStateTransfer]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemStateTransfer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE  PROCEDURE [sp_Pts_DeleteProblemStateTransfer]
@TransferID int
AS
Begin Transaction
Delete From Pts_ProblemStateTransferWorkgroup Where TransferID = @TransferID
Delete From Pts_ProblemStateTransfer where TransferID = @TransferID
commit transaction




' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProblemCreateRoleByInitialState]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemCreateRoleByInitialState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetProblemCreateRoleByInitialState]
@ProjectID int,
@InitStateID int
AS
--得到创建事务时的创建规则
SELECT * FROM Pts_ProblemCreateRoles WHERE ProjectID=@ProjectID AND DstStateID=@InitStateID' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetExpiredTimers]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetExpiredTimers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'create procedure [sp_Pts_GetExpiredTimers]
@ExpireTime Datetime
AS
SELECT * FROM [Pts_ProblemTimers1] WHERE NextExpireTime < @ExpireTime' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_WorkgroupAddPermitState]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_WorkgroupAddPermitState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create PROCEDURE [sp_Pts_WorkgroupAddPermitState]
@WorkgroupID int,
@ProblemStateID int
AS
Insert into Pts_ProblemInitStateWorkgroup
	(WorkgroupID, ProblemStateID)
Values
	(@WorkgroupID, @ProblemStateID)

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_WorkgroupDeletePermitState]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_WorkgroupDeletePermitState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create PROCEDURE [sp_Pts_WorkgroupDeletePermitState]
@WorkgroupID int,
@ProblemStateID int
AS
Delete From Pts_ProblemInitStateWorkgroup
Where
	WorkgroupID = @WorkgroupID AND ProblemStateID = @ProblemStateID

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Notify_Delete]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Notify_Delete]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_Notify_Delete]
	@NotifyID int 
AS

DELETE FROM Pts_Notify WHERE NotifyID = @NotifyID' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Notify_Insert]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Notify_Insert]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_Notify_Insert]
	@TargetType int,
	@TargetID int,
	@NotifyUsers ntext,
	@EventType int,
	@CCMails ntext,
	@Sender nvarchar(255),
	@EventTime datetime,
	@NotifyID int OUTPUT
AS

SET NOCOUNT ON

INSERT INTO [Pts_Notify] (
	[TargetType],
	[TargetID],
	[NotifyUsers],
	[EventType],
	[CCMails],
	[Sender],
	[EventTime]
) VALUES (
	@TargetType,
	@TargetID,
	@NotifyUsers,
	@EventType,
	@CCMails,
	@Sender,
	@EventTime
)

SET @NotifyID = SCOPE_IDENTITY()' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_AddNotifyTask]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddNotifyTask]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'create procedure [sp_Pts_AddNotifyTask]
@TargetType int,
	@TargetID int,
	@NotifyUsers nvarchar(1024),
	@EventType int,
	@CCMails nvarchar(2048),
	@Sender nvarchar(20),
	@EventTime datetime,
    @NotifyAssignee bit
as
--添加一条通知任务
INSERT INTO [Pts_Notify] (
	[TargetType],
	[TargetID],
	[NotifyUsers],
	[EventType],
	[CCMails],
	[Sender],
	[EventTime],
    [NotifyAssignee]
) VALUES (
	@TargetType,
	@TargetID,
	@NotifyUsers,
	@EventType,
	@CCMails,
	@Sender,
	@EventTime,
    @NotifyAssignee
)

SELECT  @@IDENTITY' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetNotitfyTasks]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetNotitfyTasks]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'create procedure [sp_Pts_GetNotitfyTasks]
AS
SELECT * FROM Pts_Notify' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_AddWorkgroupToTransfer]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddWorkgroupToTransfer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--给某工作组增加实施某个状态转换的权利
CREATE  PROCEDURE [sp_Pts_AddWorkgroupToTransfer]
@WorkgroupID int,
@TransferID int
AS

Select ID From Pts_ProblemStateTransferWorkgroup Where WorkgroupID = @WorkgroupID AND TransferID = @TransferID

if @@ROWCOUNT = 0
BEGIN
	Insert Into Pts_ProblemStateTransferWorkgroup (TransferID, WorkgroupID) values	(@TransferID, @WorkgroupID)
END






' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_RemoveWorkgroupFromTransfer]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveWorkgroupFromTransfer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





--删除某工作组的实施某个状态转换的权利
CREATE Procedure [sp_Pts_RemoveWorkgroupFromTransfer]
@WorkgroupID int,
@TransferID int
AS

Delete From Pts_ProblemStateTransferWorkgroup Where TransferID = @TransferID AND WorkgroupID = @WorkgroupID





' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetFilterList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetFilterList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'

CREATE  Procedure [sp_Pts_GetFilterList]
@ProjectID int,
@UserID int
AS

SELECT * FROM Pts_Filters WHERE (ProjectID = @ProjectID) AND (IsPublic = 1 OR (IsPublic=0 AND CreateUserID = @UserID))


' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_RemoveFilter]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveFilter]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_RemoveFilter]
@FilterID int
AS

Delete From Pts_FilterNodes Where FilterID = @FilterID

Delete From Pts_Filters Where FilterID = @FilterID

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_UpdateFilter]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateFilter]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'


Create   PROCEDURE [sp_Pts_UpdateFilter]
@FilterID int,
@FilterName nvarchar(255),
@Description nvarchar(1024),
@IsPublic bit,
@MaxNodeID int
AS
--更新Filter的描述信息
UPDATE
  Pts_Filters
Set
  FilterName = @FilterName, Description = @Description, IsPublic = @IsPublic, MaxNodeID = @MaxNodeID
WHERE
  FilterID = @FilterID

--删除Filter原有的Node列表
DELETE FROM Pts_FilterNodes WHERE FilterID = @FilterID


' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_CreateFilter]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateFilter]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'

CREATE  PROCEDURE [sp_Pts_CreateFilter]
@FilterName nvarchar(255),
@Description nvarchar(1024),
@ProjectID int,
@IsPublic bit,
@MaxNodeID int,
@CreateDate datetime,
@CreateUserID int
AS

Insert into Pts_Filters 
 (FilterName, Description, ProjectID, IsPublic, MaxNodeID, CreateDate, CreateUserID)
Values
 (@FilterName, @Description, @ProjectID, @IsPublic, @MaxNodeID, @CreateDate, @CreateUserID)

Select @@IDENTITY as FilterID

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetFilterInfo]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetFilterInfo]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_GetFilterInfo]
@FilterID int
AS

Select * From Pts_Filters Where FilterID = @FilterID

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetConnectorNotitfyTasks]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetConnectorNotitfyTasks]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'create procedure [sp_Pts_GetConnectorNotitfyTasks]
AS
SELECT * FROM Pts_OutConnectorNotifys' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_CreateFilterNode]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateFilterNode]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [sp_Pts_CreateFilterNode]
@FilterID int,
@NodeID int,
@IsLeaf bit,
@SupportRuntimeValue bit,
@LeftChildID int,
@RightChildID int,
@ParentID int,
@IsLeftChild bit,
@OperatorString nvarchar(50),
@IsNot bit,
@FieldTitle nvarchar(255),
@FieldName nvarchar(255),
@CompareString nvarchar(50),
@FieldValue nvarchar(1024),
@FieldDisplayValue nvarchar(2048)

AS

Insert into Pts_FilterNodes
 ( FilterID,
  NodeID,
  IsLeaf,
  SupportRuntimeValue,
  LeftChildID,
  RightChildID,
  ParentID,
  IsLeftChild,
  OperatorString,
  IsNot,
  FieldTitle,
  FieldName,
  CompareString,
  FieldValue, 
  FieldDisplayValue
)
 Values
 ( @FilterID,
  @NodeID,
  @IsLeaf,
  @SupportRuntimeValue,
  @LeftChildID,
  @RightChildID,
  @ParentID,
  @IsLeftChild,
  @OperatorString,
  @IsNot,
  @FieldTitle,
  @FieldName,
  @CompareString,
  @FieldValue, 
  @FieldDisplayValue
)
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetFilterNodes]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetFilterNodes]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_GetFilterNodes]
@FilterID int
AS
Select * FROM Pts_FilterNodes Where FilterID = @FilterID

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetEditableFieldsWhenCreateIssue]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetEditableFieldsWhenCreateIssue]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		CuiLiang
-- Create date: <Create Date,,>
-- Description:	Get all editable fields when creating issue
-- =============================================
CREATE PROCEDURE [sp_Pts_GetEditableFieldsWhenCreateIssue]
@ProjectID int,
@UserID int,
@DestStateID int
AS
BEGIN

SELECT * FROM Pts_DbDirectory
WHERE	ProjectID = @ProjectID
	AND
		(IsSystem = 0 or (IsSystem = 1 AND InUse = 1))
	AND
		VisibleWhenNew = 1
	AND
		(
			EditableToAll = 1
			OR
			EditableToCreateUser = 1
			OR 
			ID in 
				(SELECT FieldID FROM Pts_ProblemFieldEditableWorkgroup 
				WHERE WorkgroupID in (SELECT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID = @UserID)
				)
		)
ORDER BY 
	ListOrder ASC


END' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetUserEditableFieldByWorkgroup]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserEditableFieldByWorkgroup]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE procedure [sp_Pts_GetUserEditableFieldByWorkgroup]
@ProjectID int,
@UserID int
AS
--得到根据用户所在工作组所确定的可编辑字段的列表
SELECT DISTINCT FieldID FROM Pts_ProblemFieldEditableWorkgroup 
				WHERE WorkgroupID in (SELECT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID = @UserID)' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_AddUserToWorkgroup]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddUserToWorkgroup]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_AddUserToWorkgroup]
@UserID int,
@WorkgroupID int
AS
Insert into Pts_WorkgroupUsers (WorkgroupID, UserID) values (@WorkgroupID, @UserID)



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_RemoveUserFromWorkgroup]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveUserFromWorkgroup]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE  PROCEDURE [sp_Pts_RemoveUserFromWorkgroup]
@WorkgroupID int,
@UserID int
AS
Delete from Pts_WorkgroupUsers where WorkgroupID=@WorkgroupID and UserID = @UserID




' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_SaveProblemChange]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_SaveProblemChange]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Pts_SaveProblemChange]
@ProblemID int,
@UserID int,
@ChangeDate DateTime,
@Content ntext
AS
Insert into Pts_ProblemHistory
 (ProblemID,UserID, ChangeDate, Content)
Values
 (@ProblemID,@UserID, @ChangeDate, @Content)

SELECT @@IDENTITY
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_AddProblemRelation]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddProblemRelation]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		CuiLiang
-- Create date: 2007-4-2
-- Description:	create a relation of two problems
-- =============================================
CREATE PROCEDURE [sp_Pts_AddProblemRelation] 
	-- Add the parameters for the stored procedure here
	@ProblemID1 int = 0, 
	@ProblemID2 int = 0,

@RelationType int = 0
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
if not Exists(SELECT * FROM Pts_ProblemRelations WHERE ProblemID_1 = @ProblemID1 AND ProblemID_2 = @ProblemID2 AND RelationType = @RelationType)
BEGIN
	-- interfering with SELECT statements.
INSERT INTO Pts_ProblemRelations
(ProblemID_1, ProblemID_2, RelationType)
Values(@ProblemID1, @ProblemID2, @RelationType)

END	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT @ProblemID1, @ProblemID2
END
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_RemoveProblemRelation]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveProblemRelation]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_RemoveProblemRelation]
@ProblemID1 int,
@ProblemID2 int,
@RelationType int
AS

DELETE FROM Pts_ProblemRelations
WHERE ProblemID_1 = @ProblemID1 AND ProblemID_2 = @ProblemID2 AND RelationType=@RelationType' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetSharedItem]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSharedItem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetSharedItem] 
@ItemID int
AS
SELECT * FROM Pts_SharedItems WHERE ItemID = @ItemID' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetRecentSharedItems]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetRecentSharedItems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetRecentSharedItems]
@ProjectID int
AS
--获取最新的共享项
SELECT TOP 5 ItemID,Title,UpdateDate FROM Pts_SharedItems WHERE ProjectID=@ProjectID order by updatedate desc' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetSharedItemTypes]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSharedItemTypes]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		CuiLiang
-- Create date: 20070414
-- Description:	获取一个项目内共享列表项的分类名称
-- =============================================
CREATE PROCEDURE [sp_Pts_GetSharedItemTypes]
@ProjectID int
AS
SELECT DISTINCT [Type] FROM Pts_SharedItems WHERE ProjectID=@ProjectID ORDER BY [Type]' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_AddSharedItem]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddSharedItem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_AddSharedItem]
@ProjectID int,
@Title nvarchar(1024),
@Description ntext,
@Type  nvarchar(100),
@KeyWords nvarchar(1024),
@CreateUser int,
@CreateDate datetime,
@Options int
AS
INSERT INTO Pts_SharedItems
(ProjectID, Title, [Description], [Type], KeyWords, CreateUser, CreateDate, UpdateDate, Options)
VALUES
(@ProjectID, @Title, @Description, @Type, @KeyWords, @CreateUser, @CreateDate, @CreateDate, @Options)

SELECT @@IDENTITY' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_UpdateSharedItem]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateSharedItem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_UpdateSharedItem]
@ItemID int,
@Title nvarchar(1024),
@Description ntext,
@Type  nvarchar(100),
@KeyWords nvarchar(1024),
@UpdateDate datetime,
@Options int
AS
UPDATE Pts_SharedItems
SET
 Title = @Title, Description = @Description, Type = @Type, KeyWords = @KeyWords, UpdateDate = @UpdateDate, Options = @Options
WHERE 
ItemID = @ItemID' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_CreateFavoriteLink]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateFavoriteLink]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_CreateFavoriteLink]
@UserID int,
@Title nvarchar(100),
@Link nvarchar(1024)
AS
--SELECT * FROM Pts_FavoriteLinks WHERE UserID=@UserID ORDER BY ListOrder,Title

DECLARE @ListOrder int
SELECT @ListOrder=Max(ListOrder) FROM Pts_FavoriteLinks WHERE UserID=@UserID

if (@ListOrder is null)
BEGIN
SET @ListOrder = 1
END
ELSE
BEGIN
SET @ListOrder = @ListOrder+1
END

INSERT INTO Pts_FavoriteLinks (UserID, Title, Link, ListOrder,CreateTime) values (@UserID, @Title, @Link, @ListOrder, GetDate())' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_IsFavoriteLinkExists]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_IsFavoriteLinkExists]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_IsFavoriteLinkExists]
@UserID int,
@Link nvarchar(1024)
AS
SELECT count(1) FROM Pts_FavoriteLinks WHERE UserID=@UserID AND Link = @Link' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetUserFavoriteLinks]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserFavoriteLinks]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetUserFavoriteLinks]
@UserID int
AS
SELECT * FROM Pts_FavoriteLinks WHERE UserID=@UserID ORDER BY Title' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_RemoveSharedItemAttachment]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveSharedItemAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create PROCEDURE [sp_Pts_RemoveSharedItemAttachment]
@AttachmentID int
AS
DELETE FROM Pts_SharedItemAttachments WHERE AttachmentID = @AttachmentID


' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetSharedItemAttachment]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSharedItemAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetSharedItemAttachment]
@AttachmentID int
AS
SELECT * FROM Pts_SharedItemAttachments
WHERE AttachmentID = @AttachmentID' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_AddSharedItemAttachment]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddSharedItemAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





Create   Procedure [sp_Pts_AddSharedItemAttachment]
@ItemID int,
@SaveName varchar(255),
@OriginName varchar(255),
@FileSize int,
@Description nvarchar(255),
@CreateUser nvarchar(50),
@CreateDate datetime
AS
Insert into Pts_SharedItemAttachments 
  ( ItemID, SaveName, OriginName, FileSize, Description, CreateUser, CreateDate)
Values
  ( @ItemID, @SaveName, @OriginName, @FileSize, @Description, @CreateUser, @CreateDate)






' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_UpdateProblemCatalog]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE  PROCEDURE [sp_Pts_UpdateProblemCatalog]
@ProblemCatalogID int,
@Name nvarchar(50),
@Description nvarchar(255),
@ResponseUser int=null
AS
Update Pts_ProblemCatalogs Set
	Name = @Name,
	Description = @Description,
	ResponseUser = @ResponseUser
Where
	ProblemCatalogID = @ProblemCatalogID




' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_DeleteProblemCatalog]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





CREATE   PROCEDURE [sp_Pts_DeleteProblemCatalog]
@ProblemCatalogID int
AS
Declare @ParentID int
Declare @ProjectID int

Select @ParentID = ParentID,@ProjectID = ProjectID From Pts_ProblemCatalogs where ProblemCatalogID = @ProblemCatalogID

exec sp_Pts_DeleteProblemCatalogAndChild @ProblemCatalogID

exec sp_Pts_ResetProblemCatalogOrder @ProjectID,@ParentID




' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_DeleteProblemCatalogAndChild]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemCatalogAndChild]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





CREATE    PROCEDURE [sp_Pts_DeleteProblemCatalogAndChild]
@ProblemCatalogID int
AS

Declare @ChildCatalogID int

Select Top 1 @ChildCatalogID=ProblemCatalogID from Pts_ProblemCatalogs where ParentID = @ProblemCatalogID
while (@ChildCatalogID is not null)
BEGIN
	exec sp_Pts_DeleteProblemCatalogAndChild @ChildCatalogID
	Delete From Pts_ProblemCatalogs Where ProblemCatalogID = @ChildCatalogID

	--Next
	set @ChildCatalogID = null
	Select Top 1 @ChildCatalogID=ProblemCatalogID from Pts_ProblemCatalogs where ParentID = @ProblemCatalogID
END


Delete from Pts_ProblemCatalogs where ProblemCatalogID = @ProblemCatalogID





' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ResetProblemCatalogGlobalOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProblemCatalogGlobalOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE       Procedure [sp_Pts_ResetProblemCatalogGlobalOrder]
@ProjectID int,
@ParentID  int,
@GlobalOrder varchar(100) 
AS

if @GlobalOrder is null
	Set @GlobalOrder = ""

Declare @ProblemCatalogID int
Declare @ListOrder int
Declare @GlobalOrder1 varchar(100)
Declare @Order int

Set @Order = 1
Select Top 1 @ProblemCatalogID = ProblemCatalogID From Pts_ProblemCatalogs where ProjectID = @ProjectID AND ParentID = @ParentID AND ListOrder=@Order
while (@ProblemCatalogID is not null)
BEGIN
	--Update
	Set @GlobalOrder1 = @GlobalOrder +  CAST(@Order as varchar(20)) + "."
	
	Update Pts_ProblemCatalogs Set GlobalOrder = @GlobalOrder1 where ProblemCatalogID = @ProblemCatalogID
	EXEC sp_Pts_ResetProblemCatalogGlobalOrder @ProjectID, @ProblemCatalogID, @GlobalOrder1
	--Next
	Set @Order = @Order + 1
	set @ProblemCatalogID = null
	Select Top 1 @ProblemCatalogID = ProblemCatalogID From Pts_ProblemCatalogs where ProjectID = @ProjectID AND ParentID = @ParentID AND ListOrder=@Order
END



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProblemCtalogList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemCtalogList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_GetProblemCtalogList]
@ProjectID int
AS
Select * from Pts_ProblemCatalogs 
where ProjectID = @ProjectID
Order by ListOrder



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_LogProblemVisit]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_LogProblemVisit]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'

Create procedure [sp_Pts_LogProblemVisit]
(
@UserID int,
@ProblemID int
)
AS
--记录用户访问事务的历史。每增加一条数据，删除一次原有的数据，保持每个用户最多40条记录
--检查记录是否存在，
--如果存在,取该记录VisitIndex为VisitIndex1,设置该记录VisitIndex=0，更新所有VisitIndex小于VisitIndex1的记录VisitIndex=VisitIndex+1
--如果不存在，更新所有VisitIndex=VisitIndex+1
--删除order>40的记录
--添加新记录,新记录的VisitIndex=1,

DECLARE @OldIndex int
DECLARE @VisitID int

SELECT @OldIndex=VisitIndex, @VisitID=VisitID 
  FROM Pts_ProblemVisitHistory 
  WHERE UserID = @UserID AND ProblemID = @ProblemID

if @@ROWCOUNT>0
BEGIN
  UPDATE Pts_ProblemVisitHistory SET VisitIndex=0 WHERE VisitID=@VisitID
  UPDATE Pts_ProblemVisitHistory SET VisitIndex=VisitIndex+1 WHERE UserID=@UserID AND VisitIndex<@OldIndex
END
ELSE
BEGIN
  UPDATE Pts_ProblemVisitHistory SET VisitIndex=VisitIndex+1 Where UserID=@UserID
  DELETE Pts_ProblemVisitHistory WHERE UserID = @UserID AND VisitIndex>40 
  INSERT INTO Pts_ProblemVisitHistory (UserID, ProblemID, VisitIndex) VALUES (@UserID, @ProblemID, 1) 
END


' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_UpdateRecordType]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateRecordType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'






CREATE PROCEDURE [sp_Pts_UpdateRecordType]
@RecordTypeID int,
@TypeName nvarchar(50),
@Description nvarchar(255)
AS
Update Pts_RecordType 
Set
	TypeName = @TypeName, Description = @Description
where
	RecordTypeID = @RecordTypeID






' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ResetRecordTypeOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetRecordTypeOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE Procedure [sp_Pts_ResetRecordTypeOrder]
@ProjectID int
AS
Declare @Order int
set @Order = 1
Declare @RecordTypeID int
Declare Type_cursor CURSOR
FOR 
	Select RecordTypeID From Pts_RecordType Where ProjectID = @ProjectID
Open Type_cursor 
Fetch next from Type_cursor into @RecordTypeID
while (@@FETCH_STATUS <> -1)
BEGIN
	if (@@FETCH_STATUS <> -2)
	BEGIN
		Update Pts_RecordType set ListOrder = @Order where RecordTypeID = @RecordTypeID
		Set @Order = @Order + 1		
	END
	Fetch next from Type_cursor into @RecordTypeID
END






' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ChangeRecordTypeOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeRecordTypeOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE   procedure [sp_Pts_ChangeRecordTypeOrder]
(
     @RecordTypeID int,
     @MoveUp bit
)
AS
BEGIN
--获取ProjectID
Declare @ProjectID int
Select @ProjectID = ProjectID From Pts_RecordType where RecordTypeID = @RecordTypeID
if @ProjectID is null
	RETURN
--修改顺序
DECLARE @currentSortValue int
DECLARE @replaceSortValue int

-- Get the current sort order
SELECT @currentSortValue = ListOrder FROM Pts_RecordType WHERE RecordTypeID = @RecordTypeID

-- Move the item up or down?
IF (@MoveUp = 1)
  BEGIN
    IF (@currentSortValue != 1)
      BEGIN
        SET @replaceSortValue = @currentSortValue - 1

        UPDATE Pts_RecordType SET ListOrder = @currentSortValue WHERE ProjectID = @ProjectID AND ListOrder = @replaceSortValue
        UPDATE Pts_RecordType SET ListOrder = @replaceSortValue WHERE RecordTypeID= @RecordTypeID
      END
  END

ELSE
  BEGIN
    IF (@currentSortValue < (SELECT MAX(ListOrder) FROM Pts_RecordType))
    BEGIN
      SET @replaceSortValue = @currentSortValue + 1

      UPDATE Pts_RecordType SET ListOrder = @currentSortValue WHERE ProjectID = @ProjectID AND  ListOrder = @replaceSortValue
      UPDATE Pts_RecordType SET ListOrder = @replaceSortValue WHERE RecordTypeID= @RecordTypeID
    END
  END
END



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_CreateRecordType]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateRecordType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_CreateRecordType]
@ProjectID int,
@TypeName nvarchar(50),
@Description nvarchar(255)
AS
Insert into Pts_RecordType
	(ProjectID, TypeName, Description)
Values
	(@ProjectID, @TypeName, @Description)

exec sp_Pts_ResetRecordTypeOrder	@ProjectID








' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetRecordTypeList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetRecordTypeList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





CREATE PROCEDURE [sp_Pts_GetRecordTypeList]
@ProjectID int
AS
Select * from Pts_RecordType where ProjectID = @ProjectID order by ListOrder






' 
END
GO
/****** 对象:  View [v_Kb_ArticleList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[v_Kb_ArticleList]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [v_Kb_ArticleList]
AS
SELECT Kb_Articles.ArticleID, Kb_Articles.Title, Kb_Articles.KeyWords, 
      Kb_Articles.Summary, Kb_Categories.Path, Kb_Articles.CategoryID, 
      Kb_Articles.UpdateDate, Kb_Articles.Content, Kb_Articles.Source, 
      Kb_Articles.SourceUrl, Kb_Categories.CategoryName, 
      Kb_Articles.CreateDate, Kb_Articles.CreateUser, Kb_Articles.IsReviewed, 
      Kb_Articles.ReviewedBy, Kb_Articles.Note1, Kb_Articles.Note2, 
      Kb_Articles.Note3, Kb_Articles.Note4, Kb_Articles.Note5, 
      Kb_Articles.OriginAuthor, Kb_Articles.CreateUserID
FROM Kb_Articles INNER JOIN
      Kb_Categories ON Kb_Articles.CategoryID = Kb_Categories.CategoryID
'
GO
/****** 对象:  StoredProcedure [sp_Pts_GetRequiredFieldsWhenProcessIssue]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetRequiredFieldsWhenProcessIssue]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'create procedure [sp_Pts_GetRequiredFieldsWhenProcessIssue]
@FromStateID int,
@ToStateID int
AS
SELECT Distinct FieldID 
FROM Pts_ProblemFieldEditableState 
WHERE IsRequired=1 AND ((FromStateID=@FromStateID AND ToStateID=@ToStateID) OR (FromStateID=@FromStateID AND ToStateID=0) OR (FromStateID=0 AND ToStateID=@ToStateID))' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProblemRelatedUserIDs]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemRelatedUserIDs]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'create procedure [sp_Pts_GetProblemRelatedUserIDs]
@ProblemID int
AS
SELECT UserID FROM Pts_RelatedUsers WHERE ProblemID=@ProblemID' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_RelatedUser_Add]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RelatedUser_Add]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_RelatedUser_Add]
@ProblemID int,
@UserID int
AS
IF NOT EXISTS(SELECT * FROM Pts_RelatedUsers WHERE ProblemID=@ProblemID AND UserID=@UserID)
BEGIn
INSERT INTO Pts_RelatedUsers (ProblemID, UserID) VALUES(@ProblemID, @UserID)
END' 
END
GO
/****** 对象:  StoredProcedure [sp_Kb_AddArticleAttachment]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_AddArticleAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





Create   Procedure [sp_Kb_AddArticleAttachment]
@ArticleID int,
@SaveName varchar(255),
@OriginName varchar(255),
@FileSize int,
@Description nvarchar(255),
@CreateUser nvarchar(50),
@CreateDate datetime
AS
Insert into Kb_ArticleAttachments 
  ( ArticleID,SaveName, OriginName, FileSize, Description, CreateUser, CreateDate)
Values
  ( @ArticleID, @SaveName, @OriginName, @FileSize, @Description, @CreateUser, @CreateDate)





' 
END
GO
/****** 对象:  StoredProcedure [sp_Kb_GetArticleAttachments]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetArticleAttachments]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create PROCEDURE [sp_Kb_GetArticleAttachments]
@ArticleID int
AS
SELECT * FROM Kb_ArticleAttachments WHERE ArticleID = @ArticleID


' 
END
GO
/****** 对象:  StoredProcedure [sp_Kb_RemoveArticleAttachment]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_RemoveArticleAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create PROCEDURE [sp_Kb_RemoveArticleAttachment]
@AttachmentID int
AS
DELETE FROM Kb_ArticleAttachments WHERE AttachmentID = @AttachmentID


' 
END
GO
/****** 对象:  StoredProcedure [sp_Kb_AddComment]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_AddComment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'


CREATE   Procedure [sp_Kb_AddComment]
@ArticleID int,
@Content ntext,
@AddUser nvarchar(50),
@Rate tinyint,
@Attachment nvarchar(255)
AS
Insert into Kb_ArticleComment
(ArticleID, Content, AddUser, Rate, Attachment)
Values
(@ArticleID, @Content, @AddUser, @Rate, @Attachment)

if (@Rate > 0)
BEGIN
  UPDATE Kb_Articles Set	
	RateValue = (RateValue*NumRatings +@Rate)/(NumRatings+ 1),
	NumRatings = NumRatings+ 1
  WHERE
	ArticleID = @ArticleID
END

UPDATE Kb_Articles Set NumComment = NumComment+1 WHERE ArticleID = @ArticleID
	

SELECT @@IDENTITY




' 
END
GO
/****** 对象:  StoredProcedure [sp_Kb_GetComments]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetComments]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Kb_GetComments]
@ArticleID int
AS
SELECT * FROM
	Kb_ArticleComment 
WHERE ArticleID = @ArticleID
Order by AddDate Desc

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_RemoveSubscription]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveSubscription]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_RemoveSubscription]
@UserID int,
@TargetType int,
@TargetID int,
@EventType int,
@Params nvarchar(1024)
AS
DELETE FROM Pts_Subscription WHERE UserID=@UserID AND TargetType=@TargetType AND TargetID=@TargetID AND EventType=@EventType' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Subscription_Add]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Subscription_Add]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_Subscription_Add]
@UserID int,
@TargetType int,
@TargetID int,
@EventType int,
@Params nvarchar(1024)
AS
if NOT EXISTS(SELECT * FROM Pts_Subscription WHERE UserID=@UserID AND TargetType=@TargetType AND TargetID=@TargetID AND EventType=@EventType AND Params=@Params)
BEGIN
INSERT INTO Pts_Subscription (UserID,TargetType, TargetID, EventType, Params) VALUES(@UserID,@TargetType,@TargetID, @EventType, @Params)
END' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetSubscribeUserIDs]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSubscribeUserIDs]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE procedure [sp_Pts_GetSubscribeUserIDs]
@TargetType int,
@TargetID int,
@EventType int,
@Params nvarchar(1024)
AS
SELECT UserID FROM Pts_Subscription WHERE TargetType=@TargetType AND TargetID=@TargetID AND EventType=@EventType AND ((EventType=@EventType and Params=@Params) or EventType=0)  ' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_IsItemSubscribed]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_IsItemSubscribed]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_IsItemSubscribed]
@UserID int,
@TargetType int,
@TargetID int,
@EventType int,
@Params nvarchar(1024)
AS
SELECT COUNT(*) FROM Pts_Subscription WHERE UserID=@UserID 
	AND TargetType=@TargetType 
	AND TargetID=@TargetID 
	AND ((EventType=@EventType and Params=@Params) or EventType=0)  ' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_RemoveUserFromRole]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_RemoveUserFromRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--将用户从某角色中删除
CREATE PROCEDURE [sp_Accounts_RemoveUserFromRole]
@UserID int,
@RoleID int
AS
	DELETE Accounts_UserRoles WHERE UserID = @UserID AND RoleID = @RoleID





' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_AddUserToRole]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_AddUserToRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--将用户添加到某个角色中
CREATE PROCEDURE [sp_Accounts_AddUserToRole]
@UserID int,
@RoleID int
AS
	DECLARE @Count int
	
	SELECT @Count = Count(UserID) FROM Accounts_UserRoles WHERE
		RoleID = @RoleID AND UserID = @UserID

	IF @Count = 0
		INSERT INTO Accounts_UserRoles(UserID, RoleID)
		VALUES(@UserID, @RoleID)





' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_GetEffectivePermissionList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetEffectivePermissionList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Accounts_GetEffectivePermissionList] @UserID int
AS
 SELECT DISTINCT PermissionID
  FROM Accounts_RolePermissions
  WHERE RoleID IN
    (SELECT RoleID FROM Accounts_UserRoles WHERE UserID = @UserID)



' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_GetPermissionList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetPermissionList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'






--取得特定角色的权限列表，如果角色ID为空时，取所有权限的列表
CREATE   PROCEDURE [sp_Accounts_GetPermissionList] @RoleID int=NULL
AS
  if @RoleID IS NULL
    SELECT 	p.PermissionID, 
		p.Description, 
		p.CategoryID,
	   	c.Description as CategoryName, 
		(N''‘'' + c.Description + N''’'' + p.Description) AS FullName
      FROM Accounts_Permissions p INNER JOIN Accounts_PermissionCategories c ON p.CategoryID = c.CategoryID
      ORDER BY p.CategoryID,p.PermissionID
  ELSE
    SELECT 
	ap.PermissionID, 
	ap.Description, 
	ap.CategoryID,
	apc.Description AS CategoryName,
	(N''‘'' + apc.Description + ''’N'' + ap.Description) AS FullName
      FROM Accounts_Permissions ap
	INNER JOIN Accounts_RolePermissions apr   ON ap.PermissionID = apr.PermissionID
      	INNER JOIN Accounts_PermissionCategories apc ON ap.CategoryID = apc.CategoryID
      WHERE apr.RoleID = @RoleID
      ORDER BY ap.CategoryID,ap.PermissionID





' 
END
GO
/****** 对象:  View [v_Accounts_Permissions]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[v_Accounts_Permissions]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [v_Accounts_Permissions]
AS
SELECT p.*, pc.Description AS CategoryName, 
      N''‘'' + pc.Description + N''’'' + p.Description AS FullName
FROM Accounts_PermissionCategories pc INNER JOIN
      Accounts_Permissions p ON pc.CategoryID = p.CategoryID'
GO
/****** 对象:  StoredProcedure [sp_Accounts_ClearPermissionsFromRole]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_ClearPermissionsFromRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--清除角色权限
CREATE PROCEDURE [sp_Accounts_ClearPermissionsFromRole]
@RoleID int
AS
	DELETE Accounts_RolePermissions WHERE RoleID = @RoleID





' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_AddPermissionToRole]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_AddPermissionToRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Accounts_AddPermissionToRole]
@RoleID int,
@PermissionID int
AS
	DECLARE @Count int

	SELECT @Count = Count(PermissionID) FROM Accounts_RolePermissions WHERE
		RoleID = @RoleID and PermissionID = @PermissionID
	
	IF @Count = 0
		INSERT INTO Accounts_RolePermissions(RoleID, PermissionID)
		VALUES(@RoleID, @PermissionID)





' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_RemovePermissionFromRole]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_RemovePermissionFromRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--删除角色的某个权限    
CREATE PROCEDURE [sp_Accounts_RemovePermissionFromRole]
@RoleID int,
@PermissionID int
AS
	DELETE Accounts_RolePermissions WHERE RoleID = @RoleID and PermissionID = @PermissionID





' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_GetAccessLists]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetAccessLists]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [sp_Accounts_GetAccessLists]
AS
Select * From 
Accounts_AccessLists
' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_AddAccessList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_AddAccessList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Accounts_AddAccessList]
@AccessListName nvarchar(80)
AS
Insert into Accounts_AccessLists
(AccessListName)
values
(@AccessListName)

Select @@IDENTITY
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_AddRecordAttachment]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddRecordAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE  Procedure [sp_Pts_AddRecordAttachment]
@RecordID int,
@SaveName nvarchar(255),
@OriginName nvarchar(255),
@FileSize int,
@Description nvarchar(255),
@CreateUserID int,
@CreateDate datetime
AS
Insert into Pts_RecordAttachments 
  ( RecordID,SaveName, OriginName, FileSize, Description, CreateUserID, CreateDate)
Values
  ( @RecordID, @SaveName, @OriginName, @FileSize, @Description, @CreateUserID, @CreateDate)' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProblemRelatedArticles]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemRelatedArticles]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'create procedure [sp_Pts_GetProblemRelatedArticles]
@ProblemID int
AS
SELECT ArticleID,Title,Summary FROM KB_Articles WHERE ArticleID in (SELECT ArticleID FROM Pts_ProblemRelatedArticles WHERE ProblemID=@ProblemID) ORDER BY TItle' 
END
GO
/****** 对象:  StoredProcedure [sp_Kb_AddReadCount]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_AddReadCount]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Kb_AddReadCount]
@ArticleID int
AS
Update Kb_Articles Set NumRead = NumRead+1 Where ArticleID = @ArticleID

' 
END
GO
/****** 对象:  StoredProcedure [sp_Kb_ReviewArticle]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_ReviewArticle]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Kb_ReviewArticle]
@ArticleID int,
@ReviewUser nvarchar(50)
AS
UPDATE Kb_Articles
SET IsReviewed = 1,ReviewedBy=@ReviewUser
WHERE ArticleID = @ArticleID' 
END
GO
/****** 对象:  StoredProcedure [sp_Kb_SetArticlePublic]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_SetArticlePublic]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Kb_SetArticlePublic]
@ArticleID int,
@IsPublic bit
AS
UPDATE Kb_Articles SET IsPublic = @IsPublic WHERE ArticleID = @ArticleID' 
END
GO
/****** 对象:  StoredProcedure [sp_Kb_UpdateArticle]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_UpdateArticle]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--添加文章
Create     PROCEDURE [sp_Kb_UpdateArticle]
@ArticleID int,
@Title nvarchar(255),
@KeyWords nvarchar(1024),
@Summary nvarchar(2048),
@Content ntext,
@IsRepost bit,
@Source nvarchar(255),
@SourceUrl nvarchar(255),
@ValueLevel tinyint,
@Difficulty tinyint,
@OriginAuthor nvarchar(50),
@UpdateUser nvarchar(50)
AS
Update Kb_Articles
Set
	Title = @Title,
	KeyWords = @KeyWords,
	Summary = @Summary,
	Content = @Content,
	IsRepost = @IsRepost,
	Source = @Source,
	SourceUrl = @SourceUrl,
	ValueLevel = @ValueLevel,
	Difficulty = @Difficulty,
	OriginAuthor = @OriginAuthor,
	UpdateUser = @UpdateUser,
	UpdateDate = GetDate()
WHERE
	ArticleID = @ArticleID




' 
END
GO
/****** 对象:  StoredProcedure [sp_Kb_MoveArticle]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_MoveArticle]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Kb_MoveArticle]
@ArticleID int,
@NewCategoryID int
AS
UPDATE Kb_Articles SET CategoryID = @NewCategoryID WHERE ArticleID = @ArticleID' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_GetAccessListItems]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetAccessListItems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [sp_Accounts_GetAccessListItems]
@AccessListID int
AS
Select * From
Accounts_AccessListItems
Where AccessListID = @AccessListID
' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_AddAccessListItem]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_AddAccessListItem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Accounts_AddAccessListItem]
@AccessListID int,
@StartIPString nvarchar(20),
@EndIPString nvarchar(20),
@StartIP bigint,
@EndIP bigint,
@Description nvarchar(80)

AS

Insert into Accounts_AccessListItems
(AccessListID,
StartIPString,
EndIPString,
StartIP,
EndIP,
Description)
Values
(@AccessListID,
@StartIPString,
@EndIPString,
@StartIP,
@EndIP,
@Description)

' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_DeleteAccessListItem]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_DeleteAccessListItem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Accounts_DeleteAccessListItem]
@AccessListItemID int
AS

Delete From Accounts_AccessListItems Where AccessListItemID = @AccessListItemID


' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_UpdateProjectCatalog]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProjectCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_UpdateProjectCatalog]
@ProjectCatalogID int,
@Name varchar(50),
@Description varchar(255)
AS
Update Pts_ProjectCatalogs set 
	[Name]=@Name, 
	[Description]=@Description
Where
	[ProjectCatalogID]=@ProjectCatalogID



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ChangeProjectCatalogOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProjectCatalogOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE  procedure [sp_Pts_ChangeProjectCatalogOrder]
(
     @ProjectCatalogID int,
     @MoveUp bit
)
AS
BEGIN
DECLARE @currentSortValue int
DECLARE @replaceSortValue int

-- Get the current sort order
SELECT @currentSortValue = ListOrder FROM Pts_ProjectCatalogs WHERE ProjectCatalogID = @ProjectCatalogID

-- Move the item up or down?
IF (@MoveUp = 1)
  BEGIN
    IF (@currentSortValue != 1)
      BEGIN
        SET @replaceSortValue = @currentSortValue - 1

        UPDATE Pts_ProjectCatalogs SET ListOrder = @currentSortValue WHERE ListOrder = @replaceSortValue
        UPDATE Pts_ProjectCatalogs SET ListOrder = @replaceSortValue WHERE ProjectCatalogID = @ProjectCatalogID
      END
  END

ELSE
  BEGIN
    IF (@currentSortValue < (SELECT MAX(ListOrder) FROM Pts_ProjectCatalogs))
    BEGIN
      SET @replaceSortValue = @currentSortValue + 1

      UPDATE Pts_ProjectCatalogs SET ListOrder = @currentSortValue WHERE ListOrder = @replaceSortValue
      UPDATE Pts_ProjectCatalogs SET ListOrder = @replaceSortValue WHERE ProjectCatalogID = @ProjectCatalogID
    END
  END
END




' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ResetProjectCatalogOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProjectCatalogOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE Procedure [sp_Pts_ResetProjectCatalogOrder]
AS
Declare @Order int
set @Order = 1
Declare @catalogID int
Declare catalog_cursor CURSOR
FOR 
	Select ProjectCatalogID From Pts_ProjectCatalogs order by ListOrder
Open catalog_cursor
Fetch next from catalog_cursor into @catalogID
while (@@FETCH_STATUS <> -1)
BEGIN
	if (@@FETCH_STATUS <> -2)
	BEGIN
		Update Pts_ProjectCatalogs set ListOrder = @Order where ProjectCatalogID = @catalogID
		Set @Order = @Order + 1		
	END
	Fetch next from catalog_cursor into @catalogID
END



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProjectCatalog]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_GetProjectCatalog]
@ProjectCatalog int
AS
Select * from Pts_ProjectCatalogs where ProjectCatalogID = @ProjectCatalog



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProjectCatalogs]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectCatalogs]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_GetProjectCatalogs]
AS
Select * from Pts_ProjectCatalogs order by ListOrder



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetSysConfig]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSysConfig]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'create procedure [sp_Pts_GetSysConfig]
@ConfigItem nvarchar(100)
AS
SELECT ConfigValue FROM [Common_SystemConfig] WHERE ConfigItem=@ConfigItem' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_UpdateProjectState]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProjectState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_UpdateProjectState]
@ProjectStateID int,
@Name varchar(50),
@Description varchar(255),
@IsCloseState	bit
AS
Update Pts_ProjectStates set 
	[Name]=@Name, 
	[Description]=@Description,
	IsCloseState = @IsCloseState
Where
	[ProjectStateID]=@ProjectStateID



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_CreateProjectState]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProjectState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_CreateProjectState]
@Name	 varchar(50),
@Description varchar(255),
@IsCloseState bit
AS
Insert into Pts_ProjectStates ([Name],Description, IsCloseState) values(@Name, @Description, @IsCloseState)

exec sp_Pts_ResetProjectStateOrder



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_DeleteProjectState]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProjectState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_DeleteProjectState]
@ProjectStateID int
AS
Delete from Pts_ProjectStates where ProjectStateID = @ProjectStateID



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProjectState]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_GetProjectState]
@ProjectState int
AS
Select * from Pts_ProjectStates where ProjectStateID = @ProjectState



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ResetProjectStateOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProjectStateOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE Procedure [sp_Pts_ResetProjectStateOrder]
AS
Declare @Order int
set @Order = 1
Declare @stateID int
Declare state_cursor CURSOR
FOR 
	Select ProjectStateID From Pts_ProjectStates order by ListOrder
Open state_cursor
Fetch next from state_cursor into @stateID
while (@@FETCH_STATUS <> -1)
BEGIN
	if (@@FETCH_STATUS <> -2)
	BEGIN
		Update Pts_ProjectStates set ListOrder = @Order where ProjectStateID = @stateID
		Set @Order = @Order + 1		
	END
	Fetch next from state_cursor into @stateID
END



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProjectStates]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectStates]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_GetProjectStates]
AS
Select * from Pts_ProjectStates order by ListOrder



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ChangeProjectStateOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProjectStateOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE  procedure [sp_Pts_ChangeProjectStateOrder]
(
     @ProjectStateID int,
     @MoveUp bit
)
AS
BEGIN
DECLARE @currentSortValue int
DECLARE @replaceSortValue int

-- Get the current sort order
SELECT @currentSortValue = ListOrder FROM Pts_ProjectStates WHERE ProjectStateID = @ProjectStateID

-- Move the item up or down?
IF (@MoveUp = 1)
  BEGIN
    IF (@currentSortValue != 1)
      BEGIN
        SET @replaceSortValue = @currentSortValue - 1

        UPDATE Pts_ProjectStates SET ListOrder = @currentSortValue WHERE ListOrder = @replaceSortValue
        UPDATE Pts_ProjectStates SET ListOrder = @replaceSortValue WHERE ProjectStateID = @ProjectStateID
      END
  END

ELSE
  BEGIN
    IF (@currentSortValue < (SELECT MAX(ListOrder) FROM Pts_ProjectStates))
    BEGIN
      SET @replaceSortValue = @currentSortValue + 1

      UPDATE Pts_ProjectStates SET ListOrder = @currentSortValue WHERE ListOrder = @replaceSortValue
      UPDATE Pts_ProjectStates SET ListOrder = @replaceSortValue WHERE ProjectStateID = @ProjectStateID
    END
  END
END
' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_GetPermissionCategories]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetPermissionCategories]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--返回权限组列表
CREATE PROCEDURE [sp_Accounts_GetPermissionCategories]
AS
	SELECT * FROM Accounts_PermissionCategories




' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_GetPermissionCategoryDetails]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetPermissionCategoryDetails]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




--返回权限组的详细信息
CREATE PROCEDURE [sp_Accounts_GetPermissionCategoryDetails]
@CategoryID int
AS
	SELECT CategoryID, Description FROM Accounts_PermissionCategories WHERE CategoryID = @CategoryID





' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_CreateGlobalSelect]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateGlobalSelect]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [sp_Pts_CreateGlobalSelect]
@SelectName nvarchar(50),
@Description nvarchar(255)
AS
Insert into Pts_GlobalSelects (SelectName, Description) Values (@SelectName, @Description)

Select @@IDENTITY
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_UpdateGlobalSelect]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateGlobalSelect]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
Create Procedure [sp_Pts_UpdateGlobalSelect]
@GlobalSelectID int,
@SelectName nvarchar(50),
@Description nvarchar(255)
AS
Update Pts_GlobalSelects Set SelectName = @SelectName, Description = @Description Where GlobalSelectID = @GlobalSelectID

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetUserCoworkingIssues]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserCoworkingIssues]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetUserCoworkingIssues]
@UserID int
AS
SELECT ProblemID FROM Pts_Coworkers WHERE UserID=@UserID AND IsWorked = 0' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_DeleteProjectCatalog]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProjectCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_DeleteProjectCatalog]
@ProjectCatalogID int
AS
Delete from Pts_ProjectCatalogs where ProjectCatalogID = @ProjectCatalogID

exec sp_Pts_ResetProjectCatalogOrder



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_CreateProblemType]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProblemType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'













CREATE PROCEDURE [sp_Pts_CreateProblemType]
@ProjectID int,
@TypeName nvarchar(50),
@Description nvarchar(255)
AS
Insert into Pts_ProblemType
	(ProjectID, TypeName, Description)
Values
	(@ProjectID, @TypeName, @Description)

exec sp_Pts_ResetProblemTypeOrder	@ProjectID







' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_DeleteProblemType]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE PROCEDURE [sp_Pts_DeleteProblemType]
@ProblemTypeID int
AS

Declare @ProjectID int
Select @ProjectID = ProjectID From Pts_ProblemType where ProblemTypeID = @ProblemTypeID

Delete from Pts_ProblemType where ProblemTypeID = @ProblemTypeID

exec sp_Pts_ResetProblemTypeOrder @ProjectID



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_DeleteWorkgroup]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteWorkgroup]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'






CREATE    PROCEDURE [sp_Pts_DeleteWorkgroup]
@WorkgroupID int
AS

Declare @ProjectID int

BEGIN transaction
Delete from Pts_WorkgroupUsers where WorkgroupID = @WorkgroupID
Delete From Pts_ProblemStateTransferWorkgroup where WorkgroupID = @WorkgroupID
Delete From Pts_ProblemInitStateWorkgroup Where WorkgroupID = @WorkgroupID

Delete From Pts_ProblemFieldEditableWorkgroup Where WorkgroupID = @WorkgroupID
Delete From Pts_ProblemFieldReadableWorkgroup Where WorkgroupID = @WorkgroupID

Select @ProjectID = ProjectID From Pts_Workgroups where WorkgroupID = @WorkgroupID
Delete from Pts_Workgroups where WorkgroupID = @WorkgroupID
exec sp_Pts_ResetWorkgroupOrder @ProjectID

commit transaction


' 
END
GO
/****** 对象:  StoredProcedure [sp_Accounts_GetPermissionsNotInRole]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetPermissionsNotInRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



create procedure [sp_Accounts_GetPermissionsNotInRole]
@RoleID int
AS
Select 
	p.* 
From 
	v_Accounts_Permissions p 
where 
	p.PermissionID not in
	( Select PermissionID From Accounts_RolePermissions Where RoleID = @RoleID)



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_DeleteRecordType]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteRecordType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'






CREATE PROCEDURE [sp_Pts_DeleteRecordType]
@RecordTypeID int
AS

Declare @ProjectID int
Select @ProjectID = ProjectID From Pts_RecordType where RecordTypeID = @RecordTypeID

Delete from Pts_RecordType where RecordTypeID = @RecordTypeID

exec sp_Pts_ResetRecordTypeOrder @ProjectID








' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ComputeProjectCounter]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ComputeProjectCounter]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





CREATE   Procedure [sp_Pts_ComputeProjectCounter]
@ProjectID int
AS
Declare @ProblemCount int
Declare @RecordCount int
Declare @OpenProblemCount int

Select @ProblemCount = Count(ProblemID) From Pts_Problems Where ProjectID = @ProjectID

Select @RecordCount = Count(RecordID) From
	Pts_Records Where ProblemID in
	(Select ProblemID From Pts_Problems Where ProjectID = @ProjectID)
Select @OpenProblemCount = Count(ProblemID) From v_Pts_Problems Where ProjectID = @ProjectID AND IsClosed=0

Update Pts_Projects Set TotalProblems=@ProblemCount,TotalRecords=@RecordCount,OpenProblems=@OpenProblemCount Where ProjectID=@ProjectID




' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_DeleteProblemState]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





CREATE   PROCEDURE [sp_Pts_DeleteProblemState]
@ProblemStateID int
AS

Declare @ProjectID int
Select @ProjectID = ProjectID From Pts_ProblemState where ProblemStateID = @ProblemStateID

Delete from Pts_ProblemStateTransfer where FromProblemStateID=@ProblemStateID 
    	or ToProblemStateID = @ProblemStateID

Delete From Pts_ProblemInitStateWorkgroup Where ProblemStateID = @ProblemStateID

Delete From Pts_WorkflowSteps Where SrcStateID = @ProblemStateID OR DstStateID = @ProblemStateID

Delete From Pts_ProblemCreateRoles Where DstStateID = @ProblemStateID

Delete From Pts_ProblemFieldEditableState Where FromStateID = @ProblemStateID OR ToStateID = @ProblemStateID

Delete From Pts_StateEditableFields Where ProblemStateID = @ProblemStateID

Delete from Pts_ProblemState where ProblemStateID = @ProblemStateID

exec sp_Pts_ResetProblemStateOrder @ProjectID




' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_DeleteProblemSeverity]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemSeverity]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'








CREATE PROCEDURE [sp_Pts_DeleteProblemSeverity]
@ProblemSeverityID int
AS

Declare @ProjectID int
Select @ProjectID = ProjectID From Pts_ProblemSeverity where ProblemSeverityID = @ProblemSeverityID

Delete from Pts_ProblemSeverity where ProblemSeverityID = @ProblemSeverityID

exec sp_Pts_ResetProblemSeverityOrder @ProjectID






' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_CreateProblemSeverity]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProblemSeverity]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_CreateProblemSeverity]
@ProjectID int,
@SeverityName nvarchar(50),
@DisplayColor varchar(15),
@Description nvarchar(255)
AS
Insert into Pts_ProblemSeverity
	(ProjectID, SeverityName,DisplayColor, Description)
Values
	(@ProjectID, @SeverityName, @DisplayColor, @Description)

exec sp_Pts_ResetProblemSeverityOrder	@ProjectID



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetUserWorkgroups]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserWorkgroups]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



--返回某个用户所属于的工作组列表
CREATE PROCEDURE [sp_Pts_GetUserWorkgroups]
@UserID int
AS
Select * from v_Pts_UserWorkgroups where UserID = @UserID 
Order By ProjectID, ListOrder



' 
END
GO
/****** 对象:  View [v_Pts_Stat_StateKeepTime]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[v_Pts_Stat_StateKeepTime]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N'


CREATE VIEW [v_Pts_Stat_StateKeepTime]
AS
SELECT AVG(v_Pts_ProblemStateRecord.KeepTime) AS AvgStateKeepTime, 
      Pts_ProblemState.StateName, Pts_ProblemState.ProblemStateID, 
      Pts_ProblemState.ProjectID, Pts_ProblemState.ListOrder
FROM v_Pts_ProblemStateRecord RIGHT OUTER JOIN
      Pts_ProblemState ON 
      v_Pts_ProblemStateRecord.StateID = Pts_ProblemState.ProblemStateID
GROUP BY Pts_ProblemState.StateName, Pts_ProblemState.ProblemStateID, 
      Pts_ProblemState.ProjectID, Pts_ProblemState.ListOrder




'
GO
/****** 对象:  StoredProcedure [sp_Pts_Stat_ByProblemState]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Stat_ByProblemState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_Stat_ByProblemState]
@ProjectID int
AS
select 	Pts_ProblemState.ProblemStateID,
		Pts_ProblemState.StateName,
						sum(case when ProblemID is not null then 1 else 0 end) TotalNum, 
						sum(case when IsClosed=0 then 1 else 0 end) OpenNum,
						sum(case when IsClosed=1 then 1 else 0 end) ClosedNum 
					from v_Pts_Problems  right outer join 
						Pts_ProblemState on v_Pts_Problems.ProblemStateID = Pts_ProblemState.ProblemStateID
					Where Pts_ProblemState.ProjectID = @ProjectID AND v_Pts_Problems.ProjectID = @ProjectID
					group by Pts_ProblemState.ProblemStateID, Pts_ProblemState.StateName, Pts_ProblemState.ListOrder 
					Order by Pts_ProblemState.ListOrder' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_RecentAssignedToMe]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_RecentAssignedToMe]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [sp_Pts_Filter_RecentAssignedToMe]
@ProjectID int,
@UserID int,
@Count int
AS
--等待我处理的事务
SELECT top 500 ProblemID, ProblemCode, Title, ProblemStateName, AssignedTo, LastTime,ReadByAssignee
FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND 
	IsClosed = 0
AND
	((AssignedTo > 0 AND AssignedTo = @UserID)
	OR
	(AssignedTo < 0 AND -AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID))
	OR
	EXISTS (SELECT 1 FROM Pts_Coworkers LEFT OUTER JOIN
                      Pts_Problems ON Pts_Coworkers.ProblemID = Pts_Problems.ProblemID
			WHERE Pts_Coworkers.UserID=@UserID AND Pts_Coworkers.IsWorked=0 AND Pts_Problems.ProjectID=@ProjectID)
	)
Order BY LastTime DESC,CreateTime DESC' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_AssignedToMeNotRead]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_AssignedToMeNotRead]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_Filter_Relate_AssignedToMeNotRead]
@ProjectID int,
@UserID int
AS
--等待我处理的事务,未读
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND 
	IsClosed = 0
AND
	ReadByAssignee=0
AND
	((AssignedTo > 0 AND AssignedTo = @UserID)
	OR
	(AssignedTo < 0 AND -AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID))
	OR
	EXISTS (SELECT 1 FROM Pts_Coworkers LEFT OUTER JOIN
                      Pts_Problems ON Pts_Coworkers.ProblemID = Pts_Problems.ProblemID
			WHERE Pts_Coworkers.UserID=@UserID AND Pts_Coworkers.IsWorked=0 AND Pts_Problems.ProjectID=@ProjectID)
	)' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetIssueCounts_Mine]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetIssueCounts_Mine]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE procedure [sp_Pts_GetIssueCounts_Mine]
@ProjectID int,
@UserID int
AS

--声明表变量
Declare @t table
(
	count_type  nvarchar(30),
	count_value int
)


--提交给我的事务
declare @assignedToMe int
declare @notReaded int

SELECT @assignedToMe=sum(1),@notReaded=sum(case when ReadByAssignee=1 then 0 else 1 end) FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND 
	IsClosed = 0
AND
	((AssignedTo > 0 AND AssignedTo = @UserID)
	OR
	(AssignedTo < 0 AND -AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID))
	OR
	EXISTS (SELECT 1 FROM Pts_Coworkers LEFT OUTER JOIN
                      Pts_Problems ON Pts_Coworkers.ProblemID = Pts_Problems.ProblemID
			WHERE Pts_Coworkers.UserID=@UserID AND Pts_Coworkers.IsWorked=0 AND Pts_Problems.ProjectID=@ProjectID)
	)
insert into @t (count_type,count_value) values (''assigned_to_me'', @assignedToMe)
insert into @t (count_type,count_value) values (''assigned_to_me_not_read'', @notReaded)

--我提交的事务
declare @createdByMe int
declare @createdByMeClosed int
declare @createdByMeOpen int

select 
	@createdByMe=count(ProblemID),
	@createdByMeClosed=sum(case when IsClosed=1 then 1 else 0 end), 
	@createdByMeOpen=sum(case when IsClosed=1 then 0 else 1 end)
from v_Pts_Problems
WHERE
	ProjectID = @ProjectID AND CreateUserID=@UserID
insert into @t (count_type,count_value) values (''created_by_me'', @createdByMe)
insert into @t (count_type,count_value) values (''created_by_me_closed'', @createdByMeClosed)
insert into @t (count_type,count_value) values (''created_by_me_open'', @createdByMeOpen)

--参与过的事务
declare @processedByMe int
declare @processedByMeOpen int
declare @processedByMeClosed int

SELECT 
	@processedByMe=count(ProblemID),
	@processedByMeClosed=sum(case when IsClosed=1 then 1 else 0 end), 
	@processedByMeOpen=sum(case when IsClosed=1 then 0 else 1 end)
FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	ProblemID in
	(SELECT Distinct ProblemID From Pts_Records Where ProblemID in (select ProblemID FROM Pts_Problems WHERE ProjectID=@ProjectID) AND CreateUser = @UserID)
insert into @t (count_type,count_value) values (''processed_by_me'', @processedByMe)
insert into @t (count_type,count_value) values (''processed_by_me_closed'', @processedByMeClosed)
insert into @t (count_type,count_value) values (''processed_by_me_open'', @processedByMeOpen)

--订阅的事务
declare @subscribedByMe int
declare @subscribedByMeClosed int
declare @subscribedByMeOpen int

SELECT 
	@subscribedByMe=count(ProblemID),
	@subscribedByMeClosed=sum(case when IsClosed=1 then 1 else 0 end), 
	@subscribedByMeOpen=sum(case when IsClosed=1 then 0 else 1 end)
FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	ProblemID in
	(Select TargetID FROM Pts_Subscription WHERE UserID=@UserID AND TargetType=2)
insert into @t (count_type,count_value) values (''subscribed_by_me'', @subscribedByMe)
insert into @t (count_type,count_value) values (''subscribed_by_me_closed'', @subscribedByMeClosed)
insert into @t (count_type,count_value) values (''subscribed_by_me_open'', @subscribedByMeOpen)



--相关的事务
declare @relatedToMe int
declare @relatedToMeClosed int
declare @relatedToMeOpen int

SELECT 
	@relatedToMe=count(ProblemID),
	@relatedToMeClosed=sum(case when IsClosed=1 then 1 else 0 end), 
	@relatedToMeOpen=sum(case when IsClosed=1 then 0 else 1 end)
FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	ProblemID in
	(SELECT ProblemID FROM Pts_RelatedUsers Where UserID=@UserID)

insert into @t (count_type,count_value) values (''related_to_me'', @relatedToMe)
insert into @t (count_type,count_value) values (''related_to_me_closed'', @relatedToMeClosed)
insert into @t (count_type,count_value) values (''related_to_me_open'', @relatedToMeOpen)

--代理我处理的事务
declare @processedByAgent int

select @processedByAgent = count(ProblemID) 
FROM Pts_Problems 
WHERE ProjectID=@ProjectID AND ProblemID in (SELECT DISTINCT ProblemID FROM Pts_Records WHERE RepresentingUserID=@UserID)

insert into @t (count_type, count_value) values (''processed_by_agent'', @processedByAgent)

--输出
SELECT * FROM @t' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_UpdateProblemChildNum]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemChildNum]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE procedure [sp_Pts_UpdateProblemChildNum]
@ProblemID int
AS
DECLARE @TotalChildNum int
DECLARE @TotalClosedChild int

SELECT @TotalChildNum = SUM(1),@TotalClosedChild=sum(case when IsClosed=1 then 1 else 0 end) 
	FROM v_Pts_Problems 
	WHERE ParentID=@ProblemID

UPDATE Pts_Problems Set TotalChildNum=@TotalChildNum,ClosedChildNum=@TotalClosedChild WHERE ProblemID=@ProblemID' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetIssueCounts_Recent]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetIssueCounts_Recent]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE procedure [sp_Pts_GetIssueCounts_Recent]
@ProjectID int
AS

--声明表变量
Declare @t table
(
	count_type  nvarchar(30),
	count_value int
)




declare  @TODAY_BEGIN datetime
set @TODAY_BEGIN = Convert(varchar(10),getdate(),120)


--今日创建 time_a1
declare @todayCreated int
select @todayCreated = count(*) from Pts_Problems WHERE ProjectID=@ProjectID AND CreateTime >= @TODAY_BEGIN
insert into @t (count_type,count_value) values (''today_created'', @todayCreated)

--今日关闭 time_a2
declare @todayClosed int
SELECT @todayClosed = count(*) FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 1
AND
	CloseTime >= @TODAY_BEGIN

insert into @t (count_type,count_value) values (''today_closed'', @todayClosed)

--今日更新的 
declare @todayUpdated int
SELECT @todayUpdated = count(*) FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	LastTime >= @TODAY_BEGIN
insert into @t (count_type,count_value) values (''today_updated'', @todayUpdated)

--------------------------
--昨日
--------------------------


--昨天创建 time_a1
declare @yesterdayCreated int
select @yesterdayCreated = count(*) from Pts_Problems WHERE ProjectID=@ProjectID AND DateDiff(day, CreateTime, GetDate()) = 1
insert into @t (count_type,count_value) values (''yesterday_created'', @yesterdayCreated)

--昨天关闭 time_a2
declare @yesterdayClosed int
SELECT @yesterdayClosed = count(*) FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 1
AND
	DateDiff(day, CloseTime, GetDate()) = 1

insert into @t (count_type,count_value) values (''yesterday_closed'', @yesterdayClosed)


--本周创建
insert into @t select ''thisweek_created'',count(*) from Pts_Problems  
	where ProjectID = @ProjectID
	AND
	CreateTime >= dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7,convert(varchar,getdate(),112))

--本周关闭
insert into @t select ''thisweek_closed'',count(*) FROM v_Pts_Problems
	WHERE
		ProjectID = @ProjectID
	AND	
		IsClosed = 1
	AND
		CloseTime >= dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7,convert(varchar,getdate(),112))

--上周创建
insert into @t select ''lastweek_created'',count(*) FROM Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	CreateTime < dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7,convert(varchar,getdate(),112))
AND
	CreateTime >= dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7-7,convert(varchar,getdate(),112))
--上周关闭
insert into @t select ''lastweek_closed'',count(*) FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 1
AND
	CloseTime < dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7,convert(varchar,getdate(),112))
AND
	CloseTime >= dateadd(day,(2-@@datefirst-datepart(weekday,GetDate()))%7-7,convert(varchar,getdate(),112))

--本月创建
insert into @t select ''thismonth_created'',count(*) FROM Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	CreateTime >= dateadd(day,1-day(getdate()),convert(varchar,getdate(),112))
--本月关闭
insert into @t select ''thismonth_closed'',count(*) FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 1
AND
	CloseTime >= dateadd(day,1-day(getdate()),convert(varchar,getdate(),112))

--上月创建
insert into @t select ''lastmonth_created'',count(*) FROM Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	CreateTime >= dateadd(month,-1,dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)))
AND
	CreateTime < dateadd(day,1-day(getdate()),convert(varchar,getdate(),112))
--上月关闭
insert into @t select ''lastmonth_closed'',count(*) FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 1
AND
	CloseTime >= dateadd(month,-1,dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)))
AND
	CloseTime < dateadd(day,1-day(getdate()),convert(varchar,getdate(),112))
--一周未更新
insert into @t select ''notprocessed_oneweek'',count(*) FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0
AND
	DateDiff(d, LastTime, GetDate()) > 7

--

--即将到期事务
insert into @t select ''expiring_in3days'',count(*) FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0
AND
	Deadline is not null
AND
	DateDiff(d, GetDate(), Deadline) < 3
AND 
	Deadline > GetDate()
	

--过期的事务
insert into @t select ''expired'',count(*) FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0
AND
	Deadline is not null
AND
	Deadline < GetDate()

--步骤到期的事务
insert into @t select ''state_expired'',count(*) FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0
AND
	StateDeadline is not null
AND
	StateDeadline < GetDate()

-------------------------------------
select * from @t' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_RunInsertProblemString]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RunInsertProblemString]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





CREATE   PROCEDURE [sp_Pts_RunInsertProblemString]
@QueryString ntext,
@ProblemID int out
AS
exec sp_executesql @QueryString
if @@ROWCOUNT = 1
BEGIN
	set @ProblemID = @@Identity
	
	--记录问题开始状态
	Declare @ProblemStateID int
	Declare @UserID int

	Select @ProblemStateID = ProblemStateID,@UserID = AssignedTo From Pts_Problems Where ProblemID = @ProblemID
	if (@ProblemStateID is not null)
		exec sp_Pts_RecordStateStart @ProblemID, @ProblemStateID, @UserID
END
else
	set @ProblemID = -1





' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_1]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_1]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [sp_Pts_Filter_Relate_1]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND 
	IsClosed = 0
AND
	((AssignedTo > 0 AND AssignedTo = @UserID)
	OR
	(AssignedTo < 0 AND -AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID))
	OR
	EXISTS (SELECT 1 FROM Pts_Coworkers LEFT OUTER JOIN
                      Pts_Problems ON Pts_Coworkers.ProblemID = Pts_Problems.ProblemID
			WHERE Pts_Coworkers.UserID=@UserID AND Pts_Coworkers.IsWorked=0 AND Pts_Problems.ProjectID=@ProjectID)
	)' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetMyIssueCount]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetMyIssueCount]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE procedure [sp_Pts_GetMyIssueCount]
@UserID int
AS
Select 	COUNT(1)
From 	v_Pts_Problems Where 
--IsClosed=0 
--AND
(AssignedTo = @UserID
	OR
        (AssignedTo < 0 AND
	-AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID))
	)' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ComputeUserPendingTasks]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ComputeUserPendingTasks]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [sp_Pts_ComputeUserPendingTasks]
@UserID int
AS
--等待我处理的事务
SELECT ProjectID,COUNT(*) FROM v_Pts_Problems
WHERE
	IsClosed = 0
AND
	((AssignedTo > 0 AND AssignedTo = @UserID)
	OR
	(AssignedTo < 0 AND -AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID)))
GROUP BY ProjectID' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetUserInProblems]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserInProblems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



----等待某用户处理的问题
CREATE Procedure [sp_Pts_GetUserInProblems]
@UserID int
AS
Select 	* 
From 	v_Pts_Problems Where 
IsClosed=0
AND
(AssignedTo = @UserID
	OR
	-AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID)
	OR 
	ProblemID IN (SELECT ProblemID FROM Pts_Coworkers WHERE UserID=@UserID AND IsWorked=0)
	)




' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_AddProblemAttachment]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddProblemAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE  Procedure [sp_Pts_AddProblemAttachment]
@ProblemID int,
@SaveName varchar(255),
@OriginName varchar(255),
@FileSize int,
@Description nvarchar(255),
@CreateUserID int,
@CreateDate datetime
AS
Insert into Pts_ProblemAttachments 
  ( ProblemID,SaveName, OriginName, FileSize, Description, CreateUserID, CreateDate)
Values
  ( @ProblemID, @SaveName, @OriginName, @FileSize, @Description, @CreateUserID, @CreateDate)


--更新事务的附件数量
exec sp_Pts_UpdateProblemAttachNum @ProblemID
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_RemoveProblemAttachment]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveProblemAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_RemoveProblemAttachment]
@ProblemID int,
@AttachmentID int
AS
Delete from Pts_ProblemAttachments where AttachmentID = @AttachmentID

--更新事务的附件数量
exec sp_Pts_UpdateProblemAttachNum @ProblemID
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_IsUserRelateProblem]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_IsUserRelateProblem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE    Procedure [sp_Pts_IsUserRelateProblem]
@UserID int,
@ProblemID int
AS


Select count(*) From v_Pts_Problems Where ProblemID = @ProblemID AND 
	(CreateUserID = @UserID 
     or (AssignedTo = @UserID AND IsClosed=0)
	or @UserID in (Select Distinct CreateUser From Pts_Records Where ProblemID = @ProblemID)
	or @UserID in (Select Distinct RepresentingUserID From Pts_Records Where ProblemID = @ProblemID)
	OR Exists(SELECT ProblemID FROM Pts_RelatedUsers WHERE ProblemID=@ProblemID AND UserID=@UserID)
)






' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_ProcessedByAgent]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_ProcessedByAgent]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'create Procedure [sp_Pts_Filter_Relate_ProcessedByAgent]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND 
	ProblemID in (SELECT Distinct ProblemID FROM Pts_Records WHERE RepresentingUserID=@UserID)' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetUserProcessedProblems]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserProcessedProblems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [sp_Pts_GetUserProcessedProblems]
@UserID int,
@IsClosed int = null 
AS
if (@IsClosed is null)
BEGIN
Select * From v_Pts_Problems where ProblemID in (SELECT Distinct ProblemID From Pts_Records Where CreateUser = @UserID)
END
else 
BEGIN
Select * From v_Pts_Problems where IsClosed=@IsClosed AND ProblemID in (SELECT Distinct ProblemID From Pts_Records Where CreateUser = @UserID)
END' 
END
GO
/****** 对象:  View [v_Pts_RecordsForEvents]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[v_Pts_RecordsForEvents]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [v_Pts_RecordsForEvents]
AS
SELECT     TOP 100 PERCENT Pts_Records.RecordID, Pts_Records.ProblemID, Pts_Records.CreateUser, Pts_Records.DateCreated, 
                      Pts_Records.IsKeyRecord, Pts_Records.RecordType, Pts_Records.AssignTo, Pts_Records.Title, Pts_Records.[Content], 
                      Pts_Records.StateID, Pts_Records.HistoryID, Pts_Records.RecordClass, Pts_Records.SrcStateID, Pts_Records.SrcUserID, 
                      Pts_Records.ResponseWorkTime, Pts_Records.ResponseTotalTime, Pts_Records.RealTimeCost, Pts_Records.ExtInt1, Pts_Records.ExtInt2, 
                      Pts_Records.ExtInt3, Pts_Records.ExtString1, Pts_Records.ExtString2, Pts_Records.ExtString3, Pts_Records.ExtNtext1, 
                      Pts_Records.ExtNtext2, Pts_Records.ParentRecordID, v_Pts_Problems.Title AS ProblemTitle, v_Pts_Problems.ProblemCode, 
                      v_Pts_Problems.ProjectID
FROM         Pts_Records LEFT OUTER JOIN
                      v_Pts_Problems ON Pts_Records.ProblemID = v_Pts_Problems.ProblemID
ORDER BY Pts_Records.RecordID DESC
'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPane1' , N'USER',N'dbo', N'VIEW',N'v_Pts_RecordsForEvents', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "Pts_Records"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 278
               Right = 227
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "v_Pts_Problems"
            Begin Extent = 
               Top = 6
               Left = 265
               Bottom = 379
               Right = 460
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 29
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_RecordsForEvents'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPaneCount' , N'USER',N'dbo', N'VIEW',N'v_Pts_RecordsForEvents', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_RecordsForEvents'
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_7]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_7]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Pts_Filter_Relate_7]
@ProjectID int,
@UserID int
AS
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
(
	CreateUserID = @UserID
OR
	(AssignedTo = @UserID AND IsClosed=0)
OR
	ProblemID in
	(SELECT Distinct ProblemID From Pts_Records Where CreateUser = @UserID)
OR
	ProblemID in
	(SELECT ProblemID FROM Pts_RelatedUsers Where UserID=@UserID)
)
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_22]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_22]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [sp_Pts_Filter_Relate_22]
@ProjectID int,
@UserID int
AS
--我相关的，未关闭的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 1
AND
(
	CreateUserID = @UserID
OR
	ProblemID in
	(SELECT Distinct ProblemID From Pts_Records Where CreateUser = @UserID)
OR
	ProblemID in
	(SELECT ProblemID FROM Pts_RelatedUsers Where UserID=@UserID)
)
AND    
	DateDiff(d,CloseTime , GetDate()) < 90' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_21]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_21]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [sp_Pts_Filter_Relate_21]
@ProjectID int,
@UserID int
AS
--我相关的，未关闭的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0
AND
(
	CreateUserID = @UserID
OR
	(AssignedTo = @UserID AND IsClosed=0)
OR
	ProblemID in
	(SELECT Distinct ProblemID From Pts_Records Where CreateUser = @UserID)
OR
	ProblemID in
	(SELECT ProblemID FROM Pts_RelatedUsers Where UserID=@UserID)
)' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_ProcessedByMeClosed]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_ProcessedByMeClosed]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
create Procedure [sp_Pts_Filter_Relate_ProcessedByMeClosed]
@ProjectID int,
@UserID int
AS
--我参与的事务，跟踪中的
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 1
AND
	ProblemID in
	(SELECT Distinct ProblemID From Pts_Records Where CreateUser = @UserID)

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_5]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_5]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Pts_Filter_Relate_5]
@ProjectID int,
@UserID int
AS
--我参与的事务，跟踪中的
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0
AND
	ProblemID in
	(SELECT Distinct ProblemID From Pts_Records Where CreateUser = @UserID)

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_ProcessedByMeOpen]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_ProcessedByMeOpen]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Pts_Filter_Relate_ProcessedByMeOpen]
@ProjectID int,
@UserID int
AS
--我参与的事务，跟踪中的
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0
AND
	ProblemID in
	(SELECT Distinct ProblemID From Pts_Records Where CreateUser = @UserID)

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_ProcessedByMe]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_ProcessedByMe]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [sp_Pts_Filter_Relate_ProcessedByMe]
@ProjectID int,
@UserID int
AS
--我参与的事务，跟踪中的
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	ProblemID in
	(SELECT Distinct ProblemID From Pts_Records Where CreateUser = @UserID)

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetAllRelatedProblems]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetAllRelatedProblems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetAllRelatedProblems] 
@ProblemID int
AS
--SELECT [ProblemID],[ProblemCode],[Title] 
--FROM v_Pts_Problems 
--WHERE ProblemID IN (SELECT ProblemID_2 FROM [Pts_ProblemRelations]  WHERE ProblemID_1=@ProblemID) 
--	OR ProblemID IN (SELECT ProblemID_1 FROM [Pts_ProblemRelations]  WHERE ProblemID_2=@ProblemID) 

declare  @t table
(refdirection bit,
RelationType int,
ProblemID int,
ProblemCode nvarchar(50),
Title nvarchar(1024)
)

insert into @t 
select 1,RelationType,ProblemID,ProblemCode,TItle 
FROM Pts_ProblemRelations inner join v_Pts_Problems on Pts_ProblemRelations.ProblemID_2 = v_Pts_Problems.ProblemID
WHERE Pts_ProblemRelations.ProblemID_1 = @ProblemID

insert into @t 
select 0,RelationType,ProblemID,ProblemCode,TItle 
FROM Pts_ProblemRelations inner join v_Pts_Problems on Pts_ProblemRelations.ProblemID_1 = v_Pts_Problems.ProblemID
WHERE Pts_ProblemRelations.ProblemID_2 = @ProblemID

select * from @t order by refdirection desc,ProblemID asc' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetSharedItemAttachments]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSharedItemAttachments]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [sp_Pts_GetSharedItemAttachments]
@ItemID int
AS
SELECT * FROM v_Pts_SharedItemAttachments WHERE ItemID = @ItemID



' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ChangeProblemCatalogOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProblemCatalogOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE   procedure [sp_Pts_ChangeProblemCatalogOrder]
(
     @ProblemCatalogID int,
     @MoveUp bit
)
AS
BEGIN
--获取ParentID
Declare @ParentID int
Declare @ProjectID int
Declare @GlobalOrder varchar(100)

Select @ParentID = ParentID,@ProjectID=ProjectID From Pts_ProblemCatalogs where ProblemCatalogID = @ProblemCatalogID
if @ParentID is null
	RETURN
--修改顺序
DECLARE @currentSortValue int
DECLARE @replaceSortValue int

-- Get the current sort order
SELECT @currentSortValue = ListOrder FROM Pts_ProblemCatalogs WHERE ProblemCatalogID = @ProblemCatalogID

-- Move the item up or down?
IF (@MoveUp = 1)
  BEGIN
    IF (@currentSortValue != 1)
      BEGIN
        SET @replaceSortValue = @currentSortValue - 1

        UPDATE Pts_ProblemCatalogs SET ListOrder = @currentSortValue WHERE ParentID = @ParentID AND ListOrder = @replaceSortValue
        UPDATE Pts_ProblemCatalogs SET ListOrder = @replaceSortValue WHERE ProblemCatalogID = @ProblemCatalogID
      END
  END

ELSE
  BEGIN
    IF (@currentSortValue < (SELECT MAX(ListOrder) FROM Pts_ProblemCatalogs WHERE ParentID=@ParentID))
    BEGIN
      SET @replaceSortValue = @currentSortValue + 1

      UPDATE Pts_ProblemCatalogs SET ListOrder = @currentSortValue WHERE ParentID = @ParentID AND  ListOrder = @replaceSortValue
      UPDATE Pts_ProblemCatalogs SET ListOrder = @replaceSortValue WHERE ProblemCatalogID = @ProblemCatalogID
    END
  END
END

-- Reset Order
if @ParentID=0
	Set @GlobalOrder=''''
else
	Select @GlobalOrder = GlobalOrder From Pts_ProblemCatalogs Where ProblemCatalogID = @ParentID

exec sp_Pts_ResetProblemCatalogGlobalOrder @ProjectID, @ParentID, @GlobalOrder





' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ResetProblemCatalogOrder]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProblemCatalogOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'







CREATE     Procedure [sp_Pts_ResetProblemCatalogOrder]
@ProjectID int,
@ParentID int
AS
Declare @Order int
set @Order = 1
Declare @ProblemCatalogID int
Declare catalog_cursor CURSOR
FOR 
	Select ProblemCatalogID From Pts_ProblemCatalogs Where ProjectID=@ProjectID AND  ParentID = @ParentID Order by ListOrder
Open catalog_cursor 
Fetch next from catalog_cursor into @ProblemCatalogID
while (@@FETCH_STATUS <> -1)
BEGIN
	if (@@FETCH_STATUS <> -2)
	BEGIN
		Update Pts_ProblemCatalogs set ListOrder = @Order where ProblemCatalogID = @ProblemCatalogID
		Set @Order = @Order + 1		
	END
	Fetch next from catalog_cursor into @ProblemCatalogID
END


Declare @GlobalOrder varchar(100)
set @GlobalOrder = ""

Select @GlobalOrder = GlobalOrder From Pts_ProblemCatalogs Where ProblemCatalogID = @ParentID AND ProjectID = @ProjectID
if (@ProjectID is not null)
	exec sp_Pts_ResetProblemCatalogGlobalOrder @ProjectID, @ParentID, @GlobalOrder







' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetUserVisitedProblems]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserVisitedProblems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE  PROCEDURE [sp_Pts_GetUserVisitedProblems]
(
@UserID int
)
AS

select p.* FROM
v_Pts_Problems p INNER JOIN
                      Pts_ProblemVisitHistory h ON p.ProblemID = h.ProblemID 
where h.UserID = @UserID
order by h.VisitIndex' 
END
GO
/****** 对象:  View [v_Pts_ProblemVisitHistory]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[v_Pts_ProblemVisitHistory]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [v_Pts_ProblemVisitHistory]
AS
SELECT     v_Pts_Problems.CreateUserName, v_Pts_Problems.ProblemID, v_Pts_Problems.ProjectID, v_Pts_Problems.OriginProjectID, 
                      v_Pts_Problems.ProblemIndex, v_Pts_Problems.ProblemCatalogID, v_Pts_Problems.ProblemTypeID, v_Pts_Problems.ProblemStateID, 
                      v_Pts_Problems.ProblemSeverityID, v_Pts_Problems.ProblemPriorityID, v_Pts_Problems.CreateTime, v_Pts_Problems.CloseTime, 
                      v_Pts_Problems.CreateUserID, v_Pts_Problems.Deadline, v_Pts_Problems.AssignedTo, v_Pts_Problems.Title, v_Pts_Problems.Description, 
                      v_Pts_Problems.RecordNum, v_Pts_Problems.LastUser, v_Pts_Problems.LastTime, v_Pts_Problems.LastRecord, v_Pts_Problems.HistoryID, 
                      v_Pts_Problems.ArticleID, v_Pts_Problems.ProblemCatalogName, v_Pts_Problems.ProblemTypeName, v_Pts_Problems.ProblemStateName, 
                      v_Pts_Problems.ProblemPriorityName, v_Pts_Problems.ProblemSeverityName, v_Pts_Problems.ProblemSeverityOrder, 
                      v_Pts_Problems.ProblemStateOrder, v_Pts_Problems.ProblemTypeOrder, v_Pts_Problems.ProblemCatalogOrder, 
                      v_Pts_Problems.ProblemPriorityOrder, v_Pts_Problems.PriorityDisplayColor, v_Pts_Problems.SeverityDisplayColor, 
                      v_Pts_Problems.AssignedToUserName, v_Pts_Problems.ProjectName, v_Pts_Problems.AssignedToEmail, v_Pts_Problems.ProblemCatalogPath, 
                      v_Pts_Problems.IsClosed, v_Pts_Problems.LastUserName, v_Pts_Problems.ProblemCode, v_Pts_Problems.ProjectCode, 
                      v_Pts_Problems.StartTime, Pts_ProblemVisitHistory.VisitIndex, Pts_ProblemVisitHistory.UserID AS VisitUserID, v_Pts_Problems.ReadByAssignee, 
                      v_Pts_Problems.AssigneeReadTime
FROM         v_Pts_Problems INNER JOIN
                      Pts_ProblemVisitHistory ON v_Pts_Problems.ProblemID = Pts_ProblemVisitHistory.ProblemID
'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPane1' , N'USER',N'dbo', N'VIEW',N'v_Pts_ProblemVisitHistory', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[21] 4[40] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = -192
         Left = 0
      End
      Begin Tables = 
         Begin Table = "v_Pts_Problems"
            Begin Extent = 
               Top = 0
               Left = 33
               Bottom = 259
               Right = 239
            End
            DisplayFlags = 280
            TopColumn = 213
         End
         Begin Table = "Pts_ProblemVisitHistory"
            Begin Extent = 
               Top = 6
               Left = 271
               Bottom = 204
               Right = 461
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 227
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_ProblemVisitHistory'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPane2' , N'USER',N'dbo', N'VIEW',N'v_Pts_ProblemVisitHistory', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPane2', @value=N' = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_ProblemVisitHistory'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPane3' , N'USER',N'dbo', N'VIEW',N'v_Pts_ProblemVisitHistory', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPane3', @value=N' = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 2175
         Alias = 1605
         Table = 3270
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_ProblemVisitHistory'
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_DiagramPaneCount' , N'USER',N'dbo', N'VIEW',N'v_Pts_ProblemVisitHistory', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=3 , @level0type=N'USER',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_Pts_ProblemVisitHistory'
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_RelatedToMe]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_RelatedToMe]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [sp_Pts_Filter_Relate_RelatedToMe]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID

AND
	ProblemID in
	(SELECT ProblemID FROM Pts_RelatedUsers Where UserID=@UserID)
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_RelatedToMeOpen]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_RelatedToMeOpen]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [sp_Pts_Filter_Relate_RelatedToMeOpen]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed=0
AND
	ProblemID in
	(SELECT ProblemID FROM Pts_RelatedUsers Where UserID=@UserID)
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_RelatedToMeClosed]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_RelatedToMeClosed]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [sp_Pts_Filter_Relate_RelatedToMeClosed]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed=1
AND
	ProblemID in
	(SELECT ProblemID FROM Pts_RelatedUsers Where UserID=@UserID)
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetSharedItems]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSharedItems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetSharedItems]
@ProjectID int
AS
SELECT * FROM v_Pts_SharedItems
WHERE ProjectID = @ProjectID
ORDER BY CreateDate DESC' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetSubscribeUserSet]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSubscribeUserSet]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetSubscribeUserSet]
@TargetType int,
@TargetID int,
@EventType int,
@Params nvarchar(1024)
AS
SELECT * FROM v_Accounts_Users WHERE UserID IN 
(SELECT UserID FROM Pts_Subscription WHERE TargetType=@TargetType AND TargetID=@TargetID AND ((EventType=@EventType and Params=@Params) or EventType=0)  )' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_SubscribedByMeClosed]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_SubscribedByMeClosed]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [sp_Pts_Filter_Relate_SubscribedByMeClosed]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 1
AND
	ProblemID in
	(Select TargetID FROM Pts_Subscription WHERE UserID=@UserID AND TargetType=2)
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_SubscribedByMe]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_SubscribedByMe]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [sp_Pts_Filter_Relate_SubscribedByMe]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	ProblemID in
	(Select TargetID FROM Pts_Subscription WHERE UserID=@UserID AND TargetType=2)
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Relate_SubscribedByMeOpen]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_SubscribedByMeOpen]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [sp_Pts_Filter_Relate_SubscribedByMeOpen]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0
AND
	ProblemID in
	(Select TargetID FROM Pts_Subscription WHERE UserID=@UserID AND TargetType=2)
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetAllProjects]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetAllProjects]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetAllProjects]
AS
SELECT * FROM v_Pts_Projects 
ORDER BY ListOrder' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProblemsForWBS]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemsForWBS]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE procedure [sp_Pts_GetProblemsForWBS]
@ProjectID int,
@OnlyOpen bit --是否只返回跟踪中的事务
AS

declare @aaa table(
	ProblemID int, 
	ProblemCode nvarchar(50),
	Title nvarchar(255),
	ProblemStateID int,
	ProblemStateName nvarchar(50),
	AssignedTo int,
	IsClosed bit,
	ParentID int,
	TotalChildNum int,
	ClosedChildNum int,
	CreateTime datetime,
	CloseTime datetime,
	CreateUserID int,
	Deadline datetime,
	LastTime datetime,
	LastUser int,
	StartTime datetime,
	IsCoworking bit,
	CoworkInfo nvarchar(500),
	StateDeadline datetime,
	IsActive bit,
	RealStartTime datetime,
	RealEndTime datetime)

-- 将项目中中到事务先添加进来
if @OnlyOpen=1 
begin
	insert @aaa
	select ProblemID,ProblemCode,Title,ProblemStateID,ProblemStateName,AssignedTo,IsClosed,ParentID,TotalChildNum,ClosedChildNum,
			CreateTime,CloseTime,CreateUserID,Deadline,LastTime,LastUser,StartTime,IsCoworking,CoworkInfo,StateDeadline,IsActive,RealStartTime,RealEndTime  
	FROM v_Pts_Problems 
	WHERE ProjectID=@ProjectID and IsClosed=0
end
else
begin
	insert @aaa
	select ProblemID,ProblemCode,Title,ProblemStateID,ProblemStateName,AssignedTo,IsClosed,ParentID,TotalChildNum,ClosedChildNum,
			CreateTime,CloseTime,CreateUserID,Deadline,LastTime,LastUser,StartTime,IsCoworking,CoworkInfo,StateDeadline,IsActive,RealStartTime,RealEndTime 
	FROM v_Pts_Problems 
	WHERE ProjectID=@ProjectID
end

--循环，将所有到子事务添加进来
declare @addcount int
set @addcount = 1

while @addcount > 0
begin

insert @aaa
select ProblemID,ProblemCode,Title,ProblemStateID,ProblemStateName,AssignedTo,IsClosed,ParentID,TotalChildNum,ClosedChildNum,
			CreateTime,CloseTime,CreateUserID,Deadline,LastTime,LastUser,StartTime,IsCoworking,CoworkInfo,StateDeadline,IsActive,RealStartTime,RealEndTime 
FROM v_Pts_Problems 
WHERE ParentID in (SELECT ProblemID FROM @aaa) AND  ProblemID NOT IN (SELECT ProblemID FROM @aaa)

set @addcount=@@ROWCOUNT

end

--返回数据
select * from @aaa
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Filter_Suspended]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Suspended]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [sp_Pts_Filter_Suspended]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND 
	IsClosed = 0
AND
	IsActive = 0' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetUserCreatedProblems]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserCreatedProblems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE  Procedure [sp_Pts_GetUserCreatedProblems]
@UserID int,
@IsClosed int = null 
AS
if (@IsClosed is null)
BEGIN
Select * From v_Pts_Problems where CreateUserID = @UserID
END
else 
BEGIN
Select * From v_Pts_Problems where CreateUserID = @UserID AND IsClosed=@IsClosed
END' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetOpenClosedIssuesByTime]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetOpenClosedIssuesByTime]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'create PROCEDURE [sp_Pts_GetOpenClosedIssuesByTime]
@ProjectID int,
@FirstDate datetime,
@LastDate datetime
AS
--得到一段时间内某个项目内创建或关闭的事务
SELECT * FROM v_Pts_Problems WHERE ProjectID=@ProjectID AND ((CreateTime >= @FirstDate AND CreateTime < @LastDate) 
                            OR (IsClosed = 1 AND CloseTime >= @FirstDate AND CloseTime < @LastDate))' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Stat_ByCreator]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Stat_ByCreator]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_Stat_ByCreator]
@ProjectID int
AS
if @ProjectID=0 
BEGIN
	Select count(*) as ProblemCount,
			sum(case when IsClosed=1 then 1 else 0 end) ClosedNum , 
			sum(case when IsClosed=0 then 1 else 0 end) OpenNum,
			CreateUserID
	from v_Pts_Problems 
	group by CreateUserID
END
ELSE
BEGIN
	Select count(*) as ProblemCount,
			sum(case when IsClosed=1 then 1 else 0 end) ClosedNum , 
			sum(case when IsClosed=0 then 1 else 0 end) OpenNum,
			CreateUserID
	from v_Pts_Problems where ProjectID=@ProjectID
	group by CreateUserID
END
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetChildIssues]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetChildIssues]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetChildIssues]
@ParentID int
AS
SELECT * FROM v_Pts_Problems WHERE ParentID=@ParentID ORDER BY IsClosed,ProjectID,ProblemID' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Stat_GetGlobalNum]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Stat_GetGlobalNum]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [sp_Pts_Stat_GetGlobalNum]
@ProjectID int
AS
--获取某个项目的事务总数、打开的事务数、关闭的事务数
select 	sum(case when ProblemID is not null then 1 else 0 end) TotalNum, 
	sum(case when IsClosed=1 then 1 else 0 end) ClosedNum , 
	sum(case when IsClosed=0 then 1 else 0 end) OpenNum,
	sum(case when IsActive=0 then 1 else 0 end) SuspendNum,
	sum(case when AssignedTo<0 then 1 else 0 end) ToWorkgroup
	
from v_Pts_Problems  where ProjectID = @ProjectID' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetWorkflowStepsBySrcState]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetWorkflowStepsBySrcState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'create procedure [sp_Pts_GetWorkflowStepsBySrcState] 
@ProjectID int,
@SrcStateID int
AS
SELECT * FROM 
v_Pts_WorkflowSteps 
WHERE ProjectID=@ProjectID AND SrcStateID=@SrcStateID 
ORDER BY ListOrder,SrcStateListOrder ASC,DstStateListOrder ASC' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetProblemAttachmentList]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemAttachmentList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_GetProblemAttachmentList]
@ProblemID int
AS
Select * from  v_Pts_ProblemAttachments where ProblemID = @ProblemID
' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_DeleteAllIssues]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteAllIssues]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE procedure [sp_Pts_DeleteAllIssues]
--清空项目的所有事务
@ProjectID int
AS

Begin transaction

Delete From Pts_RecordAttachments Where RecordID in 
	(Select RecordID From Pts_Records Where ProblemID IN(SELECT ProblemID FROM Pts_Problems WHERE ProjectID=@ProjectID))
Delete From Pts_Records Where ProblemID IN (SELECT ProblemID FROM Pts_Problems WHERE ProjectID=@ProjectID) 
Delete From Pts_ProblemAttachments Where ProblemID IN (SELECT ProblemID FROM Pts_Problems WHERE ProjectID=@ProjectID) 
Delete From Pts_ProblemStateRecord Where ProblemID IN (SELECT ProblemID FROM Pts_Problems WHERE ProjectID=@ProjectID) 
Delete From Pts_ProblemHistory Where ProblemID IN (SELECT ProblemID FROM Pts_Problems WHERE ProjectID=@ProjectID) 
Delete From Pts_ProblemRelations Where ProblemID_1 IN (SELECT ProblemID FROM Pts_Problems WHERE ProjectID=@ProjectID)  OR ProblemID_2 IN (SELECT ProblemID FROM Pts_Problems WHERE ProjectID=@ProjectID) 
Delete From Pts_ProblemShiftHistory Where ProblemID IN (SELECT ProblemID FROM Pts_Problems WHERE ProjectID=@ProjectID) 

Delete From Pts_ProblemVisitHistory Where ProblemID IN (SELECT ProblemID FROM Pts_Problems WHERE ProjectID=@ProjectID) 
Delete FROM Pts_ProblemRelatedArticles WHERE ProblemID IN (SELECT ProblemID FROM Pts_Problems WHERE ProjectID=@ProjectID) 
Delete FROM Pts_Coworkers WHERE ProblemID IN (SELECT ProblemID FROM Pts_Problems WHERE ProjectID=@ProjectID) 
Delete From Pts_Problems Where ProblemID IN (SELECT ProblemID FROM Pts_Problems WHERE ProjectID=@ProjectID) 

commit transaction

exec sp_Pts_ComputeProjectCounter @ProjectID


' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_DeleteProblem]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'




CREATE  PROCEDURE [sp_Pts_DeleteProblem]
@ProblemID int
AS
Begin transaction

Declare @ParentID int
select @ParentID=ParentID FROM Pts_Problems WHERE ProblemID = @ProblemID

Delete From Pts_RecordAttachments Where RecordID in 
	(Select RecordID From Pts_Records Where ProblemID = @ProblemID)
Delete From Pts_Records Where ProblemID = @ProblemID
Delete From Pts_ProblemAttachments Where ProblemID = @ProblemID
Delete From Pts_ProblemStateRecord Where ProblemID = @ProblemID
Delete From Pts_ProblemHistory Where ProblemID = @ProblemID
Delete From Pts_ProblemRelations Where ProblemID_1 = @ProblemID OR ProblemID_2 = @ProblemID
Delete From Pts_ProblemShiftHistory Where ProblemID = @ProblemID

Delete From Pts_ProblemVisitHistory Where ProblemID = @ProblemID
Delete FROM Pts_ProblemRelatedArticles WHERE ProblemID = @ProblemID
Delete FROM Pts_Coworkers WHERE ProblemID=@ProblemID
Delete From Pts_Problems Where ProblemID = @ProblemID

if @ParentID > 0
BEGIN
	exec sp_Pts_UpdateProblemChildNum @ParentID
END

commit transaction




' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_SetParentProblem]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_SetParentProblem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_SetParentProblem]
@ProblemID int,
@ParentID int
AS
UPDATE Pts_Problems Set ParentID=@ParentID WHERE ProblemID=@ProblemID

exec sp_Pts_UpdateProblemChildNum @ParentID' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_ChangeProblemState]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProblemState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





--更新问题的状态
CREATE        PROCEDURE [sp_Pts_ChangeProblemState]
@ProblemID int,
@NextStateID int,
@UserID int
AS

DECLARE @IsClosed bit
SELECT @IsClosed = IsClosed FROM v_Pts_Problems WHERE ProblemID = @ProblemID
IF @IsClosed = 0 
BEGIN
Update  Pts_Problems Set ProblemStateID=@NextStateID,AssignedTo=@UserID,CloseTime = GetDate() 
	Where ProblemID = @ProblemID
END
ELSE
BEGIN
Update  Pts_Problems Set ProblemStateID=@NextStateID,AssignedTo=@UserID
	Where ProblemID = @ProblemID
END

exec sp_Pts_RecordStateStart @ProblemID, @NextStateID, @UserID

Declare @ProjectID int
Declare @ParentID int
Select @ProjectID = ProjectID,@ParentID =ParentID From v_Pts_Problems Where ProblemID=@ProblemID
exec sp_Pts_ComputeProjectCounter @ProjectID

if @ParentID > 0
BEGIN
	exec sp_Pts_UpdateProblemChildNum @ParentID
END' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_CreateProblemCatalog]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProblemCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'





CREATE   PROCEDURE [sp_Pts_CreateProblemCatalog]
@ProjectID int,
@ParentID int,
@Name nvarchar(50),
@Description nvarchar(255),
@ResponseUser int=null
AS
----返回缩进值
Declare @Indent int
Declare @ParentPath varchar(255)

if (@ParentID = 0)
	BEGIN
	set @Indent = 1
	set @ParentPath = ''''
	END
	
else
	BEGIN
	Select @Indent = Indent,@ParentPath=Path from Pts_ProblemCatalogs where ProblemCatalogID = @ParentID
	Set @Indent = @Indent + 1
	END
----插入记录
Insert into Pts_ProblemCatalogs 
	(ProjectID, ParentID, Indent, [Name], Description, ResponseUser)
Values
	(@ProjectID, @ParentID, @Indent, @Name, @Description, @ResponseUser)

if @@IDENTITY>0
	Update Pts_ProblemCatalogs Set Path=(@ParentPath + Cast(@@Identity as varchar(50) ) + ''.'') Where ProblemCatalogID=@@Identity
----重新排序
exec sp_Pts_ResetProblemCatalogOrder @ProjectID,@ParentID

select @@IDENTITY

' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_GetCalendarEvents]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetCalendarEvents]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetCalendarEvents]
@ProjectID int,
@StartDate datetime,
@EndDate datetime
AS
--得到一段时间内某个项目内提交的records
SELECT * FROM v_Pts_RecordsForEvents WHERE ProjectID=@ProjectID AND DateCreated>=@StartDate AND DateCreated <= @EndDate' 
END
GO
/****** 对象:  StoredProcedure [sp_Pts_Stat_GetAvgStateKeepTime]    脚本日期: 03/18/2009 13:25:20 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Stat_GetAvgStateKeepTime]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'



create procedure [sp_Pts_Stat_GetAvgStateKeepTime]
@ProjectID int
AS
Select * From v_Pts_Stat_StateKeepTime Where ProjectID = @ProjectID order by ListOrder



' 
END
GO
/****** 对象:  Default [DF_Accounts_Department_ListOrder]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Accounts_Department_ListOrder]') AND type = 'D')
BEGIN
ALTER TABLE [Accounts_Department] ADD  CONSTRAINT [DF_Accounts_Department_ListOrder]  DEFAULT ((0)) FOR [ListOrder]

END
GO
/****** 对象:  Default [DF_Accounts_Users_DeptID]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Accounts_Users_DeptID]') AND type = 'D')
BEGIN
ALTER TABLE [Accounts_Users] ADD  CONSTRAINT [DF_Accounts_Users_DeptID]  DEFAULT ((0)) FOR [DeptID]

END
GO
/****** 对象:  Default [DF_Common_Config_AllowSelfRegister]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Common_Config_AllowSelfRegister]') AND type = 'D')
BEGIN
ALTER TABLE [Common_Config] ADD  CONSTRAINT [DF_Common_Config_AllowSelfRegister]  DEFAULT ((1)) FOR [AllowSelfRegister]

END
GO
/****** 对象:  Default [DF_Common_Config_AutoSetNewUserNormal]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Common_Config_AutoSetNewUserNormal]') AND type = 'D')
BEGIN
ALTER TABLE [Common_Config] ADD  CONSTRAINT [DF_Common_Config_AutoSetNewUserNormal]  DEFAULT ((1)) FOR [AutoSetNewUserNormal]

END
GO
/****** 对象:  Default [DF_Common_Config_RequireSmtpAuthentication]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Common_Config_RequireSmtpAuthentication]') AND type = 'D')
BEGIN
ALTER TABLE [Common_Config] ADD  CONSTRAINT [DF_Common_Config_RequireSmtpAuthentication]  DEFAULT ((0)) FOR [RequireSmtpAuthentication]

END
GO
/****** 对象:  Default [DF_Common_Config_MailServer]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Common_Config_MailServer]') AND type = 'D')
BEGIN
ALTER TABLE [Common_Config] ADD  CONSTRAINT [DF_Common_Config_MailServer]  DEFAULT ('') FOR [MailServer]

END
GO
/****** 对象:  Default [DF_Common_Config_MailUsername]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Common_Config_MailUsername]') AND type = 'D')
BEGIN
ALTER TABLE [Common_Config] ADD  CONSTRAINT [DF_Common_Config_MailUsername]  DEFAULT ('') FOR [MailUsername]

END
GO
/****** 对象:  Default [DF_Common_Config_MailPassword]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Common_Config_MailPassword]') AND type = 'D')
BEGIN
ALTER TABLE [Common_Config] ADD  CONSTRAINT [DF_Common_Config_MailPassword]  DEFAULT ('') FOR [MailPassword]

END
GO
/****** 对象:  Default [DF_Common_Config_SiteUrl]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Common_Config_SiteUrl]') AND type = 'D')
BEGIN
ALTER TABLE [Common_Config] ADD  CONSTRAINT [DF_Common_Config_SiteUrl]  DEFAULT ('') FOR [SiteUrl]

END
GO
/****** 对象:  Default [DF_Common_Config_CompanyName]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Common_Config_CompanyName]') AND type = 'D')
BEGIN
ALTER TABLE [Common_Config] ADD  CONSTRAINT [DF_Common_Config_CompanyName]  DEFAULT ('') FOR [CompanyName]

END
GO
/****** 对象:  Default [DF_Common_Config_CompanyUrl]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Common_Config_CompanyUrl]') AND type = 'D')
BEGIN
ALTER TABLE [Common_Config] ADD  CONSTRAINT [DF_Common_Config_CompanyUrl]  DEFAULT ('') FOR [CompanyUrl]

END
GO
/****** 对象:  Default [DF__Common_Co__Admin__16D94F8E]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF__Common_Co__Admin__16D94F8E]') AND type = 'D')
BEGIN
ALTER TABLE [Common_Config] ADD  CONSTRAINT [DF__Common_Co__Admin__16D94F8E]  DEFAULT ('') FOR [AdminMail]

END
GO
/****** 对象:  Default [DF_Common_Config_ExtendedSettings]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Common_Config_ExtendedSettings]') AND type = 'D')
BEGIN
ALTER TABLE [Common_Config] ADD  CONSTRAINT [DF_Common_Config_ExtendedSettings]  DEFAULT (N'') FOR [ExtendedSettings]

END
GO
/****** 对象:  Default [DF_Common_PersonalConfig_ProjectID]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Common_PersonalConfig_ProjectID]') AND type = 'D')
BEGIN
ALTER TABLE [Common_PersonalConfig] ADD  CONSTRAINT [DF_Common_PersonalConfig_ProjectID]  DEFAULT ((0)) FOR [ProjectID]

END
GO
/****** 对象:  Default [DF_Kb_ArticleComment_AddDate]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Kb_ArticleComment_AddDate]') AND type = 'D')
BEGIN
ALTER TABLE [Kb_ArticleComment] ADD  CONSTRAINT [DF_Kb_ArticleComment_AddDate]  DEFAULT (getdate()) FOR [AddDate]

END
GO
/****** 对象:  Default [DF_Kb_Articles_CreateDate]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Kb_Articles_CreateDate]') AND type = 'D')
BEGIN
ALTER TABLE [Kb_Articles] ADD  CONSTRAINT [DF_Kb_Articles_CreateDate]  DEFAULT (getdate()) FOR [CreateDate]

END
GO
/****** 对象:  Default [DF_Kb_Articles_UpdateDate]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Kb_Articles_UpdateDate]') AND type = 'D')
BEGIN
ALTER TABLE [Kb_Articles] ADD  CONSTRAINT [DF_Kb_Articles_UpdateDate]  DEFAULT (getdate()) FOR [UpdateDate]

END
GO
/****** 对象:  Default [DF_Kb_Articles_NumRead]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Kb_Articles_NumRead]') AND type = 'D')
BEGIN
ALTER TABLE [Kb_Articles] ADD  CONSTRAINT [DF_Kb_Articles_NumRead]  DEFAULT ((0)) FOR [NumRead]

END
GO
/****** 对象:  Default [DF_Kb_Articles_NumRatings]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Kb_Articles_NumRatings]') AND type = 'D')
BEGIN
ALTER TABLE [Kb_Articles] ADD  CONSTRAINT [DF_Kb_Articles_NumRatings]  DEFAULT ((0)) FOR [NumRatings]

END
GO
/****** 对象:  Default [DF_Kb_Articles_RateValue]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Kb_Articles_RateValue]') AND type = 'D')
BEGIN
ALTER TABLE [Kb_Articles] ADD  CONSTRAINT [DF_Kb_Articles_RateValue]  DEFAULT ((3)) FOR [RateValue]

END
GO
/****** 对象:  Default [DF_Kb_Articles_ArticleStatus]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Kb_Articles_ArticleStatus]') AND type = 'D')
BEGIN
ALTER TABLE [Kb_Articles] ADD  CONSTRAINT [DF_Kb_Articles_ArticleStatus]  DEFAULT ((0)) FOR [ArticleStatus]

END
GO
/****** 对象:  Default [DF_Kb_Articles_CreateUserID]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Kb_Articles_CreateUserID]') AND type = 'D')
BEGIN
ALTER TABLE [Kb_Articles] ADD  CONSTRAINT [DF_Kb_Articles_CreateUserID]  DEFAULT ((0)) FOR [CreateUserID]

END
GO
/****** 对象:  Default [DF_Kb_Articles_NumComment]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Kb_Articles_NumComment]') AND type = 'D')
BEGIN
ALTER TABLE [Kb_Articles] ADD  CONSTRAINT [DF_Kb_Articles_NumComment]  DEFAULT ((0)) FOR [NumComment]

END
GO
/****** 对象:  Default [DF_Kb_Articles_IsReviewed]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Kb_Articles_IsReviewed]') AND type = 'D')
BEGIN
ALTER TABLE [Kb_Articles] ADD  CONSTRAINT [DF_Kb_Articles_IsReviewed]  DEFAULT ((0)) FOR [IsReviewed]

END
GO
/****** 对象:  Default [DF_Kb_Articles_IsPublic]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Kb_Articles_IsPublic]') AND type = 'D')
BEGIN
ALTER TABLE [Kb_Articles] ADD  CONSTRAINT [DF_Kb_Articles_IsPublic]  DEFAULT ((1)) FOR [IsPublic]

END
GO
/****** 对象:  Default [DF_Kb_Catalogs_ParentID]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Kb_Catalogs_ParentID]') AND type = 'D')
BEGIN
ALTER TABLE [Kb_Categories] ADD  CONSTRAINT [DF_Kb_Catalogs_ParentID]  DEFAULT ((0)) FOR [ParentID]

END
GO
/****** 对象:  Default [DF_Kb_Categories_CreateDate]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Kb_Categories_CreateDate]') AND type = 'D')
BEGIN
ALTER TABLE [Kb_Categories] ADD  CONSTRAINT [DF_Kb_Categories_CreateDate]  DEFAULT (getdate()) FOR [CreateDate]

END
GO
/****** 对象:  Default [DF_Kb_Categories_ArticleCount]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Kb_Categories_ArticleCount]') AND type = 'D')
BEGIN
ALTER TABLE [Kb_Categories] ADD  CONSTRAINT [DF_Kb_Categories_ArticleCount]  DEFAULT ((0)) FOR [ArticleCount]

END
GO
/****** 对象:  Default [DF_Kb_Categories_IsPublic]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Kb_Categories_IsPublic]') AND type = 'D')
BEGIN
ALTER TABLE [Kb_Categories] ADD  CONSTRAINT [DF_Kb_Categories_IsPublic]  DEFAULT ((1)) FOR [IsPublic]

END
GO
/****** 对象:  Default [DF_Kb_Categories_RequireReview]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Kb_Categories_RequireReview]') AND type = 'D')
BEGIN
ALTER TABLE [Kb_Categories] ADD  CONSTRAINT [DF_Kb_Categories_RequireReview]  DEFAULT ((1)) FOR [RequireReview]

END
GO
/****** 对象:  Default [DF_Pts_Coworkers_IsWorked]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Coworkers_IsWorked]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Coworkers] ADD  CONSTRAINT [DF_Pts_Coworkers_IsWorked]  DEFAULT ((0)) FOR [IsWorked]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_ProjectID]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_ProjectID]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_ProjectID]  DEFAULT ((1)) FOR [ProjectID]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_Hidden]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_Hidden]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_Hidden]  DEFAULT ((0)) FOR [Hidden]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_InUse]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_InUse]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_InUse]  DEFAULT ((0)) FOR [InUse]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_ListOrder]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_ListOrder]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_ListOrder]  DEFAULT ((1000)) FOR [ListOrder]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_Required]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_Required]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_Required]  DEFAULT ((1)) FOR [Required]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_HtmlEdit]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_HtmlEdit]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_HtmlEdit]  DEFAULT ((0)) FOR [HtmlEdit]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_MaxLength]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_MaxLength]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_MaxLength]  DEFAULT ((0)) FOR [MaxLength]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_UseSelectList]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_UseSelectList]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_UseSelectList]  DEFAULT ((0)) FOR [UseSelectList]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_UseGlobalSelect]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_UseGlobalSelect]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_UseGlobalSelect]  DEFAULT ((0)) FOR [UseGlobalSelect]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_ReadableToAll]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_ReadableToAll]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_ReadableToAll]  DEFAULT ((1)) FOR [ReadableToAll]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_EditableToAll]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_EditableToAll]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_EditableToAll]  DEFAULT ((1)) FOR [EditableToAll]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_IsSystem]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_IsSystem]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_IsSystem]  DEFAULT ((0)) FOR [IsSystem]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_VisibleWhenNew]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_VisibleWhenNew]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_VisibleWhenNew]  DEFAULT ((1)) FOR [VisibleWhenNew]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_VisibleWhenEdit]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_VisibleWhenEdit]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_VisibleWhenEdit]  DEFAULT ((1)) FOR [VisibleWhenEdit]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_VisibleInList]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_VisibleInList]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_VisibleInList]  DEFAULT ((0)) FOR [VisibleInList]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_VisibleToCreator]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_VisibleToCreator]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_VisibleToCreator]  DEFAULT ((1)) FOR [ReadableToCreateUser]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_EditableToCreateUser]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_EditableToCreateUser]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_EditableToCreateUser]  DEFAULT ((1)) FOR [EditableToCreateUser]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_EnableFieldQuickSearch]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_EnableFieldQuickSearch]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_EnableFieldQuickSearch]  DEFAULT ((0)) FOR [EnableFieldQuickSearch]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_EnableCollapseDisplay]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_EnableCollapseDisplay]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_EnableCollapseDisplay]  DEFAULT ((0)) FOR [EnableCollapseDisplay]

END
GO
/****** 对象:  Default [DF_Pts_DbDirectory_ShowTime]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_DbDirectory_ShowTime]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_DbDirectory] ADD  CONSTRAINT [DF_Pts_DbDirectory_ShowTime]  DEFAULT ((1)) FOR [ShowTime]

END
GO
/****** 对象:  Default [DF_Pts_FavoriteLinks_ListOrder]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_FavoriteLinks_ListOrder]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_FavoriteLinks] ADD  CONSTRAINT [DF_Pts_FavoriteLinks_ListOrder]  DEFAULT ((0)) FOR [ListOrder]

END
GO
/****** 对象:  Default [DF_Pts_FilterNodes_IsLeaf]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_FilterNodes_IsLeaf]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_FilterNodes] ADD  CONSTRAINT [DF_Pts_FilterNodes_IsLeaf]  DEFAULT ((0)) FOR [IsLeaf]

END
GO
/****** 对象:  Default [DF_Pts_FilterNodes_SupportRuntimeValue]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_FilterNodes_SupportRuntimeValue]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_FilterNodes] ADD  CONSTRAINT [DF_Pts_FilterNodes_SupportRuntimeValue]  DEFAULT ((0)) FOR [SupportRuntimeValue]

END
GO
/****** 对象:  Default [DF_Pts_FilterNodes_LeftChildID]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_FilterNodes_LeftChildID]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_FilterNodes] ADD  CONSTRAINT [DF_Pts_FilterNodes_LeftChildID]  DEFAULT ((0)) FOR [LeftChildID]

END
GO
/****** 对象:  Default [DF_Pts_FilterNodes_RightChildID]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_FilterNodes_RightChildID]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_FilterNodes] ADD  CONSTRAINT [DF_Pts_FilterNodes_RightChildID]  DEFAULT ((0)) FOR [RightChildID]

END
GO
/****** 对象:  Default [DF_Pts_FilterNodes_ParentID]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_FilterNodes_ParentID]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_FilterNodes] ADD  CONSTRAINT [DF_Pts_FilterNodes_ParentID]  DEFAULT ((0)) FOR [ParentID]

END
GO
/****** 对象:  Default [DF_Pts_FilterNodes_IsLeftChild]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_FilterNodes_IsLeftChild]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_FilterNodes] ADD  CONSTRAINT [DF_Pts_FilterNodes_IsLeftChild]  DEFAULT ((1)) FOR [IsLeftChild]

END
GO
/****** 对象:  Default [DF_Pts_FilterNodes_IsNot]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_FilterNodes_IsNot]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_FilterNodes] ADD  CONSTRAINT [DF_Pts_FilterNodes_IsNot]  DEFAULT ((0)) FOR [IsNot]

END
GO
/****** 对象:  Default [DF_Pts_Filters_FilterName]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Filters_FilterName]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Filters] ADD  CONSTRAINT [DF_Pts_Filters_FilterName]  DEFAULT ('') FOR [FilterName]

END
GO
/****** 对象:  Default [DF_Pts_Filters_IsPublic]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Filters_IsPublic]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Filters] ADD  CONSTRAINT [DF_Pts_Filters_IsPublic]  DEFAULT ((0)) FOR [IsPublic]

END
GO
/****** 对象:  Default [DF_Pts_Filters_ProjectID]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Filters_ProjectID]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Filters] ADD  CONSTRAINT [DF_Pts_Filters_ProjectID]  DEFAULT ((0)) FOR [ProjectID]

END
GO
/****** 对象:  Default [DF_Pts_Filters_MaxNodeID]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Filters_MaxNodeID]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Filters] ADD  CONSTRAINT [DF_Pts_Filters_MaxNodeID]  DEFAULT ((0)) FOR [MaxNodeID]

END
GO
/****** 对象:  Default [DF_Pts_IncomingSms_IsAutoProcessed]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_IncomingSms_IsAutoProcessed]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_IncomingSms] ADD  CONSTRAINT [DF_Pts_IncomingSms_IsAutoProcessed]  DEFAULT ((0)) FOR [IsAutoProcessed]

END
GO
/****** 对象:  Default [DF_Pts_IncomingSms_IsReviewed]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_IncomingSms_IsReviewed]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_IncomingSms] ADD  CONSTRAINT [DF_Pts_IncomingSms_IsReviewed]  DEFAULT ((0)) FOR [IsReviewed]

END
GO
/****** 对象:  Default [DF_Pts_Notify_EventTime]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Notify_EventTime]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Notify] ADD  CONSTRAINT [DF_Pts_Notify_EventTime]  DEFAULT (getdate()) FOR [EventTime]

END
GO
/****** 对象:  Default [DF_Pts_Notify_NotifyAssignee]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Notify_NotifyAssignee]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Notify] ADD  CONSTRAINT [DF_Pts_Notify_NotifyAssignee]  DEFAULT ((1)) FOR [NotifyAssignee]

END
GO
/****** 对象:  Default [DF_Pts_OutConnectorNotifys_AddTime]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_OutConnectorNotifys_AddTime]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_OutConnectorNotifys] ADD  CONSTRAINT [DF_Pts_OutConnectorNotifys_AddTime]  DEFAULT (getdate()) FOR [AddTime]

END
GO
/****** 对象:  Default [DF_HWSite_OutSms_IsSend]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_HWSite_OutSms_IsSend]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_OutSms] ADD  CONSTRAINT [DF_HWSite_OutSms_IsSend]  DEFAULT ((0)) FOR [IsSend]

END
GO
/****** 对象:  Default [DF_HWSite_OutSms_SubmitTime]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_HWSite_OutSms_SubmitTime]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_OutSms] ADD  CONSTRAINT [DF_HWSite_OutSms_SubmitTime]  DEFAULT (getdate()) FOR [SubmitTime]

END
GO
/****** 对象:  Default [DF_Pts_ProblemCatalogs_ParentID]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProblemCatalogs_ParentID]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProblemCatalogs] ADD  CONSTRAINT [DF_Pts_ProblemCatalogs_ParentID]  DEFAULT ((0)) FOR [ParentID]

END
GO
/****** 对象:  Default [DF_Pts_ProblemCatalogs_Indent]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProblemCatalogs_Indent]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProblemCatalogs] ADD  CONSTRAINT [DF_Pts_ProblemCatalogs_Indent]  DEFAULT ((0)) FOR [Indent]

END
GO
/****** 对象:  Default [DF_Pts_ProblemCatalogs_ListOrder]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProblemCatalogs_ListOrder]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProblemCatalogs] ADD  CONSTRAINT [DF_Pts_ProblemCatalogs_ListOrder]  DEFAULT ((100000)) FOR [ListOrder]

END
GO
/****** 对象:  Default [DF_Pts_ProblemFieldEditableState_IsRequired]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProblemFieldEditableState_IsRequired]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProblemFieldEditableState] ADD  CONSTRAINT [DF_Pts_ProblemFieldEditableState_IsRequired]  DEFAULT ((0)) FOR [IsRequired]

END
GO
/****** 对象:  Default [DF_Pts_ProblemPriority_DisplayColor]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProblemPriority_DisplayColor]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProblemPriority] ADD  CONSTRAINT [DF_Pts_ProblemPriority_DisplayColor]  DEFAULT ('#000000') FOR [DisplayColor]

END
GO
/****** 对象:  Default [DF_Pts_ProblemPriority_ListOrder]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProblemPriority_ListOrder]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProblemPriority] ADD  CONSTRAINT [DF_Pts_ProblemPriority_ListOrder]  DEFAULT ((100000)) FOR [ListOrder]

END
GO
/****** 对象:  Default [DF_Pts_Problems_OriginProjectID]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Problems_OriginProjectID]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Problems] ADD  CONSTRAINT [DF_Pts_Problems_OriginProjectID]  DEFAULT ((0)) FOR [OriginProjectID]

END
GO
/****** 对象:  Default [DF_Pts_Problems_ProblemIndex]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Problems_ProblemIndex]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Problems] ADD  CONSTRAINT [DF_Pts_Problems_ProblemIndex]  DEFAULT ((1)) FOR [ProblemIndex]

END
GO
/****** 对象:  Default [DF_Pts_Problems_CreateTime]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Problems_CreateTime]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Problems] ADD  CONSTRAINT [DF_Pts_Problems_CreateTime]  DEFAULT (getdate()) FOR [CreateTime]

END
GO
/****** 对象:  Default [DF_Pts_Problems_RecordNum]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Problems_RecordNum]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Problems] ADD  CONSTRAINT [DF_Pts_Problems_RecordNum]  DEFAULT ((0)) FOR [RecordNum]

END
GO
/****** 对象:  Default [DF_Pts_Problems_ReadByAssignee]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Problems_ReadByAssignee]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Problems] ADD  CONSTRAINT [DF_Pts_Problems_ReadByAssignee]  DEFAULT ((0)) FOR [ReadByAssignee]

END
GO
/****** 对象:  Default [DF_Pts_Problems_IsCoworking]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Problems_IsCoworking]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Problems] ADD  CONSTRAINT [DF_Pts_Problems_IsCoworking]  DEFAULT ((0)) FOR [IsCoworking]

END
GO
/****** 对象:  Default [DF_Pts_Problems_AttachNum]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Problems_AttachNum]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Problems] ADD  CONSTRAINT [DF_Pts_Problems_AttachNum]  DEFAULT ((0)) FOR [AttachNum]

END
GO
/****** 对象:  Default [DF_Pts_Problems_ParentID]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Problems_ParentID]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Problems] ADD  CONSTRAINT [DF_Pts_Problems_ParentID]  DEFAULT ((0)) FOR [ParentID]

END
GO
/****** 对象:  Default [DF_Pts_Problems_TotalChildNum]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Problems_TotalChildNum]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Problems] ADD  CONSTRAINT [DF_Pts_Problems_TotalChildNum]  DEFAULT ((0)) FOR [TotalChildNum]

END
GO
/****** 对象:  Default [DF_Pts_Problems_ClosedChildNum]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Problems_ClosedChildNum]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Problems] ADD  CONSTRAINT [DF_Pts_Problems_ClosedChildNum]  DEFAULT ((0)) FOR [ClosedChildNum]

END
GO
/****** 对象:  Default [DF_Pts_Problems_IsActive]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Problems_IsActive]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Problems] ADD  CONSTRAINT [DF_Pts_Problems_IsActive]  DEFAULT ((1)) FOR [IsActive]

END
GO
/****** 对象:  Default [DF_Pts_ProblemSeverity_DisplayColor]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProblemSeverity_DisplayColor]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProblemSeverity] ADD  CONSTRAINT [DF_Pts_ProblemSeverity_DisplayColor]  DEFAULT ('#000000') FOR [DisplayColor]

END
GO
/****** 对象:  Default [DF_Pts_ProblemSeverity_ListOrder]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProblemSeverity_ListOrder]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProblemSeverity] ADD  CONSTRAINT [DF_Pts_ProblemSeverity_ListOrder]  DEFAULT ((100000)) FOR [ListOrder]

END
GO
/****** 对象:  Default [DF_Pts_ProblemState_ListOrder]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProblemState_ListOrder]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProblemState] ADD  CONSTRAINT [DF_Pts_ProblemState_ListOrder]  DEFAULT ((100000)) FOR [ListOrder]

END
GO
/****** 对象:  Default [DF_Pts_ProblemState_IsFinalState]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProblemState_IsFinalState]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProblemState] ADD  CONSTRAINT [DF_Pts_ProblemState_IsFinalState]  DEFAULT ((0)) FOR [IsFinalState]

END
GO
/****** 对象:  Default [DF_Pts_ProblemState_AssignType]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProblemState_AssignType]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProblemState] ADD  CONSTRAINT [DF_Pts_ProblemState_AssignType]  DEFAULT ((0)) FOR [AssignType]

END
GO
/****** 对象:  Default [DF_Pts_StateTimeLimit_HoldingPeriod]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_StateTimeLimit_HoldingPeriod]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProblemStateTimeLimit] ADD  CONSTRAINT [DF_Pts_StateTimeLimit_HoldingPeriod]  DEFAULT ((0)) FOR [HoldingPeriod]

END
GO
/****** 对象:  Default [DF_Pts_StateTimeLimit_EndLimit]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_StateTimeLimit_EndLimit]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProblemStateTimeLimit] ADD  CONSTRAINT [DF_Pts_StateTimeLimit_EndLimit]  DEFAULT ((0)) FOR [EndLimit]

END
GO
/****** 对象:  Default [DF_Pts_ProblemStateTimeLimitRoleItems_ListOrder]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProblemStateTimeLimitRoleItems_ListOrder]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProblemStateTimeLimitRoleItems] ADD  CONSTRAINT [DF_Pts_ProblemStateTimeLimitRoleItems_ListOrder]  DEFAULT ((0)) FOR [ListOrder]

END
GO
/****** 对象:  Default [DF_Pts_ProblemTimers1_IsDeleteAfterIssueClose]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProblemTimers1_IsDeleteAfterIssueClose]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProblemTimers1] ADD  CONSTRAINT [DF_Pts_ProblemTimers1_IsDeleteAfterIssueClose]  DEFAULT ((1)) FOR [IsDeleteAfterIssueClose]

END
GO
/****** 对象:  Default [DF_Pts_ProblemTimers1_ExecuteCount]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProblemTimers1_ExecuteCount]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProblemTimers1] ADD  CONSTRAINT [DF_Pts_ProblemTimers1_ExecuteCount]  DEFAULT ((0)) FOR [ExecuteCount]

END
GO
/****** 对象:  Default [DF_Pts_ProblemType_ListOrder]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProblemType_ListOrder]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProblemType] ADD  CONSTRAINT [DF_Pts_ProblemType_ListOrder]  DEFAULT ((100000)) FOR [ListOrder]

END
GO
/****** 对象:  Default [DF_Pts_ProblemVisitHistory_VisitIndex]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProblemVisitHistory_VisitIndex]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProblemVisitHistory] ADD  CONSTRAINT [DF_Pts_ProblemVisitHistory_VisitIndex]  DEFAULT ((1)) FOR [VisitIndex]

END
GO
/****** 对象:  Default [DF_Pts_ProjectCatalogs_ListOrder]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProjectCatalogs_ListOrder]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProjectCatalogs] ADD  CONSTRAINT [DF_Pts_ProjectCatalogs_ListOrder]  DEFAULT ((100000)) FOR [ListOrder]

END
GO
/****** 对象:  Default [DF_Pts_ProjectCatalogs_ParentID]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProjectCatalogs_ParentID]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProjectCatalogs] ADD  CONSTRAINT [DF_Pts_ProjectCatalogs_ParentID]  DEFAULT ((0)) FOR [ParentID]

END
GO
/****** 对象:  Default [DF_Pts_Projects_ProjectCode]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_ProjectCode]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_ProjectCode]  DEFAULT ('') FOR [ProjectCode]

END
GO
/****** 对象:  Default [DF_Pts_Projects_ShortName]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_ShortName]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_ShortName]  DEFAULT ('') FOR [ShortName]

END
GO
/****** 对象:  Default [DF_Pts_Projects_Description]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_Description]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_Description]  DEFAULT ('') FOR [Description]

END
GO
/****** 对象:  Default [DF_Pts_Projects_TotalProblems]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_TotalProblems]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_TotalProblems]  DEFAULT ((0)) FOR [TotalProblems]

END
GO
/****** 对象:  Default [DF_Pts_Projects_TotalThreads]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_TotalThreads]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_TotalThreads]  DEFAULT ((0)) FOR [TotalThreads]

END
GO
/****** 对象:  Default [DF_Pts_Projects_TotalRecords]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_TotalRecords]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_TotalRecords]  DEFAULT ((0)) FOR [TotalRecords]

END
GO
/****** 对象:  Default [DF_Pts_Projects_DateCreated]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_DateCreated]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_DateCreated]  DEFAULT (getdate()) FOR [DateCreated]

END
GO
/****** 对象:  Default [DF_Pts_Projects_ProblemTitle]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_ProblemTitle]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_ProblemTitle]  DEFAULT (N'问题') FOR [ProblemTitle]

END
GO
/****** 对象:  Default [DF_Pts_Projects_ThreadTitle]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_ThreadTitle]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_ThreadTitle]  DEFAULT (N'任务') FOR [ThreadTitle]

END
GO
/****** 对象:  Default [DF_Pts_Projects_RecordTitle]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_RecordTitle]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_RecordTitle]  DEFAULT (N'记录') FOR [RecordTitle]

END
GO
/****** 对象:  Default [DF_Pts_Projects_ChangeOwnProblem]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_ChangeOwnProblem]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_ChangeOwnProblem]  DEFAULT ((1)) FOR [OnlyViewOwnProblem]

END
GO
/****** 对象:  Default [DF_Pts_Projects_EnableProblemState]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_EnableProblemState]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_EnableProblemState]  DEFAULT ((1)) FOR [EnableProblemState]

END
GO
/****** 对象:  Default [DF_Pts_Projects_MultipleThread]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_MultipleThread]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_MultipleThread]  DEFAULT ((0)) FOR [EnableMultipleThread]

END
GO
/****** 对象:  Default [DF_Pts_Projects_EnableEMailNotify]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_EnableEMailNotify]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_EnableEMailNotify]  DEFAULT ((1)) FOR [EnableEmailNotify]

END
GO
/****** 对象:  Default [DF_Pts_Projects_EnableProblemPriority]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_EnableProblemPriority]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_EnableProblemPriority]  DEFAULT ((1)) FOR [EnableProblemPriority]

END
GO
/****** 对象:  Default [DF_Pts_Projects_EnableProblemSeverity]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_EnableProblemSeverity]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_EnableProblemSeverity]  DEFAULT ((1)) FOR [EnableProblemSeverity]

END
GO
/****** 对象:  Default [DF_Pts_Projects_EnableProblemType]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_EnableProblemType]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_EnableProblemType]  DEFAULT ((1)) FOR [EnableProblemType]

END
GO
/****** 对象:  Default [DF_Pts_Projects_EnableProblemCatalog]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_EnableProblemCatalog]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_EnableProblemCatalog]  DEFAULT ((1)) FOR [EnableProblemCatalog]

END
GO
/****** 对象:  Default [DF_Pts_Projects_EnableProblemDeadline]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_EnableProblemDeadline]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_EnableProblemDeadline]  DEFAULT ((1)) FOR [EnableProblemDeadline]

END
GO
/****** 对象:  Default [DF_Pts_Projects_VisibleToAll]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_VisibleToAll]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_VisibleToAll]  DEFAULT ((1)) FOR [VisibleToAll]

END
GO
/****** 对象:  Default [DF_Pts_Projects_EnableStateTimer]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_EnableStateTimer]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_EnableStateTimer]  DEFAULT ((1)) FOR [EnableStateTimer]

END
GO
/****** 对象:  Default [DF_Pts_Projects_AutoAssignedTo]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_AutoAssignedTo]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_AutoAssignedTo]  DEFAULT ((1)) FOR [AutoAssignedTo]

END
GO
/****** 对象:  Default [DF__Pts_Proje__OpenP__3DE82FB7]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF__Pts_Proje__OpenP__3DE82FB7]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF__Pts_Proje__OpenP__3DE82FB7]  DEFAULT ((0)) FOR [OpenProblems]

END
GO
/****** 对象:  Default [DF_Pts_Projects_NewProblemIndex]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_NewProblemIndex]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_NewProblemIndex]  DEFAULT ((1)) FOR [NewProblemIndex]

END
GO
/****** 对象:  Default [DF_Pts_Projects_SecurityLevel]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_SecurityLevel]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_SecurityLevel]  DEFAULT ((2)) FOR [SecurityLevel]

END
GO
/****** 对象:  Default [DF_Pts_Projects_ListOrder]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Projects_ListOrder]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Projects] ADD  CONSTRAINT [DF_Pts_Projects_ListOrder]  DEFAULT ((1000)) FOR [ListOrder]

END
GO
/****** 对象:  Default [DF_Pts_ProjectStates_IsCloseState]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProjectStates_IsCloseState]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProjectStates] ADD  CONSTRAINT [DF_Pts_ProjectStates_IsCloseState]  DEFAULT ((0)) FOR [IsCloseState]

END
GO
/****** 对象:  Default [DF_Pts_ProjectStates_ListOrder]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProjectStates_ListOrder]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProjectStates] ADD  CONSTRAINT [DF_Pts_ProjectStates_ListOrder]  DEFAULT ((100000)) FOR [ListOrder]

END
GO
/****** 对象:  Default [DF_Pts_ProjectTemplets_EnableTrace]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProjectTemplets_EnableTrace]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProjectTemplets] ADD  CONSTRAINT [DF_Pts_ProjectTemplets_EnableTrace]  DEFAULT ((1)) FOR [EnableTrace]

END
GO
/****** 对象:  Default [DF_Pts_ProjectTemplets_EnableSeverity]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProjectTemplets_EnableSeverity]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProjectTemplets] ADD  CONSTRAINT [DF_Pts_ProjectTemplets_EnableSeverity]  DEFAULT ((1)) FOR [EnableSeverity]

END
GO
/****** 对象:  Default [DF_Pts_ProjectTemplets_EnableProblemState]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProjectTemplets_EnableProblemState]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProjectTemplets] ADD  CONSTRAINT [DF_Pts_ProjectTemplets_EnableProblemState]  DEFAULT ((1)) FOR [EnableProblemState]

END
GO
/****** 对象:  Default [DF_Pts_ProjectTemplets_EnableEmailNotify]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProjectTemplets_EnableEmailNotify]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProjectTemplets] ADD  CONSTRAINT [DF_Pts_ProjectTemplets_EnableEmailNotify]  DEFAULT ((1)) FOR [EnableEmailNotify]

END
GO
/****** 对象:  Default [DF_Pts_ProjectTemplets_EnableProblemDeadline]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProjectTemplets_EnableProblemDeadline]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProjectTemplets] ADD  CONSTRAINT [DF_Pts_ProjectTemplets_EnableProblemDeadline]  DEFAULT ((1)) FOR [EnableProblemDeadline]

END
GO
/****** 对象:  Default [DF_Pts_ProjectTemplets_EnableRecordType]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_ProjectTemplets_EnableRecordType]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_ProjectTemplets] ADD  CONSTRAINT [DF_Pts_ProjectTemplets_EnableRecordType]  DEFAULT ((1)) FOR [EnableRecordType]

END
GO
/****** 对象:  Default [DF_Pts_Records_DateCreated]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Records_DateCreated]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Records] ADD  CONSTRAINT [DF_Pts_Records_DateCreated]  DEFAULT (getdate()) FOR [DateCreated]

END
GO
/****** 对象:  Default [DF__Pts_Recor__State__3EDC53F0]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF__Pts_Recor__State__3EDC53F0]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Records] ADD  CONSTRAINT [DF__Pts_Recor__State__3EDC53F0]  DEFAULT ((0)) FOR [StateID]

END
GO
/****** 对象:  Default [DF_Pts_Records_Type]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Records_Type]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Records] ADD  CONSTRAINT [DF_Pts_Records_Type]  DEFAULT ((0)) FOR [RecordClass]

END
GO
/****** 对象:  Default [DF_Pts_Records_ParentRecordID]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Records_ParentRecordID]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Records] ADD  CONSTRAINT [DF_Pts_Records_ParentRecordID]  DEFAULT ((0)) FOR [ParentRecordID]

END
GO
/****** 对象:  Default [DF_Pts_RecordType_ListOrder]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_RecordType_ListOrder]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_RecordType] ADD  CONSTRAINT [DF_Pts_RecordType_ListOrder]  DEFAULT ((100000)) FOR [ListOrder]

END
GO
/****** 对象:  Default [DF_Pts_StepActions_SetFieldValueActionList]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_StepActions_SetFieldValueActionList]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_StepActions] ADD  CONSTRAINT [DF_Pts_StepActions_SetFieldValueActionList]  DEFAULT ('') FOR [SetFieldValueActionList]

END
GO
/****** 对象:  Default [DF_Pts_Subscription_EventType]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Subscription_EventType]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Subscription] ADD  CONSTRAINT [DF_Pts_Subscription_EventType]  DEFAULT ((0)) FOR [EventType]

END
GO
/****** 对象:  Default [DF_Pts_WorkflowSteps_ListOrder]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_WorkflowSteps_ListOrder]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_WorkflowSteps] ADD  CONSTRAINT [DF_Pts_WorkflowSteps_ListOrder]  DEFAULT ((100)) FOR [ListOrder]

END
GO
/****** 对象:  Default [DF_Pts_Workgroups_AdminProject]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Workgroups_AdminProject]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Workgroups] ADD  CONSTRAINT [DF_Pts_Workgroups_AdminProject]  DEFAULT ((0)) FOR [AdminProject]

END
GO
/****** 对象:  Default [DF_Pts_Workgroups_AddProblem]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Workgroups_AddProblem]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Workgroups] ADD  CONSTRAINT [DF_Pts_Workgroups_AddProblem]  DEFAULT ((0)) FOR [AddProblem]

END
GO
/****** 对象:  Default [DF_Pts_Workgroups_ChangeProblemState]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Workgroups_ChangeProblemState]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Workgroups] ADD  CONSTRAINT [DF_Pts_Workgroups_ChangeProblemState]  DEFAULT ((0)) FOR [ChangeProblemState]

END
GO
/****** 对象:  Default [DF_Pts_Workgroups_NewThread]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Workgroups_NewThread]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Workgroups] ADD  CONSTRAINT [DF_Pts_Workgroups_NewThread]  DEFAULT ((0)) FOR [ShiftProblem]

END
GO
/****** 对象:  Default [DF_Pts_Workgroups_NewRecord]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Workgroups_NewRecord]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Workgroups] ADD  CONSTRAINT [DF_Pts_Workgroups_NewRecord]  DEFAULT ((0)) FOR [AddRecord]

END
GO
/****** 对象:  Default [DF_Pts_Workgroups_ListOrder]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_Workgroups_ListOrder]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_Workgroups] ADD  CONSTRAINT [DF_Pts_Workgroups_ListOrder]  DEFAULT ((100000)) FOR [ListOrder]

END
GO
/****** 对象:  Default [DF_Pts_WorkgroupUsers_IsManager]    脚本日期: 03/18/2009 13:25:20 ******/
IF Not EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Pts_WorkgroupUsers_IsManager]') AND type = 'D')
BEGIN
ALTER TABLE [Pts_WorkgroupUsers] ADD  CONSTRAINT [DF_Pts_WorkgroupUsers_IsManager]  DEFAULT ((0)) FOR [IsManager]

END
GO
/****** 对象:  ForeignKey [FK_Accounts_Permissions_Accounts_PermissionCategories]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Accounts_Permissions_Accounts_PermissionCategories]') AND type = 'F')
ALTER TABLE [Accounts_Permissions]  WITH CHECK ADD  CONSTRAINT [FK_Accounts_Permissions_Accounts_PermissionCategories] FOREIGN KEY([CategoryID])
REFERENCES [Accounts_PermissionCategories] ([CategoryID])
GO
ALTER TABLE [Accounts_Permissions] CHECK CONSTRAINT [FK_Accounts_Permissions_Accounts_PermissionCategories]
GO
/****** 对象:  ForeignKey [FK_Accounts_RolePermissions_Accounts_Permissions]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Accounts_RolePermissions_Accounts_Permissions]') AND type = 'F')
ALTER TABLE [Accounts_RolePermissions]  WITH CHECK ADD  CONSTRAINT [FK_Accounts_RolePermissions_Accounts_Permissions] FOREIGN KEY([PermissionID])
REFERENCES [Accounts_Permissions] ([PermissionID])
GO
ALTER TABLE [Accounts_RolePermissions] CHECK CONSTRAINT [FK_Accounts_RolePermissions_Accounts_Permissions]
GO
/****** 对象:  ForeignKey [FK_Accounts_RolePermissions_Accounts_Roles]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Accounts_RolePermissions_Accounts_Roles]') AND type = 'F')
ALTER TABLE [Accounts_RolePermissions]  WITH CHECK ADD  CONSTRAINT [FK_Accounts_RolePermissions_Accounts_Roles] FOREIGN KEY([RoleID])
REFERENCES [Accounts_Roles] ([RoleID])
GO
ALTER TABLE [Accounts_RolePermissions] CHECK CONSTRAINT [FK_Accounts_RolePermissions_Accounts_Roles]
GO
/****** 对象:  ForeignKey [FK_Accounts_UserRoles_Accounts_Roles]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Accounts_UserRoles_Accounts_Roles]') AND type = 'F')
ALTER TABLE [Accounts_UserRoles]  WITH CHECK ADD  CONSTRAINT [FK_Accounts_UserRoles_Accounts_Roles] FOREIGN KEY([RoleID])
REFERENCES [Accounts_Roles] ([RoleID])
GO
ALTER TABLE [Accounts_UserRoles] CHECK CONSTRAINT [FK_Accounts_UserRoles_Accounts_Roles]
GO
/****** 对象:  ForeignKey [FK_Accounts_UserRoles_Accounts_Users]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Accounts_UserRoles_Accounts_Users]') AND type = 'F')
ALTER TABLE [Accounts_UserRoles]  WITH NOCHECK ADD  CONSTRAINT [FK_Accounts_UserRoles_Accounts_Users] FOREIGN KEY([UserID])
REFERENCES [Accounts_Users] ([UserID])
GO
ALTER TABLE [Accounts_UserRoles] CHECK CONSTRAINT [FK_Accounts_UserRoles_Accounts_Users]
GO
/****** 对象:  ForeignKey [FK_Accounts_Users_Accounts_AccessLists]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Accounts_Users_Accounts_AccessLists]') AND type = 'F')
ALTER TABLE [Accounts_Users]  WITH CHECK ADD  CONSTRAINT [FK_Accounts_Users_Accounts_AccessLists] FOREIGN KEY([AccessListID])
REFERENCES [Accounts_AccessLists] ([AccessListID])
GO
ALTER TABLE [Accounts_Users] CHECK CONSTRAINT [FK_Accounts_Users_Accounts_AccessLists]
GO
/****** 对象:  ForeignKey [FK_Accounts_Users_Accounts_UserState]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Accounts_Users_Accounts_UserState]') AND type = 'F')
ALTER TABLE [Accounts_Users]  WITH CHECK ADD  CONSTRAINT [FK_Accounts_Users_Accounts_UserState] FOREIGN KEY([UserState])
REFERENCES [Accounts_UserState] ([UserStateID])
GO
ALTER TABLE [Accounts_Users] CHECK CONSTRAINT [FK_Accounts_Users_Accounts_UserState]
GO
/****** 对象:  ForeignKey [FK_Kb_ArticleAttachments_Kb_Articles]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Kb_ArticleAttachments_Kb_Articles]') AND type = 'F')
ALTER TABLE [Kb_ArticleAttachments]  WITH CHECK ADD  CONSTRAINT [FK_Kb_ArticleAttachments_Kb_Articles] FOREIGN KEY([ArticleID])
REFERENCES [Kb_Articles] ([ArticleID])
ON DELETE CASCADE
GO
ALTER TABLE [Kb_ArticleAttachments] CHECK CONSTRAINT [FK_Kb_ArticleAttachments_Kb_Articles]
GO
/****** 对象:  ForeignKey [FK_Kb_ArticleComment_Kb_Articles]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Kb_ArticleComment_Kb_Articles]') AND type = 'F')
ALTER TABLE [Kb_ArticleComment]  WITH CHECK ADD  CONSTRAINT [FK_Kb_ArticleComment_Kb_Articles] FOREIGN KEY([ArticleID])
REFERENCES [Kb_Articles] ([ArticleID])
GO
ALTER TABLE [Kb_ArticleComment] CHECK CONSTRAINT [FK_Kb_ArticleComment_Kb_Articles]
GO
/****** 对象:  ForeignKey [FK_Pts_DbDirectory_Pts_Projects]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_DbDirectory_Pts_Projects]') AND type = 'F')
ALTER TABLE [Pts_DbDirectory]  WITH CHECK ADD  CONSTRAINT [FK_Pts_DbDirectory_Pts_Projects] FOREIGN KEY([ProjectID])
REFERENCES [Pts_Projects] ([ProjectID])
GO
ALTER TABLE [Pts_DbDirectory] CHECK CONSTRAINT [FK_Pts_DbDirectory_Pts_Projects]
GO
/****** 对象:  ForeignKey [FK_Pts_FilterNodes_Pts_Filters]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_FilterNodes_Pts_Filters]') AND type = 'F')
ALTER TABLE [Pts_FilterNodes]  WITH CHECK ADD  CONSTRAINT [FK_Pts_FilterNodes_Pts_Filters] FOREIGN KEY([FilterID])
REFERENCES [Pts_Filters] ([FilterID])
GO
ALTER TABLE [Pts_FilterNodes] CHECK CONSTRAINT [FK_Pts_FilterNodes_Pts_Filters]
GO
/****** 对象:  ForeignKey [FK_Pts_Filters_Pts_Projects]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_Filters_Pts_Projects]') AND type = 'F')
ALTER TABLE [Pts_Filters]  WITH CHECK ADD  CONSTRAINT [FK_Pts_Filters_Pts_Projects] FOREIGN KEY([ProjectID])
REFERENCES [Pts_Projects] ([ProjectID])
GO
ALTER TABLE [Pts_Filters] CHECK CONSTRAINT [FK_Pts_Filters_Pts_Projects]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemAttachments_Pts_Problems]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemAttachments_Pts_Problems]') AND type = 'F')
ALTER TABLE [Pts_ProblemAttachments]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_ProblemAttachments_Pts_Problems] FOREIGN KEY([ProblemID])
REFERENCES [Pts_Problems] ([ProblemID])
GO
ALTER TABLE [Pts_ProblemAttachments] CHECK CONSTRAINT [FK_Pts_ProblemAttachments_Pts_Problems]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemCatalogs_Pts_Projects]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemCatalogs_Pts_Projects]') AND type = 'F')
ALTER TABLE [Pts_ProblemCatalogs]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_ProblemCatalogs_Pts_Projects] FOREIGN KEY([ProjectID])
REFERENCES [Pts_Projects] ([ProjectID])
GO
ALTER TABLE [Pts_ProblemCatalogs] CHECK CONSTRAINT [FK_Pts_ProblemCatalogs_Pts_Projects]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemCreateRoles_Pts_ProblemState]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemCreateRoles_Pts_ProblemState]') AND type = 'F')
ALTER TABLE [Pts_ProblemCreateRoles]  WITH CHECK ADD  CONSTRAINT [FK_Pts_ProblemCreateRoles_Pts_ProblemState] FOREIGN KEY([DstStateID])
REFERENCES [Pts_ProblemState] ([ProblemStateID])
GO
ALTER TABLE [Pts_ProblemCreateRoles] CHECK CONSTRAINT [FK_Pts_ProblemCreateRoles_Pts_ProblemState]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemCreateRoles_Pts_Projects]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemCreateRoles_Pts_Projects]') AND type = 'F')
ALTER TABLE [Pts_ProblemCreateRoles]  WITH CHECK ADD  CONSTRAINT [FK_Pts_ProblemCreateRoles_Pts_Projects] FOREIGN KEY([ProjectID])
REFERENCES [Pts_Projects] ([ProjectID])
GO
ALTER TABLE [Pts_ProblemCreateRoles] CHECK CONSTRAINT [FK_Pts_ProblemCreateRoles_Pts_Projects]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemFieldEditableState_Pts_DbDirectory]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemFieldEditableState_Pts_DbDirectory]') AND type = 'F')
ALTER TABLE [Pts_ProblemFieldEditableState]  WITH CHECK ADD  CONSTRAINT [FK_Pts_ProblemFieldEditableState_Pts_DbDirectory] FOREIGN KEY([FieldID])
REFERENCES [Pts_DbDirectory] ([ID])
GO
ALTER TABLE [Pts_ProblemFieldEditableState] CHECK CONSTRAINT [FK_Pts_ProblemFieldEditableState_Pts_DbDirectory]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemFieldEditableGroup_Pts_DbDirectory]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemFieldEditableGroup_Pts_DbDirectory]') AND type = 'F')
ALTER TABLE [Pts_ProblemFieldEditableWorkgroup]  WITH CHECK ADD  CONSTRAINT [FK_Pts_ProblemFieldEditableGroup_Pts_DbDirectory] FOREIGN KEY([FieldID])
REFERENCES [Pts_DbDirectory] ([ID])
ON DELETE CASCADE
GO
ALTER TABLE [Pts_ProblemFieldEditableWorkgroup] CHECK CONSTRAINT [FK_Pts_ProblemFieldEditableGroup_Pts_DbDirectory]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemFieldEditableGroup_Pts_Workgroups]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemFieldEditableGroup_Pts_Workgroups]') AND type = 'F')
ALTER TABLE [Pts_ProblemFieldEditableWorkgroup]  WITH CHECK ADD  CONSTRAINT [FK_Pts_ProblemFieldEditableGroup_Pts_Workgroups] FOREIGN KEY([WorkgroupID])
REFERENCES [Pts_Workgroups] ([WorkgroupID])
ON DELETE CASCADE
GO
ALTER TABLE [Pts_ProblemFieldEditableWorkgroup] CHECK CONSTRAINT [FK_Pts_ProblemFieldEditableGroup_Pts_Workgroups]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemFieldReadableGroup_Pts_DbDirectory]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemFieldReadableGroup_Pts_DbDirectory]') AND type = 'F')
ALTER TABLE [Pts_ProblemFieldReadableWorkgroup]  WITH CHECK ADD  CONSTRAINT [FK_Pts_ProblemFieldReadableGroup_Pts_DbDirectory] FOREIGN KEY([FieldID])
REFERENCES [Pts_DbDirectory] ([ID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [Pts_ProblemFieldReadableWorkgroup] CHECK CONSTRAINT [FK_Pts_ProblemFieldReadableGroup_Pts_DbDirectory]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemFieldReadableGroup_Pts_Workgroups]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemFieldReadableGroup_Pts_Workgroups]') AND type = 'F')
ALTER TABLE [Pts_ProblemFieldReadableWorkgroup]  WITH CHECK ADD  CONSTRAINT [FK_Pts_ProblemFieldReadableGroup_Pts_Workgroups] FOREIGN KEY([WorkgroupID])
REFERENCES [Pts_Workgroups] ([WorkgroupID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [Pts_ProblemFieldReadableWorkgroup] CHECK CONSTRAINT [FK_Pts_ProblemFieldReadableGroup_Pts_Workgroups]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemHistory_Pts_Problems]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemHistory_Pts_Problems]') AND type = 'F')
ALTER TABLE [Pts_ProblemHistory]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_ProblemHistory_Pts_Problems] FOREIGN KEY([ProblemID])
REFERENCES [Pts_Problems] ([ProblemID])
ON DELETE CASCADE
GO
ALTER TABLE [Pts_ProblemHistory] CHECK CONSTRAINT [FK_Pts_ProblemHistory_Pts_Problems]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemInitStateWorkgroup_Pts_ProblemState]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemInitStateWorkgroup_Pts_ProblemState]') AND type = 'F')
ALTER TABLE [Pts_ProblemInitStateWorkgroup]  WITH CHECK ADD  CONSTRAINT [FK_Pts_ProblemInitStateWorkgroup_Pts_ProblemState] FOREIGN KEY([ProblemStateID])
REFERENCES [Pts_ProblemState] ([ProblemStateID])
ON DELETE CASCADE
GO
ALTER TABLE [Pts_ProblemInitStateWorkgroup] CHECK CONSTRAINT [FK_Pts_ProblemInitStateWorkgroup_Pts_ProblemState]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemInitStateWorkgroup_Pts_Workgroups]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemInitStateWorkgroup_Pts_Workgroups]') AND type = 'F')
ALTER TABLE [Pts_ProblemInitStateWorkgroup]  WITH CHECK ADD  CONSTRAINT [FK_Pts_ProblemInitStateWorkgroup_Pts_Workgroups] FOREIGN KEY([WorkgroupID])
REFERENCES [Pts_Workgroups] ([WorkgroupID])
ON DELETE CASCADE
GO
ALTER TABLE [Pts_ProblemInitStateWorkgroup] CHECK CONSTRAINT [FK_Pts_ProblemInitStateWorkgroup_Pts_Workgroups]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemPriority_Pts_Projects]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemPriority_Pts_Projects]') AND type = 'F')
ALTER TABLE [Pts_ProblemPriority]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_ProblemPriority_Pts_Projects] FOREIGN KEY([ProjectID])
REFERENCES [Pts_Projects] ([ProjectID])
GO
ALTER TABLE [Pts_ProblemPriority] CHECK CONSTRAINT [FK_Pts_ProblemPriority_Pts_Projects]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemRelations_Pts_Problems]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemRelations_Pts_Problems]') AND type = 'F')
ALTER TABLE [Pts_ProblemRelations]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_ProblemRelations_Pts_Problems] FOREIGN KEY([ProblemID_1])
REFERENCES [Pts_Problems] ([ProblemID])
GO
ALTER TABLE [Pts_ProblemRelations] CHECK CONSTRAINT [FK_Pts_ProblemRelations_Pts_Problems]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemRelations_Pts_Problems1]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemRelations_Pts_Problems1]') AND type = 'F')
ALTER TABLE [Pts_ProblemRelations]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_ProblemRelations_Pts_Problems1] FOREIGN KEY([ProblemID_2])
REFERENCES [Pts_Problems] ([ProblemID])
ON DELETE CASCADE
GO
ALTER TABLE [Pts_ProblemRelations] CHECK CONSTRAINT [FK_Pts_ProblemRelations_Pts_Problems1]
GO
/****** 对象:  ForeignKey [FK_Pts_Problems_Accounts_Users]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_Problems_Accounts_Users]') AND type = 'F')
ALTER TABLE [Pts_Problems]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_Problems_Accounts_Users] FOREIGN KEY([CreateUserID])
REFERENCES [Accounts_Users] ([UserID])
GO
ALTER TABLE [Pts_Problems] CHECK CONSTRAINT [FK_Pts_Problems_Accounts_Users]
GO
/****** 对象:  ForeignKey [FK_Pts_Problems_Pts_ProblemPriority]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_Problems_Pts_ProblemPriority]') AND type = 'F')
ALTER TABLE [Pts_Problems]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_Problems_Pts_ProblemPriority] FOREIGN KEY([ProblemPriorityID])
REFERENCES [Pts_ProblemPriority] ([ProblemPriorityID])
GO
ALTER TABLE [Pts_Problems] CHECK CONSTRAINT [FK_Pts_Problems_Pts_ProblemPriority]
GO
/****** 对象:  ForeignKey [FK_Pts_Problems_Pts_ProblemSeverities]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_Problems_Pts_ProblemSeverities]') AND type = 'F')
ALTER TABLE [Pts_Problems]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_Problems_Pts_ProblemSeverities] FOREIGN KEY([ProblemSeverityID])
REFERENCES [Pts_ProblemSeverity] ([ProblemSeverityID])
GO
ALTER TABLE [Pts_Problems] CHECK CONSTRAINT [FK_Pts_Problems_Pts_ProblemSeverities]
GO
/****** 对象:  ForeignKey [FK_Pts_Problems_Pts_ProblemState]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_Problems_Pts_ProblemState]') AND type = 'F')
ALTER TABLE [Pts_Problems]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_Problems_Pts_ProblemState] FOREIGN KEY([ProblemStateID])
REFERENCES [Pts_ProblemState] ([ProblemStateID])
GO
ALTER TABLE [Pts_Problems] CHECK CONSTRAINT [FK_Pts_Problems_Pts_ProblemState]
GO
/****** 对象:  ForeignKey [FK_Pts_Problems_Pts_ProblemType]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_Problems_Pts_ProblemType]') AND type = 'F')
ALTER TABLE [Pts_Problems]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_Problems_Pts_ProblemType] FOREIGN KEY([ProblemTypeID])
REFERENCES [Pts_ProblemType] ([ProblemTypeID])
GO
ALTER TABLE [Pts_Problems] CHECK CONSTRAINT [FK_Pts_Problems_Pts_ProblemType]
GO
/****** 对象:  ForeignKey [FK_Pts_Problems_Pts_Projects]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_Problems_Pts_Projects]') AND type = 'F')
ALTER TABLE [Pts_Problems]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_Problems_Pts_Projects] FOREIGN KEY([ProjectID])
REFERENCES [Pts_Projects] ([ProjectID])
GO
ALTER TABLE [Pts_Problems] CHECK CONSTRAINT [FK_Pts_Problems_Pts_Projects]
GO
/****** 对象:  ForeignKey [FK_Pts_Problems_Pts_Projects1]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_Problems_Pts_Projects1]') AND type = 'F')
ALTER TABLE [Pts_Problems]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_Problems_Pts_Projects1] FOREIGN KEY([OriginProjectID])
REFERENCES [Pts_Projects] ([ProjectID])
GO
ALTER TABLE [Pts_Problems] CHECK CONSTRAINT [FK_Pts_Problems_Pts_Projects1]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemSeverities_Pts_Projects]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemSeverities_Pts_Projects]') AND type = 'F')
ALTER TABLE [Pts_ProblemSeverity]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_ProblemSeverities_Pts_Projects] FOREIGN KEY([ProjectID])
REFERENCES [Pts_Projects] ([ProjectID])
GO
ALTER TABLE [Pts_ProblemSeverity] CHECK CONSTRAINT [FK_Pts_ProblemSeverities_Pts_Projects]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemState_Pts_Projects]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemState_Pts_Projects]') AND type = 'F')
ALTER TABLE [Pts_ProblemState]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_ProblemState_Pts_Projects] FOREIGN KEY([ProjectID])
REFERENCES [Pts_Projects] ([ProjectID])
GO
ALTER TABLE [Pts_ProblemState] CHECK CONSTRAINT [FK_Pts_ProblemState_Pts_Projects]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemStateHistory_Pts_Problems]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemStateHistory_Pts_Problems]') AND type = 'F')
ALTER TABLE [Pts_ProblemStateRecord]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_ProblemStateHistory_Pts_Problems] FOREIGN KEY([ProblemID])
REFERENCES [Pts_Problems] ([ProblemID])
GO
ALTER TABLE [Pts_ProblemStateRecord] CHECK CONSTRAINT [FK_Pts_ProblemStateHistory_Pts_Problems]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemStateHistory_Pts_ProblemState]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemStateHistory_Pts_ProblemState]') AND type = 'F')
ALTER TABLE [Pts_ProblemStateRecord]  WITH CHECK ADD  CONSTRAINT [FK_Pts_ProblemStateHistory_Pts_ProblemState] FOREIGN KEY([StateID])
REFERENCES [Pts_ProblemState] ([ProblemStateID])
GO
ALTER TABLE [Pts_ProblemStateRecord] CHECK CONSTRAINT [FK_Pts_ProblemStateHistory_Pts_ProblemState]
GO
/****** 对象:  ForeignKey [FK_Pts_StateTimeLimit_Pts_ProblemPriority]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_StateTimeLimit_Pts_ProblemPriority]') AND type = 'F')
ALTER TABLE [Pts_ProblemStateTimeLimit]  WITH CHECK ADD  CONSTRAINT [FK_Pts_StateTimeLimit_Pts_ProblemPriority] FOREIGN KEY([PriorityID])
REFERENCES [Pts_ProblemPriority] ([ProblemPriorityID])
GO
ALTER TABLE [Pts_ProblemStateTimeLimit] CHECK CONSTRAINT [FK_Pts_StateTimeLimit_Pts_ProblemPriority]
GO
/****** 对象:  ForeignKey [FK_Pts_StateTimeLimit_Pts_ProblemState]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_StateTimeLimit_Pts_ProblemState]') AND type = 'F')
ALTER TABLE [Pts_ProblemStateTimeLimit]  WITH CHECK ADD  CONSTRAINT [FK_Pts_StateTimeLimit_Pts_ProblemState] FOREIGN KEY([StateID])
REFERENCES [Pts_ProblemState] ([ProblemStateID])
GO
ALTER TABLE [Pts_ProblemStateTimeLimit] CHECK CONSTRAINT [FK_Pts_StateTimeLimit_Pts_ProblemState]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemStateTransfer_Pts_ProblemState]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemStateTransfer_Pts_ProblemState]') AND type = 'F')
ALTER TABLE [Pts_ProblemStateTransfer]  WITH CHECK ADD  CONSTRAINT [FK_Pts_ProblemStateTransfer_Pts_ProblemState] FOREIGN KEY([FromProblemStateID])
REFERENCES [Pts_ProblemState] ([ProblemStateID])
GO
ALTER TABLE [Pts_ProblemStateTransfer] CHECK CONSTRAINT [FK_Pts_ProblemStateTransfer_Pts_ProblemState]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemStateTransfer_Pts_ProblemState1]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemStateTransfer_Pts_ProblemState1]') AND type = 'F')
ALTER TABLE [Pts_ProblemStateTransfer]  WITH CHECK ADD  CONSTRAINT [FK_Pts_ProblemStateTransfer_Pts_ProblemState1] FOREIGN KEY([ToProblemStateID])
REFERENCES [Pts_ProblemState] ([ProblemStateID])
GO
ALTER TABLE [Pts_ProblemStateTransfer] CHECK CONSTRAINT [FK_Pts_ProblemStateTransfer_Pts_ProblemState1]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemStateTransferWorkgroup_Pts_ProblemStateTransfer]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemStateTransferWorkgroup_Pts_ProblemStateTransfer]') AND type = 'F')
ALTER TABLE [Pts_ProblemStateTransferWorkgroup]  WITH CHECK ADD  CONSTRAINT [FK_Pts_ProblemStateTransferWorkgroup_Pts_ProblemStateTransfer] FOREIGN KEY([TransferID])
REFERENCES [Pts_ProblemStateTransfer] ([TransferID])
GO
ALTER TABLE [Pts_ProblemStateTransferWorkgroup] CHECK CONSTRAINT [FK_Pts_ProblemStateTransferWorkgroup_Pts_ProblemStateTransfer]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemStateTransferWorkgroup_Pts_Workgroups]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemStateTransferWorkgroup_Pts_Workgroups]') AND type = 'F')
ALTER TABLE [Pts_ProblemStateTransferWorkgroup]  WITH CHECK ADD  CONSTRAINT [FK_Pts_ProblemStateTransferWorkgroup_Pts_Workgroups] FOREIGN KEY([WorkgroupID])
REFERENCES [Pts_Workgroups] ([WorkgroupID])
GO
ALTER TABLE [Pts_ProblemStateTransferWorkgroup] CHECK CONSTRAINT [FK_Pts_ProblemStateTransferWorkgroup_Pts_Workgroups]
GO
/****** 对象:  ForeignKey [FK_Pts_ProblemType_Pts_Projects]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_ProblemType_Pts_Projects]') AND type = 'F')
ALTER TABLE [Pts_ProblemType]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_ProblemType_Pts_Projects] FOREIGN KEY([ProjectID])
REFERENCES [Pts_Projects] ([ProjectID])
GO
ALTER TABLE [Pts_ProblemType] CHECK CONSTRAINT [FK_Pts_ProblemType_Pts_Projects]
GO
/****** 对象:  ForeignKey [FK_Pts_Projects_Pts_ProjectCatalogs]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_Projects_Pts_ProjectCatalogs]') AND type = 'F')
ALTER TABLE [Pts_Projects]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_Projects_Pts_ProjectCatalogs] FOREIGN KEY([ProjectCatalogID])
REFERENCES [Pts_ProjectCatalogs] ([ProjectCatalogID])
GO
ALTER TABLE [Pts_Projects] CHECK CONSTRAINT [FK_Pts_Projects_Pts_ProjectCatalogs]
GO
/****** 对象:  ForeignKey [FK_Pts_Projects_Pts_ProjectStates]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_Projects_Pts_ProjectStates]') AND type = 'F')
ALTER TABLE [Pts_Projects]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_Projects_Pts_ProjectStates] FOREIGN KEY([ProjectStateID])
REFERENCES [Pts_ProjectStates] ([ProjectStateID])
GO
ALTER TABLE [Pts_Projects] CHECK CONSTRAINT [FK_Pts_Projects_Pts_ProjectStates]
GO
/****** 对象:  ForeignKey [FK_Pts_RecordAttachments_Pts_Records]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_RecordAttachments_Pts_Records]') AND type = 'F')
ALTER TABLE [Pts_RecordAttachments]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_RecordAttachments_Pts_Records] FOREIGN KEY([RecordID])
REFERENCES [Pts_Records] ([RecordID])
GO
ALTER TABLE [Pts_RecordAttachments] CHECK CONSTRAINT [FK_Pts_RecordAttachments_Pts_Records]
GO
/****** 对象:  ForeignKey [FK_Pts_Records_Pts_Problems]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_Records_Pts_Problems]') AND type = 'F')
ALTER TABLE [Pts_Records]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_Records_Pts_Problems] FOREIGN KEY([ProblemID])
REFERENCES [Pts_Problems] ([ProblemID])
GO
ALTER TABLE [Pts_Records] CHECK CONSTRAINT [FK_Pts_Records_Pts_Problems]
GO
/****** 对象:  ForeignKey [FK_Pts_RecordType_Pts_Projects]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_RecordType_Pts_Projects]') AND type = 'F')
ALTER TABLE [Pts_RecordType]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_RecordType_Pts_Projects] FOREIGN KEY([ProjectID])
REFERENCES [Pts_Projects] ([ProjectID])
GO
ALTER TABLE [Pts_RecordType] CHECK CONSTRAINT [FK_Pts_RecordType_Pts_Projects]
GO
/****** 对象:  ForeignKey [FK_Pts_SharedItemAttachments_Pts_SharedItems]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_SharedItemAttachments_Pts_SharedItems]') AND type = 'F')
ALTER TABLE [Pts_SharedItemAttachments]  WITH CHECK ADD  CONSTRAINT [FK_Pts_SharedItemAttachments_Pts_SharedItems] FOREIGN KEY([ItemID])
REFERENCES [Pts_SharedItems] ([ItemID])
GO
ALTER TABLE [Pts_SharedItemAttachments] CHECK CONSTRAINT [FK_Pts_SharedItemAttachments_Pts_SharedItems]
GO
/****** 对象:  ForeignKey [FK_Pts_SharedItems_Pts_Projects]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_SharedItems_Pts_Projects]') AND type = 'F')
ALTER TABLE [Pts_SharedItems]  WITH CHECK ADD  CONSTRAINT [FK_Pts_SharedItems_Pts_Projects] FOREIGN KEY([ProjectID])
REFERENCES [Pts_Projects] ([ProjectID])
ON DELETE CASCADE
GO
ALTER TABLE [Pts_SharedItems] CHECK CONSTRAINT [FK_Pts_SharedItems_Pts_Projects]
GO
/****** 对象:  ForeignKey [FK_Pts_WorkflowSteps_Pts_ProblemState]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_WorkflowSteps_Pts_ProblemState]') AND type = 'F')
ALTER TABLE [Pts_WorkflowSteps]  WITH CHECK ADD  CONSTRAINT [FK_Pts_WorkflowSteps_Pts_ProblemState] FOREIGN KEY([SrcStateID])
REFERENCES [Pts_ProblemState] ([ProblemStateID])
GO
ALTER TABLE [Pts_WorkflowSteps] CHECK CONSTRAINT [FK_Pts_WorkflowSteps_Pts_ProblemState]
GO
/****** 对象:  ForeignKey [FK_Pts_WorkflowSteps_Pts_ProblemState1]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_WorkflowSteps_Pts_ProblemState1]') AND type = 'F')
ALTER TABLE [Pts_WorkflowSteps]  WITH CHECK ADD  CONSTRAINT [FK_Pts_WorkflowSteps_Pts_ProblemState1] FOREIGN KEY([DstStateID])
REFERENCES [Pts_ProblemState] ([ProblemStateID])
GO
ALTER TABLE [Pts_WorkflowSteps] CHECK CONSTRAINT [FK_Pts_WorkflowSteps_Pts_ProblemState1]
GO
/****** 对象:  ForeignKey [FK_Pts_WorkflowSteps_Pts_Projects]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_WorkflowSteps_Pts_Projects]') AND type = 'F')
ALTER TABLE [Pts_WorkflowSteps]  WITH CHECK ADD  CONSTRAINT [FK_Pts_WorkflowSteps_Pts_Projects] FOREIGN KEY([ProjectID])
REFERENCES [Pts_Projects] ([ProjectID])
GO
ALTER TABLE [Pts_WorkflowSteps] CHECK CONSTRAINT [FK_Pts_WorkflowSteps_Pts_Projects]
GO
/****** 对象:  ForeignKey [FK_Pts_WorkgroupUsers_Accounts_Users]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_WorkgroupUsers_Accounts_Users]') AND type = 'F')
ALTER TABLE [Pts_WorkgroupUsers]  WITH NOCHECK ADD  CONSTRAINT [FK_Pts_WorkgroupUsers_Accounts_Users] FOREIGN KEY([UserID])
REFERENCES [Accounts_Users] ([UserID])
GO
ALTER TABLE [Pts_WorkgroupUsers] CHECK CONSTRAINT [FK_Pts_WorkgroupUsers_Accounts_Users]
GO
/****** 对象:  ForeignKey [FK_Pts_WorkgroupUsers_Pts_Workgroups]    脚本日期: 03/18/2009 13:25:20 ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FK_Pts_WorkgroupUsers_Pts_Workgroups]') AND type = 'F')
ALTER TABLE [Pts_WorkgroupUsers]  WITH CHECK ADD  CONSTRAINT [FK_Pts_WorkgroupUsers_Pts_Workgroups] FOREIGN KEY([WorkgroupID])
REFERENCES [Pts_Workgroups] ([WorkgroupID])
GO
ALTER TABLE [Pts_WorkgroupUsers] CHECK CONSTRAINT [FK_Pts_WorkgroupUsers_Pts_Workgroups]
GO


-----------------------------------------------------------------------------------------------
/****** 对象:  StoredProcedure [sp_Common_GetTopicList]    脚本日期: 03/31/2009 17:23:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO

ALTER procedure [sp_Common_GetTopicList] 
       @a_FieldList nVarchar(2000),
       @a_TableName nVarchar(300), 
       @a_SelectWhere nVarchar(4000),
       @a_SelectOrderId nVarchar(200),
       @a_SelectOrder nVarchar(500),
       @a_intPageNo int,
       @a_intPageSize int,
       @RecordCount int OUTPUT
as
   /*定义局部变量*/
   declare @intBeginID         int
   declare @intEndID           int
   declare @intRootRecordCount int
   declare @intRowCount        int
   declare @TmpSelect          NVarchar(4000)
   /*关闭计数*/
   set nocount on
   
   /*求总共根贴数*/

   select @TmpSelect = 'set nocount on;select @SPintRootRecordCount = count(*) from '+@a_TableName+' '+@a_SelectWhere
   execute sp_executesql 
             @TmpSelect,
             N'@SPintRootRecordCount int OUTPUT',
             @SPintRootRecordCount=@intRootRecordCount OUTPUT

select @RecordCount = @intRootRecordCount

   if (@intRootRecordCount = 0)    --如果没有贴子，则返回零
       return 0
       
   /*判断页数是否正确*/
   if (@a_intPageNo - 1) * @a_intPageSize > @intRootRecordCount
      return (-1)

   /*求开始rootID*/
   set @intRowCount = (@a_intPageNo - 1) * @a_intPageSize + 1
   /*限制条数*/

   select @TmpSelect = 'set nocount on;set rowcount @SPintRowCount;select @SPintBeginID = '+@a_SelectOrderId+' from '+@a_TableName+' '+@a_SelectWhere+' '+@a_SelectOrder
   execute sp_executesql 
             @TmpSelect,
             N'@SPintRowCount int,@SPintBeginID int OUTPUT',
             @SPintRowCount=@intRowCount,@SPintBeginID=@intBeginID OUTPUT


   /*结束rootID*/
   set @intRowCount = @a_intPageNo * @a_intPageSize
   /*限制条数*/

   select @TmpSelect = 'set nocount on;set rowcount @SPintRowCount;select @SPintEndID = '+@a_SelectOrderId+' from '+@a_TableName+' '+@a_SelectWhere+' '+@a_SelectOrder
   execute sp_executesql 
             @TmpSelect,
             N'@SPintRowCount int,@SPintEndID int OUTPUT',
             @SPintRowCount=@intRowCount,@SPintEndID=@intEndID OUTPUT


if @a_SelectWhere='' or @a_SelectWhere IS NULL
   select @TmpSelect = 'set nocount off;set rowcount 0;select '+@a_FieldList+' from '+@a_TableName+' where '+@a_SelectOrderId+' between '
else
   select @TmpSelect = 'set nocount off;set rowcount 0;select '+@a_FieldList+' from '+@a_TableName+' '+@a_SelectWhere+' and '+@a_SelectOrderId+' between '

if @intEndID > @intBeginID
   select @TmpSelect = @TmpSelect+'@SPintBeginID and @SPintEndID'+' '+@a_SelectOrder
else
   select @TmpSelect = @TmpSelect+'@SPintEndID and @SPintBeginID'+' '+@a_SelectOrder

   execute sp_executesql 
             @TmpSelect,
             N'@SPintEndID int,@SPintBeginID int',
             @SPintEndID=@intEndID,@SPintBeginID=@intBeginID

   return(@@rowcount)
   --select @@rowcount

GO

-----------------------------------------------------------------------------------------------
-- 提交给我的事务更新20090423
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [sp_Pts_Filter_Relate_1]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND 
	IsClosed = 0
AND
	((AssignedTo > 0 AND AssignedTo = @UserID)
	OR
	(AssignedTo < 0 AND -AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID))
	OR
	ProblemID IN (SELECT ProblemID FROM Pts_Coworkers WHERE UserID=@UserID AND IsWorked=0)
	)

GO

-----------------------------------------------------------------------------------------------
-- 计算提交给我的事务数量
/****** Object:  StoredProcedure [sp_Pts_GetIssueCounts_Mine]    Script Date: 04/28/2009 23:38:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [sp_Pts_GetIssueCounts_Mine]
@ProjectID int,
@UserID int
AS

--声明表变量
Declare @t table
(
	count_type  nvarchar(30),
	count_value int
)


--提交给我的事务
declare @assignedToMe int
declare @notReaded int

SELECT @assignedToMe=sum(1),@notReaded=sum(case when ReadByAssignee=1 then 0 else 1 end) FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND 
	IsClosed = 0
AND
	((AssignedTo > 0 AND AssignedTo = @UserID)
	OR
	(AssignedTo < 0 AND -AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID))
	OR
	ProblemID IN (SELECT ProblemID FROM Pts_Coworkers WHERE UserID=@UserID AND IsWorked=0)
	)
insert into @t (count_type,count_value) values ('assigned_to_me', @assignedToMe)
insert into @t (count_type,count_value) values ('assigned_to_me_not_read', @notReaded)

--我提交的事务
declare @createdByMe int
declare @createdByMeClosed int
declare @createdByMeOpen int

select 
	@createdByMe=count(ProblemID),
	@createdByMeClosed=sum(case when IsClosed=1 then 1 else 0 end), 
	@createdByMeOpen=sum(case when IsClosed=1 then 0 else 1 end)
from v_Pts_Problems
WHERE
	ProjectID = @ProjectID AND CreateUserID=@UserID
insert into @t (count_type,count_value) values ('created_by_me', @createdByMe)
insert into @t (count_type,count_value) values ('created_by_me_closed', @createdByMeClosed)
insert into @t (count_type,count_value) values ('created_by_me_open', @createdByMeOpen)

--参与过的事务
declare @processedByMe int
declare @processedByMeOpen int
declare @processedByMeClosed int

SELECT 
	@processedByMe=count(ProblemID),
	@processedByMeClosed=sum(case when IsClosed=1 then 1 else 0 end), 
	@processedByMeOpen=sum(case when IsClosed=1 then 0 else 1 end)
FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	ProblemID in
	(SELECT Distinct ProblemID From Pts_Records Where ProblemID in (select ProblemID FROM Pts_Problems WHERE ProjectID=@ProjectID) AND CreateUser = @UserID)
insert into @t (count_type,count_value) values ('processed_by_me', @processedByMe)
insert into @t (count_type,count_value) values ('processed_by_me_closed', @processedByMeClosed)
insert into @t (count_type,count_value) values ('processed_by_me_open', @processedByMeOpen)

--订阅的事务
declare @subscribedByMe int
declare @subscribedByMeClosed int
declare @subscribedByMeOpen int

SELECT 
	@subscribedByMe=count(ProblemID),
	@subscribedByMeClosed=sum(case when IsClosed=1 then 1 else 0 end), 
	@subscribedByMeOpen=sum(case when IsClosed=1 then 0 else 1 end)
FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	ProblemID in
	(Select TargetID FROM Pts_Subscription WHERE UserID=@UserID AND TargetType=2)
insert into @t (count_type,count_value) values ('subscribed_by_me', @subscribedByMe)
insert into @t (count_type,count_value) values ('subscribed_by_me_closed', @subscribedByMeClosed)
insert into @t (count_type,count_value) values ('subscribed_by_me_open', @subscribedByMeOpen)



--相关的事务
declare @relatedToMe int
declare @relatedToMeClosed int
declare @relatedToMeOpen int

SELECT 
	@relatedToMe=count(ProblemID),
	@relatedToMeClosed=sum(case when IsClosed=1 then 1 else 0 end), 
	@relatedToMeOpen=sum(case when IsClosed=1 then 0 else 1 end)
FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	ProblemID in
	(SELECT ProblemID FROM Pts_RelatedUsers Where UserID=@UserID)

insert into @t (count_type,count_value) values ('related_to_me', @relatedToMe)
insert into @t (count_type,count_value) values ('related_to_me_closed', @relatedToMeClosed)
insert into @t (count_type,count_value) values ('related_to_me_open', @relatedToMeOpen)

--代理我处理的事务
declare @processedByAgent int

select @processedByAgent = count(ProblemID) 
FROM Pts_Problems 
WHERE ProjectID=@ProjectID AND ProblemID in (SELECT DISTINCT ProblemID FROM Pts_Records WHERE RepresentingUserID=@UserID)

insert into @t (count_type, count_value) values ('processed_by_agent', @processedByAgent)

--输出
SELECT * FROM @t


GO

--
-----------------------------------------------------------------------------------------------
/****** Object:  StoredProcedure [sp_Pts_ComputeUserPendingTasks]    Script Date: 04/28/2009 22:19:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [sp_Pts_ComputeUserPendingTasks]
@UserID int
AS
--等待我处理的事务
SELECT ProjectID,COUNT(*) FROM v_Pts_Problems
WHERE
	IsClosed = 0
AND
	((AssignedTo > 0 AND AssignedTo = @UserID)
	OR
	(AssignedTo < 0 AND -AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID)))
GROUP BY ProjectID

GO

-----------------------------------------------------------------------------------------------
----等待某用户处理的问题
ALTER Procedure [sp_Pts_GetUserInProblems]
@UserID int
AS
Select 	* 
From 	v_Pts_Problems Where 
IsClosed=0
AND
(AssignedTo = @UserID
	OR
	-AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID)
	OR 
	ProblemID IN (SELECT ProblemID FROM Pts_Coworkers WHERE UserID=@UserID AND IsWorked=0)
	)


GO


-----------------------------------------------------------------------------------------------
ALTER Procedure [sp_Pts_Filter_Relate_1]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND 
	IsClosed = 0
AND
	((AssignedTo > 0 AND AssignedTo = @UserID)
	OR
	(AssignedTo < 0 AND -AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID))
	OR
	ProblemID IN (SELECT ProblemID FROM Pts_Coworkers WHERE UserID=@UserID AND IsWorked=0)
	)

GO

-----------------------------------------------------------------------------------------------
ALTER PROCEDURE [sp_Pts_Filter_Relate_AssignedToMeNotRead]
@ProjectID int,
@UserID int
AS
--等待我处理的事务,未读
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND 
	IsClosed = 0
AND
	ReadByAssignee=0
AND
	((AssignedTo > 0 AND AssignedTo = @UserID)
	OR
	(AssignedTo < 0 AND -AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID))
	OR
	ProblemID IN (SELECT ProblemID FROM Pts_Coworkers WHERE UserID=@UserID AND IsWorked=0)
	)

GO

-----------------------------------------------------------------------------------------------

ALTER Procedure [sp_Pts_Filter_RecentAssignedToMe]
@ProjectID int,
@UserID int,
@Count int
AS
--等待我处理的事务
SELECT top 500 ProblemID, ProblemCode, Title, ProblemStateName, AssignedTo, LastTime,ReadByAssignee
FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND 
	IsClosed = 0
AND
	((AssignedTo > 0 AND AssignedTo = @UserID)
	OR
	(AssignedTo < 0 AND -AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID))
	OR
	ProblemID IN (SELECT ProblemID FROM Pts_Coworkers WHERE UserID=@UserID AND IsWorked=0)
	)
Order BY LastTime DESC,CreateTime DESC

GO

------------------------------------------------------------------------------------------------
ALTER PROCEDURE [sp_Pts_GetUserViewableFields]
@ProjectID int,
@UserID int,
@ProblemID int
AS
BEGIN
--字段对所有人可见，
-- 或者字段对创建人可见，而且用户是事务的创建人;或者用户在所允许的工作组中
SELECT * FROM Pts_DbDirectory
WHERE	ProjectID = @ProjectID
	AND
		(IsSystem = 0 or (IsSystem = 1 AND InUse = 1))
	AND
	(
		ReadableToAll = 1 
		 OR 
		(
			(ReadableToCreateUser = 1 AND Exists(SELECT ProblemID FROM Pts_Problems WHERE ProblemID = @ProblemID AND CreateUserID=@UserID))
			OR
			ID IN (SELECT FieldID FROM Pts_ProblemFieldReadableWorkgroup WHERE 
				WorkgroupID IN (SELECT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID = @UserID)
					)
		)
	)
	
		
ORDER BY 
	ListOrder ASC


END

GO