--
-- Script To Update dbo.Accounts_Users Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Accounts_Users Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Accounts_Users]
      ALTER COLUMN [Address] [nvarchar] (280) COLLATE Chinese_PRC_CI_AS NULL
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Accounts_Users]
      ADD [Attributes] [ntext] COLLATE Chinese_PRC_CI_AS NULL
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Accounts_Users Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Accounts_Users Table'
END
GO

--
-- Script To Update dbo.Kb_Categories Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Kb_Categories Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Kb_Catalogs_ParentID')
      ALTER TABLE [dbo].[Kb_Categories] DROP CONSTRAINT [DF_Kb_Catalogs_ParentID]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Kb_Categories_CreateDate')
      ALTER TABLE [dbo].[Kb_Categories] DROP CONSTRAINT [DF_Kb_Categories_CreateDate]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Kb_Categories_ArticleCount')
      ALTER TABLE [dbo].[Kb_Categories] DROP CONSTRAINT [DF_Kb_Categories_ArticleCount]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   CREATE TABLE [dbo].[tmp_Kb_Categories] (
   [CategoryID] [int] IDENTITY (1, 1) NOT NULL,
   [CategoryName] [nvarchar] (1024) COLLATE Chinese_PRC_CI_AS NOT NULL,
   [ParentID] [int] NOT NULL CONSTRAINT [DF_Kb_Catalogs_ParentID] DEFAULT (0),
   [CreateDate] [datetime] NOT NULL CONSTRAINT [DF_Kb_Categories_CreateDate] DEFAULT (getdate()),
   [ArticleCount] [int] NOT NULL CONSTRAINT [DF_Kb_Categories_ArticleCount] DEFAULT (0),
   [LastUpdate] [datetime] NULL,
   [Path] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [AllowedUserGroups] [nvarchar] (1000) COLLATE Chinese_PRC_CI_AS NULL,
   [IsPublic] [bit] NOT NULL CONSTRAINT [DF_Kb_Categories_IsPublic] DEFAULT (1),
   [RequireReview] [bit] NOT NULL CONSTRAINT [DF_Kb_Categories_RequireReview] DEFAULT (1)
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Kb_Categories] ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   INSERT INTO [dbo].[tmp_Kb_Categories] ([CategoryID], [CategoryName], [ParentID], [CreateDate], [ArticleCount], [LastUpdate], [Path], [AllowedUserGroups])
   SELECT [CategoryID], [CategoryName], [ParentID], [CreateDate], [ArticleCount], [LastUpdate], [Path], NULL
   FROM [dbo].[Kb_Categories]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Kb_Categories] OFF
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   DROP TABLE [dbo].[Kb_Categories]
GO

sp_rename N'[dbo].[tmp_Kb_Categories]', N'Kb_Categories'

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Kb_Categories] ADD CONSTRAINT [PK_Kb_Catalogs] PRIMARY KEY CLUSTERED ([CategoryID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Kb_Categories Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Kb_Categories Table'
END
GO

--
-- Script To Create dbo.Pts_Coworkers Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.Pts_Coworkers Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO

CREATE TABLE [dbo].[Pts_Coworkers] (
   [ID] [int] IDENTITY (1, 1) NOT NULL,
   [ProblemID] [int] NOT NULL,
   [UserID] [int] NOT NULL,
   [CreateTime] [datetime] NOT NULL,
   [IsWorked] [bit] NOT NULL CONSTRAINT [DF_Pts_Coworkers_IsWorked] DEFAULT (0),
   [SubmitTime] [datetime] NULL,
   [WorkRecordID] [int] NOT NULL,
   [ProblemStateID] [int] NOT NULL,
   [CurrentAssignee] [int] NOT NULL
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_Coworkers] ADD CONSTRAINT [PK_Pts_Coworkers] PRIMARY KEY CLUSTERED ([ID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_Coworkers Table Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.Pts_Coworkers Table'
END
GO

--
-- Script To Update dbo.Pts_ProjectCatalogs Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_ProjectCatalogs Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Projects_Pts_ProjectCatalogs')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [FK_Pts_Projects_Pts_ProjectCatalogs]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_ProjectCatalogs_ListOrder')
      ALTER TABLE [dbo].[Pts_ProjectCatalogs] DROP CONSTRAINT [DF_Pts_ProjectCatalogs_ListOrder]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   CREATE TABLE [dbo].[tmp_Pts_ProjectCatalogs] (
   [ProjectCatalogID] [int] IDENTITY (1, 1) NOT NULL,
   [Name] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
   [Description] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [ListOrder] [int] NOT NULL CONSTRAINT [DF_Pts_ProjectCatalogs_ListOrder] DEFAULT (100000),
   [ParentID] [int] NOT NULL CONSTRAINT [DF_Pts_ProjectCatalogs_ParentID] DEFAULT (0)
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Pts_ProjectCatalogs] ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   INSERT INTO [dbo].[tmp_Pts_ProjectCatalogs] ([ProjectCatalogID], [Name], [Description], [ListOrder])
   SELECT [ProjectCatalogID], [Name], [Description], [ListOrder]
   FROM [dbo].[Pts_ProjectCatalogs]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Pts_ProjectCatalogs] OFF
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   DROP TABLE [dbo].[Pts_ProjectCatalogs]
GO

sp_rename N'[dbo].[tmp_Pts_ProjectCatalogs]', N'Pts_ProjectCatalogs'

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_ProjectCatalogs] ADD CONSTRAINT [PK_Pts_ProjectCatalogs] PRIMARY KEY CLUSTERED ([ProjectCatalogID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_ProjectCatalogs Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_ProjectCatalogs Table'
END
GO


--
-- Script To Update dbo.Pts_Projects Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_Projects Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_DbDirectory_Pts_Projects')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [FK_Pts_DbDirectory_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Filters_Pts_Projects')
      ALTER TABLE [dbo].[Pts_Filters] DROP CONSTRAINT [FK_Pts_Filters_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemCatalogs_Pts_Projects')
      ALTER TABLE [dbo].[Pts_ProblemCatalogs] DROP CONSTRAINT [FK_Pts_ProblemCatalogs_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemCreateRoles_Pts_Projects')
      ALTER TABLE [dbo].[Pts_ProblemCreateRoles] DROP CONSTRAINT [FK_Pts_ProblemCreateRoles_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemPriority_Pts_Projects')
      ALTER TABLE [dbo].[Pts_ProblemPriority] DROP CONSTRAINT [FK_Pts_ProblemPriority_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_Projects')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [FK_Pts_Problems_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_Projects1')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [FK_Pts_Problems_Pts_Projects1]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemSeverities_Pts_Projects')
      ALTER TABLE [dbo].[Pts_ProblemSeverity] DROP CONSTRAINT [FK_Pts_ProblemSeverities_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemState_Pts_Projects')
      ALTER TABLE [dbo].[Pts_ProblemState] DROP CONSTRAINT [FK_Pts_ProblemState_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemType_Pts_Projects')
      ALTER TABLE [dbo].[Pts_ProblemType] DROP CONSTRAINT [FK_Pts_ProblemType_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_RecordType_Pts_Projects')
      ALTER TABLE [dbo].[Pts_RecordType] DROP CONSTRAINT [FK_Pts_RecordType_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_SharedItems_Pts_Projects')
      ALTER TABLE [dbo].[Pts_SharedItems] DROP CONSTRAINT [FK_Pts_SharedItems_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_WorkflowSteps_Pts_Projects')
      ALTER TABLE [dbo].[Pts_WorkflowSteps] DROP CONSTRAINT [FK_Pts_WorkflowSteps_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Workgroups_Pts_Projects')
      ALTER TABLE [dbo].[Pts_Workgroups] DROP CONSTRAINT [FK_Pts_Workgroups_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Projects_Pts_ProjectCatalogs')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [FK_Pts_Projects_Pts_ProjectCatalogs]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Projects_Pts_ProjectStates')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [FK_Pts_Projects_Pts_ProjectStates]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_ProjectCode')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_ProjectCode]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_ShortName')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_ShortName]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_Description')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_Description]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_TotalProblems')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_TotalProblems]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_TotalThreads')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_TotalThreads]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_TotalRecords')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_TotalRecords]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_DateCreated')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_DateCreated]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_ProblemTitle')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_ProblemTitle]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_ThreadTitle')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_ThreadTitle]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_RecordTitle')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_RecordTitle]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_ChangeOwnProblem')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_ChangeOwnProblem]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_EnableProblemState')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_EnableProblemState]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_MultipleThread')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_MultipleThread]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_EnableEMailNotify')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_EnableEMailNotify]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_EnableProblemPriority')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_EnableProblemPriority]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_EnableProblemSeverity')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_EnableProblemSeverity]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_EnableProblemType')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_EnableProblemType]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_EnableProblemCatalog')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_EnableProblemCatalog]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_EnableProblemDeadline')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_EnableProblemDeadline]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_VisibleToAll')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_VisibleToAll]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_EnableStateTimer')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_EnableStateTimer]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_AutoAssignedTo')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_AutoAssignedTo]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF__Pts_Proje__OpenP__3DE82FB7')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF__Pts_Proje__OpenP__3DE82FB7]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_NewProblemIndex')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_NewProblemIndex]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Projects_SecurityLevel')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [DF_Pts_Projects_SecurityLevel]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   CREATE TABLE [dbo].[tmp_Pts_Projects] (
   [ProjectID] [int] IDENTITY (1, 1) NOT NULL,
   [ProjectCatalogID] [int] NOT NULL,
   [ProjectCode] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL CONSTRAINT [DF_Pts_Projects_ProjectCode] DEFAULT (''),
   [Name] [nvarchar] (250) COLLATE Chinese_PRC_CI_AS NOT NULL,
   [ShortName] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NULL CONSTRAINT [DF_Pts_Projects_ShortName] DEFAULT (''),
   [Description] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL CONSTRAINT [DF_Pts_Projects_Description] DEFAULT (''),
   [ProjectStateID] [int] NOT NULL,
   [TotalProblems] [int] NOT NULL CONSTRAINT [DF_Pts_Projects_TotalProblems] DEFAULT (0),
   [TotalThreads] [int] NOT NULL CONSTRAINT [DF_Pts_Projects_TotalThreads] DEFAULT (0),
   [TotalRecords] [int] NOT NULL CONSTRAINT [DF_Pts_Projects_TotalRecords] DEFAULT (0),
   [DateCreated] [smalldatetime] NOT NULL CONSTRAINT [DF_Pts_Projects_DateCreated] DEFAULT (getdate()),
   [ProblemTitle] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL CONSTRAINT [DF_Pts_Projects_ProblemTitle] DEFAULT (N'问题'),
   [ThreadTitle] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL CONSTRAINT [DF_Pts_Projects_ThreadTitle] DEFAULT (N'任务'),
   [RecordTitle] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL CONSTRAINT [DF_Pts_Projects_RecordTitle] DEFAULT (N'记录'),
   [OnlyViewOwnProblem] [bit] NOT NULL CONSTRAINT [DF_Pts_Projects_ChangeOwnProblem] DEFAULT (1),
   [EnableProblemState] [bit] NOT NULL CONSTRAINT [DF_Pts_Projects_EnableProblemState] DEFAULT (1),
   [EnableMultipleThread] [bit] NOT NULL CONSTRAINT [DF_Pts_Projects_MultipleThread] DEFAULT (0),
   [EnableEmailNotify] [bit] NOT NULL CONSTRAINT [DF_Pts_Projects_EnableEMailNotify] DEFAULT (1),
   [EnableProblemPriority] [bit] NOT NULL CONSTRAINT [DF_Pts_Projects_EnableProblemPriority] DEFAULT (1),
   [EnableProblemSeverity] [bit] NOT NULL CONSTRAINT [DF_Pts_Projects_EnableProblemSeverity] DEFAULT (1),
   [EnableProblemType] [bit] NOT NULL CONSTRAINT [DF_Pts_Projects_EnableProblemType] DEFAULT (1),
   [EnableProblemCatalog] [bit] NOT NULL CONSTRAINT [DF_Pts_Projects_EnableProblemCatalog] DEFAULT (1),
   [EnableProblemDeadline] [bit] NOT NULL CONSTRAINT [DF_Pts_Projects_EnableProblemDeadline] DEFAULT (1),
   [VisibleToAll] [bit] NOT NULL CONSTRAINT [DF_Pts_Projects_VisibleToAll] DEFAULT (1),
   [EnableStateTimer] [bit] NOT NULL CONSTRAINT [DF_Pts_Projects_EnableStateTimer] DEFAULT (1),
   [AutoAssignedTo] [int] NULL CONSTRAINT [DF_Pts_Projects_AutoAssignedTo] DEFAULT (1),
   [OpenProblems] [int] NOT NULL CONSTRAINT [DF__Pts_Proje__OpenP__3DE82FB7] DEFAULT (0),
   [NewProblemIndex] [int] NOT NULL CONSTRAINT [DF_Pts_Projects_NewProblemIndex] DEFAULT (1),
   [ExtendedSettings] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [SecurityLevel] [int] NOT NULL CONSTRAINT [DF_Pts_Projects_SecurityLevel] DEFAULT (2),
   [KeyWords] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [ListOrder] [int] NOT NULL CONSTRAINT [DF_Pts_Projects_ListOrder] DEFAULT (1000)
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Pts_Projects] ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   INSERT INTO [dbo].[tmp_Pts_Projects] ([ProjectID], [ProjectCatalogID], [ProjectCode], [Name], [ShortName], [Description], [ProjectStateID], [TotalProblems], [TotalThreads], [TotalRecords], [DateCreated], [ProblemTitle], [ThreadTitle], [RecordTitle], [OnlyViewOwnProblem], [EnableProblemState], [EnableMultipleThread], [EnableEmailNotify], [EnableProblemPriority], [EnableProblemSeverity], [EnableProblemType], [EnableProblemCatalog], [EnableProblemDeadline], [VisibleToAll], [EnableStateTimer], [AutoAssignedTo], [OpenProblems], [NewProblemIndex], [ExtendedSettings], [SecurityLevel], [KeyWords])
   SELECT [ProjectID], [ProjectCatalogID], [ProjectCode], [Name], [ShortName], [Description], [ProjectStateID], [TotalProblems], [TotalThreads], [TotalRecords], [DateCreated], [ProblemTitle], [ThreadTitle], [RecordTitle], [OnlyViewOwnProblem], [EnableProblemState], [EnableMultipleThread], [EnableEmailNotify], [EnableProblemPriority], [EnableProblemSeverity], [EnableProblemType], [EnableProblemCatalog], [EnableProblemDeadline], [VisibleToAll], [EnableStateTimer], [AutoAssignedTo], [OpenProblems], [NewProblemIndex], [ExtendedSettings], [SecurityLevel], [KeyWords]
   FROM [dbo].[Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Pts_Projects] OFF
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   DROP TABLE [dbo].[Pts_Projects]
GO

sp_rename N'[dbo].[tmp_Pts_Projects]', N'Pts_Projects'

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_Projects] ADD CONSTRAINT [PK_Pts_Projects] PRIMARY KEY CLUSTERED ([ProjectID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_Projects Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_Projects Table'
END
GO


--
-- Script To Update dbo.Pts_DbDirectory Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_DbDirectory Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemFieldEditableState_Pts_DbDirectory')
      ALTER TABLE [dbo].[Pts_ProblemFieldEditableState] DROP CONSTRAINT [FK_Pts_ProblemFieldEditableState_Pts_DbDirectory]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemFieldEditableGroup_Pts_DbDirectory')
      ALTER TABLE [dbo].[Pts_ProblemFieldEditableWorkgroup] DROP CONSTRAINT [FK_Pts_ProblemFieldEditableGroup_Pts_DbDirectory]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemFieldReadableGroup_Pts_DbDirectory')
      ALTER TABLE [dbo].[Pts_ProblemFieldReadableWorkgroup] DROP CONSTRAINT [FK_Pts_ProblemFieldReadableGroup_Pts_DbDirectory]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_DbDirectory_Pts_Projects')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [FK_Pts_DbDirectory_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_DbDirectory_ProjectID')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [DF_Pts_DbDirectory_ProjectID]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_DbDirectory_Hidden')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [DF_Pts_DbDirectory_Hidden]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_DbDirectory_InUse')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [DF_Pts_DbDirectory_InUse]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_DbDirectory_ListOrder')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [DF_Pts_DbDirectory_ListOrder]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_DbDirectory_Required')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [DF_Pts_DbDirectory_Required]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_DbDirectory_HtmlEdit')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [DF_Pts_DbDirectory_HtmlEdit]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_DbDirectory_MaxLength')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [DF_Pts_DbDirectory_MaxLength]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_DbDirectory_UseSelectList')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [DF_Pts_DbDirectory_UseSelectList]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_DbDirectory_UseGlobalSelect')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [DF_Pts_DbDirectory_UseGlobalSelect]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_DbDirectory_ReadableToAll')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [DF_Pts_DbDirectory_ReadableToAll]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_DbDirectory_EditableToAll')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [DF_Pts_DbDirectory_EditableToAll]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_DbDirectory_IsSystem')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [DF_Pts_DbDirectory_IsSystem]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_DbDirectory_VisibleWhenNew')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [DF_Pts_DbDirectory_VisibleWhenNew]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_DbDirectory_VisibleWhenEdit')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [DF_Pts_DbDirectory_VisibleWhenEdit]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_DbDirectory_VisibleInList')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [DF_Pts_DbDirectory_VisibleInList]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_DbDirectory_VisibleToCreator')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [DF_Pts_DbDirectory_VisibleToCreator]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_DbDirectory_EditableToCreateUser')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [DF_Pts_DbDirectory_EditableToCreateUser]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   CREATE TABLE [dbo].[tmp_Pts_DbDirectory] (
   [ID] [int] IDENTITY (1, 1) NOT NULL,
   [ProjectID] [int] NOT NULL CONSTRAINT [DF_Pts_DbDirectory_ProjectID] DEFAULT (1),
   [FieldName] [varchar] (60) COLLATE Chinese_PRC_CI_AS NOT NULL,
   [Label] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NOT NULL,
   [FieldType] [varchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL,
   [Hidden] [bit] NOT NULL CONSTRAINT [DF_Pts_DbDirectory_Hidden] DEFAULT (0),
   [InUse] [bit] NOT NULL CONSTRAINT [DF_Pts_DbDirectory_InUse] DEFAULT (0),
   [ListOrder] [int] NOT NULL CONSTRAINT [DF_Pts_DbDirectory_ListOrder] DEFAULT (1000),
   [Required] [bit] NOT NULL CONSTRAINT [DF_Pts_DbDirectory_Required] DEFAULT (1),
   [DefaultValue] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [SelectItems] [nvarchar] (2000) COLLATE Chinese_PRC_CI_AS NULL,
   [FieldHelp] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [FormatString] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [HtmlEdit] [bit] NULL CONSTRAINT [DF_Pts_DbDirectory_HtmlEdit] DEFAULT (0),
   [MaxLength] [int] NULL CONSTRAINT [DF_Pts_DbDirectory_MaxLength] DEFAULT (0),
   [Width] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
   [Height] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
   [GlobalSelectID] [int] NULL,
   [UseSelectList] [bit] NULL CONSTRAINT [DF_Pts_DbDirectory_UseSelectList] DEFAULT (0),
   [UseGlobalSelect] [bit] NOT NULL CONSTRAINT [DF_Pts_DbDirectory_UseGlobalSelect] DEFAULT (0),
   [ReadableToAll] [bit] NOT NULL CONSTRAINT [DF_Pts_DbDirectory_ReadableToAll] DEFAULT (1),
   [EditableToAll] [bit] NOT NULL CONSTRAINT [DF_Pts_DbDirectory_EditableToAll] DEFAULT (1),
   [IsSystem] [bit] NOT NULL CONSTRAINT [DF_Pts_DbDirectory_IsSystem] DEFAULT (0),
   [VisibleWhenNew] [bit] NOT NULL CONSTRAINT [DF_Pts_DbDirectory_VisibleWhenNew] DEFAULT (1),
   [VisibleWhenEdit] [bit] NOT NULL CONSTRAINT [DF_Pts_DbDirectory_VisibleWhenEdit] DEFAULT (1),
   [VisibleInList] [bit] NOT NULL CONSTRAINT [DF_Pts_DbDirectory_VisibleInList] DEFAULT (0),
   [ReadableToCreateUser] [bit] NOT NULL CONSTRAINT [DF_Pts_DbDirectory_VisibleToCreator] DEFAULT (1),
   [EditableToCreateUser] [bit] NOT NULL CONSTRAINT [DF_Pts_DbDirectory_EditableToCreateUser] DEFAULT (1),
   [ExtFieldConfigData] [nvarchar] (2000) COLLATE Chinese_PRC_CI_AS NULL,
   [ExtFieldType] [nvarchar] (500) COLLATE Chinese_PRC_CI_AS NULL,
   [ValidationExpression] [nvarchar] (2000) COLLATE Chinese_PRC_CI_AS NULL,
   [EnableFieldQuickSearch] [bit] NOT NULL CONSTRAINT [DF_Pts_DbDirectory_EnableFieldQuickSearch] DEFAULT (0),
   [EnableCollapseDisplay] [bit] NOT NULL CONSTRAINT [DF_Pts_DbDirectory_EnableCollapseDisplay] DEFAULT (0),
   [ShowTime] [bit] NOT NULL CONSTRAINT [DF_Pts_DbDirectory_ShowTime] DEFAULT (1),
   [QuickWords] [ntext] COLLATE Chinese_PRC_CI_AS NULL
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Pts_DbDirectory] ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   INSERT INTO [dbo].[tmp_Pts_DbDirectory] ([ID], [ProjectID], [FieldName], [Label], [FieldType], [Hidden], [InUse], [ListOrder], [Required], [DefaultValue], [SelectItems], [FieldHelp], [FormatString], [HtmlEdit], [MaxLength], [Width], [Height], [GlobalSelectID], [UseSelectList], [UseGlobalSelect], [ReadableToAll], [EditableToAll], [IsSystem], [VisibleWhenNew], [VisibleWhenEdit], [VisibleInList], [ReadableToCreateUser], [EditableToCreateUser], [ExtFieldConfigData], [ExtFieldType], [ValidationExpression], [QuickWords])
   SELECT [ID], [ProjectID], [FieldName], [Label], [FieldType], [Hidden], [InUse], [ListOrder], [Required], [DefaultValue], [SelectItems], [FieldHelp], [FormatString], [HtmlEdit], [MaxLength], [Width], [Height], [GlobalSelectID], [UseSelectList], [UseGlobalSelect], [ReadableToAll], [EditableToAll], [IsSystem], [VisibleWhenNew], [VisibleWhenEdit], [VisibleInList], [ReadableToCreateUser], [EditableToCreateUser], NULL, NULL, NULL, NULL
   FROM [dbo].[Pts_DbDirectory]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Pts_DbDirectory] OFF
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   DROP TABLE [dbo].[Pts_DbDirectory]
GO

sp_rename N'[dbo].[tmp_Pts_DbDirectory]', N'Pts_DbDirectory'

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_DbDirectory] ADD CONSTRAINT [PK_Pts_DbDirectory] PRIMARY KEY CLUSTERED ([ID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   exec sp_addextendedproperty  N'MS_Description', N'全局选择类型，所对应的全局选择项', 'User', N'dbo', 'Table', N'Pts_DbDirectory', 'Column', N'GlobalSelectID'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   exec sp_addextendedproperty  N'MS_Description', N'是否是系统字段', 'User', N'dbo', 'Table', N'Pts_DbDirectory', 'Column', N'IsSystem'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   exec sp_addextendedproperty  N'MS_Description', N'', 'User', N'dbo', 'Table', N'Pts_DbDirectory'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   exec sp_addextendedproperty  N'MS_Description', N'使用选择列表', 'User', N'dbo', 'Table', N'Pts_DbDirectory', 'Column', N'UseSelectList'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   exec sp_addextendedproperty  N'MS_Description', N'编辑时是否可见', 'User', N'dbo', 'Table', N'Pts_DbDirectory', 'Column', N'VisibleWhenEdit'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   exec sp_addextendedproperty  N'MS_Description', N'创建时是否可见', 'User', N'dbo', 'Table', N'Pts_DbDirectory', 'Column', N'VisibleWhenNew'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_DbDirectory Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_DbDirectory Table'
END
GO


--
-- Script To Create dbo.Pts_FavoriteLinks Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.Pts_FavoriteLinks Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO

CREATE TABLE [dbo].[Pts_FavoriteLinks] (
   [Id] [int] IDENTITY (1, 1) NOT NULL,
   [UserID] [int] NOT NULL,
   [Title] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NOT NULL,
   [Link] [nvarchar] (1024) COLLATE Chinese_PRC_CI_AS NOT NULL,
   [Category] [int] NULL,
   [CreateTime] [datetime] NULL,
   [ListOrder] [int] NOT NULL CONSTRAINT [DF_Pts_FavoriteLinks_ListOrder] DEFAULT (0)
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_FavoriteLinks] ADD CONSTRAINT [PK_Pts_FavoriteLinks] PRIMARY KEY CLUSTERED ([Id])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_FavoriteLinks Table Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.Pts_FavoriteLinks Table'
END
GO

--
-- Script To Create dbo.Pts_IncomingSms Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.Pts_IncomingSms Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO

CREATE TABLE [dbo].[Pts_IncomingSms] (
   [ID] [int] IDENTITY (1, 1) NOT NULL,
   [RecvTime] [datetime] NULL,
   [SrcPhone] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NULL,
   [Content] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL,
   [SmsTime] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NULL,
   [IsAutoProcessed] [bit] NOT NULL CONSTRAINT [DF_Pts_IncomingSms_IsAutoProcessed] DEFAULT (0),
   [IsReviewed] [bit] NOT NULL CONSTRAINT [DF_Pts_IncomingSms_IsReviewed] DEFAULT (0),
   [ReviewedBy] [nvarchar] (15) COLLATE Chinese_PRC_CI_AS NULL,
   [Mark] [nvarchar] (15) COLLATE Chinese_PRC_CI_AS NULL,
   [ReviewTime] [datetime] NULL,
   [ProblemID] [int] NULL
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_IncomingSms] ADD CONSTRAINT [PK_Pts_IncomingSms] PRIMARY KEY CLUSTERED ([ID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_IncomingSms Table Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.Pts_IncomingSms Table'
END
GO

--
-- Script To Update dbo.Pts_Notify Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_Notify Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Notify_EventTime')
      ALTER TABLE [dbo].[Pts_Notify] DROP CONSTRAINT [DF_Pts_Notify_EventTime]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   CREATE TABLE [dbo].[tmp_Pts_Notify] (
   [NotifyID] [int] IDENTITY (1, 1) NOT NULL,
   [TargetType] [int] NOT NULL,
   [TargetID] [int] NOT NULL,
   [NotifyUsers] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [EventType] [int] NOT NULL,
   [CCMails] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [Sender] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [EventTime] [datetime] NOT NULL CONSTRAINT [DF_Pts_Notify_EventTime] DEFAULT (getdate()),
   [NotifyAssignee] [bit] NOT NULL CONSTRAINT [DF_Pts_Notify_NotifyAssignee] DEFAULT (1)
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Pts_Notify] ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   INSERT INTO [dbo].[tmp_Pts_Notify] ([NotifyID], [TargetType], [TargetID], [NotifyUsers], [EventType], [CCMails], [Sender], [EventTime])
   SELECT [NotifyID], [TargetType], [TargetID], [NotifyUsers], [EventType], [CCMails], [Sender], [EventTime]
   FROM [dbo].[Pts_Notify]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Pts_Notify] OFF
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   DROP TABLE [dbo].[Pts_Notify]
GO

sp_rename N'[dbo].[tmp_Pts_Notify]', N'Pts_Notify'

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_Notify] ADD CONSTRAINT [PK_Pts_Notify] PRIMARY KEY CLUSTERED ([NotifyID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_Notify Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_Notify Table'
END
GO

--
-- Script To Create dbo.Pts_OutConnectorNotifys Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.Pts_OutConnectorNotifys Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO

CREATE TABLE [dbo].[Pts_OutConnectorNotifys] (
   [ID] [int] IDENTITY (1, 1) NOT NULL,
   [TargetUserID] [int] NOT NULL,
   [MsgType] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
   [Message] [nvarchar] (200) COLLATE Chinese_PRC_CI_AS NULL,
   [Link] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [AddTime] [datetime] NOT NULL CONSTRAINT [DF_Pts_OutConnectorNotifys_AddTime] DEFAULT (getdate())
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_OutConnectorNotifys] ADD CONSTRAINT [PK_Pts_OutConnectorNotifys] PRIMARY KEY CLUSTERED ([ID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_OutConnectorNotifys Table Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.Pts_OutConnectorNotifys Table'
END
GO

--
-- Script To Create dbo.Pts_OutSms Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.Pts_OutSms Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO

CREATE TABLE [dbo].[Pts_OutSms] (
   [ID] [int] IDENTITY (1, 1) NOT NULL,
   [TargetPhone] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
   [Message] [nvarchar] (200) COLLATE Chinese_PRC_CI_AS NULL,
   [IsSend] [bit] NOT NULL CONSTRAINT [DF_HWSite_OutSms_IsSend] DEFAULT (0),
   [FailReason] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
   [FailTime] [datetime] NULL,
   [SubmitTime] [datetime] NOT NULL CONSTRAINT [DF_HWSite_OutSms_SubmitTime] DEFAULT (getdate())
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_OutSms] ADD CONSTRAINT [PK_HWSite_OutSms] PRIMARY KEY CLUSTERED ([ID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_OutSms Table Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.Pts_OutSms Table'
END
GO

--
-- Script To Update dbo.Pts_ProblemState Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_ProblemState Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_ProblemState]
      ADD [AssignInfoExtendedSettings] [ntext] COLLATE Chinese_PRC_CI_AS NULL
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_ProblemState Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_ProblemState Table'
END
GO

--
-- Script To Update dbo.Pts_ProblemCreateRoles Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_ProblemCreateRoles Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_ProblemCreateRoles]
      ADD [AssignInfoExtendedSettings] [ntext] COLLATE Chinese_PRC_CI_AS NULL
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_ProblemCreateRoles Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_ProblemCreateRoles Table'
END
GO

--
-- Script To Update dbo.Pts_ProblemFieldEditableState Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_ProblemFieldEditableState Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemFieldEditableState_Pts_DbDirectory')
      ALTER TABLE [dbo].[Pts_ProblemFieldEditableState] DROP CONSTRAINT [FK_Pts_ProblemFieldEditableState_Pts_DbDirectory]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   CREATE TABLE [dbo].[tmp_Pts_ProblemFieldEditableState] (
   [ID] [int] IDENTITY (1, 1) NOT NULL,
   [FieldID] [int] NOT NULL,
   [FromStateID] [int] NOT NULL,
   [ToStateID] [int] NOT NULL,
   [IsRequired] [bit] NOT NULL CONSTRAINT [DF_Pts_ProblemFieldEditableState_IsRequired] DEFAULT (0)
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Pts_ProblemFieldEditableState] ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   INSERT INTO [dbo].[tmp_Pts_ProblemFieldEditableState] ([ID], [FieldID], [FromStateID], [ToStateID])
   SELECT [ID], [FieldID], [FromStateID], [ToStateID]
   FROM [dbo].[Pts_ProblemFieldEditableState]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Pts_ProblemFieldEditableState] OFF
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   DROP TABLE [dbo].[Pts_ProblemFieldEditableState]
GO

sp_rename N'[dbo].[tmp_Pts_ProblemFieldEditableState]', N'Pts_ProblemFieldEditableState'

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_ProblemFieldEditableState] ADD CONSTRAINT [PK_Pts_ProblemFieldEditableState] PRIMARY KEY CLUSTERED ([ID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   exec sp_addextendedproperty  N'MS_Description', N'字段在哪些状态更改下可以编辑。可以同时指定源状态和目标状态，也可以只指定一个状态', 'User', N'dbo', 'Table', N'Pts_ProblemFieldEditableState'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_ProblemFieldEditableState Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_ProblemFieldEditableState Table'
END
GO


--
-- Script To Create dbo.Pts_ProblemFieldHiddenState Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.Pts_ProblemFieldHiddenState Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO

CREATE TABLE [dbo].[Pts_ProblemFieldHiddenState] (
   [ID] [int] IDENTITY (1, 1) NOT NULL,
   [ProblemStateID] [int] NOT NULL,
   [FieldID] [int] NOT NULL,
   [Workgroups] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_ProblemFieldHiddenState] ADD CONSTRAINT [PK_Pts_ProblemFieldHiddenState] PRIMARY KEY CLUSTERED ([ID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_ProblemFieldHiddenState Table Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.Pts_ProblemFieldHiddenState Table'
END
GO

--
-- Script To Create dbo.Pts_ProblemFieldHighlightDisplayState Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.Pts_ProblemFieldHighlightDisplayState Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO

CREATE TABLE [dbo].[Pts_ProblemFieldHighlightDisplayState] (
   [ID] [int] IDENTITY (1, 1) NOT NULL,
   [FieldID] [int] NOT NULL,
   [FromStateID] [int] NOT NULL,
   [ToStateID] [int] NOT NULL
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_ProblemFieldHighlightDisplayState] ADD CONSTRAINT [PK_Pts_ProblemFieldHighlightDisplayState] PRIMARY KEY CLUSTERED ([ID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   exec sp_addextendedproperty  N'MS_Description', N'哪些状态下需要突出显示字段', 'User', N'dbo', 'Table', N'Pts_ProblemFieldHighlightDisplayState'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_ProblemFieldHighlightDisplayState Table Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.Pts_ProblemFieldHighlightDisplayState Table'
END
GO

--
-- Script To Update dbo.Pts_Problems Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_Problems Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemAttachments_Pts_Problems')
      ALTER TABLE [dbo].[Pts_ProblemAttachments] DROP CONSTRAINT [FK_Pts_ProblemAttachments_Pts_Problems]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemHistory_Pts_Problems')
      ALTER TABLE [dbo].[Pts_ProblemHistory] DROP CONSTRAINT [FK_Pts_ProblemHistory_Pts_Problems]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemRelations_Pts_Problems')
      ALTER TABLE [dbo].[Pts_ProblemRelations] DROP CONSTRAINT [FK_Pts_ProblemRelations_Pts_Problems]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemRelations_Pts_Problems1')
      ALTER TABLE [dbo].[Pts_ProblemRelations] DROP CONSTRAINT [FK_Pts_ProblemRelations_Pts_Problems1]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemStateHistory_Pts_Problems')
      ALTER TABLE [dbo].[Pts_ProblemStateRecord] DROP CONSTRAINT [FK_Pts_ProblemStateHistory_Pts_Problems]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Records_Pts_Problems')
      ALTER TABLE [dbo].[Pts_Records] DROP CONSTRAINT [FK_Pts_Records_Pts_Problems]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Accounts_Users')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [FK_Pts_Problems_Accounts_Users]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_ProblemCatalogs')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [FK_Pts_Problems_Pts_ProblemCatalogs]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_ProblemPriority')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [FK_Pts_Problems_Pts_ProblemPriority]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_ProblemSeverities')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [FK_Pts_Problems_Pts_ProblemSeverities]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_ProblemState')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [FK_Pts_Problems_Pts_ProblemState]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_ProblemType')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [FK_Pts_Problems_Pts_ProblemType]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_Projects')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [FK_Pts_Problems_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_Projects1')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [FK_Pts_Problems_Pts_Projects1]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Problems_OriginProjectID')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [DF_Pts_Problems_OriginProjectID]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Problems_ProblemIndex')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [DF_Pts_Problems_ProblemIndex]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Problems_CreateTime')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [DF_Pts_Problems_CreateTime]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Problems_RecordNum')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [DF_Pts_Problems_RecordNum]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Problems_ReadByAssignee')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [DF_Pts_Problems_ReadByAssignee]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   CREATE TABLE [dbo].[tmp_Pts_Problems] (
   [ProblemID] [int] IDENTITY (1, 1) NOT NULL,
   [ProjectID] [int] NOT NULL,
   [OriginProjectID] [int] NOT NULL CONSTRAINT [DF_Pts_Problems_OriginProjectID] DEFAULT (0),
   [ProblemIndex] [int] NOT NULL CONSTRAINT [DF_Pts_Problems_ProblemIndex] DEFAULT (1),
   [ProblemCatalogID] [int] NULL,
   [ProblemTypeID] [int] NULL,
   [ProblemStateID] [int] NULL,
   [ProblemSeverityID] [int] NULL,
   [ProblemPriorityID] [int] NULL,
   [CreateTime] [datetime] NOT NULL CONSTRAINT [DF_Pts_Problems_CreateTime] DEFAULT (getdate()),
   [CloseTime] [datetime] NULL,
   [CreateUserID] [int] NOT NULL,
   [Deadline] [datetime] NULL,
   [AssignedTo] [int] NULL,
   [Title] [nvarchar] (250) COLLATE Chinese_PRC_CI_AS NOT NULL,
   [Description] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [RecordNum] [smallint] NOT NULL CONSTRAINT [DF_Pts_Problems_RecordNum] DEFAULT (0),
   [LastUser] [int] NULL,
   [LastTime] [datetime] NULL,
   [LastRecord] [int] NULL,
   [HistoryID] [int] NULL,
   [Int1] [int] NULL,
   [Int2] [int] NULL,
   [Int3] [int] NULL,
   [Int4] [int] NULL,
   [Int5] [int] NULL,
   [Float1] [float] NULL,
   [Float2] [float] NULL,
   [Float3] [float] NULL,
   [Float4] [float] NULL,
   [Float5] [float] NULL,
   [Date1] [datetime] NULL,
   [Date2] [datetime] NULL,
   [Date3] [datetime] NULL,
   [Date4] [datetime] NULL,
   [Date5] [datetime] NULL,
   [Date6] [datetime] NULL,
   [Date7] [datetime] NULL,
   [Date8] [datetime] NULL,
   [Date9] [datetime] NULL,
   [Date10] [datetime] NULL,
   [BigText1] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [BigText2] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [BigText3] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [BigText4] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [BigText5] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [Text1] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Text2] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Text3] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Text4] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Text5] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Select1] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Select2] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Select3] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Select4] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Select5] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Select6] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Select7] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Select8] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Select9] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Select10] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [YesNo1] [bit] NULL,
   [YesNo2] [bit] NULL,
   [YesNo3] [bit] NULL,
   [YesNo4] [bit] NULL,
   [YesNo5] [bit] NULL,
   [Url1] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Url2] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Url3] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Url4] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Url5] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [BigText6] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [BigText7] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [BigText8] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [BigText9] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [BigText10] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [Text6] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Text7] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Text8] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Text9] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Text10] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Text11] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Text12] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Text13] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Text14] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Text15] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Text16] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Text17] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Text18] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Text19] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Text20] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Select11] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Select12] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Select13] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Select14] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Select15] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Email1] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Email2] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Email3] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Email4] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Email5] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect1] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect2] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect3] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect4] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect5] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [ArticleID] [int] NULL,
   [StartTime] [datetime] NULL,
   [Int6] [int] NULL,
   [Int7] [int] NULL,
   [Int8] [int] NULL,
   [Int9] [int] NULL,
   [Int10] [int] NULL,
   [Int11] [int] NULL,
   [Int12] [int] NULL,
   [Int13] [int] NULL,
   [Int14] [int] NULL,
   [Int15] [int] NULL,
   [Int16] [int] NULL,
   [Int17] [int] NULL,
   [Int18] [int] NULL,
   [Int19] [int] NULL,
   [Int20] [int] NULL,
   [Float6] [float] NULL,
   [Float7] [float] NULL,
   [Float8] [float] NULL,
   [Float9] [float] NULL,
   [Float10] [float] NULL,
   [Float11] [float] NULL,
   [Float12] [float] NULL,
   [Float13] [float] NULL,
   [Float14] [float] NULL,
   [Float15] [float] NULL,
   [Float16] [float] NULL,
   [Float17] [float] NULL,
   [Float18] [float] NULL,
   [Float19] [float] NULL,
   [Float20] [float] NULL,
   [Date11] [datetime] NULL,
   [Date12] [datetime] NULL,
   [Date13] [datetime] NULL,
   [Date14] [datetime] NULL,
   [Date15] [datetime] NULL,
   [Date16] [datetime] NULL,
   [Date17] [datetime] NULL,
   [Date18] [datetime] NULL,
   [Date19] [datetime] NULL,
   [Date20] [datetime] NULL,
   [BigText11] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [BigText12] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [BigText13] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [BigText14] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [BigText15] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [BigText16] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [BigText17] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [BigText18] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [BigText19] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [BigText20] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [Select16] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Select17] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Select18] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Select19] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Select20] [nvarchar] (80) COLLATE Chinese_PRC_CI_AS NULL,
   [Email6] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Email7] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Email8] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Email9] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Email10] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect6] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect7] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect8] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect9] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect10] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [YesNo6] [bit] NULL,
   [YesNo7] [bit] NULL,
   [YesNo8] [bit] NULL,
   [YesNo9] [bit] NULL,
   [YesNo10] [bit] NULL,
   [YesNo11] [bit] NULL,
   [YesNo12] [bit] NULL,
   [YesNo13] [bit] NULL,
   [YesNo14] [bit] NULL,
   [YesNo15] [bit] NULL,
   [YesNo16] [bit] NULL,
   [YesNo17] [bit] NULL,
   [YesNo18] [bit] NULL,
   [YesNo19] [bit] NULL,
   [YesNo20] [bit] NULL,
   [Url6] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Url7] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Url8] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Url9] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [Url10] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect11] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect12] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect13] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect14] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect15] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect16] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect17] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect18] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect19] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [MultiSelect20] [nvarchar] (2048) COLLATE Chinese_PRC_CI_AS NULL,
   [ReadByAssignee] [bit] NOT NULL CONSTRAINT [DF_Pts_Problems_ReadByAssignee] DEFAULT (0),
   [AssigneeReadTime] [datetime] NULL,
   [IsCoworking] [bit] NOT NULL CONSTRAINT [DF_Pts_Problems_IsCoworking] DEFAULT (0),
   [CoworkInfo] [nvarchar] (500) COLLATE Chinese_PRC_CI_AS NULL,
   [CustomInputField1] [nvarchar] (1024) COLLATE Chinese_PRC_CI_AS NULL,
   [CustomInputField2] [nvarchar] (1024) COLLATE Chinese_PRC_CI_AS NULL,
   [CustomInputField3] [nvarchar] (1024) COLLATE Chinese_PRC_CI_AS NULL,
   [CustomInputField4] [nvarchar] (1024) COLLATE Chinese_PRC_CI_AS NULL,
   [CustomInputField5] [nvarchar] (1024) COLLATE Chinese_PRC_CI_AS NULL,
   [ExtField1] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [ExtField2] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [ExtField3] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [ExtField4] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [ExtField5] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [AttachNum] [int] NOT NULL CONSTRAINT [DF_Pts_Problems_AttachNum] DEFAULT (0),
   [StateDeadline] [datetime] NULL,
   [ParentID] [int] NOT NULL CONSTRAINT [DF_Pts_Problems_ParentID] DEFAULT (0),
   [TotalChildNum] [int] NOT NULL CONSTRAINT [DF_Pts_Problems_TotalChildNum] DEFAULT (0),
   [ClosedChildNum] [int] NOT NULL CONSTRAINT [DF_Pts_Problems_ClosedChildNum] DEFAULT (0),
   [IsActive] [bit] NOT NULL CONSTRAINT [DF_Pts_Problems_IsActive] DEFAULT (1),
   [RealStartTime] [datetime] NULL,
   [RealEndTime] [datetime] NULL
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Pts_Problems] ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   INSERT INTO [dbo].[tmp_Pts_Problems] ([ProblemID], [ProjectID], [OriginProjectID], [ProblemIndex], [ProblemCatalogID], [ProblemTypeID], [ProblemStateID], [ProblemSeverityID], [ProblemPriorityID], [CreateTime], [CloseTime], [CreateUserID], [Deadline], [AssignedTo], [Title], [Description], [RecordNum], [LastUser], [LastTime], [LastRecord], [HistoryID], [Int1], [Int2], [Int3], [Int4], [Int5], [Float1], [Float2], [Float3], [Float4], [Float5], [Date1], [Date2], [Date3], [Date4], [Date5], [Date6], [Date7], [Date8], [Date9], [Date10], [BigText1], [BigText2], [BigText3], [BigText4], [BigText5], [Text1], [Text2], [Text3], [Text4], [Text5], [Select1], [Select2], [Select3], [Select4], [Select5], [Select6], [Select7], [Select8], [Select9], [Select10], [YesNo1], [YesNo2], [YesNo3], [YesNo4], [YesNo5], [Url1], [Url2], [Url3], [Url4], [Url5], [BigText6], [BigText7], [BigText8], [BigText9], [BigText10], [Text6], [Text7], [Text8], [Text9], [Text10], [Text11], [Text12], [Text13], [Text14], [Text15], [Text16], [Text17], [Text18], [Text19], [Text20], [Select11], [Select12], [Select13], [Select14], [Select15], [Email1], [Email2], [Email3], [Email4], [Email5], [MultiSelect1], [MultiSelect2], [MultiSelect3], [MultiSelect4], [MultiSelect5], [ArticleID], [StartTime], [Int6], [Int7], [Int8], [Int9], [Int10], [Int11], [Int12], [Int13], [Int14], [Int15], [Int16], [Int17], [Int18], [Int19], [Int20], [Float6], [Float7], [Float8], [Float9], [Float10], [Float11], [Float12], [Float13], [Float14], [Float15], [Float16], [Float17], [Float18], [Float19], [Float20], [Date11], [Date12], [Date13], [Date14], [Date15], [Date16], [Date17], [Date18], [Date19], [Date20], [BigText11], [BigText12], [BigText13], [BigText14], [BigText15], [BigText16], [BigText17], [BigText18], [BigText19], [BigText20], [Select16], [Select17], [Select18], [Select19], [Select20], [Email6], [Email7], [Email8], [Email9], [Email10], [MultiSelect6], [MultiSelect7], [MultiSelect8], [MultiSelect9], [MultiSelect10], [YesNo6], [YesNo7], [YesNo8], [YesNo9], [YesNo10], [YesNo11], [YesNo12], [YesNo13], [YesNo14], [YesNo15], [YesNo16], [YesNo17], [YesNo18], [YesNo19], [YesNo20], [Url6], [Url7], [Url8], [Url9], [Url10], [MultiSelect11], [MultiSelect12], [MultiSelect13], [MultiSelect14], [MultiSelect15], [MultiSelect16], [MultiSelect17], [MultiSelect18], [MultiSelect19], [MultiSelect20], [ReadByAssignee], [AssigneeReadTime], [CoworkInfo], [CustomInputField1], [CustomInputField2], [CustomInputField3], [CustomInputField4], [CustomInputField5], [ExtField1], [ExtField2], [ExtField3], [ExtField4], [ExtField5], [StateDeadline], [RealStartTime], [RealEndTime])
   SELECT [ProblemID], [ProjectID], [OriginProjectID], [ProblemIndex], [ProblemCatalogID], [ProblemTypeID], [ProblemStateID], [ProblemSeverityID], [ProblemPriorityID], [CreateTime], [CloseTime], [CreateUserID], [Deadline], [AssignedTo], [Title], [Description], [RecordNum], [LastUser], [LastTime], [LastRecord], [HistoryID], [Int1], [Int2], [Int3], [Int4], [Int5], [Float1], [Float2], [Float3], [Float4], [Float5], [Date1], [Date2], [Date3], [Date4], [Date5], [Date6], [Date7], [Date8], [Date9], [Date10], [BigText1], [BigText2], [BigText3], [BigText4], [BigText5], [Text1], [Text2], [Text3], [Text4], [Text5], [Select1], [Select2], [Select3], [Select4], [Select5], [Select6], [Select7], [Select8], [Select9], [Select10], [YesNo1], [YesNo2], [YesNo3], [YesNo4], [YesNo5], [Url1], [Url2], [Url3], [Url4], [Url5], [BigText6], [BigText7], [BigText8], [BigText9], [BigText10], [Text6], [Text7], [Text8], [Text9], [Text10], [Text11], [Text12], [Text13], [Text14], [Text15], [Text16], [Text17], [Text18], [Text19], [Text20], [Select11], [Select12], [Select13], [Select14], [Select15], [Email1], [Email2], [Email3], [Email4], [Email5], [MultiSelect1], [MultiSelect2], [MultiSelect3], [MultiSelect4], [MultiSelect5], [ArticleID], [StartTime], [Int6], [Int7], [Int8], [Int9], [Int10], [Int11], [Int12], [Int13], [Int14], [Int15], [Int16], [Int17], [Int18], [Int19], [Int20], [Float6], [Float7], [Float8], [Float9], [Float10], [Float11], [Float12], [Float13], [Float14], [Float15], [Float16], [Float17], [Float18], [Float19], [Float20], [Date11], [Date12], [Date13], [Date14], [Date15], [Date16], [Date17], [Date18], [Date19], [Date20], [BigText11], [BigText12], [BigText13], [BigText14], [BigText15], [BigText16], [BigText17], [BigText18], [BigText19], [BigText20], [Select16], [Select17], [Select18], [Select19], [Select20], [Email6], [Email7], [Email8], [Email9], [Email10], [MultiSelect6], [MultiSelect7], [MultiSelect8], [MultiSelect9], [MultiSelect10], [YesNo6], [YesNo7], [YesNo8], [YesNo9], [YesNo10], [YesNo11], [YesNo12], [YesNo13], [YesNo14], [YesNo15], [YesNo16], [YesNo17], [YesNo18], [YesNo19], [YesNo20], [Url6], [Url7], [Url8], [Url9], [Url10], [MultiSelect11], [MultiSelect12], [MultiSelect13], [MultiSelect14], [MultiSelect15], [MultiSelect16], [MultiSelect17], [MultiSelect18], [MultiSelect19], [MultiSelect20], [ReadByAssignee], [AssigneeReadTime], NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
   FROM [dbo].[Pts_Problems]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Pts_Problems] OFF
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   DROP TABLE [dbo].[Pts_Problems]
GO

sp_rename N'[dbo].[tmp_Pts_Problems]', N'Pts_Problems'

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_Problems] ADD CONSTRAINT [PK_Pts_Problems] PRIMARY KEY CLUSTERED ([ProblemID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   CREATE INDEX [IX_Pts_Problems] ON [dbo].[Pts_Problems] ([ProjectID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   CREATE INDEX [IX_Pts_Problems_AssignedTo] ON [dbo].[Pts_Problems] ([AssignedTo])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   CREATE INDEX [IX_Pts_Problems_CreateTime] ON [dbo].[Pts_Problems] ([CreateTime])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   CREATE INDEX [IX_Pts_Problems_CreateUser] ON [dbo].[Pts_Problems] ([CreateUserID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   exec sp_addextendedproperty  N'MS_Description', N'父事务ID', 'User', N'dbo', 'Table', N'Pts_Problems', 'Column', N'ParentID'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_Problems Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_Problems Table'
END
GO


--
-- Script To Create dbo.Pts_ProblemSequenceRelation Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.Pts_ProblemSequenceRelation Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO

CREATE TABLE [dbo].[Pts_ProblemSequenceRelation] (
   [PrecedingProblemID] [int] NOT NULL,
   [SucceedingProblemID] [int] NOT NULL
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_ProblemSequenceRelation Table Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.Pts_ProblemSequenceRelation Table'
END
GO

--
-- Script To Update dbo.Pts_ProblemStateRecord Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_ProblemStateRecord Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_ProblemStateRecord]
      ADD [BizKeepTime] [int] NULL
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_ProblemStateRecord Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_ProblemStateRecord Table'
END
GO

--
-- Script To Create dbo.Pts_ProblemStateTimeLimitRoleItems Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.Pts_ProblemStateTimeLimitRoleItems Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO

CREATE TABLE [dbo].[Pts_ProblemStateTimeLimitRoleItems] (
   [ID] [int] IDENTITY (1, 1) NOT NULL,
   [ProblemStateID] [int] NOT NULL,
   [ListOrder] [int] NOT NULL CONSTRAINT [DF_Pts_ProblemStateTimeLimitRoleItems_ListOrder] DEFAULT (0),
   [Note] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL,
   [Settings] [nvarchar] (1024) COLLATE Chinese_PRC_CI_AS NULL,
   [TimeSpan] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_ProblemStateTimeLimitRoleItems] ADD CONSTRAINT [PK_Pts_ProblemStateTimeLimitRoleItems] PRIMARY KEY CLUSTERED ([ID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_ProblemStateTimeLimitRoleItems Table Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.Pts_ProblemStateTimeLimitRoleItems Table'
END
GO

--
-- Script To Delete dbo.Pts_ProblemTimers Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Deleting dbo.Pts_ProblemTimers Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   DROP TABLE [dbo].[Pts_ProblemTimers]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_ProblemTimers Table Deleted Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Delete dbo.Pts_ProblemTimers Table'
END
GO

--
-- Script To Create dbo.Pts_ProblemTimers1 Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.Pts_ProblemTimers1 Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO

CREATE TABLE [dbo].[Pts_ProblemTimers1] (
   [TimerID] [int] IDENTITY (1, 1) NOT NULL,
   [ProblemID] [int] NOT NULL,
   [SettingID] [int] NOT NULL,
   [CreateTime] [datetime] NOT NULL,
   [IsDeleteAfterIssueClose] [bit] NOT NULL CONSTRAINT [DF_Pts_ProblemTimers1_IsDeleteAfterIssueClose] DEFAULT (1),
   [NextExpireTime] [datetime] NOT NULL,
   [ExecuteCount] [int] NOT NULL CONSTRAINT [DF_Pts_ProblemTimers1_ExecuteCount] DEFAULT (0),
   [LastExecuteTime] [datetime] NULL,
   [IsLastExecuteOK] [bit] NOT NULL,
   [LastExecuteMsg] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [ValueToCheck] [nvarchar] (500) COLLATE Chinese_PRC_CI_AS NULL
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_ProblemTimers1] ADD CONSTRAINT [PK_Pts_ProblemTimers1] PRIMARY KEY CLUSTERED ([TimerID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   exec sp_addextendedproperty  N'MS_Description', N'对应的事务ID', 'User', N'dbo', 'Table', N'Pts_ProblemTimers1', 'Column', N'ProblemID'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   exec sp_addextendedproperty  N'MS_Description', N'模板ID', 'User', N'dbo', 'Table', N'Pts_ProblemTimers1', 'Column', N'SettingID'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_ProblemTimers1 Table Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.Pts_ProblemTimers1 Table'
END
GO

--
-- Script To Update dbo.Pts_Records Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_Records Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_RecordAttachments_Pts_Records')
      ALTER TABLE [dbo].[Pts_RecordAttachments] DROP CONSTRAINT [FK_Pts_RecordAttachments_Pts_Records]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Records_Pts_Problems')
      ALTER TABLE [dbo].[Pts_Records] DROP CONSTRAINT [FK_Pts_Records_Pts_Problems]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Records_DateCreated')
      ALTER TABLE [dbo].[Pts_Records] DROP CONSTRAINT [DF_Pts_Records_DateCreated]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF__Pts_Recor__State__3EDC53F0')
      ALTER TABLE [dbo].[Pts_Records] DROP CONSTRAINT [DF__Pts_Recor__State__3EDC53F0]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'DF_Pts_Records_Type')
      ALTER TABLE [dbo].[Pts_Records] DROP CONSTRAINT [DF_Pts_Records_Type]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   CREATE TABLE [dbo].[tmp_Pts_Records] (
   [RecordID] [int] IDENTITY (1, 1) NOT NULL,
   [ProblemID] [int] NOT NULL,
   [CreateUser] [int] NOT NULL,
   [DateCreated] [datetime] NOT NULL CONSTRAINT [DF_Pts_Records_DateCreated] DEFAULT (getdate()),
   [IsKeyRecord] [bit] NOT NULL,
   [RecordType] [int] NULL,
   [AssignTo] [int] NULL,
   [Title] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NOT NULL,
   [Content] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [StateID] [int] NOT NULL CONSTRAINT [DF__Pts_Recor__State__3EDC53F0] DEFAULT (0),
   [HistoryID] [int] NULL,
   [RecordClass] [int] NOT NULL CONSTRAINT [DF_Pts_Records_Type] DEFAULT (0),
   [SrcStateID] [int] NULL,
   [SrcUserID] [int] NULL,
   [ResponseWorkTime] [int] NULL,
   [ResponseTotalTime] [int] NULL,
   [RealTimeCost] [int] NULL,
   [ExtInt1] [int] NULL,
   [ExtInt2] [int] NULL,
   [ExtInt3] [int] NULL,
   [ExtString1] [nvarchar] (1024) COLLATE Chinese_PRC_CI_AS NULL,
   [ExtString2] [nvarchar] (1024) COLLATE Chinese_PRC_CI_AS NULL,
   [ExtString3] [nvarchar] (1024) COLLATE Chinese_PRC_CI_AS NULL,
   [ExtNtext1] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [ExtNtext2] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [ParentRecordID] [int] NOT NULL CONSTRAINT [DF_Pts_Records_ParentRecordID] DEFAULT (0),
   [RepresentingUserID] [int] NULL
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Pts_Records] ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   INSERT INTO [dbo].[tmp_Pts_Records] ([RecordID], [ProblemID], [CreateUser], [DateCreated], [IsKeyRecord], [RecordType], [AssignTo], [Title], [Content], [StateID], [HistoryID], [RecordClass], [SrcStateID], [SrcUserID], [ResponseWorkTime], [ResponseTotalTime], [RealTimeCost], [ExtInt1], [ExtInt2], [ExtInt3], [ExtString1], [ExtString2], [ExtString3], [ExtNtext1], [ExtNtext2], [RepresentingUserID])
   SELECT [RecordID], [ProblemID], [CreateUser], [DateCreated], [IsKeyRecord], [RecordType], [AssignTo], [Title], [Content], [StateID], [HistoryID], [RecordClass], [SrcStateID], [SrcUserID], NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
   FROM [dbo].[Pts_Records]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Pts_Records] OFF
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   DROP TABLE [dbo].[Pts_Records]
GO

sp_rename N'[dbo].[tmp_Pts_Records]', N'Pts_Records'

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_Records] ADD CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED ([RecordID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   CREATE INDEX [IX_Pts_Records] ON [dbo].[Pts_Records] ([ProblemID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_Records Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_Records Table'
END
GO


--
-- Script To Create dbo.Pts_SetFieldValueActions Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.Pts_SetFieldValueActions Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO

CREATE TABLE [dbo].[Pts_SetFieldValueActions] (
   [FieldActionID] [int] IDENTITY (1, 1) NOT NULL,
   [ProjectID] [int] NOT NULL,
   [FieldName] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NOT NULL,
   [FieldLabel] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
   [FieldValue] [nvarchar] (1000) COLLATE Chinese_PRC_CI_AS NOT NULL,
   [FieldDisplayValue] [nvarchar] (1000) COLLATE Chinese_PRC_CI_AS NOT NULL,
   [Note] [nvarchar] (1000) COLLATE Chinese_PRC_CI_AS NOT NULL
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_SetFieldValueActions] ADD CONSTRAINT [PK_Pts_SetFieldActions] PRIMARY KEY CLUSTERED ([FieldActionID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_SetFieldValueActions Table Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.Pts_SetFieldValueActions Table'
END
GO

--
-- Script To Create dbo.Pts_StepActions Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.Pts_StepActions Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO

CREATE TABLE [dbo].[Pts_StepActions] (
   [ID] [int] IDENTITY (1, 1) NOT NULL,
   [ProjectID] [int] NOT NULL,
   [SrcStateID] [int] NOT NULL,
   [DstStateID] [int] NOT NULL,
   [SetFieldValueActionList] [nvarchar] (1000) COLLATE Chinese_PRC_CI_AS NOT NULL CONSTRAINT [DF_Pts_StepActions_SetFieldValueActionList] DEFAULT (''),
   [Note] [ntext] COLLATE Chinese_PRC_CI_AS NULL
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_StepActions] ADD CONSTRAINT [PK_Pts_StepActions] PRIMARY KEY CLUSTERED ([ID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_StepActions Table Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.Pts_StepActions Table'
END
GO

--
-- Script To Update dbo.Pts_Subscription Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_Subscription Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   CREATE TABLE [dbo].[tmp_Pts_Subscription] (
   [SubscribeID] [int] IDENTITY (1, 1) NOT NULL,
   [UserID] [int] NOT NULL,
   [TargetType] [int] NOT NULL,
   [TargetID] [int] NOT NULL,
   [EventType] [int] NOT NULL CONSTRAINT [DF_Pts_Subscription_EventType] DEFAULT (0),
   [Params] [nvarchar] (1024) COLLATE Chinese_PRC_CI_AS NULL
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Pts_Subscription] ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   INSERT INTO [dbo].[tmp_Pts_Subscription] ([SubscribeID], [UserID], [TargetType], [TargetID], [Params])
   SELECT [SubscribeID], [UserID], [TargetType], [TargetID], NULL
   FROM [dbo].[Pts_Subscription]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Pts_Subscription] OFF
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   DROP TABLE [dbo].[Pts_Subscription]
GO

sp_rename N'[dbo].[tmp_Pts_Subscription]', N'Pts_Subscription'

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_Subscription] ADD CONSTRAINT [PK_Pts_Subscribe] PRIMARY KEY CLUSTERED ([SubscribeID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_Subscription Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_Subscription Table'
END
GO

--
-- Script To Update dbo.Pts_WorkflowSteps Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_WorkflowSteps Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_WorkflowSteps_Pts_ProblemState')
      ALTER TABLE [dbo].[Pts_WorkflowSteps] DROP CONSTRAINT [FK_Pts_WorkflowSteps_Pts_ProblemState]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_WorkflowSteps_Pts_ProblemState1')
      ALTER TABLE [dbo].[Pts_WorkflowSteps] DROP CONSTRAINT [FK_Pts_WorkflowSteps_Pts_ProblemState1]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_WorkflowSteps_Pts_Projects')
      ALTER TABLE [dbo].[Pts_WorkflowSteps] DROP CONSTRAINT [FK_Pts_WorkflowSteps_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   CREATE TABLE [dbo].[tmp_Pts_WorkflowSteps] (
   [StepID] [int] IDENTITY (1, 1) NOT NULL,
   [ProjectID] [int] NOT NULL,
   [SrcStateID] [int] NOT NULL,
   [DstStateID] [int] NOT NULL,
   [StepName] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NOT NULL,
   [Description] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [AssignType] [int] NOT NULL,
   [Assignee] [int] NULL,
   [SelectableAssigneeWorkgroupIDs] [nvarchar] (1024) COLLATE Chinese_PRC_CI_AS NULL,
   [SelectableAssigneeUserIDs] [nvarchar] (1024) COLLATE Chinese_PRC_CI_AS NULL,
   [ListOrder] [int] NOT NULL CONSTRAINT [DF_Pts_WorkflowSteps_ListOrder] DEFAULT (100),
   [RecordTitle] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
   [CommonMessages] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [AssignInfoExtendedSettings] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
   [StepExtendedSettings] [ntext] COLLATE Chinese_PRC_CI_AS NULL
)
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Pts_WorkflowSteps] ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   INSERT INTO [dbo].[tmp_Pts_WorkflowSteps] ([StepID], [ProjectID], [SrcStateID], [DstStateID], [StepName], [Description], [AssignType], [Assignee], [SelectableAssigneeWorkgroupIDs], [SelectableAssigneeUserIDs], [RecordTitle], [CommonMessages], [AssignInfoExtendedSettings], [StepExtendedSettings])
   SELECT [StepID], [ProjectID], [SrcStateID], [DstStateID], [StepName], [Description], [AssignType], [Assignee], [SelectableAssigneeWorkgroupIDs], [SelectableAssigneeUserIDs], NULL, NULL, NULL, NULL
   FROM [dbo].[Pts_WorkflowSteps]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   SET IDENTITY_INSERT [dbo].[tmp_Pts_WorkflowSteps] OFF
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   DROP TABLE [dbo].[Pts_WorkflowSteps]
GO

sp_rename N'[dbo].[tmp_Pts_WorkflowSteps]', N'Pts_WorkflowSteps'

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_WorkflowSteps] ADD CONSTRAINT [PK_Pts_WorkflowSteps] PRIMARY KEY CLUSTERED ([StepID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   exec sp_addextendedproperty  N'MS_Description', N'分配类型为指定处理人时，所指定的处理人', 'User', N'dbo', 'Table', N'Pts_WorkflowSteps', 'Column', N'Assignee'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   exec sp_addextendedproperty  N'MS_Description', N'分配类型为选择处理人时，可选处理人的列表；', 'User', N'dbo', 'Table', N'Pts_WorkflowSteps', 'Column', N'SelectableAssigneeUserIDs'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   exec sp_addextendedproperty  N'MS_Description', N'分配类型为选择处理人时，可选的处理人所在的工作组', 'User', N'dbo', 'Table', N'Pts_WorkflowSteps', 'Column', N'SelectableAssigneeWorkgroupIDs'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_WorkflowSteps Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_WorkflowSteps Table'
END
GO


--
-- Script To Update dbo.Pts_Workgroups Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_Workgroups Table'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Workgroups_Pts_Projects')
      ALTER TABLE [dbo].[Pts_Workgroups] DROP CONSTRAINT [FK_Pts_Workgroups_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   ALTER TABLE [dbo].[Pts_Workgroups]
      ADD [ExtendedSettings] [ntext] COLLATE Chinese_PRC_CI_AS NULL
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_Workgroups Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_Workgroups Table'
END
GO

--
-- Script To Update dbo.v_Accounts_Users View In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.v_Accounts_Users View'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('ALTER VIEW [dbo].[v_Accounts_Users]
AS
SELECT     Accounts_Users.UserID, Accounts_Users.Password, Accounts_Users.EmailAddress, Accounts_Users.DisplayName, Accounts_Users.UserState, 
                      Accounts_Users.DeptID, Accounts_Users.Address, Accounts_Users.OfficePhone, Accounts_Users.MobilePhone, Accounts_Users.MSN, 
                      Accounts_Users.PasswordNote, Accounts_Users.Remark, Accounts_Users.AccessListID, Accounts_Users.BindIP, Accounts_Users.BindIPString, 
                      Accounts_Users.AgentUserID, Accounts_UserState.UserStateName, Accounts_Department.DeptName, (CASE WHEN DeptName IS NOT NULL 
                      THEN N''('' + DeptName + N'')'' WHEN DeptName IS NULL THEN '''' END) + Accounts_Users.DisplayName AS FullName, Accounts_Users.ExtendedSettings, 
                      Accounts_Users.LoginName, Accounts_Users.ExpireDate, Accounts_Users.ExtIntData1, Accounts_Users.ExtIntData2, 
                      Accounts_Users.ExtIntData4, Accounts_Users.ExtIntData3, Accounts_Users.ExtStrData1, Accounts_Users.ExtStrData2, 
                      Accounts_Users.Attributes
FROM         Accounts_Users INNER JOIN
                      Accounts_UserState ON Accounts_Users.UserState = Accounts_UserState.UserStateID LEFT OUTER JOIN
                      Accounts_Department ON Accounts_Users.DeptID = Accounts_Department.DeptID')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.v_Accounts_Users View Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.v_Accounts_Users View'
END
GO

--
-- Script To Delete dbo.v_Pts_ProblemCreateRoles View In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Deleting dbo.v_Pts_ProblemCreateRoles View'
GO

   DROP VIEW [dbo].[v_Pts_ProblemCreateRoles]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.v_Pts_ProblemCreateRoles View Deleted Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Delete dbo.v_Pts_ProblemCreateRoles View'
END
GO

--
-- Script To Update dbo.v_Pts_Problems View In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.v_Pts_Problems View'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('ALTER VIEW [dbo].[v_Pts_Problems]
AS
SELECT     Accounts_Users.DisplayName AS CreateUserName, Pts_Problems.ProblemID, Pts_Problems.ProjectID, Pts_Problems.OriginProjectID, 
                      Pts_Problems.ProblemIndex, Pts_Problems.ProblemCatalogID, Pts_Problems.ProblemTypeID, Pts_Problems.ProblemStateID, 
                      Pts_Problems.ProblemSeverityID, Pts_Problems.ProblemPriorityID, Pts_Problems.CreateTime, Pts_Problems.CloseTime, 
                      Pts_Problems.CreateUserID, Pts_Problems.Deadline, Pts_Problems.AssignedTo, Pts_Problems.Title, Pts_Problems.Description, 
                      Pts_Problems.RecordNum, Pts_Problems.LastUser, Pts_Problems.LastTime, Pts_Problems.LastRecord, Pts_Problems.HistoryID, 
                      Pts_Problems.Int1, Pts_Problems.Int2, Pts_Problems.Int3, Pts_Problems.Int4, Pts_Problems.Int5, Pts_Problems.Float1, 
                      Pts_Problems.Float2, Pts_Problems.Float3, Pts_Problems.Float4, Pts_Problems.Float5, Pts_Problems.Date1, Pts_Problems.Date2, 
                      Pts_Problems.Date3, Pts_Problems.Date4, Pts_Problems.Date5, Pts_Problems.Date6, Pts_Problems.Date7, Pts_Problems.Date8, 
                      Pts_Problems.Date9, Pts_Problems.Date10, Pts_Problems.BigText1, Pts_Problems.BigText2, Pts_Problems.BigText3, 
                      Pts_Problems.BigText4, Pts_Problems.BigText5, Pts_Problems.Text1, Pts_Problems.Text2, Pts_Problems.Text3, Pts_Problems.Text4, 
                      Pts_Problems.Text5, Pts_Problems.Select1, Pts_Problems.Select2, Pts_Problems.Select3, Pts_Problems.Select4, Pts_Problems.Select5, 
                      Pts_Problems.Select6, Pts_Problems.Select7, Pts_Problems.Select8, Pts_Problems.Select9, Pts_Problems.Select10, 
                      Pts_Problems.YesNo1, Pts_Problems.YesNo2, Pts_Problems.YesNo3, Pts_Problems.YesNo4, Pts_Problems.YesNo5, Pts_Problems.Url1, 
                      Pts_Problems.Url2, Pts_Problems.Url3, Pts_Problems.Url4, Pts_Problems.Url5, Pts_Problems.BigText6, Pts_Problems.BigText7, 
                      Pts_Problems.BigText8, Pts_Problems.BigText9, Pts_Problems.BigText10, Pts_Problems.Text6, Pts_Problems.Text7, Pts_Problems.Text8, 
                      Pts_Problems.Text9, Pts_Problems.Text10, Pts_Problems.Text11, Pts_Problems.Text12, Pts_Problems.Text13, Pts_Problems.Text14, 
                      Pts_Problems.Text15, Pts_Problems.Text16, Pts_Problems.Text17, Pts_Problems.Text18, Pts_Problems.Text19, Pts_Problems.Text20, 
                      Pts_Problems.Select11, Pts_Problems.Select12, Pts_Problems.Select13, Pts_Problems.Select14, Pts_Problems.Select15, 
                      Pts_Problems.Email1, Pts_Problems.Email2, Pts_Problems.Email3, Pts_Problems.Email4, Pts_Problems.Email5, 
                      Pts_Problems.MultiSelect1, Pts_Problems.MultiSelect2, Pts_Problems.MultiSelect3, Pts_Problems.MultiSelect4, Pts_Problems.MultiSelect5, 
                      Pts_Problems.ArticleID, Pts_ProblemCatalogs.GlobalOrder + Pts_ProblemCatalogs.Name AS ProblemCatalogName, 
                      Pts_ProblemType.TypeName AS ProblemTypeName, Pts_ProblemState.StateName AS ProblemStateName, 
                      Pts_ProblemPriority.PriorityName AS ProblemPriorityName, Pts_ProblemSeverity.SeverityName AS ProblemSeverityName, 
                      Pts_ProblemSeverity.ListOrder AS ProblemSeverityOrder, Pts_ProblemState.ListOrder AS ProblemStateOrder, 
                      Pts_ProblemType.ListOrder AS ProblemTypeOrder, Pts_ProblemCatalogs.ListOrder AS ProblemCatalogOrder, 
                      Pts_ProblemPriority.ListOrder AS ProblemPriorityOrder, Pts_ProblemPriority.DisplayColor AS PriorityDisplayColor, 
                      Pts_ProblemSeverity.DisplayColor AS SeverityDisplayColor, Accounts_Users_1.DisplayName AS AssignedToUserName, Pts_Projects.Name AS ProjectName, 
                      Accounts_Users_1.EmailAddress AS AssignedToEmail, Pts_ProblemCatalogs.Path AS ProblemCatalogPath, Pts_ProblemState.IsFinalState AS IsClosed, 
                      Accounts_Users_2.DisplayName AS LastUserName, Pts_Projects.ProjectCode + N''-'' + CAST(Pts_Problems.ProblemIndex AS nvarchar(10)) AS ProblemCode, 
                      Pts_Projects.ProjectCode, Pts_Problems.StartTime, Pts_Problems.Url10, Pts_Problems.Url9, Pts_Problems.Url8, Pts_Problems.Url7, 
                      Pts_Problems.Url6, Pts_Problems.YesNo20, Pts_Problems.YesNo19, Pts_Problems.YesNo18, Pts_Problems.YesNo17, 
                      Pts_Problems.YesNo16, Pts_Problems.YesNo15, Pts_Problems.YesNo14, Pts_Problems.YesNo13, Pts_Problems.YesNo12, 
                      Pts_Problems.YesNo11, Pts_Problems.YesNo10, Pts_Problems.YesNo9, Pts_Problems.YesNo8, Pts_Problems.YesNo7, 
                      Pts_Problems.YesNo6, Pts_Problems.MultiSelect10, Pts_Problems.MultiSelect9, Pts_Problems.MultiSelect8, Pts_Problems.MultiSelect7, 
                      Pts_Problems.MultiSelect6, Pts_Problems.Email10, Pts_Problems.Email9, Pts_Problems.Email8, Pts_Problems.Email7, 
                      Pts_Problems.Email6, Pts_Problems.Select20, Pts_Problems.Select19, Pts_Problems.Select18, Pts_Problems.Select17, 
                      Pts_Problems.Select16, Pts_Problems.BigText20, Pts_Problems.BigText19, Pts_Problems.BigText18, Pts_Problems.BigText17, 
                      Pts_Problems.BigText16, Pts_Problems.BigText15, Pts_Problems.BigText14, Pts_Problems.BigText13, Pts_Problems.BigText12, 
                      Pts_Problems.BigText11, Pts_Problems.Date20, Pts_Problems.Date19, Pts_Problems.Date18, Pts_Problems.Date17, Pts_Problems.Date16, 
                      Pts_Problems.Date15, Pts_Problems.Date14, Pts_Problems.Date13, Pts_Problems.Date12, Pts_Problems.Date11, Pts_Problems.Float20, 
                      Pts_Problems.Float19, Pts_Problems.Float18, Pts_Problems.Float17, Pts_Problems.Float16, Pts_Problems.Float15, Pts_Problems.Float14, 
                      Pts_Problems.Float13, Pts_Problems.Float12, Pts_Problems.Float11, Pts_Problems.Float10, Pts_Problems.Float9, Pts_Problems.Float8, 
                      Pts_Problems.Float7, Pts_Problems.Float6, Pts_Problems.Int20, Pts_Problems.Int19, Pts_Problems.Int18, Pts_Problems.Int17, 
                      Pts_Problems.Int16, Pts_Problems.Int15, Pts_Problems.Int14, Pts_Problems.Int13, Pts_Problems.Int12, Pts_Problems.Int11, 
                      Pts_Problems.Int10, Pts_Problems.Int9, Pts_Problems.Int8, Pts_Problems.Int7, Pts_Problems.Int6, Pts_Problems.MultiSelect20, 
                      Pts_Problems.MultiSelect19, Pts_Problems.MultiSelect18, Pts_Problems.MultiSelect17, Pts_Problems.MultiSelect16, 
                      Pts_Problems.MultiSelect15, Pts_Problems.MultiSelect14, Pts_Problems.MultiSelect13, Pts_Problems.MultiSelect12, 
                      Pts_Problems.MultiSelect11, Pts_Problems.ReadByAssignee, Pts_Problems.AssigneeReadTime, Pts_Problems.IsCoworking, 
                      Pts_Problems.CoworkInfo, Pts_Problems.ExtField1, Pts_Problems.ExtField2, Pts_Problems.ExtField3, Pts_Problems.ExtField4, 
                      Pts_Problems.ExtField5, Pts_Problems.CustomInputField1, Pts_Problems.CustomInputField2, Pts_Problems.CustomInputField3, 
                      Pts_Problems.CustomInputField4, Pts_Problems.CustomInputField5, Pts_Problems.AttachNum, Pts_Problems.StateDeadline, 
                      Pts_Problems.ParentID, Pts_Problems.TotalChildNum, Pts_Problems.ClosedChildNum, Pts_Problems.IsActive, Pts_Problems.RealStartTime, 
                      Pts_Problems.RealEndTime
FROM         Pts_Problems LEFT OUTER JOIN
                      Accounts_Users AS Accounts_Users_2 ON Pts_Problems.LastUser = Accounts_Users_2.UserID LEFT OUTER JOIN
                      Pts_Projects ON Pts_Problems.ProjectID = Pts_Projects.ProjectID LEFT OUTER JOIN
                      Pts_ProblemSeverity ON Pts_Problems.ProblemSeverityID = Pts_ProblemSeverity.ProblemSeverityID LEFT OUTER JOIN
                      Pts_ProblemType ON Pts_Problems.ProblemTypeID = Pts_ProblemType.ProblemTypeID LEFT OUTER JOIN
                      Pts_ProblemPriority ON Pts_Problems.ProblemPriorityID = Pts_ProblemPriority.ProblemPriorityID LEFT OUTER JOIN
                      Pts_ProblemState ON Pts_Problems.ProblemStateID = Pts_ProblemState.ProblemStateID LEFT OUTER JOIN
                      Pts_ProblemCatalogs ON Pts_Problems.ProblemCatalogID = Pts_ProblemCatalogs.ProblemCatalogID LEFT OUTER JOIN
                      Accounts_Users ON Pts_Problems.CreateUserID = Accounts_Users.UserID LEFT OUTER JOIN
                      Accounts_Users AS Accounts_Users_1 ON Pts_Problems.AssignedTo = Accounts_Users_1.UserID')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.v_Pts_Problems View Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.v_Pts_Problems View'
END
GO

--
-- Script To Update dbo.v_Pts_Projects View In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.v_Pts_Projects View'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('ALTER VIEW [dbo].[v_Pts_Projects]
AS
SELECT     Pts_Projects.ProjectID, Pts_Projects.ProjectCatalogID, Pts_Projects.ProjectCode, Pts_Projects.Name, Pts_Projects.ShortName, 
                      Pts_Projects.Description, Pts_Projects.ProjectStateID, Pts_Projects.TotalProblems, Pts_Projects.TotalThreads, Pts_Projects.TotalRecords, 
                      Pts_Projects.DateCreated, Pts_Projects.ProblemTitle, Pts_Projects.ThreadTitle, Pts_Projects.RecordTitle, Pts_Projects.OnlyViewOwnProblem, 
                      Pts_Projects.EnableProblemState, Pts_Projects.EnableMultipleThread, Pts_Projects.EnableEmailNotify, Pts_Projects.EnableProblemPriority, 
                      Pts_Projects.EnableProblemSeverity, Pts_Projects.EnableProblemType, Pts_Projects.EnableProblemCatalog, Pts_Projects.EnableProblemDeadline, 
                      Pts_Projects.VisibleToAll, Pts_Projects.EnableStateTimer, Pts_Projects.AutoAssignedTo, Pts_Projects.OpenProblems, 
                      Pts_Projects.NewProblemIndex, Pts_Projects.ExtendedSettings, Pts_ProjectStates.Name AS StateName, Pts_ProjectStates.IsCloseState AS IsClosed, 
                      Pts_Projects.SecurityLevel, Pts_Projects.KeyWords, Pts_Projects.ListOrder
FROM         Pts_Projects INNER JOIN
                      Pts_ProjectStates ON Pts_Projects.ProjectStateID = Pts_ProjectStates.ProjectStateID')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.v_Pts_Projects View Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.v_Pts_Projects View'
END
GO

--
-- Script To Update dbo.v_Pts_Records View In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.v_Pts_Records View'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('ALTER VIEW [dbo].[v_Pts_Records]
AS
SELECT     Pts_Records.RecordID, Pts_Records.ProblemID, Pts_Records.CreateUser, Pts_Records.DateCreated, Pts_Records.IsKeyRecord, 
                      Pts_Records.RecordType, Pts_Records.AssignTo, Pts_Records.Title, Pts_Records.[Content], Pts_Records.StateID, Pts_Records.HistoryID, 
                      Accounts_Users.DisplayName AS CreateUserName, Accounts_Users_1.DisplayName AS AssignToUserName, 
                      Pts_ProblemState.StateName AS ProblemStateName, Pts_Records.RecordClass, Pts_Records.SrcStateID, Pts_Records.SrcUserID, 
                      Pts_Records.ResponseWorkTime, Pts_Records.ResponseTotalTime, Pts_Records.RealTimeCost, Pts_Records.ExtInt1, Pts_Records.ExtInt2, 
                      Pts_Records.ExtInt3, Pts_Records.ExtString1, Pts_Records.ExtString2, Pts_Records.ExtString3, Pts_Records.ExtNtext1, 
                      Pts_Records.ExtNtext2, Pts_Records.ParentRecordID, Pts_Records.RepresentingUserID
FROM         Pts_Records LEFT OUTER JOIN
                      Pts_ProblemState ON Pts_Records.StateID = Pts_ProblemState.ProblemStateID LEFT OUTER JOIN
                      Accounts_Users ON Pts_Records.CreateUser = Accounts_Users.UserID LEFT OUTER JOIN
                      Accounts_Users AS Accounts_Users_1 ON Pts_Records.AssignTo = Accounts_Users_1.UserID')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.v_Pts_Records View Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.v_Pts_Records View'
END
GO

--
-- Script To Create dbo.v_Pts_RecordsForEvents View In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.v_Pts_RecordsForEvents View'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE VIEW [dbo].[v_Pts_RecordsForEvents]
AS
SELECT     TOP 100 PERCENT Pts_Records.RecordID, Pts_Records.ProblemID, Pts_Records.CreateUser, Pts_Records.DateCreated, 
                      Pts_Records.IsKeyRecord, Pts_Records.RecordType, Pts_Records.AssignTo, Pts_Records.Title, Pts_Records.[Content], 
                      Pts_Records.StateID, Pts_Records.HistoryID, Pts_Records.RecordClass, Pts_Records.SrcStateID, Pts_Records.SrcUserID, 
                      Pts_Records.ResponseWorkTime, Pts_Records.ResponseTotalTime, Pts_Records.RealTimeCost, Pts_Records.ExtInt1, Pts_Records.ExtInt2, 
                      Pts_Records.ExtInt3, Pts_Records.ExtString1, Pts_Records.ExtString2, Pts_Records.ExtString3, Pts_Records.ExtNtext1, 
                      Pts_Records.ExtNtext2, Pts_Records.ParentRecordID, v_Pts_Problems.Title AS ProblemTitle, v_Pts_Problems.ProblemCode, 
                      v_Pts_Problems.ProjectID
FROM         Pts_Records LEFT OUTER JOIN
                      v_Pts_Problems ON Pts_Records.ProblemID = v_Pts_Problems.ProblemID
ORDER BY Pts_Records.RecordID DESC')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.v_Pts_RecordsForEvents View Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.v_Pts_RecordsForEvents View'
END
GO

--
-- Script To Update dbo.v_Pts_WorkflowSteps View In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.v_Pts_WorkflowSteps View'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('ALTER VIEW [dbo].[v_Pts_WorkflowSteps]
AS
SELECT     Pts_WorkflowSteps.StepID, Pts_WorkflowSteps.ProjectID, Pts_WorkflowSteps.SrcStateID, Pts_WorkflowSteps.DstStateID, 
                      Pts_WorkflowSteps.StepName, Pts_WorkflowSteps.Description, Pts_WorkflowSteps.AssignType, Pts_WorkflowSteps.Assignee, 
                      Pts_WorkflowSteps.SelectableAssigneeWorkgroupIDs, Pts_WorkflowSteps.SelectableAssigneeUserIDs, Pts_WorkflowSteps.ListOrder, 
                      Pts_WorkflowSteps.RecordTitle, Pts_WorkflowSteps.CommonMessages, Pts_WorkflowSteps.AssignInfoExtendedSettings, 
                      Pts_WorkflowSteps.StepExtendedSettings, src.StateName AS SrcStateName, src.Description AS SrcStateDescription, src.ListOrder AS SrcStateListOrder, 
                      src.IsFinalState AS SrcStateIsFinalState, dst.StateName AS DstStateName, dst.Description AS DstStateDescription, dst.ListOrder AS DstStateListOrder, 
                      dst.IsFinalState AS DstStateIsFinalState
FROM         Pts_WorkflowSteps INNER JOIN
                      Pts_ProblemState AS src ON Pts_WorkflowSteps.SrcStateID = src.ProblemStateID INNER JOIN
                      Pts_ProblemState AS dst ON Pts_WorkflowSteps.DstStateID = dst.ProblemStateID')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.v_Pts_WorkflowSteps View Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.v_Pts_WorkflowSteps View'
END
GO

--
-- Script To Create dbo.sp_GetRecordByPage Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_GetRecordByPage Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_GetRecordByPage]
(
@sTable nvarchar(100),  --表名
@sPkey nvarchar(50),            --主键(一定要有)
@sField nvarchar(1000)=''*'',     --字段
@iPageCurr int,                 --当前页数
@iPageSize int,                 --每页记录数
@sCondition nvarchar(1000),  --条件(不需要where)
@sOrder nvarchar(100) ,         --排序(不需要order by,需要asc和desc字符)
@Counts int=0 output,           --记录条数(已有值:外部赋值,0执行count)
@pageCount int=1 output     --查询结果分页后的总页数
)
AS
SET NOCOUNT ON

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

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


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

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

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

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

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

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

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

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

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

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

EXEC(@sSQL)

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

SET NOCOUNT OFF')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_GetRecordByPage Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_GetRecordByPage Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_AddNotifyTask Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_AddNotifyTask Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create procedure [dbo].[sp_Pts_AddNotifyTask]
@TargetType int,
	@TargetID int,
	@NotifyUsers nvarchar(1024),
	@EventType int,
	@CCMails nvarchar(2048),
	@Sender nvarchar(20),
	@EventTime datetime,
    @NotifyAssignee bit
as
--添加一条通知任务
INSERT INTO [Pts_Notify] (
	[TargetType],
	[TargetID],
	[NotifyUsers],
	[EventType],
	[CCMails],
	[Sender],
	[EventTime],
    [NotifyAssignee]
) VALUES (
	@TargetType,
	@TargetID,
	@NotifyUsers,
	@EventType,
	@CCMails,
	@Sender,
	@EventTime,
    @NotifyAssignee
)

SELECT  @@IDENTITY')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_AddNotifyTask Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_AddNotifyTask Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_AddProblemAttachment Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_AddProblemAttachment Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


SET ANSI_NULLS OFF
GO

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


--更新事务的附件数量
exec sp_Pts_UpdateProblemAttachNum @ProblemID')
GO

SET ANSI_NULLS ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_AddProblemAttachment Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_AddProblemAttachment Procedure'
END
GO

--
-- Script To Delete dbo.sp_Pts_AddProblemCreateRole Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Deleting dbo.sp_Pts_AddProblemCreateRole Procedure'
GO

   DROP PROCEDURE [dbo].[sp_Pts_AddProblemCreateRole]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_AddProblemCreateRole Procedure Deleted Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Delete dbo.sp_Pts_AddProblemCreateRole Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_ChangeProblemCatalogOrder Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_ChangeProblemCatalogOrder Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('ALTER   procedure [dbo].[sp_Pts_ChangeProblemCatalogOrder]
(
     @ProblemCatalogID int,
     @MoveUp bit
)
AS
BEGIN
--获取ParentID
Declare @ParentID int
Declare @ProjectID int
Declare @GlobalOrder varchar(100)

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

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

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

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

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

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

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

exec sp_Pts_ResetProblemCatalogGlobalOrder @ProjectID, @ParentID, @GlobalOrder')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_ChangeProblemCatalogOrder Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_ChangeProblemCatalogOrder Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_ChangeProblemState Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_ChangeProblemState Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


SET QUOTED_IDENTIFIER OFF
GO

SET ANSI_NULLS OFF
GO

exec('--更新问题的状态
ALTER        PROCEDURE [dbo].[sp_Pts_ChangeProblemState]
@ProblemID int,
@NextStateID int,
@UserID int
AS

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

exec sp_Pts_RecordStateStart @ProblemID, @NextStateID, @UserID

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

if @ParentID > 0
BEGIN
	exec sp_Pts_UpdateProblemChildNum @ParentID
END')
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_ChangeProblemState Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_ChangeProblemState Procedure'
END
GO

--
-- Script To Delete dbo.sp_Pts_CheckUserPermission Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Deleting dbo.sp_Pts_CheckUserPermission Procedure'
GO

   DROP PROCEDURE [dbo].[sp_Pts_CheckUserPermission]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_CheckUserPermission Procedure Deleted Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Delete dbo.sp_Pts_CheckUserPermission Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_ComputeUserPendingTasks Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_ComputeUserPendingTasks Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE Procedure [dbo].[sp_Pts_ComputeUserPendingTasks]
@UserID int
AS
--等待我处理的事务
SELECT ProjectID,COUNT(*) FROM v_Pts_Problems
WHERE
	IsClosed = 0
AND
	((AssignedTo > 0 AND AssignedTo = @UserID)
	OR
	(AssignedTo < 0 AND -AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID)))
GROUP BY ProjectID')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_ComputeUserPendingTasks Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_ComputeUserPendingTasks Procedure'
END
GO


--
-- Script To Create dbo.sp_Pts_CreateFavoriteLink Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_CreateFavoriteLink Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_CreateFavoriteLink]
@UserID int,
@Title nvarchar(100),
@Link nvarchar(1024)
AS
--SELECT * FROM Pts_FavoriteLinks WHERE UserID=@UserID ORDER BY ListOrder,Title

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

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

INSERT INTO Pts_FavoriteLinks (UserID, Title, Link, ListOrder,CreateTime) values (@UserID, @Title, @Link, @ListOrder, GetDate())')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_CreateFavoriteLink Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_CreateFavoriteLink Procedure'
END
GO

--
-- Script To Delete dbo.sp_Pts_CreateProblemState Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Deleting dbo.sp_Pts_CreateProblemState Procedure'
GO

   DROP PROCEDURE [dbo].[sp_Pts_CreateProblemState]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_CreateProblemState Procedure Deleted Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Delete dbo.sp_Pts_CreateProblemState Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_CreateProject Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_CreateProject Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


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

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

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

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


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




set @ProjectID = @@Identity')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_CreateProject Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_CreateProject Procedure'
END
GO

--
-- Script To Delete dbo.sp_Pts_CreateProjectCatalog Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Deleting dbo.sp_Pts_CreateProjectCatalog Procedure'
GO

   DROP PROCEDURE [dbo].[sp_Pts_CreateProjectCatalog]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_CreateProjectCatalog Procedure Deleted Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Delete dbo.sp_Pts_CreateProjectCatalog Procedure'
END
GO

--
-- Script To Delete dbo.sp_Pts_CreateWorkgroup Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Deleting dbo.sp_Pts_CreateWorkgroup Procedure'
GO

   DROP PROCEDURE [dbo].[sp_Pts_CreateWorkgroup]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_CreateWorkgroup Procedure Deleted Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Delete dbo.sp_Pts_CreateWorkgroup Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_ResetProjectCatalogOrder Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_ResetProjectCatalogOrder Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


SET QUOTED_IDENTIFIER OFF
GO

SET ANSI_NULLS OFF
GO

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

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_ResetProjectCatalogOrder Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_ResetProjectCatalogOrder Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_DeleteAllIssues Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_DeleteAllIssues Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE procedure [dbo].[sp_Pts_DeleteAllIssues]
--清空项目的所有事务
@ProjectID int
AS

Begin transaction

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

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

commit transaction

exec sp_Pts_ComputeProjectCounter @ProjectID')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_DeleteAllIssues Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_DeleteAllIssues Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_DeleteProblem Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_DeleteProblem Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


SET ANSI_NULLS OFF
GO

exec('ALTER  PROCEDURE [dbo].[sp_Pts_DeleteProblem]
@ProblemID int
AS
Begin transaction

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

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

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

if @ParentID > 0
BEGIN
	exec sp_Pts_UpdateProblemChildNum @ParentID
END

commit transaction')
GO

SET ANSI_NULLS ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_DeleteProblem Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_DeleteProblem Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_CreatedByMeNotClosed Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_CreatedByMeNotClosed Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create Procedure [dbo].[sp_Pts_Filter_CreatedByMeNotClosed]
@ProjectID int,
@UserID int,
@Count int
AS
--我创建的未关闭事务
SELECT top 500 ProblemID,ProblemCode,Title,AssignedTo,ProblemStateName,CreateUserName,LastTime,LastUser FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	CreateUserID = @UserID
AND 
	IsClosed = 0
ORDER BY CreateTime DESC')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_CreatedByMeNotClosed Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_CreatedByMeNotClosed Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_RecentAssignedToMe Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_RecentAssignedToMe Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE Procedure [dbo].[sp_Pts_Filter_RecentAssignedToMe]
@ProjectID int,
@UserID int,
@Count int
AS
--等待我处理的事务
SELECT top 500 ProblemID, ProblemCode, Title, ProblemStateName, AssignedTo, LastTime,ReadByAssignee
FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND 
	IsClosed = 0
AND
	((AssignedTo > 0 AND AssignedTo = @UserID)
	OR
	(AssignedTo < 0 AND -AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID))
	OR
	EXISTS (SELECT 1 FROM Pts_Coworkers LEFT OUTER JOIN
                      Pts_Problems ON Pts_Coworkers.ProblemID = Pts_Problems.ProblemID
			WHERE Pts_Coworkers.UserID=@UserID AND Pts_Coworkers.IsWorked=0 AND Pts_Problems.ProjectID=@ProjectID)
	)
Order BY LastTime DESC,CreateTime DESC')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_RecentAssignedToMe Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_RecentAssignedToMe Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_Filter_Relate_1 Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_Filter_Relate_1 Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('ALTER Procedure [dbo].[sp_Pts_Filter_Relate_1]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND 
	IsClosed = 0
AND
	((AssignedTo > 0 AND AssignedTo = @UserID)
	OR
	(AssignedTo < 0 AND -AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID))
	OR
	EXISTS (SELECT 1 FROM Pts_Coworkers LEFT OUTER JOIN
                      Pts_Problems ON Pts_Coworkers.ProblemID = Pts_Problems.ProblemID
			WHERE Pts_Coworkers.UserID=@UserID AND Pts_Coworkers.IsWorked=0 AND Pts_Problems.ProjectID=@ProjectID)
	)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Relate_1 Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_Filter_Relate_1 Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_Relate_21 Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_Relate_21 Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


SET QUOTED_IDENTIFIER OFF
GO

SET ANSI_NULLS OFF
GO

exec('CREATE Procedure [dbo].[sp_Pts_Filter_Relate_21]
@ProjectID int,
@UserID int
AS
--我相关的，未关闭的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0
AND
(
	CreateUserID = @UserID
OR
	(AssignedTo = @UserID AND IsClosed=0)
OR
	ProblemID in
	(SELECT Distinct ProblemID From Pts_Records Where CreateUser = @UserID)
OR
	ProblemID in
	(SELECT ProblemID FROM Pts_RelatedUsers Where UserID=@UserID)
)')
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Relate_21 Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_Relate_21 Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_Relate_22 Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_Relate_22 Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


SET QUOTED_IDENTIFIER OFF
GO

SET ANSI_NULLS OFF
GO

exec('CREATE Procedure [dbo].[sp_Pts_Filter_Relate_22]
@ProjectID int,
@UserID int
AS
--我相关的，未关闭的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 1
AND
(
	CreateUserID = @UserID
OR
	ProblemID in
	(SELECT Distinct ProblemID From Pts_Records Where CreateUser = @UserID)
OR
	ProblemID in
	(SELECT ProblemID FROM Pts_RelatedUsers Where UserID=@UserID)
)
AND    
	DateDiff(d,CloseTime , GetDate()) < 90')
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Relate_22 Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_Relate_22 Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_Filter_Relate_7 Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_Filter_Relate_7 Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


SET QUOTED_IDENTIFIER OFF
GO

SET ANSI_NULLS OFF
GO

exec('ALTER Procedure [dbo].[sp_Pts_Filter_Relate_7]
@ProjectID int,
@UserID int
AS
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
(
	CreateUserID = @UserID
OR
	(AssignedTo = @UserID AND IsClosed=0)
OR
	ProblemID in
	(SELECT Distinct ProblemID From Pts_Records Where CreateUser = @UserID)
OR
	ProblemID in
	(SELECT ProblemID FROM Pts_RelatedUsers Where UserID=@UserID)
)')
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Relate_7 Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_Filter_Relate_7 Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_Relate_AssignedToMeNotRead Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_Relate_AssignedToMeNotRead Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_Filter_Relate_AssignedToMeNotRead]
@ProjectID int,
@UserID int
AS
--等待我处理的事务,未读
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND 
	IsClosed = 0
AND
	ReadByAssignee=0
AND
	((AssignedTo > 0 AND AssignedTo = @UserID)
	OR
	(AssignedTo < 0 AND -AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID))
	OR
	EXISTS (SELECT 1 FROM Pts_Coworkers LEFT OUTER JOIN
                      Pts_Problems ON Pts_Coworkers.ProblemID = Pts_Problems.ProblemID
			WHERE Pts_Coworkers.UserID=@UserID AND Pts_Coworkers.IsWorked=0 AND Pts_Problems.ProjectID=@ProjectID)
	)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Relate_AssignedToMeNotRead Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_Relate_AssignedToMeNotRead Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_Relate_CreatedByMe_Closed Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_Relate_CreatedByMe_Closed Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('Create Procedure [dbo].[sp_Pts_Filter_Relate_CreatedByMe_Closed]
@ProjectID int,
@UserID int
AS
--我创建的未关闭事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	CreateUserID = @UserID
AND 
	IsClosed = 1')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Relate_CreatedByMe_Closed Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_Relate_CreatedByMe_Closed Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_Relate_ProcessedByAgent Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_Relate_ProcessedByAgent Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create Procedure [dbo].[sp_Pts_Filter_Relate_ProcessedByAgent]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND 
	ProblemID in (SELECT Distinct ProblemID FROM Pts_Records WHERE RepresentingUserID=@UserID)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Relate_ProcessedByAgent Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_Relate_ProcessedByAgent Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_Relate_ProcessedByMe Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_Relate_ProcessedByMe Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE Procedure [dbo].[sp_Pts_Filter_Relate_ProcessedByMe]
@ProjectID int,
@UserID int
AS
--我参与的事务，跟踪中的
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	ProblemID in
	(SELECT Distinct ProblemID From Pts_Records Where CreateUser = @UserID)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Relate_ProcessedByMe Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_Relate_ProcessedByMe Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_Relate_ProcessedByMeClosed Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_Relate_ProcessedByMeClosed Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create Procedure [dbo].[sp_Pts_Filter_Relate_ProcessedByMeClosed]
@ProjectID int,
@UserID int
AS
--我参与的事务，跟踪中的
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 1
AND
	ProblemID in
	(SELECT Distinct ProblemID From Pts_Records Where CreateUser = @UserID)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Relate_ProcessedByMeClosed Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_Relate_ProcessedByMeClosed Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_Relate_ProcessedByMeOpen Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_Relate_ProcessedByMeOpen Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE Procedure [dbo].[sp_Pts_Filter_Relate_ProcessedByMeOpen]
@ProjectID int,
@UserID int
AS
--我参与的事务，跟踪中的
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0
AND
	ProblemID in
	(SELECT Distinct ProblemID From Pts_Records Where CreateUser = @UserID)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Relate_ProcessedByMeOpen Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_Relate_ProcessedByMeOpen Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_Relate_RelatedToMe Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_Relate_RelatedToMe Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE Procedure [dbo].[sp_Pts_Filter_Relate_RelatedToMe]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID

AND
	ProblemID in
	(SELECT ProblemID FROM Pts_RelatedUsers Where UserID=@UserID)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Relate_RelatedToMe Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_Relate_RelatedToMe Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_Relate_RelatedToMeClosed Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_Relate_RelatedToMeClosed Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE Procedure [dbo].[sp_Pts_Filter_Relate_RelatedToMeClosed]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed=1
AND
	ProblemID in
	(SELECT ProblemID FROM Pts_RelatedUsers Where UserID=@UserID)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Relate_RelatedToMeClosed Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_Relate_RelatedToMeClosed Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_Relate_RelatedToMeOpen Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_Relate_RelatedToMeOpen Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE Procedure [dbo].[sp_Pts_Filter_Relate_RelatedToMeOpen]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed=0
AND
	ProblemID in
	(SELECT ProblemID FROM Pts_RelatedUsers Where UserID=@UserID)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Relate_RelatedToMeOpen Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_Relate_RelatedToMeOpen Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_Relate_SubscribedByMe Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_Relate_SubscribedByMe Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE Procedure [dbo].[sp_Pts_Filter_Relate_SubscribedByMe]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	ProblemID in
	(Select TargetID FROM Pts_Subscription WHERE UserID=@UserID AND TargetType=2)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Relate_SubscribedByMe Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_Relate_SubscribedByMe Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_Relate_SubscribedByMeClosed Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_Relate_SubscribedByMeClosed Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE Procedure [dbo].[sp_Pts_Filter_Relate_SubscribedByMeClosed]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 1
AND
	ProblemID in
	(Select TargetID FROM Pts_Subscription WHERE UserID=@UserID AND TargetType=2)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Relate_SubscribedByMeClosed Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_Relate_SubscribedByMeClosed Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_Relate_SubscribedByMeOpen Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_Relate_SubscribedByMeOpen Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE Procedure [dbo].[sp_Pts_Filter_Relate_SubscribedByMeOpen]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0
AND
	ProblemID in
	(Select TargetID FROM Pts_Subscription WHERE UserID=@UserID AND TargetType=2)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Relate_SubscribedByMeOpen Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_Relate_SubscribedByMeOpen Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_Suspended Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_Suspended Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE Procedure [dbo].[sp_Pts_Filter_Suspended]
@ProjectID int,
@UserID int
AS
--等待我处理的事务
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND 
	IsClosed = 0
AND
	IsActive = 0')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Suspended Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_Suspended Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_Time_Expired Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_Time_Expired Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE procedure [dbo].[sp_Pts_Filter_Time_Expired]
@ProjectID int,
@UserID int
AS
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0
AND
	Deadline is not null
AND
	Deadline < GetDate()')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Time_Expired Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_Time_Expired Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_Time_StateExpired Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_Time_StateExpired Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE procedure [dbo].[sp_Pts_Filter_Time_StateExpired]
@ProjectID int,
@UserID int
AS
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	IsClosed = 0
AND
	StateDeadline is not null
AND
	StateDeadline < GetDate()')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Time_StateExpired Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_Time_StateExpired Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Filter_Time_TodayUpdated Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Filter_Time_TodayUpdated Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE procedure [dbo].[sp_Pts_Filter_Time_TodayUpdated]
@ProjectID int,
@UserID int
AS
SELECT * FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND
	LastTime >= Convert(varchar(10),getdate(),120)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Filter_Time_TodayUpdated Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Filter_Time_TodayUpdated Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetAllProjects Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetAllProjects Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_GetAllProjects]
AS
SELECT * FROM v_Pts_Projects 
ORDER BY ListOrder')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetAllProjects Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetAllProjects Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetAllRelatedProblems Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetAllRelatedProblems Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_GetAllRelatedProblems] 
@ProblemID int
AS
--SELECT [ProblemID],[ProblemCode],[Title] 
--FROM v_Pts_Problems 
--WHERE ProblemID IN (SELECT ProblemID_2 FROM [Pts_ProblemRelations]  WHERE ProblemID_1=@ProblemID) 
--	OR ProblemID IN (SELECT ProblemID_1 FROM [Pts_ProblemRelations]  WHERE ProblemID_2=@ProblemID) 

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

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

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

select * from @t order by refdirection desc,ProblemID asc')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetAllRelatedProblems Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetAllRelatedProblems Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetCalendarEvents Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetCalendarEvents Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_GetCalendarEvents]
@ProjectID int,
@StartDate datetime,
@EndDate datetime
AS
--得到一段时间内某个项目内提交的records
SELECT * FROM v_Pts_RecordsForEvents WHERE ProjectID=@ProjectID AND DateCreated>=@StartDate AND DateCreated <= @EndDate')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetCalendarEvents Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetCalendarEvents Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetChildIssues Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetChildIssues Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_GetChildIssues]
@ParentID int
AS
SELECT * FROM v_Pts_Problems WHERE ParentID=@ParentID ORDER BY IsClosed,ProjectID,ProblemID')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetChildIssues Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetChildIssues Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetConnectorNotitfyTasks Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetConnectorNotitfyTasks Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create procedure [dbo].[sp_Pts_GetConnectorNotitfyTasks]
AS
SELECT * FROM Pts_OutConnectorNotifys')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetConnectorNotitfyTasks Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetConnectorNotitfyTasks Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_GetEditableFieldsWhenCreateIssue Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_GetEditableFieldsWhenCreateIssue Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('-- =============================================
-- Author:		CuiLiang
-- Create date: <Create Date,,>
-- Description:	Get all editable fields when creating issue
-- =============================================
ALTER PROCEDURE [dbo].[sp_Pts_GetEditableFieldsWhenCreateIssue]
@ProjectID int,
@UserID int,
@DestStateID int
AS
BEGIN

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


END')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetEditableFieldsWhenCreateIssue Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_GetEditableFieldsWhenCreateIssue Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetExpiredTimers Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetExpiredTimers Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create procedure [dbo].[sp_Pts_GetExpiredTimers]
@ExpireTime Datetime
AS
SELECT * FROM [Pts_ProblemTimers1] WHERE NextExpireTime < @ExpireTime')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetExpiredTimers Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetExpiredTimers Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetIssueCounts_ByFields Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetIssueCounts_ByFields Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE procedure [dbo].[sp_Pts_GetIssueCounts_ByFields]
@ProjectID int
AS
--得到根据各系统字段统计的事务数量

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

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


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


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

select * from @t')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetIssueCounts_ByFields Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetIssueCounts_ByFields Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetIssueCounts_Mine Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetIssueCounts_Mine Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE procedure [dbo].[sp_Pts_GetIssueCounts_Mine]
@ProjectID int,
@UserID int
AS

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


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

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

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

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

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

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

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

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



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

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

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

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

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

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

--输出
SELECT * FROM @t')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetIssueCounts_Mine Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetIssueCounts_Mine Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetIssueCounts_Recent Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetIssueCounts_Recent Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE procedure [dbo].[sp_Pts_GetIssueCounts_Recent]
@ProjectID int
AS

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




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


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

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

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

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

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


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

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

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


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

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

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

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

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

--

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

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

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

-------------------------------------
select * from @t')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetIssueCounts_Recent Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetIssueCounts_Recent Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetMyIssueCount Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetMyIssueCount Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE procedure [dbo].[sp_Pts_GetMyIssueCount]
@UserID int
AS
Select 	COUNT(1)
From 	v_Pts_Problems Where 
--IsClosed=0 
--AND
(AssignedTo = @UserID
	OR
        (AssignedTo < 0 AND
	-AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID))
	)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetMyIssueCount Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetMyIssueCount Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetNotitfyTasks Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetNotitfyTasks Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create procedure [dbo].[sp_Pts_GetNotitfyTasks]
AS
SELECT * FROM Pts_Notify')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetNotitfyTasks Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetNotitfyTasks Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetOpenClosedIssuesByTime Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetOpenClosedIssuesByTime Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create PROCEDURE [dbo].[sp_Pts_GetOpenClosedIssuesByTime]
@ProjectID int,
@FirstDate datetime,
@LastDate datetime
AS
--得到一段时间内某个项目内创建或关闭的事务
SELECT * FROM v_Pts_Problems WHERE ProjectID=@ProjectID AND ((CreateTime >= @FirstDate AND CreateTime < @LastDate) 
                            OR (IsClosed = 1 AND CloseTime >= @FirstDate AND CloseTime < @LastDate))')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetOpenClosedIssuesByTime Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetOpenClosedIssuesByTime Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetProblemCreateRoleByInitialState Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetProblemCreateRoleByInitialState Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_GetProblemCreateRoleByInitialState]
@ProjectID int,
@InitStateID int
AS
--得到创建事务时的创建规则
SELECT * FROM Pts_ProblemCreateRoles WHERE ProjectID=@ProjectID AND DstStateID=@InitStateID')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetProblemCreateRoleByInitialState Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetProblemCreateRoleByInitialState Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_GetProblemCtalogList Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_GetProblemCtalogList Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


SET QUOTED_IDENTIFIER OFF
GO

SET ANSI_NULLS OFF
GO

exec('ALTER PROCEDURE [dbo].[sp_Pts_GetProblemCtalogList]
@ProjectID int
AS
Select * from Pts_ProblemCatalogs 
where ProjectID = @ProjectID
Order by ListOrder')
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetProblemCtalogList Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_GetProblemCtalogList Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetProblemRecordAttachmentList Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetProblemRecordAttachmentList Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create procedure [dbo].[sp_Pts_GetProblemRecordAttachmentList]
@ProblemID int
AS
Select * From v_Pts_RecordAttachments Where RecordID in (SELECT RecordID FROM Pts_Records WHERE ProblemID = @ProblemID)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetProblemRecordAttachmentList Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetProblemRecordAttachmentList Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetProblemRelatedArticles Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetProblemRelatedArticles Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create procedure [dbo].[sp_Pts_GetProblemRelatedArticles]
@ProblemID int
AS
SELECT ArticleID,Title,Summary FROM KB_Articles WHERE ArticleID in (SELECT ArticleID FROM Pts_ProblemRelatedArticles WHERE ProblemID=@ProblemID) ORDER BY TItle')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetProblemRelatedArticles Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetProblemRelatedArticles Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetProblemRelatedUserIDs Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetProblemRelatedUserIDs Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create procedure [dbo].[sp_Pts_GetProblemRelatedUserIDs]
@ProblemID int
AS
SELECT UserID FROM Pts_RelatedUsers WHERE ProblemID=@ProblemID')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetProblemRelatedUserIDs Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetProblemRelatedUserIDs Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetProblemsForWBS Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetProblemsForWBS Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE procedure [dbo].[sp_Pts_GetProblemsForWBS]
@ProjectID int,
@OnlyOpen bit --是否只返回跟踪中的事务
AS

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

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

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

while @addcount > 0
begin

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

set @addcount=@@ROWCOUNT

end

--返回数据
select * from @aaa')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetProblemsForWBS Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetProblemsForWBS Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_GetProblemStateList Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_GetProblemStateList Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


SET QUOTED_IDENTIFIER OFF
GO

SET ANSI_NULLS OFF
GO

exec('ALTER PROCEDURE [dbo].[sp_Pts_GetProblemStateList]
@ProjectID int
AS
IF @ProjectID > 0 
BEGIN
Select * from Pts_ProblemState where ProjectID = @ProjectID order by ListOrder
END
ELSE
BEGIN
Select * from Pts_ProblemState order by ListOrder
END')
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetProblemStateList Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_GetProblemStateList Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetProblemStateTimeLimitRoleItems Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetProblemStateTimeLimitRoleItems Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_GetProblemStateTimeLimitRoleItems]
@ProblemStateID int
AS
SELECT * FROM Pts_ProblemStateTimeLimitRoleItems WHERE ProblemStateID = @ProblemStateID Order by ListOrder')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetProblemStateTimeLimitRoleItems Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetProblemStateTimeLimitRoleItems Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetProjectFieldsInUse Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetProjectFieldsInUse Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create procedure [dbo].[sp_Pts_GetProjectFieldsInUse]
@ProjectID int
AS
Select * From Pts_DbDirectory Where ProjectID = @ProjectID AND (IsSystem=0 or InUse=1) Order by ListOrder')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetProjectFieldsInUse Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetProjectFieldsInUse Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetRecentSharedItems Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetRecentSharedItems Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_GetRecentSharedItems]
@ProjectID int
AS
--获取最新的共享项
SELECT TOP 5 ItemID,Title,UpdateDate FROM Pts_SharedItems WHERE ProjectID=@ProjectID order by updatedate desc')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetRecentSharedItems Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetRecentSharedItems Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetRequiredFieldsWhenProcessIssue Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetRequiredFieldsWhenProcessIssue Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create procedure [dbo].[sp_Pts_GetRequiredFieldsWhenProcessIssue]
@FromStateID int,
@ToStateID int
AS
SELECT Distinct FieldID 
FROM Pts_ProblemFieldEditableState 
WHERE IsRequired=1 AND ((FromStateID=@FromStateID AND ToStateID=@ToStateID) OR (FromStateID=@FromStateID AND ToStateID=0) OR (FromStateID=0 AND ToStateID=@ToStateID))')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetRequiredFieldsWhenProcessIssue Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetRequiredFieldsWhenProcessIssue Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_GetSelectValues Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_GetSelectValues Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


SET QUOTED_IDENTIFIER OFF
GO

SET ANSI_NULLS OFF
GO

exec('ALTER PROCEDURE [dbo].[sp_Pts_GetSelectValues]
@GlobalSelectID int
AS
Select * From Pts_GlobalSelectValues Where GlobalSelectID = @GlobalSelectID Order by ListOrder,ValueString')
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetSelectValues Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_GetSelectValues Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetSharedItem Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetSharedItem Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_GetSharedItem] 
@ItemID int
AS
SELECT * FROM Pts_SharedItems WHERE ItemID = @ItemID')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetSharedItem Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetSharedItem Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetStartTimeExpiredSuspendedIssues Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetStartTimeExpiredSuspendedIssues Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_GetStartTimeExpiredSuspendedIssues]
--得到到达开始时间，并且设置为自动激活的被挂起的事务
@Projects nvarchar(1000)
AS
select ProblemID FROM Pts_Problems 
WHERE ProjectID in (@Projects) 
	AND IsActive=0 
	AND StartTime < GetDate()')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetStartTimeExpiredSuspendedIssues Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetStartTimeExpiredSuspendedIssues Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetStatesHaveNoRelateStep Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetStatesHaveNoRelateStep Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create procedure [dbo].[sp_Pts_GetStatesHaveNoRelateStep]
@ProjectID int
AS
SELECT * FROM [Pts_ProblemState] 
WHERE ProjectID=@ProjectID 
	AND IsFinalState=0 
	AND ProblemStateID NOT IN (SELECT SrcStateID FROM Pts_WorkflowSteps WHERE ProjectID=@ProjectID)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetStatesHaveNoRelateStep Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetStatesHaveNoRelateStep Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetStepActions Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetStepActions Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_GetStepActions]
@ProjectID int,
@SrcStateID int,
@DstStateID int
AS
SELECT * FROM Pts_StepActions WHERE ProjectID=@ProjectID AND SrcStateID=@SrcStateID AND DstStateID=@DstStateID')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetStepActions Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetStepActions Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetSubscribeUserIDs Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetSubscribeUserIDs Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE procedure [dbo].[sp_Pts_GetSubscribeUserIDs]
@TargetType int,
@TargetID int,
@EventType int,
@Params nvarchar(1024)
AS
SELECT UserID FROM Pts_Subscription WHERE TargetType=@TargetType AND TargetID=@TargetID AND EventType=@EventType AND ((EventType=@EventType and Params=@Params) or EventType=0)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetSubscribeUserIDs Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetSubscribeUserIDs Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_GetSubscribeUserSet Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_GetSubscribeUserSet Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('ALTER PROCEDURE [dbo].[sp_Pts_GetSubscribeUserSet]
@TargetType int,
@TargetID int,
@EventType int,
@Params nvarchar(1024)
AS
SELECT * FROM v_Accounts_Users WHERE UserID IN 
(SELECT UserID FROM Pts_Subscription WHERE TargetType=@TargetType AND TargetID=@TargetID AND ((EventType=@EventType and Params=@Params) or EventType=0)  )')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetSubscribeUserSet Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_GetSubscribeUserSet Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetSysConfig Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetSysConfig Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create procedure [dbo].[sp_Pts_GetSysConfig]
@ConfigItem nvarchar(100)
AS
SELECT ConfigValue FROM [Common_SystemConfig] WHERE ConfigItem=@ConfigItem')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetSysConfig Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetSysConfig Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetUserCoworkingIssues Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetUserCoworkingIssues Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_GetUserCoworkingIssues]
@UserID int
AS
SELECT ProblemID FROM Pts_Coworkers WHERE UserID=@UserID AND IsWorked = 0')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetUserCoworkingIssues Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetUserCoworkingIssues Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetUserCreatedProblems Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetUserCreatedProblems Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


SET ANSI_NULLS OFF
GO

exec('CREATE  Procedure [dbo].[sp_Pts_GetUserCreatedProblems]
@UserID int,
@IsClosed int = null 
AS
if (@IsClosed is null)
BEGIN
Select * From v_Pts_Problems where CreateUserID = @UserID
END
else 
BEGIN
Select * From v_Pts_Problems where CreateUserID = @UserID AND IsClosed=@IsClosed
END')
GO

SET ANSI_NULLS ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetUserCreatedProblems Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetUserCreatedProblems Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetUserEditableFieldByWorkgroup Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetUserEditableFieldByWorkgroup Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE procedure [dbo].[sp_Pts_GetUserEditableFieldByWorkgroup]
@ProjectID int,
@UserID int
AS
--得到根据用户所在工作组所确定的可编辑字段的列表
SELECT DISTINCT FieldID FROM Pts_ProblemFieldEditableWorkgroup 
				WHERE WorkgroupID in (SELECT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID = @UserID)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetUserEditableFieldByWorkgroup Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetUserEditableFieldByWorkgroup Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetUserFavoriteLinks Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetUserFavoriteLinks Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_GetUserFavoriteLinks]
@UserID int
AS
SELECT * FROM Pts_FavoriteLinks WHERE UserID=@UserID ORDER BY Title')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetUserFavoriteLinks Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetUserFavoriteLinks Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_GetUserInProblems Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_GetUserInProblems Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


SET ANSI_NULLS OFF
GO

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

SET ANSI_NULLS ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetUserInProblems Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_GetUserInProblems Procedure'
END
GO

--
-- Script To Delete dbo.sp_Pts_GetUserLastProcessedProblems Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Deleting dbo.sp_Pts_GetUserLastProcessedProblems Procedure'
GO

   DROP PROCEDURE [dbo].[sp_Pts_GetUserLastProcessedProblems]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetUserLastProcessedProblems Procedure Deleted Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Delete dbo.sp_Pts_GetUserLastProcessedProblems Procedure'
END
GO

--
-- Script To Delete dbo.sp_Pts_GetUserOutProblems Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Deleting dbo.sp_Pts_GetUserOutProblems Procedure'
GO

   DROP PROCEDURE [dbo].[sp_Pts_GetUserOutProblems]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetUserOutProblems Procedure Deleted Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Delete dbo.sp_Pts_GetUserOutProblems Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetUserProcessedProblems Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetUserProcessedProblems Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE Procedure [dbo].[sp_Pts_GetUserProcessedProblems]
@UserID int,
@IsClosed int = null 
AS
if (@IsClosed is null)
BEGIN
Select * From v_Pts_Problems where ProblemID in (SELECT Distinct ProblemID From Pts_Records Where CreateUser = @UserID)
END
else 
BEGIN
Select * From v_Pts_Problems where IsClosed=@IsClosed AND ProblemID in (SELECT Distinct ProblemID From Pts_Records Where CreateUser = @UserID)
END')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetUserProcessedProblems Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetUserProcessedProblems Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_GetUserViewableFields Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_GetUserViewableFields Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('-- =============================================
-- Author:		CuiLiang
-- Create date: <Create Date,,>
-- Description:	Get all viewable fields 
-- 
-- =============================================
ALTER PROCEDURE [dbo].[sp_Pts_GetUserViewableFields]
@ProjectID int,
@UserID int,
@ProblemID int
AS
BEGIN
--字段对所有人可见，
-- 或者字段对创建人可见，而且用户是事务的创建人;或者用户在所允许的工作组中
SELECT * FROM Pts_DbDirectory
WHERE	ProjectID = @ProjectID
	AND
		(IsSystem = 0 or (IsSystem = 1 AND InUse = 1))
	AND
	(
		ReadableToAll = 1 
		 OR 
		(
			(ReadableToCreateUser = 1 AND Exists(SELECT ProblemID FROM Pts_Problems WHERE ProblemID = @ProblemID AND CreateUserID=@UserID))
			OR
			ID IN (SELECT FieldID FROM Pts_ProblemFieldReadableWorkgroup WHERE 
				WorkgroupID IN (SELECT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID = @UserID)
					)
		)
	)
	AND 
	(
		ID NOT IN (SELECT FieldID FROM Pts_ProblemFieldHiddenState WHERE ProblemStateID = (SELECT ProblemStateID FROM Pts_Problems WHERE ProblemID = @ProblemID))
	)
		
ORDER BY 
	ListOrder ASC


END')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetUserViewableFields Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_GetUserViewableFields Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_GetUserVisitedProblems Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_GetUserVisitedProblems Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('ALTER  PROCEDURE [dbo].[sp_Pts_GetUserVisitedProblems]
(
@UserID int
)
AS

select p.* FROM
v_Pts_Problems p INNER JOIN
                      Pts_ProblemVisitHistory h ON p.ProblemID = h.ProblemID 
where h.UserID = @UserID
order by h.VisitIndex')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetUserVisitedProblems Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_GetUserVisitedProblems Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetWorkflowStepBySrcAndDstStates Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetWorkflowStepBySrcAndDstStates Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_GetWorkflowStepBySrcAndDstStates]
@SrcStateID int,
@DstStateID int
AS
SELECT * FROM Pts_WorkflowSteps 
WHERE SrcStateID=@SrcStateID AND DstStateID=@DstStateID')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetWorkflowStepBySrcAndDstStates Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetWorkflowStepBySrcAndDstStates Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetWorkflowStepsBySrcState Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetWorkflowStepsBySrcState Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create procedure [dbo].[sp_Pts_GetWorkflowStepsBySrcState] 
@ProjectID int,
@SrcStateID int
AS
SELECT * FROM 
v_Pts_WorkflowSteps 
WHERE ProjectID=@ProjectID AND SrcStateID=@SrcStateID 
ORDER BY ListOrder,SrcStateListOrder ASC,DstStateListOrder ASC')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetWorkflowStepsBySrcState Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetWorkflowStepsBySrcState Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_GetWorkgroupAllowedInitStates Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_GetWorkgroupAllowedInitStates Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_GetWorkgroupAllowedInitStates]
@ProjectID int
AS
----得到某个项目各个工作组所允许的初始状态的ID
SELECT WorkgroupID,ProblemStateID FROM Pts_ProblemInitStateWorkgroup 
WHERE WorkgroupID IN (SELECT WorkgroupID FROM Pts_Workgroups WHERE ProjectID=@ProjectID)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_GetWorkgroupAllowedInitStates Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_GetWorkgroupAllowedInitStates Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_IsFavoriteLinkExists Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_IsFavoriteLinkExists Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_IsFavoriteLinkExists]
@UserID int,
@Link nvarchar(1024)
AS
SELECT count(1) FROM Pts_FavoriteLinks WHERE UserID=@UserID AND Link = @Link')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_IsFavoriteLinkExists Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_IsFavoriteLinkExists Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_IsItemSubscribed Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_IsItemSubscribed Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('ALTER PROCEDURE [dbo].[sp_Pts_IsItemSubscribed]
@UserID int,
@TargetType int,
@TargetID int,
@EventType int,
@Params nvarchar(1024)
AS
SELECT COUNT(*) FROM Pts_Subscription WHERE UserID=@UserID 
	AND TargetType=@TargetType 
	AND TargetID=@TargetID 
	AND ((EventType=@EventType and Params=@Params) or EventType=0)')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_IsItemSubscribed Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_IsItemSubscribed Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_IsUserRelateProblem Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_IsUserRelateProblem Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('ALTER    Procedure [dbo].[sp_Pts_IsUserRelateProblem]
@UserID int,
@ProblemID int
AS


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

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_IsUserRelateProblem Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_IsUserRelateProblem Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_RemoveProblemAttachment Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_RemoveProblemAttachment Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


SET QUOTED_IDENTIFIER OFF
GO

SET ANSI_NULLS OFF
GO

exec('ALTER PROCEDURE [dbo].[sp_Pts_RemoveProblemAttachment]
@ProblemID int,
@AttachmentID int
AS
Delete from Pts_ProblemAttachments where AttachmentID = @AttachmentID

--更新事务的附件数量
exec sp_Pts_UpdateProblemAttachNum @ProblemID')
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_RemoveProblemAttachment Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_RemoveProblemAttachment Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_RemoveSubscription Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_RemoveSubscription Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('ALTER PROCEDURE [dbo].[sp_Pts_RemoveSubscription]
@UserID int,
@TargetType int,
@TargetID int,
@EventType int,
@Params nvarchar(1024)
AS
DELETE FROM Pts_Subscription WHERE UserID=@UserID AND TargetType=@TargetType AND TargetID=@TargetID AND EventType=@EventType')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_RemoveSubscription Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_RemoveSubscription Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_ResetProblemCatalogOrder Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_ResetProblemCatalogOrder Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


SET QUOTED_IDENTIFIER OFF
GO

SET ANSI_NULLS OFF
GO

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


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

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

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_ResetProblemCatalogOrder Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_ResetProblemCatalogOrder Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_ResetProblemPriorityOrder Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_ResetProblemPriorityOrder Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


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

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_ResetProblemPriorityOrder Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_ResetProblemPriorityOrder Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_ResetProblemSeverityOrder Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_ResetProblemSeverityOrder Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


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

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_ResetProblemSeverityOrder Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_ResetProblemSeverityOrder Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_ResetProblemStateOrder Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_ResetProblemStateOrder Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


SET QUOTED_IDENTIFIER OFF
GO

SET ANSI_NULLS OFF
GO

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

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_ResetProblemStateOrder Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_ResetProblemStateOrder Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_ResetProblemTypeOrder Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_ResetProblemTypeOrder Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


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

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_ResetProblemTypeOrder Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_ResetProblemTypeOrder Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_ResetProjectStateOrder Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_ResetProjectStateOrder Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


SET QUOTED_IDENTIFIER OFF
GO

SET ANSI_NULLS OFF
GO

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

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_ResetProjectStateOrder Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_ResetProjectStateOrder Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_ResetWorkgroupOrder Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_ResetWorkgroupOrder Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


SET QUOTED_IDENTIFIER OFF
GO

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

SET QUOTED_IDENTIFIER ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_ResetWorkgroupOrder Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_ResetWorkgroupOrder Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_SetAssigneeReadFlag Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_SetAssigneeReadFlag Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create procedure [dbo].[sp_Pts_SetAssigneeReadFlag]
@ProblemID int
AS
Update Pts_Problems SET ReadByAssignee=1,AssigneeReadTime=GetDate() WHERE ProblemID=@ProblemID AND ReadByAssignee=0')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_SetAssigneeReadFlag Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_SetAssigneeReadFlag Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_UpdateProblemChildNum Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_UpdateProblemChildNum Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE procedure [dbo].[sp_Pts_UpdateProblemChildNum]
@ProblemID int
AS
DECLARE @TotalChildNum int
DECLARE @TotalClosedChild int

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

UPDATE Pts_Problems Set TotalChildNum=@TotalChildNum,ClosedChildNum=@TotalClosedChild WHERE ProblemID=@ProblemID')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_UpdateProblemChildNum Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_UpdateProblemChildNum Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_SetParentProblem Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_SetParentProblem Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_SetParentProblem]
@ProblemID int,
@ParentID int
AS
UPDATE Pts_Problems Set ParentID=@ParentID WHERE ProblemID=@ProblemID

exec sp_Pts_UpdateProblemChildNum @ParentID')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_SetParentProblem Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_SetParentProblem Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Stat_ByAssignee Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Stat_ByAssignee Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_Stat_ByAssignee]
@ProjectID int
AS
if @ProjectID=0 
BEGIN
	Select count(*) as ProblemCount,AssignedTo
	from v_Pts_Problems where IsClosed=0
	group by AssignedTo
END
ELSE
BEGIN
	Select count(*) as ProblemCount,AssignedTo
	from v_Pts_Problems where IsClosed=0 and ProjectID=@ProjectID
	group by AssignedTo
END')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Stat_ByAssignee Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Stat_ByAssignee Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Stat_ByCreator Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Stat_ByCreator Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_Stat_ByCreator]
@ProjectID int
AS
if @ProjectID=0 
BEGIN
	Select count(*) as ProblemCount,
			sum(case when IsClosed=1 then 1 else 0 end) ClosedNum , 
			sum(case when IsClosed=0 then 1 else 0 end) OpenNum,
			CreateUserID
	from v_Pts_Problems 
	group by CreateUserID
END
ELSE
BEGIN
	Select count(*) as ProblemCount,
			sum(case when IsClosed=1 then 1 else 0 end) ClosedNum , 
			sum(case when IsClosed=0 then 1 else 0 end) OpenNum,
			CreateUserID
	from v_Pts_Problems where ProjectID=@ProjectID
	group by CreateUserID
END')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Stat_ByCreator Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Stat_ByCreator Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Stat_ByProblemState Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Stat_ByProblemState Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('CREATE PROCEDURE [dbo].[sp_Pts_Stat_ByProblemState]
@ProjectID int
AS
select 	Pts_ProblemState.ProblemStateID,
		Pts_ProblemState.StateName,
						sum(case when ProblemID is not null then 1 else 0 end) TotalNum, 
						sum(case when IsClosed=0 then 1 else 0 end) OpenNum,
						sum(case when IsClosed=1 then 1 else 0 end) ClosedNum 
					from v_Pts_Problems  right outer join 
						Pts_ProblemState on v_Pts_Problems.ProblemStateID = Pts_ProblemState.ProblemStateID
					Where Pts_ProblemState.ProjectID = @ProjectID AND v_Pts_Problems.ProjectID = @ProjectID
					group by Pts_ProblemState.ProblemStateID, Pts_ProblemState.StateName, Pts_ProblemState.ListOrder 
					Order by Pts_ProblemState.ListOrder')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Stat_ByProblemState Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Stat_ByProblemState Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_Stat_GetGlobalNum Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_Stat_GetGlobalNum Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('ALTER Procedure [dbo].[sp_Pts_Stat_GetGlobalNum]
@ProjectID int
AS
--获取某个项目的事务总数、打开的事务数、关闭的事务数
select 	sum(case when ProblemID is not null then 1 else 0 end) TotalNum, 
	sum(case when IsClosed=1 then 1 else 0 end) ClosedNum , 
	sum(case when IsClosed=0 then 1 else 0 end) OpenNum,
	sum(case when IsActive=0 then 1 else 0 end) SuspendNum,
	sum(case when AssignedTo<0 then 1 else 0 end) ToWorkgroup
	
from v_Pts_Problems  where ProjectID = @ProjectID')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Stat_GetGlobalNum Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_Stat_GetGlobalNum Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_Subscription_Add Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_Subscription_Add Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('ALTER PROCEDURE [dbo].[sp_Pts_Subscription_Add]
@UserID int,
@TargetType int,
@TargetID int,
@EventType int,
@Params nvarchar(1024)
AS
if NOT EXISTS(SELECT * FROM Pts_Subscription WHERE UserID=@UserID AND TargetType=@TargetType AND TargetID=@TargetID AND EventType=@EventType AND Params=@Params)
BEGIN
INSERT INTO Pts_Subscription (UserID,TargetType, TargetID, EventType, Params) VALUES(@UserID,@TargetType,@TargetID, @EventType, @Params)
END')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Subscription_Add Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_Subscription_Add Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_UpdateProblemAttachNum Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_UpdateProblemAttachNum Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create procedure [dbo].[sp_Pts_UpdateProblemAttachNum]
@ProblemID int
AS

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

UPDATE Pts_Problems SET AttachNum=@AttachNum WHERE ProblemID=@ProblemID')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_UpdateProblemAttachNum Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_UpdateProblemAttachNum Procedure'
END
GO

--
-- Script To Delete dbo.sp_Pts_UpdateProblemCreateRole Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Deleting dbo.sp_Pts_UpdateProblemCreateRole Procedure'
GO

   DROP PROCEDURE [dbo].[sp_Pts_UpdateProblemCreateRole]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_UpdateProblemCreateRole Procedure Deleted Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Delete dbo.sp_Pts_UpdateProblemCreateRole Procedure'
END
GO

--
-- Script To Delete dbo.sp_Pts_UpdateProblemState Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Deleting dbo.sp_Pts_UpdateProblemState Procedure'
GO

   DROP PROCEDURE [dbo].[sp_Pts_UpdateProblemState]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_UpdateProblemState Procedure Deleted Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Delete dbo.sp_Pts_UpdateProblemState Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_UpdateProject Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_UpdateProject Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


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

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_UpdateProject Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_UpdateProject Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_UpdateUserField Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_UpdateUserField Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('ALTER  Procedure [dbo].[sp_Pts_UpdateUserField]
@ID int,
@Label	nvarchar(100),
@ListOrder int,
@InUse	bit,
@Required bit,
@DefaultValue ntext = null,
@SelectItems nvarchar(2000) = null,
@FieldHelp nvarchar(255) = null,
@FormatString nvarchar(255) = null,
@HtmlEdit bit,
@MaxLength int = null,
@Width	varchar(50) = null,
@Height varchar(50) = null,
@GlobalSelectID int = null,
@UseSelectList	bit = false,
@UseGlobalSelect bit = false,
@VisibleWhenNew bit = true,
@VisibleWhenEdit bit = true,
@VisibleInList bit = false
AS

Update Pts_DbDirectory
Set
	Label = @Label,
	ListOrder = @ListOrder,
	InUse = @InUse,
	Required = @Required,
	DefaultValue = @DefaultValue,
	SelectItems = @SelectItems,
	FieldHelp = @FieldHelp,
	FormatString = @FormatString,
	HtmlEdit = @HtmlEdit,
	MaxLength = @MaxLength,
	Width = @Width,
	Height = @Height,
	GlobalSelectID = @GlobalSelectID,
	UseSelectList = @UseSelectList,
	UseGlobalSelect = @UseGlobalSelect,
	[VisibleWhenNew] = @VisibleWhenNew,
	[VisibleWhenEdit] = @VisibleWhenEdit,
	[VisibleInList] = @VisibleInList
Where 
	[ID] = @ID')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_UpdateUserField Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_UpdateUserField Procedure'
END
GO

--
-- Script To Create dbo.sp_Pts_Utility_UpdateAllIssueAttachNum Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Creating dbo.sp_Pts_Utility_UpdateAllIssueAttachNum Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


exec('create procedure [dbo].[sp_Pts_Utility_UpdateAllIssueAttachNum]
AS
--更新所有事务的附件数量
declare rs CURSOR LOCAL SCROLL FOR
select problemID, COUNT(1) FROM Pts_ProblemAttachments GROUP BY ProblemID

DECLARE @ProblemID int
DECLARE @AttachNum int

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

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

CLOSE rs')
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Utility_UpdateAllIssueAttachNum Procedure Added Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Add dbo.sp_Pts_Utility_UpdateAllIssueAttachNum Procedure'
END
GO

--
-- Script To Delete dbo.sp_Pts_Workflow_AddWorkflowStep Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Deleting dbo.sp_Pts_Workflow_AddWorkflowStep Procedure'
GO

   DROP PROCEDURE [dbo].[sp_Pts_Workflow_AddWorkflowStep]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Workflow_AddWorkflowStep Procedure Deleted Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Delete dbo.sp_Pts_Workflow_AddWorkflowStep Procedure'
END
GO

--
-- Script To Update dbo.sp_Pts_Workflow_GetAvilibleNextStateList Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.sp_Pts_Workflow_GetAvilibleNextStateList Procedure'
GO

SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL ON
GO

SET NUMERIC_ROUNDABORT OFF
GO


SET QUOTED_IDENTIFIER OFF
GO

SET ANSI_NULLS OFF
GO

exec('--获取问题状态列表中未被设置为某状态的可转化状态的列表
ALTER   PROCEDURE [dbo].[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')
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Workflow_GetAvilibleNextStateList Procedure Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.sp_Pts_Workflow_GetAvilibleNextStateList Procedure'
END
GO

--
-- Script To Delete dbo.sp_Pts_Workflow_UpdateWorkflowStep Procedure In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Deleting dbo.sp_Pts_Workflow_UpdateWorkflowStep Procedure'
GO

   DROP PROCEDURE [dbo].[sp_Pts_Workflow_UpdateWorkflowStep]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.sp_Pts_Workflow_UpdateWorkflowStep Procedure Deleted Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Delete dbo.sp_Pts_Workflow_UpdateWorkflowStep Procedure'
END
GO

--
-- Script To Update dbo.Pts_ProjectCatalogs Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_ProjectCatalogs Table'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Projects_Pts_ProjectCatalogs')
      ALTER TABLE [dbo].[Pts_Projects] WITH NOCHECK ADD CONSTRAINT [FK_Pts_Projects_Pts_ProjectCatalogs] FOREIGN KEY ([ProjectCatalogID]) REFERENCES [dbo].[Pts_ProjectCatalogs] ([ProjectCatalogID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_ProjectCatalogs Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_ProjectCatalogs Table'
END
GO


--
-- Script To Update dbo.Pts_Projects Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_Projects Table'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Projects_Pts_ProjectCatalogs')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [FK_Pts_Projects_Pts_ProjectCatalogs]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Projects_Pts_ProjectCatalogs')
      ALTER TABLE [dbo].[Pts_Projects] WITH NOCHECK ADD CONSTRAINT [FK_Pts_Projects_Pts_ProjectCatalogs] FOREIGN KEY ([ProjectCatalogID]) REFERENCES [dbo].[Pts_ProjectCatalogs] ([ProjectCatalogID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Projects_Pts_ProjectStates')
      ALTER TABLE [dbo].[Pts_Projects] DROP CONSTRAINT [FK_Pts_Projects_Pts_ProjectStates]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Projects_Pts_ProjectStates')
      ALTER TABLE [dbo].[Pts_Projects] WITH NOCHECK ADD CONSTRAINT [FK_Pts_Projects_Pts_ProjectStates] FOREIGN KEY ([ProjectStateID]) REFERENCES [dbo].[Pts_ProjectStates] ([ProjectStateID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_DbDirectory_Pts_Projects')
      ALTER TABLE [dbo].[Pts_DbDirectory] ADD CONSTRAINT [FK_Pts_DbDirectory_Pts_Projects] FOREIGN KEY ([ProjectID]) REFERENCES [dbo].[Pts_Projects] ([ProjectID]) ON DELETE CASCADE
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Filters_Pts_Projects')
      ALTER TABLE [dbo].[Pts_Filters] ADD CONSTRAINT [FK_Pts_Filters_Pts_Projects] FOREIGN KEY ([ProjectID]) REFERENCES [dbo].[Pts_Projects] ([ProjectID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemCatalogs_Pts_Projects')
      ALTER TABLE [dbo].[Pts_ProblemCatalogs] WITH NOCHECK ADD CONSTRAINT [FK_Pts_ProblemCatalogs_Pts_Projects] FOREIGN KEY ([ProjectID]) REFERENCES [dbo].[Pts_Projects] ([ProjectID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemCreateRoles_Pts_Projects')
      ALTER TABLE [dbo].[Pts_ProblemCreateRoles] ADD CONSTRAINT [FK_Pts_ProblemCreateRoles_Pts_Projects] FOREIGN KEY ([ProjectID]) REFERENCES [dbo].[Pts_Projects] ([ProjectID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemPriority_Pts_Projects')
      ALTER TABLE [dbo].[Pts_ProblemPriority] WITH NOCHECK ADD CONSTRAINT [FK_Pts_ProblemPriority_Pts_Projects] FOREIGN KEY ([ProjectID]) REFERENCES [dbo].[Pts_Projects] ([ProjectID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_Projects')
      ALTER TABLE [dbo].[Pts_Problems] WITH NOCHECK ADD CONSTRAINT [FK_Pts_Problems_Pts_Projects] FOREIGN KEY ([ProjectID]) REFERENCES [dbo].[Pts_Projects] ([ProjectID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_Projects1')
      ALTER TABLE [dbo].[Pts_Problems] WITH NOCHECK ADD CONSTRAINT [FK_Pts_Problems_Pts_Projects1] FOREIGN KEY ([OriginProjectID]) REFERENCES [dbo].[Pts_Projects] ([ProjectID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemSeverities_Pts_Projects')
      ALTER TABLE [dbo].[Pts_ProblemSeverity] WITH NOCHECK ADD CONSTRAINT [FK_Pts_ProblemSeverities_Pts_Projects] FOREIGN KEY ([ProjectID]) REFERENCES [dbo].[Pts_Projects] ([ProjectID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemState_Pts_Projects')
      ALTER TABLE [dbo].[Pts_ProblemState] WITH NOCHECK ADD CONSTRAINT [FK_Pts_ProblemState_Pts_Projects] FOREIGN KEY ([ProjectID]) REFERENCES [dbo].[Pts_Projects] ([ProjectID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemType_Pts_Projects')
      ALTER TABLE [dbo].[Pts_ProblemType] WITH NOCHECK ADD CONSTRAINT [FK_Pts_ProblemType_Pts_Projects] FOREIGN KEY ([ProjectID]) REFERENCES [dbo].[Pts_Projects] ([ProjectID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_RecordType_Pts_Projects')
      ALTER TABLE [dbo].[Pts_RecordType] WITH NOCHECK ADD CONSTRAINT [FK_Pts_RecordType_Pts_Projects] FOREIGN KEY ([ProjectID]) REFERENCES [dbo].[Pts_Projects] ([ProjectID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_SharedItems_Pts_Projects')
      ALTER TABLE [dbo].[Pts_SharedItems] ADD CONSTRAINT [FK_Pts_SharedItems_Pts_Projects] FOREIGN KEY ([ProjectID]) REFERENCES [dbo].[Pts_Projects] ([ProjectID]) ON DELETE CASCADE
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_WorkflowSteps_Pts_Projects')
      ALTER TABLE [dbo].[Pts_WorkflowSteps] ADD CONSTRAINT [FK_Pts_WorkflowSteps_Pts_Projects] FOREIGN KEY ([ProjectID]) REFERENCES [dbo].[Pts_Projects] ([ProjectID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Workgroups_Pts_Projects')
      ALTER TABLE [dbo].[Pts_Workgroups] WITH NOCHECK ADD CONSTRAINT [FK_Pts_Workgroups_Pts_Projects] FOREIGN KEY ([ProjectID]) REFERENCES [dbo].[Pts_Projects] ([ProjectID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_Projects Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_Projects Table'
END
GO


--
-- Script To Update dbo.Pts_DbDirectory Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_DbDirectory Table'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_DbDirectory_Pts_Projects')
      ALTER TABLE [dbo].[Pts_DbDirectory] DROP CONSTRAINT [FK_Pts_DbDirectory_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_DbDirectory_Pts_Projects')
      ALTER TABLE [dbo].[Pts_DbDirectory] ADD CONSTRAINT [FK_Pts_DbDirectory_Pts_Projects] FOREIGN KEY ([ProjectID]) REFERENCES [dbo].[Pts_Projects] ([ProjectID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemFieldEditableState_Pts_DbDirectory')
      ALTER TABLE [dbo].[Pts_ProblemFieldEditableState] ADD CONSTRAINT [FK_Pts_ProblemFieldEditableState_Pts_DbDirectory] FOREIGN KEY ([FieldID]) REFERENCES [dbo].[Pts_DbDirectory] ([ID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemFieldEditableGroup_Pts_DbDirectory')
      ALTER TABLE [dbo].[Pts_ProblemFieldEditableWorkgroup] ADD CONSTRAINT [FK_Pts_ProblemFieldEditableGroup_Pts_DbDirectory] FOREIGN KEY ([FieldID]) REFERENCES [dbo].[Pts_DbDirectory] ([ID]) ON DELETE CASCADE
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemFieldReadableGroup_Pts_DbDirectory')
      ALTER TABLE [dbo].[Pts_ProblemFieldReadableWorkgroup] ADD CONSTRAINT [FK_Pts_ProblemFieldReadableGroup_Pts_DbDirectory] FOREIGN KEY ([FieldID]) REFERENCES [dbo].[Pts_DbDirectory] ([ID]) ON UPDATE CASCADE ON DELETE CASCADE
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_DbDirectory Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_DbDirectory Table'
END
GO


--
-- Script To Update dbo.Pts_ProblemFieldEditableState Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_ProblemFieldEditableState Table'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemFieldEditableState_Pts_DbDirectory')
      ALTER TABLE [dbo].[Pts_ProblemFieldEditableState] DROP CONSTRAINT [FK_Pts_ProblemFieldEditableState_Pts_DbDirectory]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemFieldEditableState_Pts_DbDirectory')
      ALTER TABLE [dbo].[Pts_ProblemFieldEditableState] ADD CONSTRAINT [FK_Pts_ProblemFieldEditableState_Pts_DbDirectory] FOREIGN KEY ([FieldID]) REFERENCES [dbo].[Pts_DbDirectory] ([ID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_ProblemFieldEditableState Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_ProblemFieldEditableState Table'
END
GO


--
-- Script To Update dbo.Pts_Problems Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_Problems Table'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Accounts_Users')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [FK_Pts_Problems_Accounts_Users]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Accounts_Users')
      ALTER TABLE [dbo].[Pts_Problems] WITH NOCHECK ADD CONSTRAINT [FK_Pts_Problems_Accounts_Users] FOREIGN KEY ([CreateUserID]) REFERENCES [dbo].[Accounts_Users] ([UserID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_ProblemPriority')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [FK_Pts_Problems_Pts_ProblemPriority]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_ProblemPriority')
      ALTER TABLE [dbo].[Pts_Problems] WITH NOCHECK ADD CONSTRAINT [FK_Pts_Problems_Pts_ProblemPriority] FOREIGN KEY ([ProblemPriorityID]) REFERENCES [dbo].[Pts_ProblemPriority] ([ProblemPriorityID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_ProblemSeverities')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [FK_Pts_Problems_Pts_ProblemSeverities]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_ProblemSeverities')
      ALTER TABLE [dbo].[Pts_Problems] WITH NOCHECK ADD CONSTRAINT [FK_Pts_Problems_Pts_ProblemSeverities] FOREIGN KEY ([ProblemSeverityID]) REFERENCES [dbo].[Pts_ProblemSeverity] ([ProblemSeverityID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_ProblemState')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [FK_Pts_Problems_Pts_ProblemState]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_ProblemState')
      ALTER TABLE [dbo].[Pts_Problems] WITH NOCHECK ADD CONSTRAINT [FK_Pts_Problems_Pts_ProblemState] FOREIGN KEY ([ProblemStateID]) REFERENCES [dbo].[Pts_ProblemState] ([ProblemStateID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_ProblemType')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [FK_Pts_Problems_Pts_ProblemType]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_ProblemType')
      ALTER TABLE [dbo].[Pts_Problems] WITH NOCHECK ADD CONSTRAINT [FK_Pts_Problems_Pts_ProblemType] FOREIGN KEY ([ProblemTypeID]) REFERENCES [dbo].[Pts_ProblemType] ([ProblemTypeID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_Projects')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [FK_Pts_Problems_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_Projects')
      ALTER TABLE [dbo].[Pts_Problems] WITH NOCHECK ADD CONSTRAINT [FK_Pts_Problems_Pts_Projects] FOREIGN KEY ([ProjectID]) REFERENCES [dbo].[Pts_Projects] ([ProjectID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_Projects1')
      ALTER TABLE [dbo].[Pts_Problems] DROP CONSTRAINT [FK_Pts_Problems_Pts_Projects1]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Problems_Pts_Projects1')
      ALTER TABLE [dbo].[Pts_Problems] WITH NOCHECK ADD CONSTRAINT [FK_Pts_Problems_Pts_Projects1] FOREIGN KEY ([OriginProjectID]) REFERENCES [dbo].[Pts_Projects] ([ProjectID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemAttachments_Pts_Problems')
      ALTER TABLE [dbo].[Pts_ProblemAttachments] WITH NOCHECK ADD CONSTRAINT [FK_Pts_ProblemAttachments_Pts_Problems] FOREIGN KEY ([ProblemID]) REFERENCES [dbo].[Pts_Problems] ([ProblemID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemHistory_Pts_Problems')
      ALTER TABLE [dbo].[Pts_ProblemHistory] WITH NOCHECK ADD CONSTRAINT [FK_Pts_ProblemHistory_Pts_Problems] FOREIGN KEY ([ProblemID]) REFERENCES [dbo].[Pts_Problems] ([ProblemID]) ON DELETE CASCADE
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemRelations_Pts_Problems')
      ALTER TABLE [dbo].[Pts_ProblemRelations] WITH NOCHECK ADD CONSTRAINT [FK_Pts_ProblemRelations_Pts_Problems] FOREIGN KEY ([ProblemID_1]) REFERENCES [dbo].[Pts_Problems] ([ProblemID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemRelations_Pts_Problems1')
      ALTER TABLE [dbo].[Pts_ProblemRelations] WITH NOCHECK ADD CONSTRAINT [FK_Pts_ProblemRelations_Pts_Problems1] FOREIGN KEY ([ProblemID_2]) REFERENCES [dbo].[Pts_Problems] ([ProblemID]) ON DELETE CASCADE
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_ProblemStateHistory_Pts_Problems')
      ALTER TABLE [dbo].[Pts_ProblemStateRecord] WITH NOCHECK ADD CONSTRAINT [FK_Pts_ProblemStateHistory_Pts_Problems] FOREIGN KEY ([ProblemID]) REFERENCES [dbo].[Pts_Problems] ([ProblemID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Records_Pts_Problems')
      ALTER TABLE [dbo].[Pts_Records] WITH NOCHECK ADD CONSTRAINT [FK_Pts_Records_Pts_Problems] FOREIGN KEY ([ProblemID]) REFERENCES [dbo].[Pts_Problems] ([ProblemID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_Problems Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_Problems Table'
END
GO


--
-- Script To Update dbo.Pts_Records Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_Records Table'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Records_Pts_Problems')
      ALTER TABLE [dbo].[Pts_Records] DROP CONSTRAINT [FK_Pts_Records_Pts_Problems]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_Records_Pts_Problems')
      ALTER TABLE [dbo].[Pts_Records] WITH NOCHECK ADD CONSTRAINT [FK_Pts_Records_Pts_Problems] FOREIGN KEY ([ProblemID]) REFERENCES [dbo].[Pts_Problems] ([ProblemID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_RecordAttachments_Pts_Records')
      ALTER TABLE [dbo].[Pts_RecordAttachments] WITH NOCHECK ADD CONSTRAINT [FK_Pts_RecordAttachments_Pts_Records] FOREIGN KEY ([RecordID]) REFERENCES [dbo].[Pts_Records] ([RecordID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_Records Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_Records Table'
END
GO


--
-- Script To Update dbo.Pts_WorkflowSteps Table In (local).URTracker320
-- Generated 星期三, 三月 18, 2009, at 02:08 PM
--
-- Please backup (local).URTracker320 before executing this script
--


BEGIN TRANSACTION
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

PRINT 'Updating dbo.Pts_WorkflowSteps Table'
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_WorkflowSteps_Pts_ProblemState')
      ALTER TABLE [dbo].[Pts_WorkflowSteps] DROP CONSTRAINT [FK_Pts_WorkflowSteps_Pts_ProblemState]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_WorkflowSteps_Pts_ProblemState')
      ALTER TABLE [dbo].[Pts_WorkflowSteps] ADD CONSTRAINT [FK_Pts_WorkflowSteps_Pts_ProblemState] FOREIGN KEY ([SrcStateID]) REFERENCES [dbo].[Pts_ProblemState] ([ProblemStateID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_WorkflowSteps_Pts_ProblemState1')
      ALTER TABLE [dbo].[Pts_WorkflowSteps] DROP CONSTRAINT [FK_Pts_WorkflowSteps_Pts_ProblemState1]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_WorkflowSteps_Pts_ProblemState1')
      ALTER TABLE [dbo].[Pts_WorkflowSteps] ADD CONSTRAINT [FK_Pts_WorkflowSteps_Pts_ProblemState1] FOREIGN KEY ([DstStateID]) REFERENCES [dbo].[Pts_ProblemState] ([ProblemStateID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_WorkflowSteps_Pts_Projects')
      ALTER TABLE [dbo].[Pts_WorkflowSteps] DROP CONSTRAINT [FK_Pts_WorkflowSteps_Pts_Projects]
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
   IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = N'FK_Pts_WorkflowSteps_Pts_Projects')
      ALTER TABLE [dbo].[Pts_WorkflowSteps] ADD CONSTRAINT [FK_Pts_WorkflowSteps_Pts_Projects] FOREIGN KEY ([ProjectID]) REFERENCES [dbo].[Pts_Projects] ([ProjectID])
GO

IF @@ERROR <> 0
   IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION
GO

IF @@TRANCOUNT = 1
BEGIN
   PRINT 'dbo.Pts_WorkflowSteps Table Updated Successfully'
   COMMIT TRANSACTION
END ELSE
BEGIN
   PRINT 'Failed To Update dbo.Pts_WorkflowSteps Table'
END
GO

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

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

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

select @RecordCount = @intRootRecordCount

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

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

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


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

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


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

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

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

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

GO

ALTER TABLE [Pts_Workgroups] DROP CONSTRAINT [FK_Pts_Workgroups_Pts_Projects]
GO

-- 更新事务的附件标志
exec sp_Pts_Utility_UpdateAllIssueAttachNum

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

GO


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

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


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

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

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

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

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

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

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

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



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

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

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

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

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

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

--输出
SELECT * FROM @t


GO

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

GO

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


GO


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

GO

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

GO

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

ALTER Procedure [dbo].[sp_Pts_Filter_RecentAssignedToMe]
@ProjectID int,
@UserID int,
@Count int
AS
--等待我处理的事务
SELECT top 500 ProblemID, ProblemCode, Title, ProblemStateName, AssignedTo, LastTime,ReadByAssignee
FROM v_Pts_Problems
WHERE
	ProjectID = @ProjectID
AND 
	IsClosed = 0
AND
	((AssignedTo > 0 AND AssignedTo = @UserID)
	OR
	(AssignedTo < 0 AND -AssignedTo in (SELECT DISTINCT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID=@UserID))
	OR
	ProblemID IN (SELECT ProblemID FROM Pts_Coworkers WHERE UserID=@UserID AND IsWorked=0)
	)
Order BY LastTime DESC,CreateTime DESC

GO


------------------------------------------------------------------------------------------------
ALTER PROCEDURE [sp_Pts_GetUserViewableFields]
@ProjectID int,
@UserID int,
@ProblemID int
AS
BEGIN
--字段对所有人可见，
-- 或者字段对创建人可见，而且用户是事务的创建人;或者用户在所允许的工作组中
SELECT * FROM Pts_DbDirectory
WHERE	ProjectID = @ProjectID
	AND
		(IsSystem = 0 or (IsSystem = 1 AND InUse = 1))
	AND
	(
		ReadableToAll = 1 
		 OR 
		(
			(ReadableToCreateUser = 1 AND Exists(SELECT ProblemID FROM Pts_Problems WHERE ProblemID = @ProblemID AND CreateUserID=@UserID))
			OR
			ID IN (SELECT FieldID FROM Pts_ProblemFieldReadableWorkgroup WHERE 
				WorkgroupID IN (SELECT WorkgroupID FROM Pts_WorkgroupUsers WHERE UserID = @UserID)
					)
		)
	)
	
		
ORDER BY 
	ListOrder ASC


END

GO