IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetFilterNodes]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetFilterNodes]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_AddAccessList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_AddAccessList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetNewProblemIndex]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetNewProblemIndex]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_AddAccessListItem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_AddAccessListItem]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetNextStateList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetNextStateList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_AddPermissionToRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_AddPermissionToRole]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetNextStateListByUser]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetNextStateListByUser]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_AddUserToRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_AddUserToRole]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemAttachmentList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProblemAttachmentList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_ChangeDeptOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_ChangeDeptOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemByCode]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProblemByCode]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_ClearPermissionsFromRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_ClearPermissionsFromRole]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemCtalogList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProblemCtalogList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_CreateDepartment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_CreateDepartment]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemInfo]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProblemInfo]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_CreateRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_CreateRole]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProblemList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_CreateUser]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_CreateUser]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemPriorityList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProblemPriorityList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_DeleteAccessList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_DeleteAccessList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemSeverityList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProblemSeverityList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_DeleteAccessListItem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_DeleteAccessListItem]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemStateList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProblemStateList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_DeleteDepartment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_DeleteDepartment]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemTypeList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProblemTypeList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_DeleteRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_DeleteRole]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProjectCatalog]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_DeleteUser]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_DeleteUser]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Common_AddEvent]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Common_AddEvent]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectCatalogs]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProjectCatalogs]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetAccessListItems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_GetAccessListItems]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectInfo]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProjectInfo]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetAccessLists]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_GetAccessLists]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProjectList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetAllRoles]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_GetAllRoles]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectSeverity]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProjectSeverity]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetDepartmentList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_GetDepartmentList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProjectState]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetEffectivePermissionList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_GetEffectivePermissionList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectStateTransferList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProjectStateTransferList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetPermissionCategories]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_GetPermissionCategories]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectStates]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProjectStates]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetPermissionCategoryDetails]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_GetPermissionCategoryDetails]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProjectType]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetPermissionList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_GetPermissionList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProjectUsers]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetPermissionsNotInRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_GetPermissionsNotInRole]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetRecord]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetRecord]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetRoleDetails]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_GetRoleDetails]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetRecordAttachmentList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetRecordAttachmentList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetRoleUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_GetRoleUsers]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetRecordList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetRecordList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetUserDetails]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_GetUserDetails]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetRecordTypeList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetRecordTypeList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetUserDetailsByEmail]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_GetUserDetailsByEmail]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSelectUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetSelectUsers]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetUserRoles]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_GetUserRoles]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSelectValues]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetSelectValues]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_GetUsers]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetStateAllowedUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetStateAllowedUsers]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetUsersNotInRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_GetUsersNotInRole]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetStateEditableFields]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetStateEditableFields]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_RemovePermissionFromRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_RemovePermissionFromRole]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_CanUserViewArticle]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_CanUserViewArticle]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetTransferCandidateWrokgroups]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetTransferCandidateWrokgroups]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_RemoveUserFromRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_RemoveUserFromRole]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_ReviewArticle]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_ReviewArticle]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetTransferWorkgroups]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetTransferWorkgroups]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_ResetDepartmentOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_ResetDepartmentOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetArticleAllowedViewer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_GetArticleAllowedViewer]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserAllowedInitStates]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetUserAllowedInitStates]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_TestPassword]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_TestPassword]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetArticleNotAllowedViewer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_GetArticleNotAllowedViewer]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserCanAddProblemProjects]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetUserCanAddProblemProjects]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_UpdateDepartment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_UpdateDepartment]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetArticleNotAllowedViewerByDept]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_GetArticleNotAllowedViewerByDept]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserFields]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetUserFields]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_UpdateRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_UpdateRole]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_AddUserToArticleViewer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_AddUserToArticleViewer]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserInProblems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetUserInProblems]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_UpdateUser]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_UpdateUser]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_RemoveUserFromArticleViewer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_RemoveUserFromArticleViewer]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserLastProcessedProblems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetUserLastProcessedProblems]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_ValidateLogin]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_ValidateLogin]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_SetArticlePublic]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_SetArticlePublic]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserNeedProcessProjects]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetUserNeedProcessProjects]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_ValidateUserIP]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_ValidateUserIP]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_MoveArticle]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_MoveArticle]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserOutProblems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetUserOutProblems]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Common_GetRecordFromPage]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Common_GetRecordFromPage]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserRelateProblems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetUserRelateProblems]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Common_GetTopicList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Common_GetTopicList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserVisitedProblems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetUserVisitedProblems]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_AddArticle]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_AddArticle]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserWorkgroups]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetUserWorkgroups]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_AddArticleAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_AddArticleAttachment]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUsersNotInWorkgroup]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetUsersNotInWorkgroup]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_AddComment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_AddComment]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetWorkgroupInfo]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetWorkgroupInfo]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_AddReadCount]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_AddReadCount]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetWorkgroupList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetWorkgroupList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_CreateCategory]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_CreateCategory]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetWorkgroupNotPermitInitStates]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetWorkgroupNotPermitInitStates]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_DeleteArticle]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_DeleteArticle]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetWorkgroupPermitInitStates]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetWorkgroupPermitInitStates]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetArticleAttachments]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_GetArticleAttachments]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetWorkgroupUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetWorkgroupUsers]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetCategoryList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_GetCategoryList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_IsUserInProject]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_IsUserInProject]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetComments]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_GetComments]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddProblemRelation]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_AddProblemRelation]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_IsUserRelateProblem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_IsUserRelateProblem]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetPathCategories]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_GetPathCategories]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveProblemRelation]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_RemoveProblemRelation]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_LogProblemVisit]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_LogProblemVisit]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_RemoveArticleAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_RemoveArticleAttachment]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_MergeProjectCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_MergeProjectCatalog]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_UpdateArticle]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Kb_UpdateArticle]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetUserAgent]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_GetUserAgent]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_MergeProjectState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_MergeProjectState]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddProblemAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_AddProblemAttachment]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetRepresentedUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Accounts_GetRepresentedUsers]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RecordProblemEnd]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_RecordProblemEnd]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddRecordAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_AddRecordAttachment]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_SaveProblemChange]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_SaveProblemChange]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSharedItemTypes]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetSharedItemTypes]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_SearchProblem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_SearchProblem]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ShiftProblem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ShiftProblem]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Stat_GetAvgStateKeepTime]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Stat_GetAvgStateKeepTime]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Stat_GetGlobalNum]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Stat_GetGlobalNum]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateFilter]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_CreateFilter]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Stat_RunQueryString]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Stat_RunQueryString]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateFilterNode]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_CreateFilterNode]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateFilter]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_UpdateFilter]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateGlobalSelect]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_CreateGlobalSelect]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RelatedUser_Add]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_RelatedUser_Add]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateGlobalSelect]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_UpdateGlobalSelect]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateNewUserField]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_CreateNewUserField]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_UpdateProblemCatalog]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProblemCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_CreateProblemCatalog]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RelatedUsers_Get]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_RelatedUsers_Get]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemOriginInfoHistoryID]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_UpdateProblemOriginInfoHistoryID]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProblemPriority]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_CreateProblemPriority]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemPriority]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_UpdateProblemPriority]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProblemSeverity]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_CreateProblemSeverity]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemSeverity]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_UpdateProblemSeverity]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProblemState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_CreateProblemState]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Subscription_Add]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Subscription_Add]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_UpdateProblemState]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProblemStateTransfer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_CreateProblemStateTransfer]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_IsItemSubscribed]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_IsItemSubscribed]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_UpdateProblemType]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProblemType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_CreateProblemType]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveSubscription]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_RemoveSubscription]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProject]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_UpdateProject]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProject]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_CreateProject]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSubscribeUserSet]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetSubscribeUserSet]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProjectCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_UpdateProjectCatalog]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProjectCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_CreateProjectCatalog]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProjectState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_UpdateProjectState]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProjectState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_CreateProjectState]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateRecord]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_UpdateRecord]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateRecord]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_CreateRecord]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateRecordProperty]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_UpdateRecordProperty]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateRecordType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_CreateRecordType]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateRecordType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_UpdateRecordType]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateWorkgroup]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_CreateWorkgroup]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateUserField]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_UpdateUserField]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteGlobalSelect]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_DeleteGlobalSelect]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Notify_Insert]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Notify_Insert]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateWorkgroup]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_UpdateWorkgroup]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_DeleteProblem]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Notify_Delete]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Notify_Delete]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_WorkgroupAddPermitState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_WorkgroupAddPermitState]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_DeleteProblemCatalog]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_WorkgroupDeletePermitState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_WorkgroupDeletePermitState]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemCatalogAndChild]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_DeleteProblemCatalogAndChild]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_stat_CurrentProjectStat]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_stat_CurrentProjectStat]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemPriority]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_DeleteProblemPriority]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemSeverity]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_DeleteProblemSeverity]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_DeleteProblemState]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemStateTransfer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_DeleteProblemStateTransfer]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_DeleteProblemType]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetFieldReadableWorkgroups]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetFieldReadableWorkgroups]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProject]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_DeleteProject]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetFieldEditableWorkgroups]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetFieldEditableWorkgroups]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProjectCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_DeleteProjectCatalog]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProjectState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_DeleteProjectState]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetFieldEditableStates]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetFieldEditableStates]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteRecord]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_DeleteRecord]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteRecordType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_DeleteRecordType]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteSelectValue]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_DeleteSelectValue]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteWorkgroup]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_DeleteWorkgroup]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_LastWeekNew]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_LastWeekNew]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_1]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Relate_1]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetEditableFieldsWhenCreateIssue]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetEditableFieldsWhenCreateIssue]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_2]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Relate_2]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetEditableFieldsWhenEditIssue]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetEditableFieldsWhenEditIssue]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_3]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Relate_3]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetEditableFieldsWhenProcessIssue]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetEditableFieldsWhenProcessIssue]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_4]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Relate_4]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_5]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Relate_5]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserViewableFields]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetUserViewableFields]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_6]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Relate_6]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[GetAssigneeName]') AND xtype in (N'FN', N'IF', N'TF'))
DROP FUNCTION [GetAssigneeName]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_7]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Relate_7]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_8]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Relate_8]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_State_1]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_State_1]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_a1]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Time_a1]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_State_2]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_State_2]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_a2]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Time_a2]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_State_3]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_State_3]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_ThisMonthNew]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_ThisMonthNew]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_a4]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Time_a4]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_ThisWeekNew]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_ThisWeekNew]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_a3]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Time_a3]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddProblemCreateRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_AddProblemCreateRole]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_1]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Time_1]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Accounts_Users]') AND OBJECTPROPERTY(id, N'IsView') = 1)
DROP VIEW [v_Accounts_Users]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_ProblemCreateRoles]') AND OBJECTPROPERTY(id, N'IsView') = 1)
DROP VIEW [v_Pts_ProblemCreateRoles]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_10]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Time_10]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Kb_ArticleList]') AND OBJECTPROPERTY(id, N'IsView') = 1)
DROP VIEW [v_Kb_ArticleList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemCreateRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_UpdateProblemCreateRole]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_2]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Time_2]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddSelectValue]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_AddSelectValue]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveFilter]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_RemoveFilter]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddUserToWorkgroup]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_AddUserToWorkgroup]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveProblemAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_RemoveProblemAttachment]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddWorkgroupToTransfer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_AddWorkgroupToTransfer]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveRecordAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_RemoveRecordAttachment]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProblemCatalogOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ChangeProblemCatalogOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveUserFromWorkgroup]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_RemoveUserFromWorkgroup]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProblemPriorityOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ChangeProblemPriorityOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveWorkgroupFromTransfer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_RemoveWorkgroupFromTransfer]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProblemSeverityOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ChangeProblemSeverityOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_Records]') AND OBJECTPROPERTY(id, N'IsView') = 1)
DROP VIEW [v_Pts_Records]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetFilterInfo]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetFilterInfo]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetFilterList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetFilterList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProblemStateOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ChangeProblemStateOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSharedItems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetSharedItems]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProblemPriorityOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ResetProblemPriorityOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProblemTypeOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ChangeProblemTypeOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProjectStateOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ChangeProjectStateOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProblemTypeOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ResetProblemTypeOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeRecordTypeOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ChangeRecordTypeOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddSharedItemAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_AddSharedItemAttachment]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProjectCatalogOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ResetProjectCatalogOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CheckUserPermission]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_CheckUserPermission]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSharedItemAttachments]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetSharedItemAttachments]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProjectStateOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ResetProjectStateOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProblemSeverityOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ResetProblemSeverityOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProjectCatalogOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ChangeProjectCatalogOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveSharedItemAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_RemoveSharedItemAttachment]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetRecordTypeOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ResetRecordTypeOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSharedItemAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetSharedItemAttachment]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_3]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Time_3]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_ProblemHistory]') AND OBJECTPROPERTY(id, N'IsView') = 1)
DROP VIEW [v_Pts_ProblemHistory]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemStateInfo]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetProblemStateInfo]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_4]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Time_4]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_ProblemVisitHistory]') AND OBJECTPROPERTY(id, N'IsView') = 1)
DROP VIEW [v_Pts_ProblemVisitHistory]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Workflow_GetAvilibleNextStateList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Workflow_GetAvilibleNextStateList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_5]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Time_5]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_Projects]') AND OBJECTPROPERTY(id, N'IsView') = 1)
DROP VIEW [v_Pts_Projects]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Workflow_AddWorkflowStep]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Workflow_AddWorkflowStep]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_6]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Time_6]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProblemState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ChangeProblemState]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddSharedItem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_AddSharedItem]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateSharedItem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_UpdateSharedItem]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RunInsertProblemString]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_RunInsertProblemString]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Workflow_UpdateWorkflowStep]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Workflow_UpdateWorkflowStep]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_7]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Time_7]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_8]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Time_8]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Workflow_DeleteWorkflowStep]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Workflow_DeleteWorkflowStep]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_9]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_Filter_Time_9]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_WorkflowSteps]') AND OBJECTPROPERTY(id, N'IsView') = 1)
DROP VIEW [v_Pts_WorkflowSteps]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetAvilibleNextStateList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_GetAvilibleNextStateList]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_ProblemAttachments]') AND OBJECTPROPERTY(id, N'IsView') = 1)
DROP VIEW [v_Pts_ProblemAttachments]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Accounts_Permissions]') AND OBJECTPROPERTY(id, N'IsView') = 1)
DROP VIEW [v_Accounts_Permissions]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_RecordAttachments]') AND OBJECTPROPERTY(id, N'IsView') = 1)
DROP VIEW [v_Pts_RecordAttachments]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_Problems]') AND OBJECTPROPERTY(id, N'IsView') = 1)
DROP VIEW [v_Pts_Problems]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_UserWorkgroups]') AND OBJECTPROPERTY(id, N'IsView') = 1)
DROP VIEW [v_Pts_UserWorkgroups]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ComputeProjectCounter]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ComputeProjectCounter]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RecordStateStart]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_RecordStateStart]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_SharedItems]') AND OBJECTPROPERTY(id, N'IsView') = 1)
DROP VIEW [v_Pts_SharedItems]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_SharedItemAttachments]') AND OBJECTPROPERTY(id, N'IsView') = 1)
DROP VIEW [v_Pts_SharedItemAttachments]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_Stat_StateKeepTime]') AND OBJECTPROPERTY(id, N'IsView') = 1)
DROP VIEW [v_Pts_Stat_StateKeepTime]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProblemCatalogOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ResetProblemCatalogOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProblemStateOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ResetProblemStateOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetWorkgroupOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ResetWorkgroupOrder]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_ProblemStateRecord]') AND OBJECTPROPERTY(id, N'IsView') = 1)
DROP VIEW [v_Pts_ProblemStateRecord]
GO
IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProblemCatalogGlobalOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [sp_Pts_ResetProblemCatalogGlobalOrder]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemByCode]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemInfo]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



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



' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'





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





' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemStateList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



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



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectInfo]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



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




' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectSeverity]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'





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




' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'







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





' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetRecord]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'


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



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetUserDetailsByEmail]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'




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



' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



CREATE PROCEDURE [sp_Accounts_GetUsers]  AS
Select * from v_Accounts_Users



' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetRecordList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



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



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetUserDetails]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'




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



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserLastProcessedProblems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
Create Procedure [sp_Pts_GetUserLastProcessedProblems]
@UserID int,
@State int
AS

IF @State=0
	Select * From v_Pts_Problems Where LastUser = @UserID
ELSE IF @State=1
	Select * From v_Pts_Problems Where LastUser = @UserID AND IsClosed=0
ELSE IF @State=2
	Select * From v_Pts_Problems Where LastUser = @UserID AND IsClosed=1
ELSE
	Select * From v_Pts_Problems Where LastUser = @UserID AND IsClosed=0

' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserOutProblems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'




CREATE  Procedure [sp_Pts_GetUserOutProblems]
@UserID int
AS
Select * From v_Pts_Problems where CreateUserID = @UserID AND IsClosed=0



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Common_GetRecordFromPage]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserRelateProblems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Common_GetTopicList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
CREATE procedure [sp_Common_GetTopicList] 
       @a_FieldList Varchar(200),
       @a_TableName Varchar(30), 
       @a_SelectWhere Varchar(500),
       @a_SelectOrderId Varchar(20),
       @a_SelectOrder Varchar(50),
       @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(600)
   /*关闭计数*/
   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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserVisitedProblems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'


CREATE  PROCEDURE [sp_Pts_GetUserVisitedProblems]
(
@UserID int
)
AS
SELECT * From v_Pts_ProblemVisitHistory Where VisitUserID=@UserID Order by VisitIndex



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserNeedProcessProjects]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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 
		AssignedTo = @UserID AND
		IsClosed = 0
	)
AND
	IsClosed = 0	

Order By 
	CatalogOrder


' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_MergeProjectCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



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



' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_MergeProjectState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



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



' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveRecordAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



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



' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProblemStateOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProblemStateOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Stat_GetGlobalNum]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
	
from v_Pts_Problems  where ProjectID = @ProjectID

' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Stat_RunQueryString]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'




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




' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemOriginInfoHistoryID]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_LastWeekNew]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_2]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_3]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_4]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_6]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_State_1]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_State_2]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_State_3]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_ThisMonthNew]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_ThisWeekNew]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_1]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_2]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_10]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_3]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemStateInfo]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_4]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_5]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_7]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_8]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_6]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemCatalogAndChild]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_Problems]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC 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.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
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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProject]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProblemCatalogOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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_ProblemPriority))
    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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProblemCatalogGlobalOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemCtalogList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



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



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemPriorityList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'







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




' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProblemPriorityOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemPriority]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProblemPriority]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProblemPriorityOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemPriority]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemSeverity]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProblemSeverityOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProblemSeverityOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProblemSeverity]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemSeverity]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemSeverityList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'







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




' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_Records]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC 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
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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserAllowedInitStates]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProblemState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'






CREATE    PROCEDURE [sp_Pts_CreateProblemState]
@ProjectID int,
@StateName nvarchar(50),
@Description nvarchar(255),
@IsFinalState bit,
	@AssignType int,
	@Assignee int,
	@SelectableAssigneeWorkgroupIDs nvarchar(1024),
	@SelectableAssigneeUserIDs nvarchar(1024),
	@ExtendedSettings ntext
AS
Insert into Pts_ProblemState
	(ProjectID, StateName, Description, IsFinalState,
	[AssignType],
	[Assignee],
	[SelectableAssigneeWorkgroupIDs],
	[SelectableAssigneeUserIDs],[ExtendedSettings])
Values
	(@ProjectID, @StateName, @Description,  @IsFinalState,
	@AssignType,
	@Assignee,
	@SelectableAssigneeWorkgroupIDs,
	@SelectableAssigneeUserIDs,
	@ExtendedSettings)

exec sp_Pts_ResetProblemStateOrder	@ProjectID

SELECT @@IDENTITY





' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'




CREATE  PROCEDURE [sp_Pts_DeleteProblem]
@ProblemID int
AS
Begin transaction

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_Problems Where ProblemID = @ProblemID

commit transaction




' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_ProblemStateRecord]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC sp_executesql @statement = N'



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




'
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RecordStateStart]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RecordProblemEnd]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemStateTransfer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetAvilibleNextStateList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProblemStateTransfer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetStateAllowedUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectStateTransferList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetNextStateListByUser]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetNextStateList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetTransferWorkgroups]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetTransferCandidateWrokgroups]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddWorkgroupToTransfer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveWorkgroupFromTransfer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProblemType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProblemType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProblemTypeOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProblemTypeOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemTypeList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



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



' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetCategoryList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
CREATE PROCEDURE [sp_Kb_GetCategoryList]
@ParentID int
AS
SELECT * FROM Kb_Categories WHERE ParentID = @ParentID
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetPathCategories]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_CreateCategory]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Kb_ArticleList]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveFilter]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateFilter]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateFilter]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetFilterInfo]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
Create Procedure [sp_Pts_GetFilterInfo]
@FilterID int
AS

Select * From Pts_Filters Where FilterID = @FilterID

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetFilterList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Common_AddEvent]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetRecordTypeList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'





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






' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeRecordTypeOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetRecordTypeOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateRecordType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateRecordType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteRecordType]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteRecord]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_stat_CurrentProjectStat]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_5]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_7]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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)
)
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateRecord]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateRecord]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateRecordProperty]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ComputeProjectCounter]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_SearchProblem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_IsUserRelateProblem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



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


Select count(*) From Pts_Problems Where ProblemID = @ProblemID AND 
	(CreateUserID = @UserID or AssignedTo = @UserID
	or @UserID in (Select CreateUser From Pts_Records Where ProblemID = @ProblemID))






' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSelectUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserCanAddProblemProjects]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_IsUserInProject]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetWorkgroupUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUsersNotInWorkgroup]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddUserToWorkgroup]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveUserFromWorkgroup]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CheckUserPermission]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'





--判断用户在项目内的权限
CREATE   Procedure [sp_Pts_CheckUserPermission]
@UserID int,
@ProjectID int,
@Permission int
AS

declare @Result int
Set @Result = 0

--admin project
if (@Permission = 1)
BEGIN
	Select * From Pts_Workgroups Where ProjectID = @ProjectID 
		AND AdminProject=1 
		AND WorkgroupID in
			(Select WorkgroupID From Pts_WorkgroupUsers Where UserID = @UserID)
	Set @Result = @@RowCount
END

--addproblem
if (@Permission = 2)
BEGIN
	Select * From Pts_Workgroups Where ProjectID = @ProjectID 
		AND AddProblem=1 
		AND WorkgroupID in
			(Select WorkgroupID From Pts_WorkgroupUsers Where UserID = @UserID)
	Set @Result = @@RowCount
END

--change problem state
if (@Permission = 3)
BEGIN
	Select * From Pts_Workgroups Where ProjectID = @ProjectID 
		AND ( ChangeProblemState=1)
		AND WorkgroupID in
			(Select WorkgroupID From Pts_WorkgroupUsers Where UserID = @UserID)
	Set @Result = @@RowCount
END

--add thread
if (@Permission = 4)
BEGIN
	Select * From Pts_Workgroups Where ProjectID = @ProjectID 
		AND ShiftProblem=1 
		AND WorkgroupID in
			(Select WorkgroupID From Pts_WorkgroupUsers Where UserID = @UserID)
	Set @Result = @@RowCount
END

--add record
if (@Permission = 5)
BEGIN
	Select * From Pts_Workgroups Where ProjectID = @ProjectID 
		AND AddRecord=1 
		AND WorkgroupID in
			(Select WorkgroupID From Pts_WorkgroupUsers Where UserID = @UserID)
	Set @Result = @@RowCount
END

return @Result





' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserViewableFields]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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)
					)
		)
	)
		
ORDER BY 
	ListOrder ASC


END' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetEditableFieldsWhenProcessIssue]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetEditableFieldsWhenEditIssue]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetEditableFieldsWhenCreateIssue]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
			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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_UserWorkgroups]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[GetAssigneeName]') AND xtype in (N'FN', N'IF', N'TF'))
BEGIN
execute 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetFieldReadableWorkgroups]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetFieldEditableWorkgroups]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateWorkgroup]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateWorkgroup]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'




CREATE  PROCEDURE [sp_Pts_CreateWorkgroup]
@ProjectID	int,
@Name nvarchar (50),
@Description nvarchar(255),
@AdminProject bit,
@AddProblem bit,
@ChangeProblemState bit,
@ShiftProblem bit,
@AddRecord bit,
@WorkgroupID int output
AS
Insert into Pts_Workgroups
  (ProjectID, [Name], Description,  AdminProject, AddProblem, ChangeProblemState, ShiftProblem, AddRecord)
Values
    (@ProjectID, @Name, @Description,  @AdminProject, @AddProblem, @ChangeProblemState, @ShiftProblem, @AddRecord)

set @WorkgroupID = @@IDENTITY

exec sp_Pts_ResetWorkgroupOrder @ProjectID
return 1




' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetWorkgroupOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetWorkgroupInfo]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetWorkgroupList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'




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




' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetStateEditableFields]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetComments]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_DeleteArticle]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_AddComment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_CanUserViewArticle]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetArticleAllowedViewer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetArticleNotAllowedViewer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_RemoveUserFromArticleViewer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetArticleNotAllowedViewerByDept]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_AddUserToArticleViewer]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserFields]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_GetUserFields]
@ProjectID int
AS
Select * From Pts_DbDirectory where ProjectID = @ProjectID  Order by ListOrder
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateNewUserField]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateUserField]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'CREATE  Procedure [sp_Pts_UpdateUserField]
@ID int,
@Label	nvarchar(25),
@ListOrder int,
@InUse	bit,
@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,
@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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddProblemRelation]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveProblemRelation]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_SaveProblemChange]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_ProblemHistory]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveSharedItemAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
Create PROCEDURE [sp_Pts_RemoveSharedItemAttachment]
@AttachmentID int
AS
DELETE FROM Pts_SharedItemAttachments WHERE AttachmentID = @AttachmentID


' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_SharedItemAttachments]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSharedItemAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetSharedItemAttachment]
@AttachmentID int
AS
SELECT * FROM Pts_SharedItemAttachments
WHERE AttachmentID = @AttachmentID' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddSharedItemAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_SharedItems]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateSharedItem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSharedItemTypes]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddSharedItem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateFilterNode]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetFilterNodes]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
Create Procedure [sp_Pts_GetFilterNodes]
@FilterID int
AS
Select * FROM Pts_FilterNodes Where FilterID = @FilterID

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_Projects]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC 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_ProjectCatalogs.Name AS CatalogName, 
                      Pts_ProjectCatalogs.ListOrder AS CatalogOrder, Pts_ProjectStates.Name AS StateName, Pts_ProjectStates.IsCloseState AS IsClosed, 
                      Pts_Projects.SecurityLevel, Pts_Projects.KeyWords
FROM         Pts_Projects INNER JOIN
                      Pts_ProjectCatalogs ON Pts_Projects.ProjectCatalogID = Pts_ProjectCatalogs.ProjectCatalogID INNER JOIN
                      Pts_ProjectStates ON Pts_Projects.ProjectStateID = Pts_ProjectStates.ProjectStateID
'
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProject]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'




CREATE  procedure [sp_Pts_UpdateProject]
@ProjectID int,
@ProjectCatalogID int,
@ProjectCode varchar(15),
@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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProject]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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



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
)
	
values
	(
@ProjectCatalogID,
@Name,
@ProjectCode,
@Description,
@ProjectStateID ,
@ProblemTitle ,
@ThreadTitle ,
@RecordTitle ,
@OnlyViewOwnProblem ,
@EnableProblemState ,
@EnableMultipleThread ,
@EnableEmailNotify ,
@EnableProblemPriority ,
@EnableProblemSeverity ,
@EnableProblemType ,
@EnableProblemCatalog ,
@EnableProblemDeadline ,
@VisibleToAll ,
@EnableStateTimer ,
@AutoAssignedTo,
@ExtendedSettings,
@SecurityLevel,
@KeyWords
 )




set @ProjectID = @@Identity
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetNewProblemIndex]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_LogProblemVisit]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RelatedUser_Add]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RelatedUsers_Get]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveSubscription]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_RemoveSubscription]
@UserID int,
@TargetType int,
@TargetID int
AS
DELETE FROM Pts_Subscription WHERE UserID=@UserID AND TargetType=@TargetType AND TargetID=@TargetID' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_IsItemSubscribed]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_IsItemSubscribed]
@UserID int,
@TargetType int,
@TargetID int
AS
SELECT COUNT(*) FROM Pts_Subscription WHERE UserID=@UserID AND TargetType=@TargetType AND TargetID=@TargetID' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Subscription_Add]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_Subscription_Add]
@UserID int,
@TargetType int,
@TargetID int
AS
if NOT EXISTS(SELECT * FROM Pts_Subscription WHERE UserID=@UserID AND TargetType=@TargetType AND TargetID=@TargetID)
BEGIN
INSERT INTO Pts_Subscription (UserID,TargetType, TargetID) VALUES(@UserID,@TargetType,@TargetID)
END' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSubscribeUserSet]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_GetSubscribeUserSet]
@TargetType int,
@TargetID int
AS
SELECT * FROM v_Accounts_Users WHERE UserID IN (SELECT UserID FROM Pts_Subscription WHERE TargetType=@TargetType AND TargetID=@TargetID)' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_RemoveArticleAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
Create PROCEDURE [sp_Kb_RemoveArticleAttachment]
@AttachmentID int
AS
DELETE FROM Kb_ArticleAttachments WHERE AttachmentID = @AttachmentID


' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_GetArticleAttachments]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
Create PROCEDURE [sp_Kb_GetArticleAttachments]
@ArticleID int
AS
SELECT * FROM Kb_ArticleAttachments WHERE ArticleID = @ArticleID


' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_AddArticleAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_WorkgroupDeletePermitState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_WorkgroupAddPermitState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetWorkgroupNotPermitInitStates]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetWorkgroupPermitInitStates]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_AddAccessList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
CREATE Procedure [sp_Accounts_AddAccessList]
@AccessListName nvarchar(80)
AS
Insert into Accounts_AccessLists
(AccessListName)
values
(@AccessListName)

Select @@IDENTITY
' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetAccessLists]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
CREATE PROCEDURE [sp_Accounts_GetAccessLists]
AS
Select * From 
Accounts_AccessLists
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_DeleteAccessList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_ReviewArticle]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_AddArticle]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_UpdateArticle]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_AddReadCount]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
Create Procedure [sp_Kb_AddReadCount]
@ArticleID int
AS
Update Kb_Articles Set NumRead = NumRead+1 Where ArticleID = @ArticleID

' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_SetArticlePublic]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Kb_MoveArticle]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetFieldEditableStates]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Notify_Delete]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'CREATE PROCEDURE [sp_Pts_Notify_Delete]
	@NotifyID int 
AS

DELETE FROM Pts_Notify WHERE NotifyID = @NotifyID' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Notify_Insert]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddRecordAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_ResetDepartmentOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_UpdateDepartment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_DeleteDepartment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetDepartmentList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
CREATE PROCEDURE [sp_Accounts_GetDepartmentList]
AS
SELECT 
 *
FROM 
 Accounts_Department
ORDER BY 
 ListOrder ASC
' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_ChangeDeptOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_CreateDepartment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Accounts_Users]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC 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
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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_AddAccessListItem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetAccessListItems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
CREATE PROCEDURE [sp_Accounts_GetAccessListItems]
@AccessListID int
AS
Select * From
Accounts_AccessListItems
Where AccessListID = @AccessListID
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_DeleteAccessListItem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
Create Procedure [sp_Accounts_DeleteAccessListItem]
@AccessListItemID int
AS

Delete From Accounts_AccessListItems Where AccessListItemID = @AccessListItemID


' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_ValidateUserIP]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddProblemAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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)




' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RemoveProblemAttachment]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



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



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_ProblemAttachments]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProjectCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProjectCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_CreateProjectCatalog] 
@Name	 varchar(50),
@Description varchar(255)
AS
Insert into Pts_ProjectCatalogs ([Name],Description) values(@Name, @Description)
exec sp_Pts_ResetProjectCatalogOrder



' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProjectCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProjectCatalogOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProjectCatalogOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



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



' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectCatalogs]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



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



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_DeleteUser]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_RecordAttachments]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_CreateUser]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_TestPassword]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_UpdateUser]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_ValidateLogin]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetUsersNotInRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetRoleUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetRepresentedUsers]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetUserAgent]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'CREATE PROCEDURE [sp_Accounts_GetUserAgent]
@UserID int
AS
--得到一个帐号的代理人用户ID
SELECT AgentUserID FROM Accounts_Users WHERE UserID = @UserID
' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteProjectState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



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



' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProjectState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProjectState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProjectStateOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProjectStateOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectStates]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



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



' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProjectState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



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



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_ProblemCreateRoles]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC sp_executesql @statement = N'CREATE VIEW [v_Pts_ProblemCreateRoles]
AS
SELECT     Pts_ProblemCreateRoles.*, Pts_ProblemState.ProblemStateID, Pts_ProblemState.StateName
FROM         Pts_ProblemCreateRoles LEFT OUTER JOIN
                      Pts_ProblemState ON Pts_ProblemCreateRoles.DstStateID = Pts_ProblemState.ProblemStateID
'
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddProblemCreateRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Leal using CodeSmith 4.0.0.0
-- Template:       StoredProcedures.cst
-- Procedure Name: [sp_InsertPts_ProblemCreateRole]
-- Date Generated: Monday, July 23, 2007
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [sp_Pts_AddProblemCreateRole]
	@ProjectID int,
	@DstStateID int,
	@Description nvarchar(1024),
	@AssignType int,
	@Assignee int,
	@SelectableAssigneeWorkgroupIDs nvarchar(1024),
	@SelectableAssigneeUserIDs nvarchar(1024)
AS

SET NOCOUNT ON

INSERT INTO [Pts_ProblemCreateRoles] (
	[ProjectID],
	[DstStateID],
	[Description],
	[AssignType],
	[Assignee],
	[SelectableAssigneeWorkgroupIDs],
	[SelectableAssigneeUserIDs]
) VALUES (
	@ProjectID,
	@DstStateID,
	@Description,
	@AssignType,
	@Assignee,
	@SelectableAssigneeWorkgroupIDs,
	@SelectableAssigneeUserIDs
)

SELECT SCOPE_IDENTITY()' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemCreateRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'-- =============================================
-- Author:		CuiLiang
-- Create date: <Create Date,,>
-- Description:	更新事务创建规则
-- =============================================
CREATE PROCEDURE [sp_Pts_UpdateProblemCreateRole]
	@ID int,
	@ProjectID int,
	@DstStateID int,
	@Description nvarchar(1024),
	@AssignType int,
	@Assignee int,
	@SelectableAssigneeWorkgroupIDs nvarchar(1024),
	@SelectableAssigneeUserIDs nvarchar(1024)
AS

SET NOCOUNT ON

UPDATE [Pts_ProblemCreateRoles] SET
	[ProjectID] = @ProjectID,
	[DstStateID] = @DstStateID,
	[Description] = @Description,
	[AssignType] = @AssignType,
	[Assignee] = @Assignee,
	[SelectableAssigneeWorkgroupIDs] = @SelectableAssigneeWorkgroupIDs,
	[SelectableAssigneeUserIDs] = @SelectableAssigneeUserIDs
WHERE
	[ID] = @ID' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Workflow_GetAvilibleNextStateList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Workflow_AddWorkflowStep]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'--region [sp_InsertPts_WorkflowStep]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Leal using CodeSmith 4.0.0.0
-- Template:       StoredProcedures.cst
-- Procedure Name: [sp_InsertPts_WorkflowStep]
-- Date Generated: Monday, August 27, 2007
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [sp_Pts_Workflow_AddWorkflowStep]
	@ProjectID int,
	@SrcStateID int,
	@DstStateID int,
	@StepName nvarchar(100),
	@Description ntext,
	@AssignType int,
	@Assignee int,
	@SelectableAssigneeWorkgroupIDs nvarchar(1024),
	@SelectableAssigneeUserIDs nvarchar(1024)
AS

SET NOCOUNT ON

INSERT INTO [Pts_WorkflowSteps] (
	[ProjectID],
	[SrcStateID],
	[DstStateID],
	[StepName],
	[Description],
	[AssignType],
	[Assignee],
	[SelectableAssigneeWorkgroupIDs],
	[SelectableAssigneeUserIDs]
) VALUES (
	@ProjectID,
	@SrcStateID,
	@DstStateID,
	@StepName,
	@Description,
	@AssignType,
	@Assignee,
	@SelectableAssigneeWorkgroupIDs,
	@SelectableAssigneeUserIDs
)

SELECT SCOPE_IDENTITY()

--endregion' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Workflow_DeleteWorkflowStep]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_WorkflowSteps]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC sp_executesql @statement = N'CREATE VIEW [v_Pts_WorkflowSteps]
AS
SELECT     Pts_WorkflowSteps.*, 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Workflow_UpdateWorkflowStep]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
--region [sp_UpdatePts_WorkflowStep]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Leal using CodeSmith 4.0.0.0
-- Template:       StoredProcedures.cst
-- Procedure Name: [sp_UpdatePts_WorkflowStep]
-- Date Generated: Monday, August 27, 2007
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [sp_Pts_Workflow_UpdateWorkflowStep]
	@StepID int,
	@ProjectID int,
	@SrcStateID int,
	@DstStateID int,
	@StepName nvarchar(100),
	@Description ntext,
	@AssignType int,
	@Assignee int,
	@SelectableAssigneeWorkgroupIDs nvarchar(1024),
	@SelectableAssigneeUserIDs nvarchar(1024)
AS

SET NOCOUNT ON

UPDATE [Pts_WorkflowSteps] SET
	[ProjectID] = @ProjectID,
	[SrcStateID] = @SrcStateID,
	[DstStateID] = @DstStateID,
	[StepName] = @StepName,
	[Description] = @Description,
	[AssignType] = @AssignType,
	[Assignee] = @Assignee,
	[SelectableAssigneeWorkgroupIDs] = @SelectableAssigneeWorkgroupIDs,
	[SelectableAssigneeUserIDs] = @SelectableAssigneeUserIDs
WHERE
	[StepID] = @StepID' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Accounts_Permissions]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetPermissionCategoryDetails]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'




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





' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetPermissionCategories]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'




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




' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetPermissionList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_RemovePermissionFromRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetEffectivePermissionList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_DeleteRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_ClearPermissionsFromRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'




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





' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_AddPermissionToRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateGlobalSelect]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateGlobalSelect]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteGlobalSelect]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_CreateRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'




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





' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetAllRoles]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'




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



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetUserRoles]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetRoleDetails]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'




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





' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_UpdateRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_RemoveUserFromRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_AddUserToRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSelectValues]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
CREATE PROCEDURE [sp_Pts_GetSelectValues]
@GlobalSelectID int
AS
Select * From Pts_GlobalSelectValues Where GlobalSelectID = @GlobalSelectID Order by ListOrder
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_AddSelectValue]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteSelectValue]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
Create Procedure [sp_Pts_DeleteSelectValue]
@SelectValueID int
AS
Delete From Pts_GlobalSelectValues Where SelectValueID = @SelectValueID

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ShiftProblem]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ResetProblemCatalogOrder]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetRecordAttachmentList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
create procedure [sp_Pts_GetRecordAttachmentList]
@RecordID int
AS
Select * From v_Pts_RecordAttachments Where RecordID = @RecordID

' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_UpdateProblemState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



--更新问题状态定义
CREATE PROCEDURE [sp_Pts_UpdateProblemState]
@ProblemStateID int,
@StateName nvarchar(50),
@Description nvarchar(255),
@IsFinalState bit,
	@AssignType int,
	@Assignee int,
	@SelectableAssigneeWorkgroupIDs nvarchar(1024),
	@SelectableAssigneeUserIDs nvarchar(1024),
@ExtendedSettings ntext
AS
Update Pts_ProblemState 
Set
	StateName = @StateName, Description = @Description, IsFinalState=@IsFinalState,
	[AssignType] = @AssignType,
	[Assignee] = @Assignee,
	[SelectableAssigneeWorkgroupIDs] = @SelectableAssigneeWorkgroupIDs,
	[SelectableAssigneeUserIDs] = @SelectableAssigneeUserIDs,
	[ExtendedSettings] = @ExtendedSettings
where
	ProblemStateID = @ProblemStateID

declare @ProjectID int
Select @ProjectID = ProjectID From Pts_ProblemState Where ProblemStateID = @ProblemStateID
exec sp_Pts_ComputeProjectCounter @ProjectID	




' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_DeleteWorkgroup]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_1]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
	(AssignedTo = @UserID
	OR
	-AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID)
	)
AND 
	IsClosed = 0

' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserInProblems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



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



' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_RunInsertProblemString]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_ChangeProblemState]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
Select @ProjectID = ProjectID From Pts_Problems Where ProblemID=@ProblemID
exec sp_Pts_ComputeProjectCounter @ProjectID' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSharedItems]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetSharedItemAttachments]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'
CREATE PROCEDURE [sp_Pts_GetSharedItemAttachments]
@ItemID int
AS
SELECT * FROM v_Pts_SharedItemAttachments WHERE ItemID = @ItemID



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_ProblemVisitHistory]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Accounts_GetPermissionsNotInRole]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Relate_8]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_a1]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_a2]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_a3]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_a4]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Filter_Time_9]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetUserWorkgroups]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_GetProblemAttachmentList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC sp_executesql @statement = N'



CREATE PROCEDURE [sp_Pts_GetProblemAttachmentList]
@ProblemID int
AS
Select * from  v_Pts_ProblemAttachments where ProblemID = @ProblemID
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[v_Pts_Stat_StateKeepTime]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_CreateProblemCatalog]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[sp_Pts_Stat_GetAvgStateKeepTime]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC 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
