SET NUMERIC_ROUNDABORT OFF
GO
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors')) DROP TABLE #tmpErrors
GO
CREATE TABLE #tmpErrors (Error int)
GO
SET XACT_ABORT ON
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO
BEGIN TRANSACTION
GO
PRINT N'Altering [dbo].[PR_Crm_ClientHistory_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_ClientHistory_Add]
    (
      @ClientHistoryID INT ,
      @ClientID NVARCHAR(20) ,
      @Actionname NVARCHAR(50) ,
      @ActionTime DATETIME ,
      @UserIp NVARCHAR(50) ,
      @UserName NVARCHAR(100) ,
      @Remark NVARCHAR(200)
    )
AS 
    INSERT  INTO pe_ClientHistory
            ( ClientHistoryID ,
              ClientID ,
              Actionname ,
              ActionTime ,
              UserIp ,
              UserName ,
              Remark 
            )
    VALUES  ( @ClientHistoryID ,
              @ClientID ,
              @Actionname ,
              @ActionTime ,
              @UserIp ,
              @UserName ,
              @Remark 
            )
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PE_Department]'
GO
ALTER TABLE [dbo].[PE_Department] ADD
[SerialNumberYear] [int] NULL
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Contents_CommentPK_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_CommentPK_Add]
    (
      @PKId INT ,
      @CommentId INT ,
      @Title NVARCHAR(255) ,
      @Content NTEXT ,
      @IP NVARCHAR(50) ,
      @UpdateTime DATETIME ,
      @UserName NVARCHAR(100) ,
      @Position INT
    )
AS 
    INSERT  INTO PE_CommentPK
            ( PKId ,
              CommentId ,
              Title ,
              [Content] ,
              IP ,
              UpdateTime ,
              UserName ,
              [Position]
	      )
    VALUES  ( @PKId ,
              @CommentId ,
              @Title ,
              @Content ,
              @IP ,
              @UpdateTime ,
              @UserName ,
              @Position
	      )
    RETURN
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Contents_Comment_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Comment_Add]
    (
      @CommentId INT ,
      @GeneralId INT ,
      @TopicId INT ,
      @NodeId INT ,
      @CommentTitle NVARCHAR(255) ,
      @Content NTEXT ,
      @UpdateTime DATETIME ,
      @Score INT ,
      @Position INT ,
      @Status BIT ,
      @Ip NVARCHAR(50) ,
      @IsPrivate BIT ,
      @UserName NVARCHAR(100) ,
      @Face NVARCHAR(255)
    )
AS 
    INSERT  INTO [PE_Comment]
            ( CommentId ,
              GeneralId ,
              TopicId ,
              NodeId ,
              CommentTitle ,
              [Content] ,
              UpdateTime ,
              Score ,
              [Position] ,
              Status ,
              Agree ,
              Oppose ,
              Neutral ,
              IP ,
              IsElite ,
              IsPrivate ,
              UserName ,
              Face
		)
    VALUES  ( @CommentId ,
              @GeneralId ,
              @TopicId ,
              @NodeId ,
              @CommentTitle ,
              @Content ,
              @UpdateTime ,
              @Score ,
              @Position ,
              @Status ,
              0 ,
              0 ,
              0 ,
              @IP ,
              0 ,
              @IsPrivate ,
              @UserName ,
              @Face
		)
    RETURN
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Accessories_PaymentLog_Update2]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_PaymentLog_Update2]
    (
      @PaymentLogID INT ,
      @UserName NVARCHAR(100) ,
      @OrderID INT ,
      @PaymentNum NVARCHAR(50) ,
      @PlatformID INT ,
      @MoneyPay MONEY ,
      @MoneyTrue MONEY ,
      @PayTime DATETIME ,
      @SuccessTime DATETIME ,
      @Status INT ,
      @PlatformInfo NVARCHAR(200) ,
      @Remark NVARCHAR(255) ,
      @Point INT ,
      @TradeNum NVARCHAR(50)
    )
AS 
    UPDATE  [PE_PaymentLog]
    SET     [UserName] = @UserName ,
            [OrderID] = @OrderID ,
            [PaymentNum] = @PaymentNum ,
            [PlatformID] = @PlatformID ,
            [MoneyPay] = @MoneyPay ,
            [MoneyTrue] = @MoneyTrue ,
            [PayTime] = @PayTime ,
            [SuccessTime] = @SuccessTime ,
            [Status] = @Status ,
            [PlatformInfo] = @PlatformInfo ,
            [Remark] = @Remark ,
            [Point] = @Point ,
            [TradeNum] = @TradeNum
    WHERE   [PaymentLogID] = @PaymentLogID
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Shop_PaymentLog_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_PaymentLog_Add]
    (
      @PaymentLogID INT ,
      @UserName NVARCHAR(100) ,
      @OrderID INT ,
      @PaymentNum NVARCHAR(50) ,
      @PlatformID INT ,
      @MoneyPay MONEY ,
      @MoneyTrue MONEY ,
      @PayTime DATETIME ,
      @SuccessTime DATETIME ,
      @Status INT ,
      @PlatformInfo NVARCHAR(200) ,
      @Remark NVARCHAR(255) ,
      @Point INT ,
      @TradeNum NVARCHAR(50)
    )
AS 
    INSERT  INTO [PE_PaymentLog]
            ( [PaymentLogID] ,
              [UserName] ,
              [OrderID] ,
              [PaymentNum] ,
              [PlatformID] ,
              [MoneyPay] ,
              [MoneyTrue] ,
              [PayTime] ,
              [SuccessTime] ,
              [Status] ,
              [PlatformInfo] ,
              [Remark] ,
              [Point] ,
              [TradeNum]
		)
    VALUES  ( @PaymentLogID ,
              @UserName ,
              @OrderID ,
              @PaymentNum ,
              @PlatformID ,
              @MoneyPay ,
              @MoneyTrue ,
              @PayTime ,
              @SuccessTime ,
              @Status ,
              @PlatformInfo ,
              @Remark ,
              @Point ,
              @TradeNum
	      )
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Shop_OrderHistory_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_OrderHistory_Add]
    (
      @OrderHistoryID INT ,
      @Ordernum NVARCHAR(20) ,
      @Actionname NVARCHAR(50) ,
      @ActionTime DATETIME ,
      @UserIp NVARCHAR(50) ,
      @UserName NVARCHAR(100) ,
      @Remark NVARCHAR(200)
    )
AS 
    INSERT  INTO pe_OrderHistory
            ( OrderHistoryID ,
              Ordernum ,
              Actionname ,
              ActionTime ,
              UserIp ,
              UserName ,
              Remark 
            )
    VALUES  ( @OrderHistoryID ,
              @Ordernum ,
              @Actionname ,
              @ActionTime ,
              @UserIp ,
              @UserName ,
              @Remark 
            )
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PE_Present]'
GO
ALTER TABLE [dbo].[PE_Present] ADD
[EnableSale] [bit] NULL
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Crm_Contacter_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_Contacter_Update]
    (
      @ContacterID INT ,
      @ClientID INT ,
      @ParentID INT ,
      @UserName NVARCHAR(100) ,
      @UserType INT ,
      @TrueName NVARCHAR(20) ,
      @Sex INT ,
      @Title NVARCHAR(50) ,
      @Company NVARCHAR(100) ,
      @Department NVARCHAR(50) ,
      @Position NVARCHAR(50) ,
      @Operation NVARCHAR(50) ,
      @CompanyAddress NVARCHAR(200) ,
      @Email NVARCHAR(100) ,
      @Homepage NVARCHAR(255) ,
      @QQ NVARCHAR(20) ,
      @ICQ NVARCHAR(25) ,
      @MSN NVARCHAR(100) ,
      @Yahoo NVARCHAR(100) ,
      @UC NVARCHAR(100) ,
      @Aim NVARCHAR(100) ,
      @OfficePhone NVARCHAR(30) ,
      @HomePhone NVARCHAR(30) ,
      @PHS NVARCHAR(30) ,
      @Fax NVARCHAR(30) ,
      @Mobile NVARCHAR(30) ,
      @Country NVARCHAR(50) ,
      @Province NVARCHAR(30) ,
      @City NVARCHAR(30) ,
      @Address NVARCHAR(255) ,
      @ZipCode NVARCHAR(10) ,
      @NativePlace NVARCHAR(50) ,
      @Nation NVARCHAR(50) ,
      @Birthday DATETIME ,
      @IDCard NVARCHAR(20) ,
      @Marriage INT ,
      @Family NTEXT ,
      @Income INT ,
      @Education INT ,
      @GraduateFrom NVARCHAR(255) ,
      @InterestsOfLife NVARCHAR(255) ,
      @InterestsOfCulture NVARCHAR(255) ,
      @InterestsOfAmusement NVARCHAR(255) ,
      @InterestsOfSport NVARCHAR(255) ,
      @InterestsOfOther NVARCHAR(255) ,
      @UpdateTime DATETIME
    )
AS 
    UPDATE  [PE_Contacter]
    SET     [ClientID] = @ClientID ,
            [ParentID] = @ParentID ,
            [ContacterID] = @ContacterID ,
            [UserName] = @UserName ,
            [UserType] = @UserType ,
            [TrueName] = @TrueName ,
            [Sex] = @Sex ,
            [Title] = @Title ,
            [Company] = @Company ,
            [Department] = @Department ,
            [Position] = @Position ,
            [Operation] = @Operation ,
            [CompanyAddress] = @CompanyAddress ,
            [Email] = @Email ,
            [Homepage] = @Homepage ,
            [QQ] = @QQ ,
            [ICQ] = @ICQ ,
            [MSN] = @MSN ,
            [Yahoo] = @Yahoo ,
            [UC] = @UC ,
            [Aim] = @Aim ,
            [OfficePhone] = @OfficePhone ,
            [HomePhone] = @HomePhone ,
            [PHS] = @PHS ,
            [Fax] = @Fax ,
            [Mobile] = @Mobile ,
            [Country] = @Country ,
            [Province] = @Province ,
            [City] = @City ,
            [Address] = @Address ,
            [ZipCode] = @ZipCode ,
            [NativePlace] = @NativePlace ,
            [Nation] = @Nation ,
            [Birthday] = @Birthday ,
            [IDCard] = @IDCard ,
            [Marriage] = @Marriage ,
            [Family] = @Family ,
            [Income] = @Income ,
            [Education] = @Education ,
            [GraduateFrom] = @GraduateFrom ,
            [InterestsOfLife] = @InterestsOfLife ,
            [InterestsOfCulture] = @InterestsOfCulture ,
            [InterestsOfAmusement] = @InterestsOfAmusement ,
            [InterestsOfSport] = @InterestsOfSport ,
            [InterestsOfOther] = @InterestsOfOther ,
            [UpdateTime] = @UpdateTime
    WHERE   [ContacterID] = @ContacterID
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Crm_Contacter_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_Contacter_Add]
    (
      @ContacterID INT ,
      @ClientID INT ,
      @ParentID INT ,
      @UserName NVARCHAR(100) ,
      @UserType INT ,
      @TrueName NVARCHAR(20) ,
      @Sex INT ,
      @Title NVARCHAR(50) ,
      @Company NVARCHAR(100) ,
      @Department NVARCHAR(50) ,
      @Position NVARCHAR(50) ,
      @Operation NVARCHAR(50) ,
      @CompanyAddress NVARCHAR(200) ,
      @Email NVARCHAR(100) ,
      @Homepage NVARCHAR(255) ,
      @QQ NVARCHAR(20) ,
      @ICQ NVARCHAR(25) ,
      @MSN NVARCHAR(100) ,
      @Yahoo NVARCHAR(100) ,
      @UC NVARCHAR(100) ,
      @Aim NVARCHAR(100) ,
      @OfficePhone NVARCHAR(30) ,
      @HomePhone NVARCHAR(30) ,
      @PHS NVARCHAR(30) ,
      @Fax NVARCHAR(30) ,
      @Mobile NVARCHAR(30) ,
      @Country NVARCHAR(50) ,
      @Province NVARCHAR(30) ,
      @City NVARCHAR(30) ,
      @Address NVARCHAR(255) ,
      @ZipCode NVARCHAR(10) ,
      @NativePlace NVARCHAR(50) ,
      @Nation NVARCHAR(50) ,
      @Birthday DATETIME ,
      @IDCard NVARCHAR(20) ,
      @Marriage INT ,
      @Family NTEXT ,
      @Income INT ,
      @Education INT ,
      @GraduateFrom NVARCHAR(255) ,
      @InterestsOfLife NVARCHAR(255) ,
      @InterestsOfCulture NVARCHAR(255) ,
      @InterestsOfAmusement NVARCHAR(255) ,
      @InterestsOfSport NVARCHAR(255) ,
      @InterestsOfOther NVARCHAR(255) ,
      @CreateTime DATETIME ,
      @UpdateTime DATETIME ,
      @Owner NVARCHAR(50) = ''  
    )
AS 
    INSERT  INTO [PE_Contacter]
            ( [ContacterID] ,
              [ClientID] ,
              [ParentID] ,
              [UserName] ,
              [UserType] ,
              [TrueName] ,
              [Sex] ,
              [Title] ,
              [Company] ,
              [Department] ,
              [Position] ,
              [Operation] ,
              [CompanyAddress] ,
              [Email] ,
              [Homepage] ,
              [QQ] ,
              [ICQ] ,
              [MSN] ,
              [Yahoo] ,
              [UC] ,
              [Aim] ,
              [OfficePhone] ,
              [HomePhone] ,
              [PHS] ,
              [Fax] ,
              [Mobile] ,
              [Country] ,
              [Province] ,
              [City] ,
              [Address] ,
              [ZipCode] ,
              [NativePlace] ,
              [Nation] ,
              [Birthday] ,
              [IDCard] ,
              [Marriage] ,
              [Family] ,
              [Income] ,
              [Education] ,
              [GraduateFrom] ,
              [InterestsOfLife] ,
              [InterestsOfCulture] ,
              [InterestsOfAmusement] ,
              [InterestsOfSport] ,
              [InterestsOfOther] ,
              [CreateTime] ,
              [UpdateTime] ,
              [Owner]    
		)
    VALUES  ( @ContacterID ,
              @ClientID ,
              @ParentID ,
              @UserName ,
              @UserType ,
              @TrueName ,
              @Sex ,
              @Title ,
              @Company ,
              @Department ,
              @Position ,
              @Operation ,
              @CompanyAddress ,
              @Email ,
              @Homepage ,
              @QQ ,
              @ICQ ,
              @MSN ,
              @Yahoo ,
              @UC ,
              @Aim ,
              @OfficePhone ,
              @HomePhone ,
              @PHS ,
              @Fax ,
              @Mobile ,
              @Country ,
              @Province ,
              @City ,
              @Address ,
              @ZipCode ,
              @NativePlace ,
              @Nation ,
              @Birthday ,
              @IDCard ,
              @Marriage ,
              @Family ,
              @Income ,
              @Education ,
              @GraduateFrom ,
              @InterestsOfLife ,
              @InterestsOfCulture ,
              @InterestsOfAmusement ,
              @InterestsOfSport ,
              @InterestsOfOther ,
              @CreateTime ,
              @UpdateTime ,
              @Owner
	      )
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PE_Client]'
GO
ALTER TABLE [dbo].[PE_Client] ADD
[OwnerChanger] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PE_Model]'
GO
ALTER TABLE [dbo].[PE_Model] ADD
[TitleField] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Contents_Model_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Model_Update]
    @ModelID INT ,
    @ModelName NVARCHAR(50) ,
    @Description NVARCHAR(250) = '' ,
    @TableName NVARCHAR(50) ,
    @ItemName NVARCHAR(50) ,
    @ItemUnit NVARCHAR(50) ,
    @ItemIcon NVARCHAR(255) ,
    @IsCountHits BIT ,
    @ModelType INT ,
    @Disabled BIT ,
    @DefaultTemplateFile NVARCHAR(255) = '' ,
    @EnableCharge BIT ,
    @EnableSignin BIT ,
    @EnableVote BIT ,
    @AddInfoFilePath NVARCHAR(200) ,
    @ManageInfoFilePath NVARCHAR(200) ,
    @PreviewInfoFilePath NVARCHAR(200) ,
    @BatchInfoFilePath NVARCHAR(200) ,
    @MaxPerUser INT ,
    @PrintTemplate NVARCHAR(255) ,
    @SearchTemplate NVARCHAR(255) ,
    @AdvanceSearchFormTemplate NVARCHAR(255) ,
    @AdvanceSearchTemplate NVARCHAR(255) ,
    @ChargeTips NVARCHAR(255) ,
    @NeedPointChargeTips NVARCHAR(255) ,
    @OutTimeChargeTips NVARCHAR(255) ,
    @UsePointChargeTips NVARCHAR(500) ,
    @CommentManageTemplate NVARCHAR(255) ,
    @UserAddContentTemplate NVARCHAR(255) ,
    @AnonymouseTemplate NVARCHAR(255) ,
    @EnableVerificationCode BIT ,
    @EnableWap BIT ,
    @WapField NTEXT ,
	@TitleField NVARCHAR(255)
AS 
    UPDATE  [PE_Model]
    SET     ModelID = @ModelID ,
            ModelName = @ModelName ,
            Description = @Description ,
            TableName = @TableName ,
            ItemName = @ItemName ,
            ItemUnit = @ItemUnit ,
            ItemIcon = @ItemIcon ,
            IsCountHits = @IsCountHits ,
            ModelType = @ModelType ,
            Disabled = @Disabled ,
            DefaultTemplateFile = @DefaultTemplateFile ,
            EnableCharge = @EnableCharge ,
            EnableSignin = @EnableSignin ,
            AddInfoFilePath = @AddInfoFilePath ,
            ManageInfoFilePath = @ManageInfoFilePath ,
            PreviewInfoFilePath = @PreviewInfoFilePath ,
            BatchInfoFilePath = @BatchInfoFilePath ,
            MaxPerUser = @MaxPerUser ,
            PrintTemplate = @PrintTemplate ,
            EnableVote = @EnableVote ,
            SearchTemplate = @SearchTemplate ,
            AdvanceSearchFormTemplate = @AdvanceSearchFormTemplate ,
            AdvanceSearchTemplate = @AdvanceSearchTemplate ,
            ChargeTips = @ChargeTips ,
            NeedPointChargeTips = @NeedPointChargeTips ,
            OutTimeChargeTips = @OutTimeChargeTips ,
            UsePointChargeTips = @UsePointChargeTips ,
            CommentManageTemplate = @CommentManageTemplate ,
            UserAddContentTemplate = @UserAddContentTemplate ,
            AnonymouseTemplate = @AnonymouseTemplate ,
            EnableVerificationCode = @EnableVerificationCode ,
            EnableWap = @EnableWap ,
            WapField = @WapField ,
			TitleField = @TitleField
    WHERE   ModelID = @ModelID
    RETURN
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Contents_Model_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Model_Add]
    @ModelID INT ,
    @ModelName NVARCHAR(50) ,
    @Description NVARCHAR(250) = '' ,
    @TableName NVARCHAR(50) ,
    @ItemName NVARCHAR(50) ,
    @ItemUnit NVARCHAR(50) ,
    @ItemIcon NVARCHAR(255) ,
    @IsCountHits BIT ,
    @ModelType INT ,
    @Disabled BIT ,
    @Field NTEXT = '' ,
    @DefaultTemplateFile NVARCHAR(255) = '' ,
    @EnableCharge BIT ,
    @EnableSignin BIT ,
    @EnableVote BIT ,
    @AddInfoFilePath NVARCHAR(200) ,
    @ManageInfoFilePath NVARCHAR(200) ,
    @PreviewInfoFilePath NVARCHAR(200) ,
    @ChargeTips NVARCHAR(255) ,
    @NeedPointChargeTips NVARCHAR(255) ,
    @OutTimeChargeTips NVARCHAR(255) ,
    @UsePointChargeTips NVARCHAR(500) ,
    @BatchInfoFilePath NVARCHAR(200) ,
    @Character INT ,
    @MaxPerUser INT ,
    @PrintTemplate NVARCHAR(255) ,
    @SearchTemplate NVARCHAR(255) ,
    @AdvanceSearchFormTemplate NVARCHAR(255) ,
    @AdvanceSearchTemplate NVARCHAR(255) ,
    @CommentManageTemplate NVARCHAR(255) ,
    @UserAddContentTemplate NVARCHAR(255) ,
    @AnonymouseTemplate NVARCHAR(255) ,
    @EnableVerificationCode BIT ,
    @EnableWap BIT ,
    @WapField NTEXT,
	@TitleField NVARCHAR(255)
AS 
    INSERT  INTO [PE_Model]
            ( [ModelID] ,
              [ModelName] ,
              [Description] ,
              [TableName] ,
              [ItemName] ,
              [ItemUnit] ,
              [ItemIcon] ,
              [IsCountHits] ,
              [ModelType] ,
              [Disabled] ,
              [Field] ,
              [DefaultTemplateFile] ,
              [EnableCharge] ,
              [EnableSignin] ,
              [EnableVote] ,
              [AddInfoFilePath] ,
              [ManageInfoFilePath] ,
              [PreviewInfoFilePath] ,
              [BatchInfoFilePath] ,
              [Character] ,
              [MaxPerUser] ,
              [PrintTemplate] ,
              [SearchTemplate] ,
              [AdvanceSearchFormTemplate] ,
              [AdvanceSearchTemplate] ,
              [ChargeTips] ,
              [NeedPointChargeTips] ,
              [OutTimeChargeTips] ,
              [UsePointChargeTips] ,
              [CommentManageTemplate] ,
              [UserAddContentTemplate] ,
              [AnonymouseTemplate] ,
              [EnableVerificationCode] ,
              [EnableWap] ,
              [WapField] ,
			  [TitleField]
		)
    VALUES  ( @ModelID ,
              @ModelName ,
              @Description ,
              @TableName ,
              @ItemName ,
              @ItemUnit ,
              @ItemIcon ,
              @IsCountHits ,
              @ModelType ,
              @Disabled ,
              @Field ,
              @DefaultTemplateFile ,
              @EnableCharge ,
              @EnableSignin ,
              @EnableVote ,
              @AddInfoFilePath ,
              @ManageInfoFilePath ,
              @PreviewInfoFilePath ,
              @BatchInfoFilePath ,
              @Character ,
              @MaxPerUser ,
              @PrintTemplate ,
              @SearchTemplate ,
              @AdvanceSearchFormTemplate ,
              @AdvanceSearchTemplate ,
              @ChargeTips ,
              @NeedPointChargeTips ,
              @OutTimeChargeTips ,
              @UsePointChargeTips ,
              @CommentManageTemplate ,
              @UserAddContentTemplate ,
              @AnonymouseTemplate ,
              @EnableVerificationCode ,
              @EnableWap ,
              @WapField ,
			  @TitleField
	      )
    BEGIN
		-- 不存在创建指定数据表
        EXEC('CREATE TABLE [dbo].'+ @TableName+' (ID int,CONSTRAINT [PK_'+@TableName+'_ID] PRIMARY KEY CLUSTERED
        (
        [ID] ASC
        ))')

    END
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Crm_Address_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_Address_Update]
    (
      @AddressID INT ,    --地址Id
      @UserName NVARCHAR(100) ,    --用户名
      @ConsigneeName NVARCHAR(255) ,
      @HomePhone NVARCHAR(30) ,    --固定电话
      @Mobile NVARCHAR(30) ,    --移动电话
      @Country NVARCHAR(50) ,    --国家
      @Province NVARCHAR(30) ,    --省份
      @City NVARCHAR(30) ,    --城市
      @Area NVARCHAR(30) ,
      @Address NVARCHAR(255) ,    --详细地址
      @ZipCode NVARCHAR(50)    --邮编
    )
AS 
    UPDATE  [PE_Address]
    SET     [AddressID] = @AddressID ,    --地址Id
            [UserName] = @UserName ,    --用户名
            [ConsigneeName] = @ConsigneeName ,
            [HomePhone] = @HomePhone ,    --固定电话
            [Mobile] = @Mobile ,    --移动电话
            [Country] = @Country ,    --国家
            [Province] = @Province ,    --省份	
            [City] = @City ,    --城市
            [Area] = @Area ,
            [Address] = @Address ,    --详细地址
            [ZipCode] = @ZipCode    --邮编
    WHERE   [AddressID] = @AddressID
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Crm_Address_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_Address_Add]
    (
      @AddressID INT ,    --地址Id
      @UserName NVARCHAR(100) ,    --用户名
      @HomePhone NVARCHAR(30) = '' ,    --固定电话
      @Mobile NVARCHAR(30) = '' ,    --移动电话
      @Country NVARCHAR(50) ,    --国家
      @Province NVARCHAR(30) ,    --省份
      @City NVARCHAR(30) = '' ,    --城市
      @Area NVARCHAR(30) = '' ,
      @Address NVARCHAR(255) ,    --详细地址
      @ZipCode NVARCHAR(50) = '' ,    --邮编
      @ConsigneeName NVARCHAR(255) = ''
	
    )
AS 
    DECLARE @IsDefault BIT
    SET @IsDefault = 0
    IF NOT EXISTS ( SELECT  *
                    FROM    PE_Address
                    WHERE   UserName = @UserName ) 
        BEGIN
            SET @IsDefault = 1
        END

    INSERT  INTO [PE_Address]
            ( [AddressID] ,    --地址Id
              [UserName] ,    --用户名
              [ConsigneeName] ,
              [HomePhone] ,    --固定电话
              [Mobile] ,    --移动电话
              [Country] ,    --国家
              [Province] ,    --省份
              [City] ,    --城市
              [Area] ,
              [Address] ,    --详细地址
              [ZipCode] ,    --邮编
              [IsDefault]    --是否默认
		)
    VALUES  ( @AddressID ,
              @UserName ,
              @ConsigneeName ,
              @HomePhone ,
              @Mobile ,
              @Country ,
              @Province ,
              @City ,
              @Area ,
              @Address ,
              @ZipCode ,
              @IsDefault
	      )
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Accessories_BankrollItem_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_BankrollItem_Update]
    @ItemId INT ,
    @UserName NVARCHAR(100) ,
    @ClientId INT ,
    @DateAndTime DATETIME ,
    @Money MONEY ,
    @MoneyType INT ,
    @CurrencyType INT ,
    @eBankId INT ,
    @Bank NVARCHAR(50) ,
    @OrderID INT ,
    @PaymentId INT ,
    @Remark NVARCHAR(255) ,
    @LogTime DATETIME ,
    @Inputer NVARCHAR(50) ,
    @IP NVARCHAR(50) ,
    @Status INT ,
    @IsConsume BIT
AS 
    SET NOCOUNT OFF

    UPDATE  [dbo].[PE_BankrollItem]
    SET     [UserName] = @UserName ,
            [ClientId] = @ClientId ,
            [DateAndTime] = @DateAndTime ,
            [Money] = @Money ,
            [MoneyType] = @MoneyType ,
            [CurrencyType] = @CurrencyType ,
            [eBankId] = @eBankId ,
            [Bank] = @Bank ,
            [OrderId] = @OrderId ,
            [PaymentId] = @PaymentId ,
            [Remark] = @Remark ,
            [LogTime] = @LogTime ,
            [Inputer] = @Inputer ,
            [IP] = @IP ,
            [Status] = @Status ,
            [IsConsume] = @IsConsume
    WHERE   [ItemId] = @ItemId
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Accessories_BankrollItem_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_BankrollItem_Add]
    @UserName NVARCHAR(100) = NULL ,
    @ClientID INT = 0 ,
    @DateAndTime DATETIME = 0 ,
    @Money MONEY = 0 ,
    @MoneyType INT = 0 ,
    @CurrencyType INT = 0 ,
    @eBankID INT = 0 ,
    @Bank NVARCHAR(50) = NULL ,
    @OrderID INT = 0 ,
    @PaymentID INT = 0 ,
    @Remark NVARCHAR(255) = NULL ,
    @LogTime DATETIME = 0 ,
    @Inputer NVARCHAR(50) = NULL ,
    @IP NVARCHAR(50) = NULL ,
    @Status INT ,
    @Memo NTEXT ,
    @IsConsume BIT
AS 
    SET NOCOUNT OFF

    INSERT  INTO [dbo].[PE_BankrollItem]
            ( [UserName] ,
              [ClientID] ,
              [DateAndTime] ,
              [Money] ,
              [MoneyType] ,
              [CurrencyType] ,
              [eBankID] ,
              [Bank] ,
              [OrderID] ,
              [PaymentID] ,
              [Remark] ,
              [LogTime] ,
              [Inputer] ,
              [IP] ,
              [Status] ,
              [Memo] ,
              [IsConsume]
            )
    VALUES  ( @UserName ,
              @ClientID ,
              @DateAndTime ,
              @Money ,
              @MoneyType ,
              @CurrencyType ,
              @eBankID ,
              @Bank ,
              @OrderID ,
              @PaymentID ,
              @Remark ,
              @LogTime ,
              @Inputer ,
              @IP ,
              @Status ,
              @Memo ,
              @IsConsume
            )
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_UserManage_Users_Update]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_Users_Update]
    (
      @UserID INT ,
      @GroupID INT ,
      @CompanyID INT ,
      @ClientID INT ,
      @UserType INT ,
      @UserName NVARCHAR(100) ,
      @UserPassword NVARCHAR(32) ,
      @LastPassword NVARCHAR(32) ,
      @Question NVARCHAR(50) ,
      @Answer NVARCHAR(50) ,
      @Email NVARCHAR(100) ,
      @Sex INT ,
      @RegTime DATETIME ,
      @JoinTime DATETIME ,
      @LoginTimes INT ,
      @LastLoginTime DATETIME ,
      @LastPresentTime DATETIME ,
      @LastLoginIP NVARCHAR(50) ,
      @LastPasswordChangedTime DATETIME ,
      @LastLockoutTime DATETIME ,
      @FailedPasswordAttemptCount INT ,
      @FirstFailedPasswordAttempTime DATETIME ,
      @FailedPasswordAnswerAttempCount INT ,
      @FirstFailedPasswordAnswerAttempTime DATETIME ,
      @Status INT ,
      @CheckNum CHAR(10) ,
      @EnableResetPassword BIT ,
      @UserCultureName NVARCHAR(10) ,
      @UserFace NVARCHAR(255) ,
      @FaceWidth INT ,
      @FaceHeight INT ,
      @Sign NTEXT ,
      @PrivacySetting INT ,
      @Balance MONEY ,
      @UserPoint INT ,
      @UserExp INT ,
      @ConsumeMoney INT ,
      @ConsumePoint INT ,
      @ConsumeExp INT ,
      @PostItems INT ,
      @PassedItems INT ,
      @RejectItems INT ,
      @DelItems INT ,
      @EndTime DATETIME ,
      @IsInheritGroupRole BIT ,
      @TrueName NVARCHAR(255) ,
      @PayPassword NVARCHAR(32) ,
      @IsShowUpgradeTipsFalse BIT ,
      @LastAddPointTime DATETIME ,
      @GetPasswordSid NVARCHAR(32) ,
      @GetPasswordTime DATETIME ,
      @HonorType INT
    )
AS 
    UPDATE  [PE_Users]
    SET     [UserID] = @UserID ,
            [GroupID] = @GroupID ,
            [CompanyID] = @CompanyID ,
            [ClientID] = @ClientID ,
            [UserType] = @UserType ,
            [UserName] = @UserName ,
            [UserPassword] = @UserPassword ,
            [LastPassword] = @LastPassword ,
            [Question] = @Question ,
            [Answer] = @Answer ,
            [Email] = @Email ,
            [Sex] = @Sex ,
            [RegTime] = @RegTime ,
            [JoinTime] = @JoinTime ,
            [LoginTimes] = @LoginTimes ,
            [LastLoginTime] = @LastLoginTime ,
            [LastPresentTime] = @LastPresentTime ,
            [LastLoginIP] = @LastLoginIP ,
            [LastPasswordChangedTime] = @LastPasswordChangedTime ,
            [LastLockoutTime] = @LastLockoutTime ,
            [FailedPasswordAttemptCount] = @FailedPasswordAttemptCount ,
            [FirstFailedPasswordAttempTime] = @FirstFailedPasswordAttempTime ,
            [FailedPasswordAnswerAttempCount] = @FailedPasswordAnswerAttempCount ,
            [FirstFailedPasswordAnswerAttempTime] = @FirstFailedPasswordAnswerAttempTime ,
            [Status] = @Status ,
            [CheckNum] = @CheckNum ,
            [EnableResetPassword] = @EnableResetPassword ,
            [UserCultureName] = @UserCultureName ,
            [UserFace] = @UserFace ,
            [FaceWidth] = @FaceWidth ,
            [FaceHeight] = @FaceHeight ,
            [Sign] = @Sign ,
            [PrivacySetting] = @PrivacySetting ,
            [Balance] = @Balance ,
            [UserPoint] = @UserPoint ,
            [UserExp] = @UserExp ,
            [ConsumeMoney] = @ConsumeMoney ,
            [ConsumePoint] = @ConsumePoint ,
            [ConsumeExp] = @ConsumeExp ,
            [PostItems] = @PostItems ,
            [PassedItems] = @PassedItems ,
            [RejectItems] = @RejectItems ,
            [DelItems] = @DelItems ,
            [EndTime] = @EndTime ,
            [IsInheritGroupRole] = @IsInheritGroupRole ,
            [TrueName] = @TrueName ,
            [PayPassword] = @PayPassword ,
            [IsShowUpgradeTipsFalse] = @IsShowUpgradeTipsFalse ,
            [LastAddPointTime] = @LastAddPointTime ,
            [GetPasswordSid] = @GetPasswordSid ,
            [GetPasswordTime] = @GetPasswordTime ,
            [HonorType] = @HonorType
    WHERE   [UserID] = @UserID
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Shop_ShoppingCarts_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_ShoppingCarts_Add]
    (
      @UserName NVARCHAR(100) ,
      @CartID NVARCHAR(50) ,
      @ProductID INT ,
      @Quantity INT ,
      @IsPresent BIT ,
      @UpdateTime DATETIME ,
      @TableName NVARCHAR(200) ,
      @Property NVARCHAR(50) ,
      @BelongsToProductID INT
	
    )
AS 
    INSERT  INTO PE_ShoppingCarts
            ( UserName ,
              CartID ,
              ProductID ,
              Quantity ,
              IsPresent ,
              UpdateTime ,
              TableName ,
              Property ,
              BelongsToProductID	
	      )
    VALUES  ( @UserName ,
              @CartID ,
              @ProductID ,
              @Quantity ,
              @IsPresent ,
              @UpdateTime ,
              @TableName ,
              @Property ,
              @BelongsToProductID	
	      )
	
    RETURN
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_UserManage_Users_Add]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_Users_Add]
    (
      @UserID INT ,
      @GroupID INT ,
      @CompanyID INT ,
      @ClientID INT ,
      @UserType INT ,
      @UserName NVARCHAR(100) ,
      @UserPassword NVARCHAR(32) ,
      @LastPassword NVARCHAR(32) ,
      @Question NVARCHAR(50) ,
      @Answer NVARCHAR(50) ,
      @Email NVARCHAR(100) ,
      @Sex INT ,
      @RegTime DATETIME ,
      @JoinTime DATETIME ,
      @LoginTimes INT ,
      @LastLoginTime DATETIME ,
      @LastPresentTime DATETIME ,
      @LastLoginIP NVARCHAR(255) ,
      @LastPasswordChangedTime DATETIME ,
      @LastLockoutTime DATETIME ,
      @FailedPasswordAttemptCount INT ,
      @FirstFailedPasswordAttempTime DATETIME ,
      @FailedPasswordAnswerAttempCount INT ,
      @FirstFailedPasswordAnswerAttempTime DATETIME ,
      @Status INT ,
      @CheckNum CHAR(10) ,
      @EnableResetPassword BIT ,
      @UserCultureName NVARCHAR(10) ,
      @UserFace NVARCHAR(255) ,
      @FaceWidth INT ,
      @FaceHeight INT ,
      @Sign NTEXT ,
      @PrivacySetting INT ,
      @Balance MONEY ,
      @UserPoint INT ,
      @UserExp INT ,
      @ConsumeMoney INT ,
      @ConsumePoint INT ,
      @ConsumeExp INT ,
      @PostItems INT ,
      @PassedItems INT ,
      @RejectItems INT ,
      @DelItems INT ,
      @EndTime DATETIME ,
      @IsInheritGroupRole BIT ,
      @UserFriendGroup NVARCHAR(255) ,
      @TrueName NVARCHAR(255) ,
      @PayPassword NVARCHAR(32) ,
      @IsShowUpgradeTipsFalse BIT ,
      @LastAddPointTime DATETIME ,
      @GetPasswordSid NVARCHAR(32) ,
      @GetPasswordTime DATETIME ,
      @AlipayUserID NVARCHAR(16) ,
      @HonorType INT 
    )
AS 
    INSERT  INTO [PE_Users]
            ( [UserID] ,
              [GroupID] ,
              [CompanyID] ,
              [ClientID] ,
              [UserType] ,
              [UserName] ,
              [UserPassword] ,
              [LastPassword] ,
              [Question] ,
              [Answer] ,
              [Email] ,
              [Sex] ,
              [RegTime] ,
              [JoinTime] ,
              [LoginTimes] ,
              [LastLoginTime] ,
              [LastPresentTime] ,
              [LastLoginIP] ,
              [LastPasswordChangedTime] ,
              [LastLockoutTime] ,
              [FailedPasswordAttemptCount] ,
              [FirstFailedPasswordAttempTime] ,
              [FailedPasswordAnswerAttempCount] ,
              [FirstFailedPasswordAnswerAttempTime] ,
              [Status] ,
              [CheckNum] ,
              [EnableResetPassword] ,
              [UserCultureName] ,
              [UserFace] ,
              [FaceWidth] ,
              [FaceHeight] ,
              [Sign] ,
              [PrivacySetting] ,
              [Balance] ,
              [UserPoint] ,
              [UserExp] ,
              [ConsumeMoney] ,
              [ConsumePoint] ,
              [ConsumeExp] ,
              [PostItems] ,
              [PassedItems] ,
              [RejectItems] ,
              [DelItems] ,
              [EndTime] ,
              [IsInheritGroupRole] ,
              [UserFriendGroup] ,
              [TrueName] ,
              [PayPassword] ,
              [IsShowUpgradeTipsFalse] ,
              [LastAddPointTime] ,
              [GetPasswordSid] ,
              [GetPasswordTime] ,
              [Alipay_UserID] ,
              [HonorType]
		)
    VALUES  ( @UserID ,
              @GroupID ,
              @CompanyID ,
              @ClientID ,
              @UserType ,
              @UserName ,
              @UserPassword ,
              @LastPassword ,
              @Question ,
              @Answer ,
              @Email ,
              @Sex ,
              @RegTime ,
              @JoinTime ,
              @LoginTimes ,
              @LastLoginTime ,
              @LastPresentTime ,
              @LastLoginIP ,
              @LastPasswordChangedTime ,
              @LastLockoutTime ,
              @FailedPasswordAttemptCount ,
              @FirstFailedPasswordAttempTime ,
              @FailedPasswordAnswerAttempCount ,
              @FirstFailedPasswordAnswerAttempTime ,
              @Status ,
              @CheckNum ,
              @EnableResetPassword ,
              @UserCultureName ,
              @UserFace ,
              @FaceWidth ,
              @FaceHeight ,
              @Sign ,
              @PrivacySetting ,
              @Balance ,
              @UserPoint ,
              @UserExp ,
              @ConsumeMoney ,
              @ConsumePoint ,
              @ConsumeExp ,
              @PostItems ,
              @PassedItems ,
              @RejectItems ,
              @DelItems ,
              @EndTime ,
              @IsInheritGroupRole ,
              @UserFriendGroup ,
              @TrueName ,
              @PayPassword ,
              @IsShowUpgradeTipsFalse ,
              @LastAddPointTime ,
              @GetPasswordSid ,
              @GetPasswordTime ,
              @AlipayUserID ,
              @HonorType
	      )
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_UserManage_Cards_Update]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_Cards_Update]
    (
      @CardID INT ,    --充值卡ID
      @CardType INT ,    --充值卡类型
      @ProductID INT ,    --充值卡所属商品
      @TableName NVARCHAR(255) ,
      @CardNum NVARCHAR(30) ,    --卡号
      @Password NVARCHAR(50) ,    --密码
      @AgentName NVARCHAR(100) ,    --代理商
      @Money MONEY ,    --面值
      @ValidNum INT ,    --点数
      @ValidUnit INT ,    --点数单位
      @EndDate DATETIME ,    --截止日期
      @UserName NVARCHAR(100) ,    --使用者
      @UseTime DATETIME ,    --充值时间
      @CreateTime DATETIME ,    --生成时间
      @OrderItemID INT    --售出
    )
AS 
    UPDATE  [PE_Cards]
    SET     [CardType] = @CardType ,    --充值卡类型
            [ProductID] = @ProductID ,    --充值卡所属商品
            [TableName] = @TableName ,
            [CardNum] = @CardNum ,    --卡号
            [Password] = @Password ,    --密码
            [AgentName] = @AgentName ,    --代理商
            [Money] = @Money ,    --面值
            [ValidNum] = @ValidNum ,    --点数
            [ValidUnit] = @ValidUnit ,    --点数单位
            [EndDate] = @EndDate ,    --截止日期
            [UserName] = @UserName ,    --使用者
            [UseTime] = @UseTime ,    --充值时间
            [CreateTime] = @CreateTime ,    --生成时间
            [OrderItemID] = @OrderItemID    --售出
    WHERE   [CardID] = @CardID
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_UserManage_Cards_Add]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_Cards_Add]
    (
      @CardType INT ,
      @ProductID INT ,
      @TableName NVARCHAR(255) = '' ,
      @CardNum NVARCHAR(30) ,
      @Password NVARCHAR(50) ,
      @AgentName NVARCHAR(100) ,
      @Money MONEY ,
      @ValidNum INT ,
      @ValidUnit INT ,
      @EndDate DATETIME ,
      @UserName NVARCHAR(100) ,
      @CreateTime DATETIME ,
      @OrderItemID INT ,
      @ProductName NVARCHAR(255)
    )
AS 
    IF EXISTS ( SELECT  *
                FROM    PE_Cards
                WHERE   CardNum = @CardNum
                        AND Password = @Password
                        AND ProductID = @ProductID ) 
        BEGIN
            RETURN 0 --存在着相同的充值卡,不添加记录,返回 0
        END
    ELSE 
        BEGIN
            INSERT  INTO [PE_Cards]
                    ( [CardType] ,
                      [ProductID] ,
                      [TableName] ,
                      [CardNum] ,
                      [Password] ,
                      [AgentName] ,
                      [Money] ,
                      [ValidNum] ,
                      [ValidUnit] ,
                      [EndDate] ,
                      [UserName] ,
                      [CreateTime] ,
                      [OrderItemID] ,
                      [ProductName]
                    )
            VALUES  ( @CardType ,
                      @ProductID ,
                      @TableName ,
                      @CardNum ,
                      @Password ,
                      @AgentName ,
                      @Money ,
                      @ValidNum ,
                      @ValidUnit ,
                      @EndDate ,
                      @UserName ,
                      @CreateTime ,
                      @OrderItemID ,
                      @ProductName
                    ) 
            RETURN @@ROWCOUNT
        END
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PE_Survey]'
GO
ALTER TABLE [dbo].[PE_Survey] ADD
[IsResultPublic] [bit] NULL CONSTRAINT [DF_PE_Survey_IsResultPublic] DEFAULT ((1))
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Survey_SurveyManager_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Survey_SurveyManager_Update]
    (
      @SurveyID INT ,
      @SurveyName NVARCHAR(50) ,
      @Description NTEXT ,
      @FileName NVARCHAR(50) ,
      @IPRepeat INT ,
      @CreateDate DATETIME ,
      @EndTime DATETIME ,
      @IsOpen INT ,
      @NeedLogin INT ,
      @PresentPoint INT ,
      @LockIPType INT ,
      @SetIPLock NTEXT ,
      @LockUrl NTEXT ,
      @SetPassword NVARCHAR(50) ,
      @Template NVARCHAR(255),
      @QuestionField NTEXT,
      @QuestionMaxID INT,
      @IsResultPublic BIT      
    )
AS 
    UPDATE  [PE_Survey]
    SET     [SurveyName] = @SurveyName ,
            [Description] = @Description ,
            [FileName] = @FileName ,
            [IPRepeat] = @IPRepeat ,
            [CreateDate] = @CreateDate ,
            [EndTime] = @EndTime ,
            [IsOpen] = @IsOpen ,
            [NeedLogin] = @NeedLogin ,
            [PresentPoint] = @PresentPoint ,
            [LockIPType] = @LockIPType ,
            [SetIPLock] = @SetIPLock ,
            [LockUrl] = @LockUrl ,
            [SetPassword] = @SetPassword ,
            [Template] = @Template,
            [QuestionField] = @QuestionField,
            [QuestionMaxID] = @QuestionMaxID,
            [IsResultPublic] = @IsResultPublic
    WHERE   [SurveyID] = @SurveyID
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Survey_SurveyManager_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Survey_SurveyManager_Add]
    (
      @SurveyID INT ,
      @SurveyName NVARCHAR(50) ,
      @Description NTEXT ,
      @FileName NVARCHAR(50) ,
      @IPRepeat INT ,
      @CreateDate DATETIME ,
      @EndTime DATETIME ,
      @IsOpen INT ,
      @NeedLogin INT ,
      @PresentPoint INT ,
      @LockIPType INT ,
      @SetIPLock NTEXT ,
      @LockUrl NTEXT ,
      @SetPassword NVARCHAR(50) ,
      @Template NVARCHAR(255),
      @QuestionField NTEXT,
      @QuestionMaxID INT,
      @IsResultPublic BIT
    )
AS 
    INSERT  INTO [PE_Survey]
            ( [SurveyID] ,
              [SurveyName] ,
              [Description] ,
              [FileName] ,
              [IPRepeat] ,
              [CreateDate] ,
              [EndTime] ,
              [IsOpen] ,
              [NeedLogin] ,
              [PresentPoint] ,
              [LockIPType] ,
              [SetIPLock] ,
              [LockUrl] ,
              [SetPassword] ,
              [Template],
              [QuestionField],
              [QuestionMaxID],
              [IsResultPublic]    
		)
    VALUES  ( @SurveyID ,
              @SurveyName ,
              @Description ,
              @FileName ,
              @IPRepeat ,
              @CreateDate ,
              @EndTime ,
              @IsOpen ,
              @NeedLogin ,
              @PresentPoint ,
              @LockIPType ,
              @SetIPLock ,
              @LockUrl ,
              @SetPassword ,
              @Template,
              @QuestionField,
              @QuestionMaxID,
              @IsResultPublic
	      )
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Shop_Order_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Order_Update]
    (
      @OrderID INT ,
      @UserName NVARCHAR(100) ,
      @AgentName NVARCHAR(50) ,
      @Functionary NVARCHAR(50) ,
      @MoneyTotal MONEY ,
      @MoneyGoods MONEY ,
      @NeedInvoice BIT ,
      @InvoiceContent NTEXT ,
      @BeginDate DATETIME ,
      @Address NVARCHAR(255) ,
      @ZipCode NVARCHAR(10) ,
      @Mobile NVARCHAR(50) ,
      @Phone NVARCHAR(50) ,
      @Email NVARCHAR(50) ,
      @PaymentType INT ,
      @DeliverType INT ,
      @Status INT ,
      @Discount_Payment FLOAT ,
      @MoneyReceipt MONEY ,
      @EnableDownload BIT ,
      @DeliverStatus INT ,
      @Charge_Deliver MONEY ,
      @Memo NTEXT ,
      @PresentMoney MONEY ,
      @PresentPoint INT ,
      @PresentExp INT ,
      @OrderType INT ,
      @CouponID INT ,
      @InputTime DATETIME ,
      @ContacterName NVARCHAR(50) ,
      @OrderKind INT
	
    )
AS 
    UPDATE  [PE_Orders]
    SET     [UserName] = @UserName ,
            [AgentName] = @AgentName ,
            [Functionary] = @Functionary ,
            [MoneyTotal] = @MoneyTotal ,
            [MoneyGoods] = @MoneyGoods ,
            [NeedInvoice] = @NeedInvoice ,
            [InvoiceContent] = @InvoiceContent ,
            [BeginDate] = @BeginDate ,
            [Address] = @Address ,
            [ZipCode] = @ZipCode ,
            [Mobile] = @Mobile ,
            [Phone] = @Phone ,
            [Email] = @Email ,
            [PaymentType] = @PaymentType ,
            [DeliverType] = @DeliverType ,
            [Status] = @Status ,
            [Discount_Payment] = @Discount_Payment ,
            [Charge_Deliver] = @Charge_Deliver ,
            [MoneyReceipt] = @MoneyReceipt ,
            [EnableDownload] = @EnableDownload ,
            [DeliverStatus] = @DeliverStatus ,
            [Memo] = @Memo ,
            [PresentMoney] = @PresentMoney ,
            [PresentPoint] = @PresentPoint ,
            [PresentExp] = @PresentExp ,
            [OrderType] = @OrderType ,
            [CouponID] = @CouponID ,
            [InputTime] = @InputTime ,
            [ContacterName] = @ContacterName ,
            [OrderKind] = @OrderKind
    WHERE   OrderID = @OrderID
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Shop_Refund_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Refund_Add]
    (
      @OrderID INT ,
      @Money MONEY ,
      @HandlingCharge MONEY ,
      @RefundType INT
	
    )
AS 
    DECLARE @ClientID INT ,
        @UserName NVARCHAR(100)
    SELECT  @ClientID = ClientID ,
            @UserName = UserName
    FROM    PE_Orders
    WHERE   OrderID = @OrderID
            AND MoneyReceipt >= ( @Money + @HandlingCharge )
	
    IF ( @UserName IS NOT NULL
         OR @ClientID IS NOT NULL
       ) 
        BEGIN	
            UPDATE  PE_Orders
            SET     Status = 1 ,
                    MoneyReceipt = MoneyReceipt - ( @Money + @HandlingCharge )
            WHERE   OrderID = @OrderID 
            IF ( @RefundType = 1 ) 
                BEGIN
                    IF ( @UserName IS NOT NULL
                         OR @ClientID IS NOT NULL
                       ) 
                        BEGIN
                            IF ( @UserName <> '' ) 
                                BEGIN
                                    UPDATE  PE_Users
                                    SET     Balance = Balance + @Money
                                    WHERE   UserName = @UserName
                                END
                            ELSE 
                                BEGIN
                                    UPDATE  PE_Client
                                    SET     Balance = Balance + @Money
                                    WHERE   ClientID = @ClientID
                                END
                        END
                END 
        END
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Shop_Order_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Order_Add]
    (
      @OrderID INT ,
      @OrderNum NVARCHAR(20) ,
      @UserName NVARCHAR(100) ,
      @AgentName NVARCHAR(50) ,
      @Functionary NVARCHAR(50) ,
      @ClientID INT ,
      @MoneyTotal MONEY ,
      @MoneyGoods MONEY ,
      @NeedInvoice BIT ,
      @InvoiceContent NTEXT ,
      @Invoiced BIT ,
      @MoneyReceipt MONEY ,
      @BeginDate DATETIME ,
      @InputTime DATETIME ,
      @ContacterName NVARCHAR(50) ,
      @Address NVARCHAR(255) ,
      @ZipCode NVARCHAR(10) ,
      @Mobile NVARCHAR(50) ,
      @Phone NVARCHAR(50) ,
      @Email NVARCHAR(50) ,
      @PaymentType INT ,
      @DeliverType INT ,
      @Status INT ,
      @DeliverStatus INT ,
      @EnableDownload BIT ,
      @PresentMoney MONEY ,
      @PresentPoint INT ,
      @PresentExp INT ,
      @Discount_Payment FLOAT ,
      @Charge_Deliver MONEY ,
      @Memo NTEXT ,
      @OrderType INT ,
      @CouponID INT ,
      @OrderKind INT
    )
AS 
    INSERT  INTO [PE_Orders]
            ( [OrderID] ,
              [OrderNum] ,
              [UserName] ,
              [AgentName] ,
              [Functionary] ,
              [ClientID] ,
              [MoneyTotal] ,
              [MoneyGoods] ,
              [NeedInvoice] ,
              [InvoiceContent] ,
              [Invoiced] ,
              [MoneyReceipt] ,
              [BeginDate] ,
              [InputTime] ,
              [ContacterName] ,
              [Address] ,
              [ZipCode] ,
              [Mobile] ,
              [Phone] ,
              [Email] ,
              [PaymentType] ,
              [DeliverType] ,
              [Status] ,
              [DeliverStatus] ,
              [EnableDownload] ,
              [PresentMoney] ,
              [PresentPoint] ,
              [PresentExp] ,
              [Discount_Payment] ,
              [Charge_Deliver] ,
              [Memo] ,
              [OrderType] ,
              [CouponID] ,
              [OrderKind]
		)
    VALUES  ( @OrderID ,
              @OrderNum ,
              @UserName ,
              @AgentName ,
              @Functionary ,
              @ClientID ,
              @MoneyTotal ,
              @MoneyGoods ,
              @NeedInvoice ,
              @InvoiceContent ,
              @Invoiced ,
              @MoneyReceipt ,
              @BeginDate ,
              @InputTime ,
              @ContacterName ,
              @Address ,
              @ZipCode ,
              @Mobile ,
              @Phone ,
              @Email ,
              @PaymentType ,
              @DeliverType ,
              @Status ,
              @DeliverStatus ,
              @EnableDownload ,
              @PresentMoney ,
              @PresentPoint ,
              @PresentExp ,
              @Discount_Payment ,
              @Charge_Deliver ,
              @Memo ,
              @OrderType ,
              @CouponID ,
              @OrderKind
	      )
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Shop_Invoice_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Invoice_Add]
    (
      @ClientId INT ,
      @UserName NVARCHAR(100) ,
      @OrderId INT ,
      @InvoiceType INT ,
      @InvoiceDate DATETIME ,
      @InvoiceNum NVARCHAR(50) ,
      @InvoiceTitle NVARCHAR(50) ,
      @InvoiceContent NTEXT ,
      @Memo NTEXT ,
      @TotalMoney MONEY ,
      @Drawer NVARCHAR(50) ,
      @Inputer NVARCHAR(50)
    )
AS 
    INSERT  INTO PE_InvoiceItem
            ( ClientID ,
              UserName ,
              OrderID ,
              InvoiceType ,
              InvoiceDate ,
              InvoiceNum ,
              InvoiceTitle ,
              InvoiceContent ,
              Memo ,
              TotalMoney ,
              Drawer ,
              Inputer ,
              InputTime
            )
    VALUES  ( @ClientId ,
              @UserName ,
              @OrderId ,
              @InvoiceType ,
              @InvoiceDate ,
              @InvoiceNum ,
              @InvoiceTitle ,
              @InvoiceContent ,
              @Memo ,
              @TotalMoney ,
              @Drawer ,
              @Inputer ,
              GETDATE()
            )
    
    UPDATE  PE_Orders
    SET     Invoiced = 1
    WHERE   OrderID = @OrderId
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PE_CommonModel]'
GO
ALTER TABLE [dbo].[PE_CommonModel] ADD
[SGType] [int] NULL CONSTRAINT [DF_PE_CommonModel_SGType] DEFAULT ((0)),
[SGDataId] [int] NULL CONSTRAINT [DF_PE_CommonModel_SGDataId] DEFAULT ((0))
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Shop_Order_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Order_Delete]
    (
      @OrderId NVARCHAR(4000) ,
      @ErrOrderNum NVARCHAR(4000) OUTPUT 
    )
AS 
    BEGIN

        DECLARE @UserName NVARCHAR(100) ,
            @ClientID INT ,
            @MoneyReceipt MONEY ,
            @Status INT ,
            @ID INT ,
            @OrderNum NVARCHAR(20)
        DECLARE cur1 CURSOR FOR SELECT UserName,ClientID,MoneyReceipt,Status,OrderID,OrderNum FROM PE_Orders WHERE CHARINDEX(',' + RTRIM(OrderID) + ',', ',' + CAST ((@OrderId) AS NVARCHAR(100)) + ',') > 0
        OPEN cur1
        FETCH NEXT FROM cur1 INTO @UserName, @ClientID, @MoneyReceipt, @Status, @ID, @OrderNum

        WHILE @@FETCH_STATUS = 0 
            BEGIN
                BEGIN
                    IF @MoneyReceipt > 0
                        OR @Status = 2 
                        BEGIN
                            IF @ErrOrderNum IS NULL 
                                SET @ErrOrderNum = @OrderNum
						--SET @ErrOrderNum = 'dfasdsd'
                            ELSE
						--SET @ErrOrderNum = @ErrOrderNum + 'dfasdsd'
                                SET @ErrOrderNum = @ErrOrderNum + ','
                                    + @OrderNum
					--ErrMsg = ErrMsg & "<li>订单：" & rsOrder("OrderFormNum") & "已经付款，不能再删除！</li>"
                        END
                    ELSE 
                        BEGIN
                            DECLARE @ProductID INT ,
                                @TableName NVARCHAR(255) ,
                                @Property NVARCHAR(255) ,
                                @Amount INT
                            DECLARE cur2 CURSOR FOR SELECT ProductID,TableName,[Property],Amount FROM PE_OrderItem WHERE OrderID = @ID
                            OPEN cur2
                            FETCH NEXT FROM cur2 INTO @ProductID,@TableName,@Property,@Amount
                            WHILE @@FETCH_STATUS = 0 
                                BEGIN
                                    UPDATE  PE_CommonProduct
                                    SET     OrderNum = OrderNum - @Amount
                                    WHERE   ProductID = @ProductID
                                            AND TableName = @TableName
                                    BEGIN
                                        IF @Property IS NOT NULL 
                                            UPDATE  PE_ProductData
                                            SET     OrderNum = OrderNum
                                                    - @Amount
                                            WHERE   ProductID = @ProductID
                                                    AND TableName = @TableName
                                                    AND PropertyValue = @Property
                                    END
                                    FETCH NEXT FROM cur2 INTO @ProductID,@TableName,@Property,@Amount
                                END
						
                            CLOSE cur2
                            DEALLOCATE cur2

                            DELETE  FROM PE_BankrollItem
                            WHERE   OrderID = @ID
                            DELETE  FROM PE_DeliverItem
                            WHERE   OrderID = @ID
                            DELETE  FROM PE_OrderItem
                            WHERE   OrderID = @ID
					--不能删除在线支付记录，因为有时会员对订单进行在线支付但不是即时成功，此时删除了这个订单，就会造成找不到实际应该成功的在线支付记录
					--DELETE FROM PE_Payment WHERE OrderID = @ID
                            DELETE  FROM PE_TransferLog
                            WHERE   OrderID = @ID
                            DELETE  FROM PE_Orders
                            WHERE   OrderID = @ID
                            DELETE  FROM PE_U_OrderText
                            WHERE   ID = @ID 
                        END
                END
                FETCH NEXT FROM cur1 INTO @UserName, @ClientID, @MoneyReceipt, @Status, @ID, @OrderNum
            END
        CLOSE cur1
        DEALLOCATE cur1

        SELECT  @ErrOrderNum AS N'@ErrOrderNum'
    END
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[PE_Roles_ScopePermissions]'
GO
CREATE TABLE [dbo].[PE_Roles_ScopePermissions]
(
[RoleID] [int] NOT NULL,
[ScopeCode] [int] NOT NULL,
[Scope] [int] NULL
)
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_PE_Roles_ScopePermissions] on [dbo].[PE_Roles_ScopePermissions]'
GO
ALTER TABLE [dbo].[PE_Roles_ScopePermissions] ADD CONSTRAINT [PK_PE_Roles_ScopePermissions] PRIMARY KEY CLUSTERED  ([RoleID], [ScopeCode])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Common_PagingLarge]'
GO
ALTER PROCEDURE [dbo].[PR_Common_PagingLarge]
    @TableNames VARCHAR(4000) ,
    @PrimaryKey VARCHAR(4000) ,
    @Fields VARCHAR(4000) ,
    @PageSize INT ,
    @StartRow INT ,
    @Filter VARCHAR(4000) = '' ,
    @Group VARCHAR(4000) = '' ,
    @Order VARCHAR(4000) = ''
AS 
    BEGIN
        DECLARE @SortColumn VARCHAR(4000)
        DECLARE @DeclareSortType VARCHAR(4000)
        DECLARE @SortColumnBegin VARCHAR(4000)
        DECLARE @SortColumnWhere VARCHAR(4000)
        DECLARE @FirstSortColumnWhere VARCHAR(4000)
        DECLARE @Operator CHAR(2)
        DECLARE @SortTable VARCHAR(4000)
        DECLARE @SortName VARCHAR(4000)
        SET @DeclareSortType = ''
        SET @SortColumnBegin = ''
        SET @SortColumnWhere = ''
        SET @FirstSortColumnWhere = ''
        IF @Fields = '' 
            SET @Fields = '*'
        IF @Filter = '' 
            SET @Filter = 'Where 1=1'
        ELSE 
            SET @Filter = 'Where ' + @Filter
        IF @Group <> '' 
            SET @Group = 'GROUP BY ' + @Group
        IF @Order = '' 
            SET @Order = @PrimaryKey + ' ASC'
        DECLARE @StartRows INT
        SET @StartRows = @StartRow
        IF @StartRows <= 0 
            SET @StartRows = 0
        DECLARE @equalOperator CHAR(2)
        IF @StartRows = 0 
            BEGIN
                SET @equalOperator = '='
                SET @StartRows = 1
            END
        ELSE 
            SET @equalOperator = ''

        DECLARE @pos1 INT ,
            @pos2 INT ,
            @Depth INT
        DECLARE @SubOrder VARCHAR(4000) ,
            @EndOrder VARCHAR(2000) ,
            @PreOrder VARCHAR(2000) ,
            @PreTable VARCHAR(2000) ,
            @PreSortColumn VARCHAR(2000)
        SET @EndOrder = ''
        SET @Depth = 1
        SET @EndOrder = @Order
        SET @pos1 = 1

        WHILE @pos1 > 0 
            BEGIN
                SET @pos1 = CHARINDEX(',', @EndOrder)
                IF @pos1 > 0 
                    SET @SubOrder = SUBSTRING(@EndOrder, 1, @pos1 - 1)
                ELSE 
                    SET @SubOrder = @EndOrder
                SET @EndOrder = SUBSTRING(@EndOrder, @pos1 + 1,
                                          LEN(@EndOrder) - @pos1)
                SET @SubOrder = REPLACE(REPLACE(@SubOrder, ' asc', ' ASC'),
                                        ' desc', ' DESC')
                IF CHARINDEX(' DESC', @SubOrder) > 0 
                    IF CHARINDEX(' ASC', @SubOrder) > 0 
                        BEGIN
                            IF CHARINDEX(' DESC', @SubOrder) < CHARINDEX(' ASC',
                                                              @SubOrder) 
                                SET @Operator = '<'
                            ELSE 
                                SET @Operator = '>'
                        END
                    ELSE 
                        SET @Operator = '<'
                ELSE 
                    SET @Operator = '>'
					      
                SET @SortColumn = REPLACE(REPLACE(REPLACE(@SubOrder, ' ASC',
                                                          ''), ' DESC', ''),
                                          ' ', '')
                SET @pos2 = CHARINDEX('.', @SortColumn)
			
                DECLARE @dataType VARCHAR(50)
                SET @dataType = NULL
                DECLARE @prec INT
				DECLARE @xscale INT

                IF @pos2 > 0 
                    BEGIN
                        SET @SortTable = SUBSTRING(@SortColumn, 1, @pos2 - 1)
                        SET @SortName = SUBSTRING(@SortColumn, @pos2 + 1,
                                                  LEN(@SortColumn) - @pos2)
                    END
                ELSE 
                    BEGIN
                        SET @SortTable = @TableNames
                        SET @SortName = @SortColumn
                    END
                SELECT  @dataType = t.name ,
                        @prec = c.prec ,
						@xscale = c.xscale
                FROM    sysobjects o
                        JOIN syscolumns c ON o.id = c.id
                        JOIN systypes t ON c.xusertype = t.xusertype
                WHERE   o.name = @SortTable
                        AND c.name = @SortName
                IF @dataType IS NULL 
                    BEGIN
                        DECLARE @posTableName1 INT ,
                            @posTableName2 INT ,
                            @TableNameSubStr VARCHAR(4000)
                        SET @posTableName1 = CHARINDEX(' ' + @SortTable + ' ',
                                                       @TableNames + ' ')
                        IF @posTableName1 > 0 
                            BEGIN
                                SET @TableNameSubStr = SUBSTRING(@TableNames,
                                                              1,
                                                              @posTableName1
                                                              - 1)
                                SET @TableNameSubStr = LTRIM(@TableNameSubStr)
                                SET @TableNameSubStr = RTRIM(@TableNameSubStr)
			              
                                SET @posTableName2 = CHARINDEX(' ',
                                                              @TableNameSubStr)
                                WHILE @posTableName2 > 0 
                                    BEGIN
                                        SET @TableNameSubStr = SUBSTRING(@TableNameSubStr,
                                                              @posTableName2,
                                                              LEN(@TableNameSubStr)
                                                              - 1)
                                        SET @TableNameSubStr = LTRIM(@TableNameSubStr)
                                        SET @TableNameSubStr = RTRIM(@TableNameSubStr)
                                        SET @posTableName2 = CHARINDEX(' ',
                                                              @TableNameSubStr)
                                    END
                            END
                        SELECT  @dataType = t.name ,
                                @prec = c.prec ,
								@xscale = c.xscale
                        FROM    sysobjects o
                                JOIN syscolumns c ON o.id = c.id
                                JOIN systypes t ON c.xusertype = t.xusertype
                        WHERE   o.name = @TableNameSubStr
                                AND c.name = @SortName
                    END
                IF CHARINDEX('char', @dataType) > 0 
                    BEGIN
                        SET @dataType = @dataType + '('
                            + CAST(@prec AS VARCHAR) + ')'
                    END
				IF CHARINDEX('numeric', @dataType) > 0 
                    BEGIN
                        SET @dataType = @dataType + '('
                            + CAST(@prec AS VARCHAR) + ',' + CAST(@xscale AS VARCHAR) + ')'
                    END

                SET @DeclareSortType = @DeclareSortType + '
                DECLARE @SortColumnBegin' + @SortName + ' ' + @dataType
                IF @Depth <> 1 
                    BEGIN
                        SET @SortColumnBegin = @SortColumnBegin + ','
                            + '@SortColumnBegin' + @SortName + '='
                            + @SortColumn
                        IF @pos1 > 0 
							BEGIN
								SET @SortColumnWhere = @SortColumnWhere + ' OR ('
                                + @FirstSortColumnWhere + ' AND ' + @SortColumn
                                + @Operator + '@SortColumnBegin' + @SortName
                                + ')';
                                SET @FirstSortColumnWhere =  @FirstSortColumnWhere + ' AND ' +
									+ @SortColumn + '=@SortColumnBegin' + @SortName
                            END
                        ELSE 
							BEGIN
								SET @SortColumnWhere = @SortColumnWhere + ' OR ('
                                + @FirstSortColumnWhere + ' AND ' + @SortColumn
                                + @Operator + @equalOperator
                                + '@SortColumnBegin' + @SortName + ')';
                                SET @FirstSortColumnWhere = @FirstSortColumnWhere+ ' AND ' +
									+ @SortColumn + '=@SortColumnBegin' + @SortName
                            END
                    END                    
                ELSE 
                    BEGIN
                        SET @SortColumnBegin = @SortColumnBegin
                            + '@SortColumnBegin' + @SortName + '='
                            + @SortColumn 
                        IF @pos1 > 0 
							BEGIN
								SET @SortColumnWhere = @SortColumnWhere
									+ @SortColumn + @Operator + '@SortColumnBegin'
									+ @SortName
									
                                SET @FirstSortColumnWhere = @FirstSortColumnWhere
									+ @SortColumn + '=@SortColumnBegin'
									+ @SortName
                            END
                        ELSE
							BEGIN
								SET @SortColumnWhere = @SortColumnWhere
									+ @SortColumn + @Operator + @equalOperator
									+ '@SortColumnBegin' + @SortName
							    SET @FirstSortColumnWhere = @FirstSortColumnWhere
									+ @SortColumn + '=@SortColumnBegin'
									+ @SortName
                            END
                    END 
                SET @Depth = @Depth + 1
                SET @PreOrder = @SortName
                SET @PreTable = @SortTable
                SET @PreSortColumn = @SortColumn
            END
        EXEC ( '
        ' + @DeclareSortType + '
        SET ROWCOUNT ' + @StartRows + '
        Select ' + @SortColumnBegin + ' FROM ' + @TableNames + ' ' + @Filter + ' ' + @Group + ' ORDER BY ' + @Order + '
        SET ROWCOUNT ' + @PageSize + '
        Select ' + @Fields + ' FROM    ' + @TableNames + ' ' + @Filter + ' AND (' + @SortColumnWhere + ') ' + @Group + ' ORDER BY ' + @Order + '    
        '
        )
    END
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_PE_ClientConfirm] on [dbo].[PE_ClientConfirm]'
GO
ALTER TABLE [dbo].[PE_ClientConfirm] ADD CONSTRAINT [PK_PE_ClientConfirm] PRIMARY KEY CLUSTERED  ([ID])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_PE_ClientItem] on [dbo].[PE_ClientItem]'
GO
ALTER TABLE [dbo].[PE_ClientItem] ADD CONSTRAINT [PK_PE_ClientItem] PRIMARY KEY CLUSTERED  ([ID])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT>0 BEGIN
PRINT 'The database update succeeded'
COMMIT TRANSACTION
END
ELSE PRINT 'The database update failed'
GO
DROP TABLE #tmpErrors
GO
