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'Dropping constraints from [dbo].[PE_StatIp]'
GO
ALTER TABLE [dbo].[PE_StatIp] DROP CONSTRAINT [PK_PE_StatIp]
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'Dropping index [TIp] from [dbo].[PE_StatIp]'
GO
DROP INDEX [dbo].[PE_StatIp].[TIp]
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_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(50) ,
      @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].[PR_Crm_ClientHistoryItem_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_ClientHistoryItem_Add]
    (
      @ClientHistoryID INT ,
      @ItemID INT ,
      @FieldName NVARCHAR(50) ,
      @OldValue NVARCHAR(500) ,
      @NewValue NVARCHAR(500)
    )
AS 
    INSERT  INTO pe_ClientHistoryItem
            ( ClientHistoryID ,
              ItemID ,
              FieldName ,
              OldValue ,
              NewValue
            )
    VALUES  ( @ClientHistoryID ,
              @ItemID ,
              @FieldName ,
              @OldValue ,
              @NewValue
            )
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_PresentProject_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_PresentProject_Update]
    (
      @ProjectId INT ,
      @ProjectName NVARCHAR(50) ,
      @BeginDate DATETIME ,
      @EndDate DATETIME ,
      @MinMoney MONEY ,
      @MaxMoney MONEY ,
      @PresentContent NVARCHAR(50) ,
      @Price MONEY ,
      @PresentID NTEXT ,
      @Cash INT ,
      @PresentExp INT ,
      @PresentPoint INT ,
      @Disabled BIT
	
    )
AS 
    SET IDENTITY_INSERT PE_PresentProject ON
    UPDATE  PE_PresentProject
    SET     ProjectName = @ProjectName ,
            BeginDate = @BeginDate ,
            EndDate = @EndDate ,
            MinMoney = @MinMoney ,
            MaxMoney = @MaxMoney ,
            PresentContent = @PresentContent ,
            Price = @Price ,
            PresentID = @PresentID ,
            Cash = @Cash ,
            PresentExp = @PresentExp ,
            PresentPoint = @PresentPoint
    WHERE   ProjectId = @ProjectId
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_PresentProject_Locked]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_PresentProject_Locked]
    (
      @ProjectId INT ,
      @Disabled BIT
	
    )
AS 
    UPDATE  PE_PresentProject
    SET     [Disabled] = @Disabled
    WHERE   [ProjectID] = @ProjectId
		
    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_Shop_PresentProject_List]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_PresentProject_List] ( @id INT = 0 )
AS 
    IF ( @id = 0 ) 
        BEGIN
            SELECT  [ProjectID] ,
                    [ProjectName] ,
                    [BeginDate] ,
                    [EndDate] ,
                    [MinMoney] ,
                    [MaxMoney] ,
                    [PresentContent] ,
                    [Price] ,
                    [PresentID] ,
                    [Cash] ,
                    [PresentExp] ,
                    [PresentPoint] ,
                    [Disabled]
            FROM    PE_PresentProject
            ORDER BY ProjectID ASC
        END
    ELSE 
        BEGIN
            SELECT  [ProjectID] ,
                    [ProjectName] ,
                    [BeginDate] ,
                    [EndDate] ,
                    [MinMoney] ,
                    [MaxMoney] ,
                    [PresentContent] ,
                    [Price] ,
                    [PresentID] ,
                    [Cash] ,
                    [PresentExp] ,
                    [PresentPoint] ,
                    [Disabled]
            FROM    PE_PresentProject
            WHERE   ProjectID = @id
        END 
    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_MoodScheme_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_MoodScheme_Add]
    (
      @MoodID INT ,
      @MoodName NVARCHAR(300) ,
      @MoodOption VARCHAR(4000) ,
      @MoodStatus BIT ,
      @IsOnlyMember BIT
    )
AS 
    INSERT  INTO PE_MoodScheme
            ( MoodID ,
              MoodName ,
              MoodOption ,
              MoodStatus ,
              IsOnlyMember
            )
    VALUES  ( @MoodID ,
              @MoodName ,
              @MoodOption ,
              @MoodStatus ,
              @IsOnlyMember
            )
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_PresentProject_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_PresentProject_Delete] ( @ProjectId INT )
AS 
    DELETE  PE_PresentProject
    WHERE   [ProjectID] = @ProjectId
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_MoodScheme_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_MoodScheme_Update]
    (
      @MoodID INT ,
      @MoodName NVARCHAR(300) ,
      @MoodOption VARCHAR(4000) ,
      @MoodStatus BIT ,
      @IsOnlyMember BIT
    )
AS 
    BEGIN
        UPDATE  PE_MoodScheme
        SET     MoodName = @MoodName ,
                MoodOption = @MoodOption ,
                MoodStatus = @MoodStatus ,
                IsOnlyMember = @IsOnlyMember
        WHERE   MoodID = @MoodID
    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_PresentProject_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_PresentProject_Add]
    (
      @ProjectName NVARCHAR(50) ,
      @BeginDate DATETIME ,
      @EndDate DATETIME ,
      @MinMoney MONEY ,
      @MaxMoney MONEY ,
      @PresentContent NVARCHAR(50) ,
      @Price MONEY ,
      @PresentID NTEXT ,
      @Cash INT ,
      @PresentExp INT ,
      @PresentPoint INT ,
      @Disabled BIT
	
    )
AS
    INSERT  INTO PE_PresentProject
            ( [ProjectName] ,
              [BeginDate] ,
              [EndDate] ,
              [MinMoney] ,
              [MaxMoney] ,
              [PresentContent] ,
              [Price] ,
              [PresentID] ,
              [Cash] ,
              [PresentExp] ,
              [PresentPoint] ,
              [Disabled]
            )
    VALUES  ( @ProjectName ,
              @BeginDate ,
              @EndDate ,
              @MinMoney ,
              @MaxMoney ,
              @PresentContent ,
              @Price ,
              @PresentID ,
              @Cash ,
              @PresentExp ,
              @PresentPoint ,
              @Disabled
            )
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_Comment]'
GO
ALTER TABLE [dbo].[PE_Comment] ADD
[CommentType] [int] NULL,
[WapId] [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_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(50) ,
      @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_Contents_Model_ModelIdExists]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Model_ModelIdExists]
    (
      @NodeId INT ,
      @ModelId INT
    )
AS 
    SELECT  COUNT(*)
    FROM    PE_Nodes_Model_Template
    WHERE   NodeId = @NodeId
            AND ModelId = @ModelId
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_AddModelForNodes]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Model_AddModelForNodes]
    (
      @NodeId INT ,
      @ModelId INT ,
      @DefaultTemplateFile NVARCHAR(255)
    )
AS 
    INSERT  INTO PE_Nodes_Model_Template
            ( NodeId ,
              ModelId ,
              DefaultTemplateFile
            )
    VALUES  ( @NodeId ,
              @ModelId ,
              @DefaultTemplateFile
            )  
    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_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(50) ,
      @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_Shop_ProductPrice_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_ProductPrice_Update]
    (
      @TableName NVARCHAR(255) ,
      @ProductId INT ,
      @GroupId INT ,
      @Price MONEY ,
      @PropertyValue NVARCHAR(255)
    )
AS 
    IF EXISTS ( SELECT  *
                FROM    PE_ProductPrice
                WHERE   TableName = @TableName
                        AND GroupID = @GroupID
                        AND ProductId = @ProductId
                        AND PropertyValue = @PropertyValue ) 
        BEGIN
            UPDATE  PE_ProductPrice
            SET     Price = @Price
            WHERE   GroupID = @GroupID
                    AND TableName = @TableName
                    AND ProductId = @ProductId
                    AND ( ISNULL(PropertyValue, '') = @PropertyValue )
        END
    ELSE 
        BEGIN
            INSERT  INTO PE_ProductPrice
                    ( TableName ,
                      ProductID ,
                      GroupID ,
                      Price ,
                      PropertyValue
		        )
            VALUES  ( @TableName ,
                      @ProductId ,
                      @GroupId ,
                      @Price ,
                      @PropertyValue
		        )
        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_Contents_Model_AddTemplateForNodes]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Model_AddTemplateForNodes]
    (
      @NodeId INT ,
      @TemplateId INT ,
      @IsDefault BIT
	
    )
AS 
    INSERT  INTO PE_Nodes_Template
            ( NodeId, TemplateId, IsDefault )
    VALUES  ( @NodeId, @TemplateId, @IsDefault )  
    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_TemplateIdExists]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Model_TemplateIdExists]
    (
      @NodeId INT ,
      @TemplateId INT
    )
AS 
    SELECT  COUNT(*)
    FROM    PE_Nodes_Template
    WHERE   NodeId = @NodeId
            AND TemplateId = @TemplateId
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_UserGroups_Update]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_UserGroups_Update]
    (
      @GroupID INT ,
      @GroupName NVARCHAR(20) ,
      @Description NTEXT ,
      @Settings NTEXT ,
      @GroupType INT ,
      @GroupSetting NTEXT ,
      @UpgradeSetting NTEXT  
    )
AS 
    UPDATE  [PE_UserGroups]
    SET     [GroupName] = @GroupName ,
            [Description] = @Description ,
            [Settings] = @Settings ,
            [GroupType] = @GroupType ,
            [GroupSetting] = @GroupSetting ,
            [UpgradeSetting] = @UpgradeSetting
    WHERE   [GroupID] = @GroupID
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_UserGroups_Add]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_UserGroups_Add]
    (
      @GroupID INT ,
      @GroupName NVARCHAR(20) ,
      @Description NTEXT ,
      @Settings NTEXT ,
      @GroupType INT ,
      @GroupSetting NTEXT ,
      @UpgradeSetting NTEXT
    )
AS 
    INSERT  INTO [PE_UserGroups]
            ( [GroupID] ,
              [GroupName] ,
              [Description] ,
              [Settings] ,
              [GroupType] ,
              [GroupSetting] ,
              [UpgradeSetting]
		)
    VALUES  ( @GroupID ,
              @GroupName ,
              @Description ,
              @Settings ,
              @GroupType ,
              @GroupSetting ,
              @UpgradeSetting
	      )
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(50) ,
      @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].[PR_Shop_Courier_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Courier_Update]
    (
      @courierId INT ,
      @shortName NVARCHAR(64) ,
      @fullName NVARCHAR(256) ,
      @address NVARCHAR(512) ,
      @telePhone NVARCHAR(64) ,
      @Contacter NVARCHAR(64) ,
      @searchUrl NVARCHAR(256) ,
      @status INT ,
      @orderId INT
	
    )
AS --处理 排序
    DECLARE @oldOrderId INT
    DECLARE @max INT
    DECLARE @min INT
    DECLARE @count INT
    DECLARE @icount INT
    DECLARE @offset1 INT
    DECLARE @offset2 INT

    SET @icount = 1
    DECLARE @tmp TABLE
        (
          Row INT IDENTITY(1, 1)
                  PRIMARY KEY ,
          id INT ,
          oid INT
        )
    SELECT  @oldOrderId = OrderId
    FROM    [PE_Courier]
    WHERE   CourierID = @courierId
    IF ( @orderId <> @oldOrderId ) 
        BEGIN
            IF ( @orderId > @oldOrderId ) 
                BEGIN
                    SET @max = @orderId
                    SET @min = @oldOrderId
                    SET @offset1 = 0
                    SET @offset2 = 1
                END
            ELSE 
                BEGIN
                    SET @max = @oldOrderId
                    SET @min = @orderId
                    SET @offset1 = 1
                    SET @offset2 = 0
                END
            INSERT  INTO @tmp
                    ( id ,
                      oid
                    )
                    SELECT  CourierId ,
                            OrderId
                    FROM    [PE_Courier]
                    WHERE   OrderId BETWEEN @min AND @max
                    ORDER BY OrderId
            SELECT  @count = COUNT(*)
            FROM    @tmp

            WHILE ( @icount < @count ) 
                BEGIN
                    UPDATE  [PE_Courier]
                    SET     OrderId = ( SELECT  oid
                                        FROM    @tmp
                                        WHERE   Row = @icount + @offset1
                                      )
                    WHERE   CourierId = ( SELECT    Id
                                          FROM      @tmp
                                          WHERE     Row = @icount + @offset2
                                        )
                    SET @icount = @icount + 1
                END 
        END
	
    UPDATE  [PE_Courier]
    SET     [ShortName] = @shortName ,
            [FullName] = @fullName ,
            [Address] = @address ,
            [Telephone] = @telePhone ,
            [Contacter] = @Contacter ,
            [SearchUrl] = @searchUrl ,
            [Status] = @status ,
            [OrderId] = @orderId
    WHERE   CourierId = @courierId
    IF NOT ( @@error > 0 ) 
        BEGIN
            RETURN 1
        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_OrderHistoryItem_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_OrderHistoryItem_Add]
    (
      @OrderHistoryID INT ,
      @ItemID INT ,
      @FieldName NVARCHAR(50) ,
      @OldValue NVARCHAR(500) ,
      @NewValue NVARCHAR(500)
    )
AS 
    INSERT  INTO pe_OrderHistoryItem
            ( OrderHistoryID ,
              ItemID ,
              FieldName ,
              OldValue ,
              NewValue
            )
    VALUES  ( @OrderHistoryID ,
              @ItemID ,
              @FieldName ,
              @OldValue ,
              @NewValue
            )
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_DeliverItemDetails_Add]'
GO
ALTER PROC [dbo].[PR_Shop_DeliverItemDetails_Add]
    (
      @DeliverItemDetailsID INT ,
      @OrderID INT ,
      @DeliverID INT ,
      @productID INT ,
      @ProductNum INT ,
      @Property NVARCHAR(255)
    )
AS 
    INSERT  INTO PE_DeliverItemDetails
            ( DeliverItemDetailsID ,
              OrderID ,
              DeliverID ,
              productID ,
              ProductNum ,
              [property]
            )
    VALUES  ( @DeliverItemDetailsID ,
              @OrderID ,
              @DeliverID ,
              @productID ,
              @ProductNum ,
              @property 
            )
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_TransferLog_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_TransferLog_Add]
    (
      --@TransferLogID int,    --订单过户记录ID
      @OrderID INT ,    --订单ID
      @TransferTime DATETIME ,    --过户时间
      @OwnerUserName NVARCHAR(20) ,    --过户人
      @PayerUserName NVARCHAR(20) ,    --付款人
      @TargetUserName NVARCHAR(20) ,    --过户给
      @Poundage MONEY ,    --过户费
      @Inputer NVARCHAR(20) ,    --经手人
      @Remark NVARCHAR(50)    --备注
	
    )
AS 
    INSERT  INTO [PE_TransferLog]
            ( --[TransferLogID],    --订单过户记录ID
              [OrderID] ,    --订单ID
              [TransferTime] ,    --过户时间
              [OwnerUserName] ,    --过户人
              [PayerUserName] ,    --付款人
              [TargetUserName] ,    --过户给
              [Poundage] ,    --过户费
              [Inputer] ,    --经手人
              [Remark]    --备注
	      )
    VALUES  ( --@TransferLogID,
              @OrderID ,
              @TransferTime ,
              @OwnerUserName ,
              @PayerUserName ,
              @TargetUserName ,
              @Poundage ,
              @Inputer ,
              @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].[SplitID]'
GO
SET ANSI_NULLS OFF
GO
ALTER  FUNCTION  [dbo].[SplitID] 
(  
     @String  nvarchar(4000),  --格式：“1，2，3，4，”
     @SplitChar  nvarchar(10)  --格式：“，”
)  
RETURNS    @table  Table(ID  varchar(100))  
AS  
BEGIN  
   DECLARE  @Index  INT  
   SET  @Index  =  0  

        IF @String <> ''
        Begin
            IF RIGHT(@String,1)<> @SplitChar 
                SET @String = @String + @SplitChar
            IF LEFT(@String,1)= @SplitChar 
            SET @String = STUFF(@String, 1, 1, '')
        End
 
       WHILE  CHARINDEX(@SplitChar,@String,@Index)  >  0    
       BEGIN  
           INSERT INTO @table(ID)
               VALUES (SUBSTRING(@String, @Index, CHARINDEX(@SplitChar, @String,
                @Index) - @Index))
               SET @index = CHARINDEX(@SplitChar, @String, @Index) + 1 END  
RETURN  
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_Accessories_Choiceset_SetFieldValue]'
GO
SET ANSI_NULLS ON
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Choiceset_SetFieldValue]
    (
      @FieldValue NTEXT ,
      @TableName NVARCHAR(30) ,
      @FieldName NVARCHAR(30)
    )
AS
    UPDATE  PE_Dictionary
    SET     FieldValue = @FieldValue
    WHERE   TableName = @TableName
            AND FieldName = @FieldName
    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_Choiceset_GetChoicesetFieldValueByName]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Choiceset_GetChoicesetFieldValueByName]
    (
      @TableName NVARCHAR(30) ,
      @FieldName NVARCHAR(30)
    )
AS 
    SET NOCOUNT ON 
    SELECT  *
    FROM    PE_Dictionary
    WHERE   TableName = @TableName
            AND FieldName = @FieldName
    ORDER BY FieldID ASC
    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_DownloadError_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_DownloadError_Add]
    (
      @ErrorID INT ,
      @InfoID INT ,
      @ErrorUrl NVARCHAR(255) ,
      @ErrorTimes INT ,
      @ErrorDate DATETIME
	
    )
AS
    INSERT  INTO PE_DownloadError
            ( ErrorID ,
              InfoID ,
              ErrorUrl ,
              ErrorTimes ,
              ErrorDate
            )
    VALUES  ( @ErrorID ,
              @InfoID ,
              @ErrorUrl ,
              @ErrorTimes ,
              @ErrorDate
            )  
    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_DownloadError_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_DownloadError_Delete]
    (
      @ErrorID NVARCHAR(4000) ,--格式：“1，2，3，4，”
      @SplitChar NVARCHAR(10)--格式：“，”
	
    )
AS
    DECLARE @TemptTable TABLE ( ID INT )   
    DECLARE @Index INT   
    SET @Index = 0   
    IF CHARINDEX(@SplitChar, @ErrorID, @Index) = 0 
        DELETE  FROM PE_DownloadError
        WHERE   ErrorID = @ErrorID
    ELSE 
        WHILE CHARINDEX(@SplitChar, @ErrorID, @Index) > 0 
            BEGIN   
                INSERT  INTO @TemptTable
                        ( ID
                        )
                VALUES  ( SUBSTRING(@ErrorID, @Index,
                                    CHARINDEX(@SplitChar, @ErrorID, @Index)
                                    - @Index)
                        )     
                SET @Index = CHARINDEX(@SplitChar, @ErrorID, @Index) + 1   
            END  
		 
    DELETE  FROM PE_DownloadError
    WHERE   ( ErrorID IN ( SELECT   ID
                           FROM     @TemptTable ) )  
    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_DownServer_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_DownServer_Add]
    (
      @ServerID INT ,
      @ServerName NVARCHAR(50) ,
      @ServerUrl NVARCHAR(50) ,
      @ServerLogo NVARCHAR(255) ,
      @OrderID INT ,
      @ShowType INT
	
    )
AS 
    INSERT  INTO PE_DownServer
            ( ServerID ,
              ServerName ,
              ServerUrl ,
              ServerLogo ,
              OrderID ,
              ShowType
            )
    VALUES  ( @ServerID ,
              @ServerName ,
              @ServerUrl ,
              @ServerLogo ,
              @OrderID ,
              @ShowType
            )  
    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_DownServer_GetDownOrderId]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_DownServer_GetDownOrderId] ( @OrderID INT )
AS 
    SET NOCOUNT ON
    SELECT  ServerID ,
            ServerName ,
            ServerUrl ,
            ServerLogo ,
            OrderID ,
            ShowType
    FROM    PE_DownServer
    WHERE   OrderID > @OrderID
    ORDER BY OrderID ASC 
    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_DownServer_GetById]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_DownServer_GetById] ( @ServerID INT )
AS 
    SET NOCOUNT ON
    SELECT  ServerID ,
            ServerName ,
            ServerUrl ,
            ServerLogo ,
            OrderID ,
            ShowType
    FROM    PE_DownServer
    WHERE   ServerID = @ServerID
    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_DownServer_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_DownServer_Delete] ( @ServerID INT )
AS 
    DECLARE @oldOrderId INT
    SELECT  @oldOrderId = OrderId
    FROM    PE_DownServer
    WHERE   ServerID = @ServerID
    IF ( @oldOrderId >= 0 ) 
        BEGIN
            UPDATE  PE_DownServer
            SET     OrderID = OrderID - 1
            WHERE   OrderID > @oldOrderId 
        END
    DELETE  PE_DownServer
    WHERE   [ServerID] = @ServerID  
    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_DownServer_MoveOrderId]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_DownServer_MoveOrderId]
    (
      @ServerID INT ,
      @AddNum INT
	
    )
AS
    UPDATE  PE_DownServer
    SET     OrderID = OrderID + @AddNum
    WHERE   ServerID = @ServerID 
    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_DownServer_MoveDownOrderId]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_DownServer_MoveDownOrderId] ( @OrderID INT )
AS 
    SET NOCOUNT ON
    UPDATE  PE_DownServer
    SET     OrderID = OrderID - 1
    WHERE   OrderID = @OrderID 
    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_DownServer_GetUpOrderId]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_DownServer_GetUpOrderId] ( @OrderID INT )
AS 
    SET NOCOUNT ON
    SELECT  ServerID ,
            ServerName ,
            ServerUrl ,
            ServerLogo ,
            OrderID ,
            ShowType
    FROM    PE_DownServer
    WHERE   OrderID < @OrderID
    ORDER BY OrderID DESC  
    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(50) ,
      @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_Accessories_DownServer_GetServerUrlByServerId]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_DownServer_GetServerUrlByServerId] ( @ServerID INT )
AS 
    SET NOCOUNT ON
    SELECT  ServerID ,
            ServerUrl
    FROM    PE_DownServer
    WHERE   ServerID = @ServerID
    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_DownServer_UpdateOrderId]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_DownServer_UpdateOrderId]
    (
      @ServerID INT ,
      @OrderID INT
    )
AS 
    UPDATE  PE_DownServer
    SET     OrderID = @OrderID
    WHERE   ServerID = @ServerID
    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_DownServer_SetShowType]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_DownServer_SetShowType]
    (
      @ServerID NVARCHAR(4000) ,--格式：“1，2，3，4，”
      @ShowType INT ,
      @SplitChar NVARCHAR(10)--格式：“，”
	
    )
AS
    DECLARE @TemptTable TABLE ( ID INT )   
    DECLARE @Index INT   
    SET @Index = 0   
    IF CHARINDEX(@SplitChar, @ServerID, @Index) = 0 
        UPDATE  PE_DownServer
        SET     ShowType = @ShowType
        WHERE   ServerID = @ServerID
    ELSE 
        WHILE CHARINDEX(@SplitChar, @ServerID, @Index) > 0   --循环   
            BEGIN   
                INSERT  INTO @TemptTable
                        ( ID
                        )
                VALUES  ( SUBSTRING(@ServerID, @Index,
                                    CHARINDEX(@SplitChar, @ServerID, @Index)
                                    - @Index)
                        )     
                SET @Index = CHARINDEX(@SplitChar, @ServerID, @Index) + 1   
            END   

    UPDATE  PE_DownServer
    SET     ShowType = @ShowType
    WHERE   ( ServerID IN ( SELECT  ID
                            FROM    @TemptTable ) )  
    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_Shop_PaymentLog_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_PaymentLog_Add]
    (
      @PaymentLogID INT ,
      @UserName NVARCHAR(50) ,
      @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_Accessories_DownServer_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_DownServer_Update]
    (
      @ServerID INT ,
      @ServerName NVARCHAR(50) ,
      @ServerUrl NVARCHAR(50) ,
      @ServerLogo NVARCHAR(255) ,
      @OrderID INT ,
      @ShowType INT
	
    )
AS 
    UPDATE  PE_DownServer
    SET     ServerName = @ServerName ,
            ServerUrl = @ServerUrl ,
            ServerLogo = @ServerLogo ,
            OrderID = @OrderID ,
            ShowType = @ShowType
    WHERE   ServerID = @ServerID
    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_DownServer_MoveUpOrderId]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_DownServer_MoveUpOrderId] ( @OrderID INT )
AS 
    SET NOCOUNT ON
    UPDATE  PE_DownServer
    SET     OrderID = OrderID + 1
    WHERE   OrderID = @OrderID 
    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].[PE_StatOnline]'
GO
ALTER TABLE [dbo].[PE_StatOnline] ALTER COLUMN [UserIP] [varchar] (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].[PR_Accessories_PayPlatform_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_PayPlatform_Update]
    (
      @PayPlatformID INT ,
      @PayPlatformName NVARCHAR(100) ,
      @AccountsID NVARCHAR(100) ,
      @MD5 NVARCHAR(200) ,
      @Rate DECIMAL(18, 10) ,
      @OrderID INT ,
      @IsDisabled BIT ,
      @IsDefault BIT
    )
AS 
    IF @IsDefault = 1 
        BEGIN
            UPDATE  PE_PayPlatform
            SET     [IsDefault] = 0
            WHERE   [IsDefault] = 1
        END
 
    UPDATE  PE_PayPlatform
    SET     [PayPlatformName] = @PayPlatformName ,
            [AccountsID] = @AccountsID ,
            [MD5] = @MD5 ,
            [Rate] = @Rate ,
            [OrderID] = @OrderID ,
            [IsDisabled] = @IsDisabled ,
            [IsDefault] = @IsDefault
    WHERE   [PayPlatformID] = @PayPlatformID
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_PayPlatform_SetOrderId]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_PayPlatform_SetOrderId]
    (
      @PayPlatformID INT ,
      @OrderID INT
    )
AS 
    IF NOT EXISTS ( SELECT  OrderID
                    FROM    PE_PayPlatform
                    WHERE   OrderID = @OrderID
                            AND [PayPlatformID] = @PayPlatformID ) 
        BEGIN
            UPDATE  PE_PayPlatform
            SET     [OrderID] = @OrderID
            WHERE   [PayPlatformID] = @PayPlatformID
        END	
    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_PayPlatform_SetDefault]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_PayPlatform_SetDefault] ( @PayPlatformId INT )
AS 
    UPDATE  PE_PayPlatForm
    SET     [IsDefault] = 0
		
    UPDATE  PE_PayPlatForm
    SET     [IsDefault] = 1
    WHERE   [PayPlatformId] = @PayPlatformId
    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_PayPlatform_GetListOfdisabled]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_PayPlatform_GetListOfdisabled] ( @isdisabled BIT )
AS 
    SELECT  *
    FROM    PE_PayPlatform
    WHERE   IsDisabled = @isdisabled
    ORDER BY OrderID ASC
    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_PayPlatform_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_PayPlatform_Delete] ( @PayPlatformID INT )
AS 
    SET NOCOUNT OFF
    DELETE  PE_PayPlatform
    WHERE   [PayPlatformID] = @PayPlatformID
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_PayPlatform_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_PayPlatform_Add]
    (
      @PayPlatformID INT ,
      @PayPlatformName NVARCHAR(100) ,
      @AccountsID NVARCHAR(100) ,
      @MD5 NVARCHAR(200) ,
      @Rate NUMERIC(18, 2) ,
      @IsDisabled BIT ,
      @IsDefault BIT
    )
AS 
    DECLARE @TempID INT
    SELECT  @TempID = ( ISNULL(MAX(OrderId), 0) + 1 )
    FROM    PE_PayPlatform
    IF @IsDefault = 1 
        BEGIN
            UPDATE  PE_PayPlatform
            SET     [IsDefault] = 0
            WHERE   [IsDefault] = 1
        END	
    SET NOCOUNT OFF 
    INSERT  INTO PE_PayPlatform
            ( [PayPlatformID] ,
              [PayPlatformName] ,
              [AccountsID] ,
              [MD5] ,
              [Rate] ,
              [OrderID] ,
              [IsDisabled] ,
              [IsDefault]
		)
    VALUES  ( @PayPlatformID ,
              @PayPlatformName ,
              @AccountsID ,
              @MD5 ,
              @Rate ,
              @TempID ,
              @IsDisabled ,
              @IsDefault
		)
    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_PayPlatform_DisablePayPlatform]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_PayPlatform_DisablePayPlatform]
    (
      @PayPlatformID INT ,
      @IsDisabled BIT
    )
AS 
    SET NOCOUNT OFF
    UPDATE  PE_PayPlatform
    SET     [IsDisabled] = @IsDisabled
    WHERE   [PayPlatformID] = @PayPlatformID
    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_PayPlatform_GetInfoById]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_PayPlatform_GetInfoById] ( @PayPlatformID INT )
AS 
    SELECT  *
    FROM    PE_PayPlatform
    WHERE   [PayPlatformID] = @PayPlatformID
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_ClientAnnal_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_ClientAnnal_Update]
    (
      @ClientAnnalID INT ,
      @ClientID INT ,
      @RelationType NVARCHAR(30) ,
      @Contacter NVARCHAR(50) ,
      @RelationTime DATETIME ,
      @AnnalContent NVARCHAR(1000) ,
      @AnnalTime DATETIME ,
      @Inputer NVARCHAR(20)
    )
AS 
    SET NOCOUNT OFF 

    UPDATE  PE_ClientAnnal
    SET     Contacter = @Contacter ,
            ClientID = @ClientID ,
            RelationTime = @RelationTime ,
            RelationType = @RelationType ,
            AnnalContent = @AnnalContent ,
            Annaltime = @AnnalTime ,
            Inputer = @Inputer
    WHERE   ClientAnnalID = @ClientAnnalID
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_Message_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Message_Add]
    (
      @Title NVARCHAR(100) ,
      @Content NTEXT ,
      @Sender NVARCHAR(20) ,
      @Incept NVARCHAR(4000) ,
      @SendTime DATETIME ,
      @IsSend INT ,
      @IsDelInbox INT ,
      @IsDelSendbox INT ,
      @IsRead INT
    )
AS 
    DECLARE @Index INT 
    DECLARE @SplitChar NVARCHAR(2) --分隔符，格式：“，”

    SET @Index = 0
    SET @SplitChar = ','   
    IF CHARINDEX(@SplitChar, @Incept, @Index) = 0 
        BEGIN
            INSERT  INTO [PE_Message]
                    ( [Title] ,
                      [Content] ,
                      [Sender] ,
                      [Incept] ,
                      [SendTime] ,
                      [IsSend] ,
                      [IsDelInbox] ,
                      [IsDelSendbox] ,
                      [IsRead]
				
                    )
            VALUES  ( @Title ,
                      @Content ,
                      @Sender ,
                      @Incept ,
                      @SendTime ,
                      @IsSend ,
                      @IsDelInbox ,
                      @IsDelSendbox ,
                      @IsRead
			  )
        END
    ELSE 
        BEGIN
            WHILE CHARINDEX(@SplitChar, @Incept, @Index) > 0 
                BEGIN
                    INSERT  INTO [PE_Message]
                            ( [Title] ,
                              [Content] ,
                              [Sender] ,
                              [Incept] ,
                              [SendTime] ,
                              [IsSend] ,
                              [IsDelInbox] ,
                              [IsDelSendbox] ,
                              [IsRead]
                            )
                    VALUES  ( @Title ,
                              @Content ,
                              @Sender ,
                              SUBSTRING(@Incept, @Index,
                                        CHARINDEX(@SplitChar, @Incept, @Index)
                                        - @Index) ,
                              @SendTime ,
                              @IsSend ,
                              @IsDelInbox ,
                              @IsDelSendbox ,
                              @IsRead
					
                            )
                    SET @Index = CHARINDEX(@SplitChar, @Incept, @Index) + 1 
                END
            INSERT  INTO [PE_Message]
                    ( [Title] ,
                      [Content] ,
                      [Sender] ,
                      [Incept] ,
                      [SendTime] ,
                      [IsSend] ,
                      [IsDelInbox] ,
                      [IsDelSendbox] ,
                      [IsRead]
						
                    )
            VALUES  ( @Title ,
                      @Content ,
                      @Sender ,
                      SUBSTRING(@Incept, @Index, LEN(@Incept) - @Index + 1) ,
                      @SendTime ,
                      @IsSend ,
                      @IsDelInbox ,
                      @IsDelSendbox ,
                      @IsRead
                    )
        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_ClientAnnal_GetClientAnnalByID]'
GO
ALTER PROC [dbo].[PR_Crm_ClientAnnal_GetClientAnnalByID] @ClientAnnalID INT
AS 
    SELECT  *
    FROM    PE_ClientAnnal
    WHERE   ClientAnnalID = @ClientAnnalID
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_Message_GetById]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Message_GetById] ( @MessageId INT )
AS 
    SET NOCOUNT OFF 
    SELECT  *
    FROM    PE_Message
    WHERE   MessageID = @MessageId
    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_Crm_ClientAnnal_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_ClientAnnal_Add]
    (
      @Contacter NVARCHAR(50) ,
      @ClientAnnalID INT ,
      @ClientID INT ,
      @RelationType NVARCHAR(30) ,
      @RelationTime DATETIME ,
      @AnnalContent NVARCHAR(1000) ,
      @AnnalTime DATETIME ,
      @Inputer NVARCHAR(20)
    )
AS 
    SET NOCOUNT OFF 
    SELECT  @ClientAnnalID = ( ISNULL(MAX(ClientAnnalID), 0) + 1 )
    FROM    PE_ClientAnnal

    INSERT  INTO PE_ClientAnnal
            ( Contacter ,
              ClientAnnalID ,
              ClientID ,
              RelationType ,
              RelationTime ,
              AnnalContent ,
              Annaltime ,
              Inputer
	      )
    VALUES  ( @Contacter ,
              @ClientAnnalID ,
              @ClientID ,
              @RelationType ,
              @RelationTime ,
              @AnnalContent ,
              @AnnalTime ,
              @Inputer
	      )
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
[EnableWap] [bit] NULL,
[WapField] [ntext] 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_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
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]
		)
    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
	      )
    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_Contents_Model_IsTableNameExists]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Model_IsTableNameExists]
    (
      @TableName NVARCHAR(50)
    )
AS 
    BEGIN
        SET NOCOUNT OFF	
        SELECT  COUNT(*)
        FROM    [PE_Model]
        WHERE   TableName = @TableName

    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_UserManage_UserPointLog_UserPointSum]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_UserPointLog_UserPointSum]
    (
      @SumType INT ,
      @UserName NVARCHAR(100)
    )
AS 
    SET NOCOUNT OFF
    IF ( @SumType = 1 ) 
        BEGIN
            SELECT  SUM(Point)
            FROM    PE_PointLog
            WHERE   IncomePayout = 1
                    AND UserName = @UserName
        END
    ELSE 
        BEGIN
            SELECT  SUM(Point)
            FROM    PE_PointLog
            WHERE   IncomePayout = 2
                    AND UserName = @UserName
        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_Contents_Model_GetById]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Model_GetById] ( @ModelID INT )
AS 
    SELECT  *
    FROM    [PE_Model]
    WHERE   ModelID = @ModelID
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_UserPointLog_Exists]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_UserPointLog_Exists]
    (
      @UserName NVARCHAR(100)
    )
AS 
    SELECT  UserName
    FROM    PE_PointLog
    WHERE   UserName = @UserName
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_IsModelNameExists]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Model_IsModelNameExists]
    (
      @ModelName NVARCHAR(50)
    )
AS 
    SELECT  COUNT(*)
    FROM    [PE_Model]
    WHERE   ModelName = @ModelName
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_UserPointLog_UpdateTimes]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_UserPointLog_UpdateTimes]
    (
      @Id INT ,
      @Ip NVARCHAR(100)
    )
AS 
    SET NOCOUNT OFF

    BEGIN
        UPDATE  PE_PointLog
        SET     Times = Times + 1 ,
                IP = @Ip
        WHERE   LogID = @Id 
    END

    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_Disabled]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Model_Disabled]
    (
      @ModelID INT ,
      @Disabled BIT
    )
AS 
    UPDATE  [PE_Model]
    SET     Disabled = @Disabled
    WHERE   ModelID = @ModelID
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_UserPointLog_PointSum]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_UserPointLog_PointSum] ( @SumType INT )
AS 
    SET NOCOUNT OFF
    IF ( @SumType = 1 ) 
        BEGIN
            SELECT  SUM(Point)
            FROM    PE_PointLog
            WHERE   IncomePayout = 1
        END
    ELSE 
        BEGIN
            SELECT  SUM(Point)
            FROM    PE_PointLog
            WHERE   IncomePayout = 2
        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_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
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
    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_UserManage_UserPointLog_DeleteUser]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_UserPointLog_DeleteUser]
    (
      @UserName VARCHAR(4000)
    )
AS 
    SET NOCOUNT OFF
    BEGIN 
	 
        DELETE  FROM PE_PointLog
        WHERE   UserName IN ( @UserName )
	 
    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_Contents_ModelTemplates_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_ModelTemplates_Add]
    (
      @TemplateId INT ,
      @TemplateName NVARCHAR(255) ,
      @TemplateDescription NTEXT ,
      @Field NTEXT ,
      @IsEshop BIT  
    )
AS 
    INSERT  INTO [PE_ModelTemplates]
            ( [TemplateId] ,
              [TemplateName] ,
              [TemplateDescription] ,
              [Field] ,
              [IsEshop]   
		)
    VALUES  ( @TemplateId ,
              @TemplateName ,
              @TemplateDescription ,
              @Field ,
              @IsEshop
	      )
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_UserPointLog_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_UserPointLog_Delete]
    (
      @Date DATETIME ,
      @IsDelConsume BIT
	
    )
AS 
    SET NOCOUNT OFF
    BEGIN  
        DELETE  PE_PointLog
        WHERE   LogTime < @Date
                AND ( IsConsume IS NULL
                      OR IsConsume <= @IsDelConsume
                    )
    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_Contents_ModelTemplates_GetField]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_ModelTemplates_GetField] ( @TemplateId INT )
AS 
    SELECT  Field
    FROM    PE_ModelTemplates
    WHERE   TemplateId = @TemplateId
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_UserPointLog_Add]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_UserPointLog_Add]
    (
      @LogID INT ,
      @UserName NVARCHAR(100) ,
      @ModuleType INT ,
      @InfoID INT ,
      @Point REAL ,
      @LogTime DATETIME ,
      @Times INT ,
      @IncomePayOut INT ,
      @Remark NTEXT ,
      @IP NVARCHAR(100) ,
      @Inputer NVARCHAR(100) ,
      @Memo NTEXT ,
      @IsConsume BIT
    )
AS
    INSERT  INTO PE_PointLog
            ( UserName ,
              ModuleType ,
              InfoID ,
              Point ,
              LogTime ,
              Times ,
              IncomePayOut ,
              Remark ,
              IP ,
              Inputer ,
              Memo ,
              IsConsume
            )
    VALUES  ( @UserName ,
              @ModuleType ,
              @InfoID ,
              @Point ,
              @LogTime ,
              @Times ,
              @IncomePayOut ,
              @Remark ,
              @IP ,
              @Inputer ,
              @Memo ,
              @IsConsume
            )  
    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_ModelTemplates_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_ModelTemplates_Update]
    (
      @TemplateId INT ,
      @TemplateName NVARCHAR(255) ,
      @TemplateDescription NTEXT ,
      @Field NTEXT = '' ,
      @IsEshop BIT  
	
    )
AS 
    UPDATE  [PE_ModelTemplates]
    SET     TemplateName = @TemplateName ,
            TemplateDescription = @TemplateDescription
    WHERE   TemplateID = @TemplateId
    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_ModelTemplates_GetInfoById]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_ModelTemplates_GetInfoById] ( @TemplateId INT )
AS 
    SELECT  *
    FROM    PE_ModelTemplates
    WHERE   TemplateId = @TemplateId
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_Mood_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Mood_Add]
    (
      @GeneralID INT ,
      @MoodID INT ,
      @M0 INT ,
      @M1 INT ,
      @M2 INT ,
      @M3 INT ,
      @M4 INT ,
      @M5 INT ,
      @M6 INT ,
      @M7 INT ,
      @M8 INT ,
      @M9 INT ,
      @M10 INT ,
      @M11 INT ,
      @M12 INT ,
      @M13 INT ,
      @M14 INT ,
      @M15 INT ,
      @M16 INT ,
      @M17 INT ,
      @M18 INT ,
      @M19 INT
    )
AS 
    INSERT  INTO PE_Mood
            ( GeneralID ,
              MoodID ,
              M0 ,
              M1 ,
              M2 ,
              M3 ,
              M4 ,
              M5 ,
              M6 ,
              M7 ,
              M8 ,
              M9 ,
              M10 ,
              M11 ,
              M12 ,
              M13 ,
              M14 ,
              M15 ,
              M16 ,
              M17 ,
              M18 ,
              M19
            )
    VALUES  ( @GeneralID ,
              @MoodID ,
              @M0 ,
              @M1 ,
              @M2 ,
              @M3 ,
              @M4 ,
              @M5 ,
              @M6 ,
              @M7 ,
              @M8 ,
              @M9 ,
              @M10 ,
              @M11 ,
              @M12 ,
              @M13 ,
              @M14 ,
              @M15 ,
              @M16 ,
              @M17 ,
              @M18 ,
              @M19
            )
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_Nodes_GetById]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Nodes_GetById] ( @NodeID INT )
AS 
    SET NOCOUNT ON
    SELECT  *
    FROM    PE_Nodes
    WHERE   NodeID = @NodeID
    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_Nodes_GetUpOrderListByOrderId]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Nodes_GetUpOrderListByOrderId]
    (
      @ParentID INT ,
      @OrderID INT
	
    )
AS 
    SET NOCOUNT ON
    SELECT  NodeID ,
            OrderID ,
            Child ,
            ParentPath ,
            PrevID ,
            NextID
    FROM    PE_Nodes
    WHERE   ParentID = @ParentID
            AND OrderID < @OrderID
    ORDER BY OrderID DESC  
    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_Shop_Producer_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Producer_Add]
    (
      @ProducerID INT ,
      @ProducerName NVARCHAR(200) ,
      @ProducerShortName NVARCHAR(50) ,
      @Birthday DATETIME ,
      @Address NVARCHAR(255) ,
      @Phone NVARCHAR(50) ,
      @Fax NVARCHAR(50) ,
      @Postcode NVARCHAR(10) ,
      @Homepage NVARCHAR(50) ,
      @Email NVARCHAR(50) ,
      @ProducerIntro NTEXT ,
      @ProducerPhoto NVARCHAR(255) ,
      @ProducerType INT
	
    )
AS 
    INSERT  INTO PE_Producer
            ( ProducerID ,
              ProducerName ,
              ProducerShortName ,
              BirthDay ,
              Address ,
              Phone ,
              Fax ,
              Postcode ,
              Homepage ,
              Email ,
              ProducerIntro ,
              ProducerPhoto ,
              ProducerType ,
              Passed ,
              LastUseTime
            )
    VALUES  ( @ProducerID ,
              @ProducerName ,
              @ProducerShortName ,
              @Birthday ,
              @Address ,
              @Phone ,
              @Fax ,
              @Postcode ,
              @Homepage ,
              @Email ,
              @ProducerIntro ,
              @ProducerPhoto ,
              @ProducerType ,
              1 ,
              GETDATE()
            )
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_Nodes_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Nodes_Add]
    (
      @NodeId INT ,
      @NodeIdentifier NVARCHAR(50) ,
      @NodeType INT ,
      @ParentId INT ,
      @ParentPath NTEXT ,
      @Depth INT ,
      @RootId INT ,
      @Child INT ,
      @ArrChildId NTEXT ,
      @PrevId INT ,
      @NextId INT ,
      @OrderId INT ,
      @NodeDir NVARCHAR(50) ,
      @ParentDir NTEXT ,
      @NodeName NVARCHAR(50) ,
      @Tips NVARCHAR(255) ,
      @Description NTEXT ,
      @NodePicUrl NVARCHAR(255) ,
      @MetaKeywords NVARCHAR(255) ,
      @MetaDescription NVARCHAR(255) ,
      @ShowOnMenu BIT ,
      @ShowOnPath BIT ,
      @ShowOnMap BIT ,
      @ShowOnListIndex BIT ,
      @ShowOnListParent BIT ,
      @PurviewType INT ,
      @Creater NVARCHAR(50) ,
      @InheritPurviewFromParent INT ,
      @WorkFlowId INT ,
      @HitsOfHot INT ,
      @LeastOfEliteLevel INT ,
      @OpenType INT ,
      @ItemCount INT ,
      @ItemChecked INT ,
      @CommentCount INT ,
      @CustomContent NTEXT ,
      @EnableSubDomain BIT ,
      @IsCreateContentPage BIT ,
      @IsCreateListPage BIT ,
      @AutoCreateHtmlType INT ,
      @ContentPageHtmlRule NVARCHAR(255) ,
      @ListPageHtmlRule NVARCHAR(255) ,
      @ListPageSavePathType INT ,
      @ListPagePostfix NVARCHAR(50) ,
      @RelateNode NTEXT ,
      @RelateSpecial NTEXT ,
      @ItemAspxFileName NVARCHAR(50) ,
      @DefaultTemplateFile NVARCHAR(255) ,
      @ContainChildTemplateFile NVARCHAR(255) ,
      @ItemOpenType INT ,
      @ItemListOrderType INT ,
      @ItemPageSize INT ,
      @LinkUrl NVARCHAR(255) ,
      @Settings NTEXT ,
      @IPLock NTEXT ,
      @NeedCreateHtml BIT ,
      @CultureName NVARCHAR(10) ,
      @SubDomain NVARCHAR(255) ,
      @ConfigType INT ,
      @IncrementalUpdatePages INT
    )
AS 
    INSERT  INTO PE_Nodes
            ( NodeID ,
              NodeIdentifier ,
              NodeType ,
              ParentID ,
              ParentPath ,
              Depth ,
              RootID ,
              Child ,
              arrChildID ,
              PrevID ,
              NextID ,
              OrderID ,
              NodeDir ,
              ParentDir ,
              NodeName ,
              Tips ,
              Description ,
              NodePicUrl ,
              Meta_Keywords ,
              Meta_Description ,
              ShowOnMenu ,
              ShowOnPath ,
              ShowOnMap ,
              ShowOnList_Index ,
              ShowOnList_Parent ,
              PurviewType ,
              Creater ,
              InheritPurviewFromParent ,
              WorkFlowID ,
              HitsOfHot ,
              LeastOfEliteLevel ,
              OpenType ,
              ItemCount ,
              ItemChecked ,
              CommentCount ,
              Custom_Content ,
              IsCreateContentPage ,
              IsCreateListPage ,
              AutoCreateHtmlType ,
              ContentPageHtmlRule ,
              ListPageHtmlRule ,
              ListPageSavePathType ,
              ListPagePostfix ,
              RelateNode ,
              RelateSpecial ,
              ItemAspxFileName ,
              DefaultTemplateFile ,
              ContainChildTemplateFile ,
              ItemOpenType ,
              ItemListOrderType ,
              ItemPageSize ,
              LinkUrl ,
              Settings ,
              IPLock ,
              NeedCreateHtml ,
              CultureName ,
              EnableSubDomain ,
              SubDomain ,
              ConfigType ,
              IncrementalUpdatePages
		)
    VALUES  ( @NodeId ,
              @NodeIdentifier ,
              @NodeType ,
              @ParentId ,
              @ParentPath ,
              @Depth ,
              @RootId ,
              @Child ,
              @ArrChildId ,
              @PrevId ,
              @NextId ,
              @OrderId ,
              @NodeDir ,
              @ParentDir ,
              @NodeName ,
              @Tips ,
              @Description ,
              @NodePicUrl ,
              @MetaKeywords ,
              @MetaDescription ,
              @ShowOnMenu ,
              @ShowOnPath ,
              @ShowOnMap ,
              @ShowOnListIndex ,
              @ShowOnListParent ,
              @PurviewType ,
              @Creater ,
              @InheritPurviewFromParent ,
              @WorkFlowId ,
              @HitsOfHot ,
              @LeastOfEliteLevel ,
              @OpenType ,
              @ItemCount ,
              @ItemChecked ,
              @CommentCount ,
              @CustomContent ,
              @IsCreateContentPage ,
              @IsCreateListPage ,
              @AutoCreateHtmlType ,
              @ContentPageHtmlRule ,
              @ListPageHtmlRule ,
              @ListPageSavePathType ,
              @ListPagePostfix ,
              @RelateNode ,
              @RelateSpecial ,
              @ItemAspxFileName ,
              @DefaultTemplateFile ,
              @ContainChildTemplateFile ,
              @ItemOpenType ,
              @ItemListOrderType ,
              @ItemPageSize ,
              @LinkUrl ,
              @Settings ,
              @IPLock ,
              @NeedCreateHtml ,
              @CultureName ,
              @EnableSubDomain ,
              @SubDomain ,
              @ConfigType ,
              @IncrementalUpdatePages
		)
    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_Shop_Producer_SetOnTop]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Producer_SetOnTop]
    (
      @ProducerID INT ,
      @OnTop BIT
    )
AS 
    UPDATE  PE_Producer
    SET     OnTop = @OnTop
    WHERE   ProducerID = @ProducerID
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_Nodes_UpdateOrderId]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Nodes_UpdateOrderId]
    (
      @NodeID INT ,
      @OrderID INT
	
    )
AS
    UPDATE  PE_Nodes
    SET     OrderID = @OrderID
    WHERE   NodeID = @NodeID
    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_Shop_Producer_SetElite]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Producer_SetElite]
    (
      @ProducerID INT ,
      @IsElite BIT
    )
AS 
    UPDATE  PE_Producer
    SET     IsElite = @IsElite
    WHERE   ProducerID = @ProducerID
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_Nodes_UpDate]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Nodes_UpDate]
    (
      @NodeId INT ,
      @NodeIdentifier NVARCHAR(50) ,
      @ParentId INT ,
      @ParentPath NTEXT ,
      @Depth INT ,
      @RootId INT ,
      @Child INT ,
      @ArrChildId NTEXT ,
      @PrevId INT ,
      @NextId INT ,
      @OrderId INT ,
      @NodeDir NVARCHAR(50) ,
      @ParentDir NTEXT ,
      @EnableSubDomain BIT ,
      @NodeName NVARCHAR(50) ,
      @Tips NVARCHAR(255) ,
      @Description NTEXT ,
      @NodePicUrl NVARCHAR(255) ,
      @MetaKeywords NVARCHAR(255) ,
      @MetaDescription NVARCHAR(255) ,
      @ShowOnMenu BIT ,
      @ShowOnPath BIT ,
      @ShowOnMap BIT ,
      @ShowOnListIndex BIT ,
      @ShowOnListParent BIT ,
      @PurviewType INT ,
      @Creater NVARCHAR(50) ,
      @InheritPurviewFromParent INT ,
      @WorkFlowId INT ,
      @HitsOfHot INT ,
      @LeastOfEliteLevel INT ,
      @OpenType INT ,
      @ItemCount INT ,
      @ItemChecked INT ,
      @CommentCount INT ,
      @CustomContent NTEXT ,
      @IsCreateContentPage BIT ,
      @IsCreateListPage BIT ,
      @AutoCreateHtmlType INT ,
      @ContentPageHtmlRule NVARCHAR(255) ,
      @ListPageHtmlRule NVARCHAR(255) ,
      @ListPageSavePathType INT ,
      @ListPagePostfix NVARCHAR(50) ,
      @RelateNode NTEXT ,
      @RelateSpecial NTEXT ,
      @ItemAspxFileName NVARCHAR(50) ,
      @DefaultTemplateFile NVARCHAR(255) ,
      @ContainChildTemplateFile NVARCHAR(255) ,
      @ItemOpenType INT ,
      @ItemListOrderType INT ,
      @ItemPageSize INT ,
      @LinkUrl NVARCHAR(255) ,
      @Settings NTEXT ,
      @IPLock NTEXT ,
      @NeedCreateHtml BIT ,
      @CultureName NVARCHAR(10) ,
      @SubDomain NVARCHAR(255) ,
      @ConfigType INT ,
      @IncrementalUpdatePages INT
    )
AS 
    UPDATE  PE_Nodes
    SET     NodeIdentifier = @NodeIdentifier ,
            ParentId = @ParentId ,
            ParentPath = @ParentPath ,
            Depth = @Depth ,
            RootId = @RootId ,
            Child = @Child ,
            ArrChildId = @ArrChildId ,
            PrevId = @PrevId ,
            NextId = @NextId ,
            OrderId = @OrderId ,
            NodeDir = @NodeDir ,
            ParentDir = @ParentDir ,
            EnableSubDomain = @EnableSubDomain ,
            NodeName = @NodeName ,
            Tips = @Tips ,
            Description = @Description ,
            NodePicUrl = @NodePicUrl ,
            Meta_Keywords = @MetaKeywords ,
            Meta_Description = @MetaDescription ,
            ShowOnMenu = @ShowOnMenu ,
            ShowOnPath = @ShowOnPath ,
            ShowOnMap = @ShowOnMap ,
            ShowOnList_Index = @ShowOnListIndex ,
            ShowOnList_Parent = @ShowOnListParent ,
            PurviewType = @PurviewType ,
            Creater = @Creater ,
            InheritPurviewFromParent = @InheritPurviewFromParent ,
            WorkFlowID = @WorkFlowId ,
            HitsOfHot = @HitsOfHot ,
            LeastOfEliteLevel = @LeastOfEliteLevel ,
            OpenType = @OpenType ,
            ItemCount = @ItemCount ,
            ItemChecked = @ItemChecked ,
            CommentCount = @CommentCount ,
            Custom_Content = @CustomContent ,
            IsCreateContentPage = @IsCreateContentPage ,
            IsCreateListPage = @IsCreateListPage ,
            AutoCreateHtmlType = @AutoCreateHtmlType ,
            ContentPageHtmlRule = @ContentPageHtmlRule ,
            ListPageHtmlRule = @ListPageHtmlRule ,
            ListPageSavePathType = @ListPageSavePathType ,
            ListPagePostfix = @ListPagePostfix ,
            RelateNode = @RelateNode ,
            RelateSpecial = @RelateSpecial ,
            ItemAspxFileName = @ItemAspxFileName ,
            DefaultTemplateFile = @DefaultTemplateFile ,
            ContainChildTemplateFile = @ContainChildTemplateFile ,
            ItemOpenType = @ItemOpenType ,
            ItemListOrderType = @ItemListOrderType ,
            ItemPageSize = @ItemPageSize ,
            LinkUrl = @LinkUrl ,
            Settings = @Settings ,
            IPLock = @IPLock ,
            NeedCreateHtml = @NeedCreateHtml ,
            CultureName = @CultureName ,
            SubDomain = @SubDomain ,
            ConfigType = @ConfigType ,
            IncrementalUpdatePages = @IncrementalUpdatePages
    WHERE   NodeID = @NodeID
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_Producer_ProducerNameExists]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Producer_ProducerNameExists]
    (
      @ProducerName NVARCHAR(200)
    )
AS 
    DECLARE @result INT

    SELECT  *
    FROM    PE_Producer
    WHERE   ProducerName = @ProducerName

    IF @@rowcount > 0 
        SET @result = 1
    ELSE 
        SET @result = 0

    RETURN @result
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_Nodes_UpdateNextId]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Nodes_UpdateNextId]
    (
      @NodeID INT ,
      @NextID INT
	
    )
AS
    UPDATE  PE_Nodes
    SET     NextID = @NextID
    WHERE   NodeID = @NodeID
    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_Shop_Producer_GetById]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Producer_GetById] ( @ProducerID INT )
AS 
    SELECT  ProducerID ,
            ProducerName ,
            ProducerShortName ,
            BirthDay ,
            Address ,
            Phone ,
            Fax ,
            Postcode ,
            Homepage ,
            Email ,
            ProducerIntro ,
            ProducerPhoto ,
            ProducerType ,
            Passed ,
            onTop ,
            IsElite
    FROM    PE_Producer
    WHERE   ProducerID = @ProducerID
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_Nodes_UpdateBasicNodesInfo]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_Nodes_UpdateBasicNodesInfo]
    (
      @NodeID INT ,
      @RootID INT ,
      @OrderID INT ,
      @ParentID INT ,
      @Child INT ,
      @ParentPath NTEXT ,
      @Depth INT ,
      @PrevID INT ,
      @NextID INT ,
      @ArrChildID NTEXT ,
      @ParentDir NTEXT ,
      @NodeDir NVARCHAR(50) ,
      @PurviewType INT
    )
AS 
    UPDATE  PE_Nodes
    SET     RootID = @RootID ,
            OrderID = @OrderID ,
            ParentID = @ParentID ,
            Child = @Child ,
            ParentPath = @ParentPath ,
            Depth = @Depth ,
            PrevID = @PrevID ,
            NextID = @NextID ,
            arrChildID = @ArrChildID ,
            ParentDir = @ParentDir ,
            NodeDir = @NodeDir ,
            PurviewType = @PurviewType
    WHERE   NodeID = @NodeID
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_Producer_SetPassed]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Producer_SetPassed]
    (
      @ProducerID INT ,
      @Passed BIT
    )
AS 
    UPDATE  PE_Producer
    SET     Passed = @Passed
    WHERE   ProducerID = @ProducerID
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_Producer_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Producer_Update]
    (
      @ProducerID INT ,
      @ProducerName NVARCHAR(200) ,
      @ProducerShortName NVARCHAR(50) ,
      @Birthday DATETIME ,
      @Address NVARCHAR(255) ,
      @Phone NVARCHAR(50) ,
      @Fax NVARCHAR(50) ,
      @Postcode NVARCHAR(10) ,
      @Homepage NVARCHAR(50) ,
      @Email NVARCHAR(50) ,
      @ProducerIntro NTEXT ,
      @ProducerPhoto NVARCHAR(255) ,
      @ProducerType INT
	
    )
AS 
    UPDATE  PE_Producer
    SET     ProducerName = @ProducerName ,
            ProducerShortName = @ProducerShortName ,
            BirthDay = @Birthday ,
            Address = @Address ,
            Phone = @Phone ,
            Fax = @Fax ,
            Postcode = @Postcode ,
            Homepage = @Homepage ,
            Email = @Email ,
            ProducerIntro = @ProducerIntro ,
            ProducerPhoto = @ProducerPhoto ,
            ProducerType = @ProducerType
    WHERE   ProducerID = @ProducerID
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(20) ,    --用户名
      @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_Crm_Address_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_Address_Update]
    (
      @AddressID INT ,    --地址Id
      @UserName NVARCHAR(20) ,    --用户名
      @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_Company_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_Company_Update]
    (
      @CompanyID INT ,
      @ClientID INT ,
      @CompanyName NVARCHAR(200) ,
      @Country NVARCHAR(50) ,
      @Province NVARCHAR(30) ,
      @City NVARCHAR(30) ,
      @Address NVARCHAR(255) ,
      @ZipCode NVARCHAR(50) ,
      @Phone NVARCHAR(50) ,
      @Fax NVARCHAR(30) ,
      @Homepage NVARCHAR(255) ,
      @BankOfDeposit NVARCHAR(50) ,
      @BankAccount NVARCHAR(50) ,
      @TaxNum NVARCHAR(50) ,
      @StatusInField INT ,
      @CompanySize INT ,
      @BusinessScope NVARCHAR(200) ,
      @AnnualSales NVARCHAR(50) ,
      @ManagementForms INT ,
      @RegisteredCapital NVARCHAR(50) ,
      @CompanyIntro NTEXT ,
      @CompanyPic NVARCHAR(255)
    )
AS 
    UPDATE  PE_Company
    SET     ClientID = @ClientID ,
            CompanyName = @CompanyName ,
            Country = @Country ,
            Province = @Province ,
            City = @City ,
            Address = @Address ,
            ZipCode = @ZipCode ,
            Phone = @Phone ,
            Fax = @Fax ,
            Homepage = @Homepage ,
            BankOfDeposit = @BankOfDeposit ,
            BankAccount = @BankAccount ,
            TaxNum = @TaxNum ,
            StatusInField = @StatusInField ,
            CompanySize = @CompanySize ,
            BusinessScope = @BusinessScope ,
            AnnualSales = @AnnualSales ,
            ManagementForms = @ManagementForms ,
            RegisteredCapital = @RegisteredCapital ,
            CompanyIntro = @CompanyIntro ,
            CompanyPic = @CompanyPic
    WHERE   CompanyID = @CompanyID
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)
    )
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
    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_Crm_Company_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_Company_Add]
    (
      @CompanyID INT ,
      @ClientID INT ,
      @CompanyName NVARCHAR(200) ,
      @Country NVARCHAR(50) ,
      @Province NVARCHAR(30) ,
      @City NVARCHAR(30) ,
      @Address NVARCHAR(255) ,
      @ZipCode NVARCHAR(50) ,
      @Phone NVARCHAR(50) ,
      @Fax NVARCHAR(30) ,
      @Homepage NVARCHAR(255) ,
      @BankOfDeposit NVARCHAR(50) ,
      @BankAccount NVARCHAR(50) ,
      @TaxNum NVARCHAR(50) ,
      @StatusInField INT ,
      @CompanySize INT ,
      @BusinessScope NVARCHAR(200) ,
      @AnnualSales NVARCHAR(50) ,
      @ManagementForms INT ,
      @RegisteredCapital NVARCHAR(50) ,
      @CompanyIntro NTEXT ,
      @CompanyPic NVARCHAR(255)
    )
AS 
    INSERT  INTO [PE_Company]
            ( [CompanyID] ,
              [ClientID] ,
              [CompanyName] ,
              [Country] ,
              [Province] ,
              [City] ,
              [Address] ,
              [ZipCode] ,
              [Phone] ,
              [Fax] ,
              [Homepage] ,
              [BankOfDeposit] ,
              [BankAccount] ,
              [TaxNum] ,
              [StatusInField] ,
              [CompanySize] ,
              [BusinessScope] ,
              [AnnualSales] ,
              [ManagementForms] ,
              [RegisteredCapital] ,
              [CompanyIntro] ,
              [CompanyPic] 
            )
    VALUES  ( @CompanyID ,
              @ClientID ,
              @CompanyName ,
              @Country ,
              @Province ,
              @City ,
              @Address ,
              @ZipCode ,
              @Phone ,
              @Fax ,
              @Homepage ,
              @BankOfDeposit ,
              @BankAccount ,
              @TaxNum ,
              @StatusInField ,
              @CompanySize ,
              @BusinessScope ,
              @AnnualSales ,
              @ManagementForms ,
              @RegisteredCapital ,
              @CompanyIntro ,
              @CompanyPic 
            )
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_SetPassed]'
GO
ALTER PROCEDURE [dbo].[PR_Survey_SurveyManager_SetPassed]
    (
      @SurveyID INT ,
      @TableName NVARCHAR(50)
    )
AS 
    UPDATE  PE_Survey
    SET     IsOpen = 1
    WHERE   SurveyID = @SurveyID
        
    IF OBJECT_ID(@TableName) IS NOT NULL
        BEGIN
            EXEC('drop table ' +@TableName)
        END
    EXEC('CREATE TABLE '+ @TableName+' ([RecordID] [int] IDENTITY (1, 1) PRIMARY KEY,[SurveyID] [int],[UserName] [nvarchar](255),[IP] [nvarchar](255),[SubmitTime] [datetime])')
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_Company_GetByClientId]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_Company_GetByClientId] ( @ClientID INT )
AS 
    SELECT  *
    FROM    PE_Company
    WHERE   ClientID = @ClientID
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)
    )
AS 
    INSERT  INTO [PE_Survey]
            ( [SurveyID] ,
              [SurveyName] ,
              [Description] ,
              [FileName] ,
              [IPRepeat] ,
              [CreateDate] ,
              [EndTime] ,
              [IsOpen] ,
              [NeedLogin] ,
              [PresentPoint] ,
              [LockIPType] ,
              [SetIPLock] ,
              [LockUrl] ,
              [SetPassword] ,
              [Template]    
		)
    VALUES  ( @SurveyID ,
              @SurveyName ,
              @Description ,
              @FileName ,
              @IPRepeat ,
              @CreateDate ,
              @EndTime ,
              @IsOpen ,
              @NeedLogin ,
              @PresentPoint ,
              @LockIPType ,
              @SetIPLock ,
              @LockUrl ,
              @SetPassword ,
              @Template
	      )
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_ComplainItem_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_ComplainItem_Update]
    (
      @ItemID INT ,
      @ClientID INT ,
      @ContacterID INT ,
      @ComplainType INT ,
      @ComplainMode INT ,
      @Title NVARCHAR(50) ,
      @Content NTEXT ,
      @FirstReceiver NVARCHAR(50) ,
      @DateAndTime DATETIME ,
      @MagnitudeOfExigence INT ,
      @Process NTEXT ,
      @Processor NVARCHAR(50) ,
      @Result NVARCHAR(50) ,
      @EndTime DATETIME ,
      @Feedback NVARCHAR(255) ,
      @ConfirmTime DATETIME ,
      @ConfirmCaller NVARCHAR(50) ,
      @ConfirmScore INT ,
      @ConfirmFeedback NVARCHAR(255) ,
      @Status INT ,
      @CurrentOwner NVARCHAR(50) ,
      @Remark NVARCHAR(255) ,
      @Defendant NVARCHAR(50)
    )
AS 
    UPDATE  [PE_ComplainItem]
    SET     [ClientID] = @ClientID ,
            [ContacterID] = @ContacterID ,
            [ComplainType] = @ComplainType ,
            [ComplainMode] = @ComplainMode ,
            [Title] = @Title ,
            [Content] = @Content ,
            [FirstReceiver] = @FirstReceiver ,
            [DateAndTime] = @DateAndTime ,
            [MagnitudeOfExigence] = @MagnitudeOfExigence ,
            [Process] = @Process ,
            [Processor] = @Processor ,
            [Result] = @Result ,
            [EndTime] = @EndTime ,
            [Feedback] = @Feedback ,
            [ConfirmTime] = @ConfirmTime ,
            [ConfirmCaller] = @ConfirmCaller ,
            [ConfirmScore] = @ConfirmScore ,
            [ConfirmFeedback] = @ConfirmFeedback ,
            [Status] = @Status ,
            [CurrentOwner] = @CurrentOwner ,
            [Remark] = @Remark ,
            [Defendant] = @Defendant
    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_Crm_ComplainItem_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_ComplainItem_Add]
    (
      @ItemID INT ,
      @ClientID INT ,
      @ContacterID INT ,
      @ComplainType INT ,
      @ComplainMode INT ,
      @Title NVARCHAR(50) ,
      @Content NTEXT ,
      @FirstReceiver NVARCHAR(50) ,
      @DateAndTime DATETIME ,
      @MagnitudeOfExigence INT ,
      @Process NTEXT ,
      @Processor NVARCHAR(50) ,
      @Result NVARCHAR(50) ,
      @EndTime DATETIME ,
      @Feedback NVARCHAR(255) ,
      @ConfirmTime DATETIME ,
      @ConfirmCaller NVARCHAR(50) ,
      @ConfirmScore INT ,
      @ConfirmFeedback NVARCHAR(255) ,
      @Status INT ,
      @CurrentOwner NVARCHAR(50) ,
      @Remark NVARCHAR(255) ,
      @Defendant NVARCHAR(50)
    )
AS 
    INSERT  INTO [PE_ComplainItem]
            ( [ItemID] ,
              [ClientID] ,
              [ContacterID] ,
              [ComplainType] ,
              [ComplainMode] ,
              [Title] ,
              [Content] ,
              [FirstReceiver] ,
              [DateAndTime] ,
              [MagnitudeOfExigence] ,
              [Process] ,
              [Processor] ,
              [Result] ,
              [EndTime] ,
              [Feedback] ,
              [ConfirmTime] ,
              [ConfirmCaller] ,
              [ConfirmScore] ,
              [ConfirmFeedback] ,
              [Status] ,    --记录状态，0为未处理，1为处理中，2为已处理，3为已回访
              [CurrentOwner] ,
              [Remark] ,
              [Defendant]
		)
    VALUES  ( @ItemID ,
              @ClientID ,
              @ContacterID ,
              @ComplainType ,
              @ComplainMode ,
              @Title ,
              @Content ,
              @FirstReceiver ,
              @DateAndTime ,
              @MagnitudeOfExigence ,
              @Process ,
              @Processor ,
              @Result ,
              @EndTime ,
              @Feedback ,
              @ConfirmTime ,
              @ConfirmCaller ,
              @ConfirmScore ,
              @ConfirmFeedback ,
              @Status ,
              @CurrentOwner ,
              @Remark ,
              @Defendant
	      )
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(20) ,
      @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].[PR_Crm_Contacter_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_Contacter_Update]
    (
      @ContacterID INT ,
      @ClientID INT ,
      @ParentID INT ,
      @UserName NVARCHAR(20) ,
      @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].[PE_Users]'
GO
ALTER TABLE [dbo].[PE_Users] ADD
[WidgetSetting] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
[WidgetPara] [ntext] 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_Accessories_Region_CheckArea]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Region_CheckArea]
    (
      @Area VARCHAR(25) ,
      @Province VARCHAR(30) ,
      @City VARCHAR(25)
    )
AS 
    SET NOCOUNT ON
    SELECT  COUNT(*)
    FROM    PE_Region
    WHERE   Area = @Area
            AND Province = @Province
            AND City = @City
    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_Region_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Region_Add]
    (
      @Country NVARCHAR(20) ,
      @Province NVARCHAR(25) ,
      @City NVARCHAR(30) ,
      @Area VARCHAR(25) ,
      @PostCode VARCHAR(10) ,
      @AreaCode NVARCHAR(8)
    )
AS 
    SET NOCOUNT OFF
    INSERT  INTO PE_Region
            ( Country ,
              Province ,
              City ,
              Area ,
              PostCode ,
              AreaCode
            )
    VALUES  ( @Country ,
              @Province ,
              @City ,
              @Area ,
              @PostCode ,
              @AreaCode
            )
    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_Region_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Region_Delete] ( @RegionID VARCHAR(10) )
AS 
    SET NOCOUNT OFF 
    DELETE  PE_Region
    WHERE   [RegionID] = @RegionID
    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_Region_GetByID]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Region_GetByID] ( @RegionId INT )
AS 
    SELECT  *
    FROM    PE_Region
    WHERE   RegionID = @RegionId  
    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_Region_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Region_Update]
    (
      @RegionID INT ,
      @Country NVARCHAR(20) ,
      @Province NVARCHAR(25) ,
      @City NVARCHAR(30) ,
      @Area VARCHAR(25) ,
      @PostCode VARCHAR(10) ,
      @AreaCode NVARCHAR(8)
    )
AS 
    SET NOCOUNT OFF
    UPDATE  PE_Region
    SET     Country = @Country ,
            Province = @Province ,
            City = @City ,
            Area = @Area ,
            PostCode = @PostCode ,
            AreaCode = @AreaCode
    WHERE   RegionID = @RegionID
    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_UpdateUserStatus]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_Users_UpdateUserStatus]
    (
      @UserID INT ,
      @Status INT
    )
AS 
    UPDATE  PE_Users
    SET     [Status] = @Status
    WHERE   UserID = @UserID
	
    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_Update]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_Users_Update]
    (
      @UserID INT ,
      @GroupID INT ,
      @CompanyID INT ,
      @ClientID INT ,
      @UserType INT ,
      @UserName NVARCHAR(20) ,
      @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_UserManage_Users_GetById]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_Users_GetById] ( @UserID INT )
AS 
    SELECT  *
    FROM    PE_Users
    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_UserManage_Users_BatchLock]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_Users_BatchLock]
    (
      @strUserID VARCHAR(4000) ,
      @LockState INT ,
      @ApproveState INT
    )
AS 
    DECLARE @UserId INT
    DECLARE @split VARCHAR(1)
	
    SET @split = ','
    SET @strUserId = @strUserId + ','
	
    WHILE CHARINDEX(@split, @strUserId) > 0 
        BEGIN
            SET @UserId = CONVERT(INT, LEFT(@strUserId,
                                            CHARINDEX(@split, @strUserId) - 1))
            SET @strUserId = STUFF(@strUserId, 1,
                                   CHARINDEX(@split, @strUserId), '')
	
            UPDATE  PE_Users
            SET     [Status] = @LockState ,
                    LastLockoutTime = GETDATE()
            WHERE   UserId = @UserId
                    AND [Status] = @ApproveState
        END
	
    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_BatchUnLock]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_Users_BatchUnLock]
    (
      @strUserID VARCHAR(4000) ,
      @LockState INT ,
      @ApproveState INT
	
    )
AS
    DECLARE @Status BIT
    DECLARE @UserId INT
    DECLARE @split VARCHAR(1)
	
    SET @split = ','
    SET @strUserId = @strUserId + ','
	
    WHILE CHARINDEX(@split, @strUserId) > 0 
        BEGIN
            SET @UserId = CONVERT(INT, LEFT(@strUserId,
                                            CHARINDEX(@split, @strUserId) - 1))
            SET @strUserId = STUFF(@strUserId, 1,
                                   CHARINDEX(@split, @strUserId), '')
	
            UPDATE  PE_Users
            SET     [Status] = @ApproveState
            WHERE   UserId = @UserId
                    AND [Status] = @LockState
        END
	
    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_BatchInquire]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_Users_BatchInquire]
    (
      @UserType INT ,
      @Id NVARCHAR(4000) ,
      @EndTime DATETIME
    )
AS 
    BEGIN
        SET NOCOUNT OFF
				
        IF ( @UserType = 0 ) 
            BEGIN
                UPDATE  PE_Users
                SET     EndTime = @EndTime 
            END
        ELSE 
            IF ( @UserType = 1 ) 
                BEGIN
                    UPDATE  PE_Users
                    SET     EndTime = @EndTime
                    WHERE   GroupId IN ( @Id )
                END
            ELSE 
                IF ( @UserType = 2 ) 
                    BEGIN
                        UPDATE  PE_Users
                        SET     EndTime = @EndTime
                        WHERE   UserId IN ( @Id )
                    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_UserManage_User_Exists]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_User_Exists]
    (
      @UserName NVARCHAR(250)
    )
AS 
    SELECT  COUNT(*)
    FROM    PE_Users
    WHERE   UserName = @UserName
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_UserNameIsRepeat]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_UserNameIsRepeat]
    (
      @UserName NVARCHAR(4000)
    )
AS 
    BEGIN
        SET NOCOUNT ON
        DECLARE @Total INT 
	
        SELECT  @Total = COUNT(*)
        FROM    PE_Users
        WHERE   UserName = @UserName
	
        RETURN @Total
    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_UserManage_Users_Add]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_Users_Add]
    (
      @UserID INT ,
      @GroupID INT ,
      @CompanyID INT ,
      @ClientID INT ,
      @UserType INT ,
      @UserName NVARCHAR(20) ,
      @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_UserValidLog_DeleteUser]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_UserValidLog_DeleteUser]
    (
      @UserName VARCHAR(4000)
    )
AS 
    SET NOCOUNT OFF
    BEGIN 
	 
        DELETE  FROM PE_ValidLog
        WHERE   UserName IN ( @UserName )
	 
    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_UserManage_UserValidLog_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_UserValidLog_Delete] ( @Date DATETIME )
AS 
    SET NOCOUNT OFF
    BEGIN 
	 
        DELETE  FROM PE_ValidLog
        WHERE   LogTime < @Date
	 
    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_UserManage_UserValidLog_Add]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_UserValidLog_Add]
    (
      @LogID INT ,
      @UserName NVARCHAR(100) ,
      @ValidNum INT ,
      @IncomePayOut INT ,
      @LogTime DATETIME ,
      @Remark NTEXT ,
      @IP NVARCHAR(100) ,
      @Inputer NVARCHAR(100) ,
      @Memo NTEXT
    )
AS
    INSERT  INTO PE_ValidLog
            ( UserName ,
              ValidNum ,
              IncomePayout ,
              LogTime ,
              Remark ,
              IP ,
              Inputer ,
              Memo
            )
    VALUES  ( @UserName ,
              @ValidNum ,
              @IncomePayout ,
              @LogTime ,
              @Remark ,
              @IP ,
              @Inputer ,
              @Memo
            )  
    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_UserValidLog_Exists]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_UserValidLog_Exists]
    (
      @UserName NVARCHAR(100)
    )
AS 
    SELECT  COUNT(*)
    FROM    PE_ValidLog
    WHERE   UserName = @UserName
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_SurveyVote_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Survey_SurveyVote_Add]
    (
      @SurveyId INT ,
      @QuestionId INT ,
      @OptionCount INT 
    )
AS 
    DECLARE @option INT
    SET @option = 0

    WHILE ( @option < @OptionCount ) 
        BEGIN
            INSERT  INTO [PE_SurveyVote]
                    ( SurveyId, QuestionId, OptionId )
            VALUES  ( @SurveyId, @QuestionId, @option )
            SET @option = @option + 1
        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_DeliverCharge_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_DeliverCharge_Delete] ( @id INT )
AS 
    BEGIN

        DELETE  PE_DeliverCharge
        WHERE   ID = @id
	
    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_Trademark_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Trademark_Update]
    (
      @TrademarkID INT ,
      @TrademarkName NVARCHAR(200) ,
      @TrademarkType INT ,
      @ProducerId INT ,
      @IsElite BIT ,
      @TrademarkPhoto NVARCHAR(255) ,
      @TrademarkIntro NTEXT	
	
    )
AS 
    UPDATE  PE_Trademark
    SET     TrademarkName = @TrademarkName ,
            TrademarkType = @TrademarkType ,
            ProducerID = @ProducerId ,
            IsElite = @IsElite ,
            TrademarkPhoto = @TrademarkPhoto ,
            TrademarkIntro = @TrademarkIntro
    WHERE   TrademarkID = @TrademarkID
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_DeliverCharge_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_DeliverCharge_Add]
    (
      @deliverTypeID INT ,
      @areaType INT = 0 ,
      @arrArea NTEXT ,
      @charge_Min MONEY = 0 ,
      @weight_Min FLOAT = 0 ,
      @chargePerUnit MONEY = 0 ,
      @weightPerUnit FLOAT = 0 ,
      @charge_Max MONEY = 0
    )
AS 
    SET NOCOUNT OFF
 
    INSERT  INTO PE_DeliverCharge
            ( [DeliverTypeID] ,
              [AreaType] ,
              [arrArea] ,
              [Charge_Min] ,
              [Weight_Min] ,
              [ChargePerUnit] ,
              [WeightPerUnit] ,
              [Charge_Max]
            )
    VALUES  ( @deliverTypeID ,
              @areaType ,
              @arrArea ,
              @charge_Min ,
              @weight_Min ,
              @chargePerUnit ,
              @weightPerUnit ,
              @charge_Max
	      )
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_Trademark_TrademarkNameExists]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Trademark_TrademarkNameExists]
    (
      @TrademarkName NVARCHAR(200)
    )
AS 
    DECLARE @result INT

    SELECT  *
    FROM    PE_Trademark
    WHERE   TrademarkName = @TrademarkName

    IF @@rowcount > 0 
        SET @result = 1
    ELSE 
        SET @result = 0

    RETURN @result
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_DeliverCharge_GetByTypeId]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_DeliverCharge_GetByTypeId] ( @typeId INT )
AS 
    SELECT  [Id] ,
            [DeliverTypeId] ,
            [AreaType] ,
            [arrArea] ,
            [Charge_Min] ,
            [Weight_Min] ,
            [ChargePerUnit] ,
            [WeightPerUnit] ,
            [Charge_Max]
    FROM    [PE_DeliverCharge]
    WHERE   [DeliverTypeId] = @typeId

    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_Shop_Trademark_SetPassed]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Trademark_SetPassed]
    (
      @TrademarkID INT ,
      @Passed BIT
    )
AS 
    UPDATE  PE_Trademark
    SET     Passed = @Passed
    WHERE   TrademarkID = @TrademarkID
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_DeliverCharge_GetById]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_DeliverCharge_GetById] ( @id INT )
AS 
    SELECT  [Id] ,
            [DeliverTypeId] ,
            [AreaType] ,
            [arrArea] ,
            [Charge_Min] ,
            [Weight_Min] ,
            [ChargePerUnit] ,
            [WeightPerUnit] ,
            [Charge_Max]
    FROM    [PE_DeliverCharge]
    WHERE   [Id] = @id

    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_Shop_Trademark_SetOnTop]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Trademark_SetOnTop]
    (
      @TrademarkID INT ,
      @OnTop BIT
    )
AS 
    UPDATE  PE_Trademark
    SET     OnTop = @OnTop
    WHERE   TrademarkID = @TrademarkID
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_DeliverCharge_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_DeliverCharge_Update]
    (
      @id INT ,
      @areaType INT = 0 ,
      @arrArea NTEXT ,
      @charge_Min MONEY = 0 ,
      @weight_Min FLOAT = 0 ,
      @chargePerUnit MONEY = 0 ,
      @weightPerUnit FLOAT = 0 ,
      @charge_Max MONEY = 0
    )
AS 
    UPDATE  [PE_DeliverCharge]
    SET     [AreaType] = @areaType ,
            [arrArea] = @arrArea ,
            [Charge_Min] = @charge_Min ,
            [Weight_Min] = @weight_Min ,
            [ChargePerUnit] = @chargePerUnit ,
            [WeightPerUnit] = @weightPerUnit ,
            [Charge_Max] = @charge_Max
    WHERE   [Id] = @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'Altering [dbo].[PR_Shop_Trademark_SetElite]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Trademark_SetElite]
    (
      @TrademarkID INT ,
      @IsElite BIT
    )
AS 
    UPDATE  PE_Trademark
    SET     IsElite = @IsElite
    WHERE   TrademarkID = @TrademarkID
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_DeliverCharge_GetListByAreaType]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_DeliverCharge_GetListByAreaType]
    (
      @typeId INT ,
      @areaType INT
    )
AS 
    SELECT  [Id] ,
            [DeliverTypeId] ,
            [AreaType] ,
            [arrArea] ,
            [Charge_Min] ,
            [Weight_Min] ,
            [ChargePerUnit] ,
            [WeightPerUnit] ,
            [Charge_Max]
    FROM    [PE_DeliverCharge]
    WHERE   [DeliverTypeId] = @typeId
            AND [AreaType] = @areaType

    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_Shop_Trademark_GetById]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Trademark_GetById] ( @TrademarkID INT )
AS 
    SELECT  TrademarkID ,
            TrademarkName ,
            ProducerID ,
            IsElite ,
            TrademarkType ,
            TrademarkPhoto ,
            TrademarkIntro ,
            Passed ,
            OnTop ,
            IsElite
    FROM    PE_Trademark
    WHERE   TrademarkID = @TrademarkID
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_Trademark_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Trademark_Add]
    (
      @TrademarkName NVARCHAR(200) ,
      @TrademarkType INT ,
      @ProducerID INT ,
      @IsElite BIT ,
      @TrademarkPhoto NVARCHAR(255) ,
      @TrademarkIntro NTEXT	
	
    )
AS 
    INSERT  INTO PE_Trademark
            ( TrademarkName ,
              TrademarkType ,
              ProducerID ,
              IsElite ,
              TrademarkPhoto ,
              TrademarkIntro ,
              Passed
            )
    VALUES  ( @TrademarkName ,
              @TrademarkType ,
              @ProducerID ,
              @IsElite ,
              @TrademarkPhoto ,
              @TrademarkIntro ,
              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_Shop_Product_DeleteAll]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Product_DeleteAll]
    (
      @TableName NVARCHAR(255) ,
      @ProductID INT
    )
AS 
    BEGIN

        IF NOT EXISTS ( SELECT  *
                        FROM    PE_OrderItem
                        WHERE   TableName = @TableName
                                AND ProductID = @ProductID ) 
            BEGIN
                DELETE  PE_CommonProduct
                WHERE   TableName = @TableName
                        AND ProductID = @ProductID
                DELETE  PE_ProductData
                WHERE   TableName = @TableName
                        AND ProductID = @ProductID
                DELETE  PE_ProductPrice
                WHERE   TableName = @TableName
                        AND ProductID = @ProductID
                IF EXISTS ( SELECT  *
                            FROM    PE_Cards
                            WHERE   TableName = @TableName
                                    AND ProductID = @ProductID ) 
                    BEGIN
                        UPDATE  PE_Cards
                        SET     ProductID = 0 ,
                                TableName = ''
                        WHERE   TableName = @TableName
                                AND ProductID = @ProductID
                    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_Common_PagingLarge]'
GO
ALTER PROCEDURE [dbo].[PR_Common_PagingLarge]
    @TableNames VARCHAR(200) ,
    @PrimaryKey VARCHAR(100) ,
    @Fields VARCHAR(200) ,
    @PageSize INT ,
    @StartRow INT ,
    @Filter VARCHAR(4000) = '' ,
    @Group VARCHAR(4000) = '' ,
    @Order VARCHAR(4000) = ''
AS 
    BEGIN
        DECLARE @SortColumn VARCHAR(200)
        DECLARE @DeclareSortType VARCHAR(4000)
        DECLARE @SortColumnBegin VARCHAR(4000)
        DECLARE @SortColumnWhere VARCHAR(4000)
        DECLARE @Operator CHAR(2)
        DECLARE @SortTable VARCHAR(200)
        DECLARE @SortName VARCHAR(200)
        SET @DeclareSortType = ''
        SET @SortColumnBegin = ''
        SET @SortColumnWhere = ''
        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(200) ,
            @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(200)
                        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 
                            SET @SortColumnWhere = @SortColumnWhere + ' OR ('
                                + @PreSortColumn + '=@SortColumnBegin'
                                + @PreOrder + ' AND ' + @SortColumn
                                + @Operator + '@SortColumnBegin' + @SortName
                                + ')'
                        ELSE 
                            SET @SortColumnWhere = @SortColumnWhere + ' OR ('
                                + @PreSortColumn + '=@SortColumnBegin'
                                + @PreOrder + ' AND ' + @SortColumn
                                + @Operator + @equalOperator
                                + '@SortColumnBegin' + @SortName + ')'
                    END                    
                ELSE 
                    BEGIN
                        SET @SortColumnBegin = @SortColumnBegin
                            + '@SortColumnBegin' + @SortName + '='
                            + @SortColumn 
                        IF @pos1 > 0 
                            SET @SortColumnWhere = @SortColumnWhere
                                + @SortColumn + @Operator + '@SortColumnBegin'
                                + @SortName
                        ELSE 
                            SET @SortColumnWhere = @SortColumnWhere
                                + @SortColumn + @Operator + @equalOperator
                                + '@SortColumnBegin' + @SortName
                    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'Altering [dbo].[PR_Shop_OrderItem_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_OrderItem_Update]
    (
      @ItemID INT ,    --ID
      @OrderID INT ,    --订单ID
      @ProductID INT ,    --产品ID
      @TableName NVARCHAR(255) ,
      @Property NVARCHAR(255) ,
      @SaleType INT ,    --销售类型  1--正常销售  2--换购  3--赠送  4--批发
      @Price_Market MONEY ,    --原价
      @Price MONEY ,    --系统计算出的销售价
      @TruePrice MONEY ,    --实际销售价
      @Amount INT ,    --订购数量
      @SubTotal MONEY ,    --订购金额
      @BeginDate DATETIME ,    --开始计算服务期限日期
      @ServiceTerm INT ,    --服务期限
      @ServiceTermUnit INT ,    --服务年限单位　0－年，1－月，2－日
      @Remark NVARCHAR(255) ,    --备注
      @PresentMoney MONEY ,    --返还的现金券
      @PresentPoint INT ,    --赠送点券
      @PresentExp INT ,   --赠送的积分
      @ProductCharacter INT ,
      @ProductName NVARCHAR(255) ,
      @Unit NVARCHAR(20) ,
      @Weight FLOAT ,
      @TrueAmount INT ,
      @TrueSubTotal MONEY
    )
AS 
    UPDATE  [PE_OrderItem]
    SET     [ItemID] = @ItemID ,    --ID
            [OrderID] = @OrderID ,    --订单ID
            [ProductID] = @ProductID ,    --产品ID
            [TableName] = @TableName ,
            [Property] = @Property ,
            [SaleType] = @SaleType ,    --销售类型  1--正常销售  2--换购  3--赠送  4--批发
            [Price_Market] = @Price_Market ,    --原价
            [Price] = @Price ,    --系统计算出的销售价
            [TruePrice] = @TruePrice ,    --实际销售价
            [Amount] = @Amount ,    --订购数量
            [SubTotal] = @SubTotal ,    --订购金额
            [BeginDate] = @BeginDate ,    --开始计算服务期限日期
            [ServiceTerm] = @ServiceTerm ,    --服务期限
            [ServiceTermUnit] = @ServiceTermUnit ,    --服务年限单位　0－年，1－月，2－日
            [Remark] = @Remark ,    --备注
            [PresentMoney] = @PresentMoney ,    --返还的现金券
            [PresentPoint] = @PresentPoint ,    --赠送点券
            [PresentExp] = @PresentExp ,    --赠送的积分
            [ProductCharacter] = @ProductCharacter ,
            [ProductName] = @ProductName ,
            [Unit] = @Unit ,
            [Weight] = @Weight ,
            [TrueAmount] = @TrueAmount ,
            [TrueSubTotal] = @TrueSubTotal
    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_Shop_DeliverItem_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_DeliverItem_Add]
    (
      @DeliverID INT ,
      @OrderID INT ,
      @DeliverDate DATETIME ,
      @DeliverDirection INT ,
      @HandlerName NVARCHAR(20) ,
      @CourierId INT ,
      @ExpressNumber NVARCHAR(50) ,
      @Inputer NVARCHAR(20) ,
      @Remark NVARCHAR(255) ,
      @Memo NTEXT
	
    )
AS 
    INSERT  INTO PE_DeliverItem
            ( DeliverID ,
              OrderID ,
              DeliverDate ,
              DeliverDirection ,
              HandlerName ,
              CourierId ,
              ExpressNumber ,
              Inputer ,
              Remark ,
              Memo
            )
    VALUES  ( @DeliverID ,
              @OrderID ,
              @DeliverDate ,
              @DeliverDirection ,
              @HandlerName ,
              @CourierId ,
              @ExpressNumber ,
              @Inputer ,
              @Remark ,
              @Memo
            )
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_OrderItem_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_OrderItem_Add]
    (
      @ItemID INT ,
      @OrderID INT ,
      @ProductID INT ,
      @TableName NVARCHAR(255) ,
      @Property NVARCHAR(255) ,
      @SaleType INT ,
      @Price_Market MONEY ,
      @Price MONEY ,
      @TruePrice MONEY ,
      @Amount INT ,
      @SubTotal MONEY ,
      @BeginDate DATETIME ,
      @ServiceTerm INT ,
      @ServiceTermUnit INT ,
      @Remark NVARCHAR(255) ,
      @PresentMoney MONEY ,
      @PresentPoint INT ,
      @PresentExp INT ,
      @ProductCharacter INT ,
      @ProductName NVARCHAR(255) ,
      @Unit NVARCHAR(20) ,
      @Weight FLOAT ,
      @TrueAmount INT ,
      @TrueSubTotal MONEY
	
    )
AS 
    INSERT  INTO PE_OrderItem
            ( [ItemID] ,
              [OrderID] ,
              [ProductID] ,
              [TableName] ,
              [Property] ,
              [SaleType] ,
              [Price_Market] ,
              [Price] ,
              [TruePrice] ,
              [Amount] ,
              [SubTotal] ,
              [BeginDate] ,
              [ServiceTerm] ,
              [ServiceTermUnit] ,
              [Remark] ,
              [PresentMoney] ,
              [PresentPoint] ,
              [PresentExp] ,
              [ProductCharacter] ,
              [ProductName] ,
              [Unit] ,
              [Weight] ,
              [TrueAmount] ,
              [TrueSubTotal]
            )
    VALUES  ( @ItemID ,
              @OrderID ,
              @ProductID ,
              @TableName ,
              @Property ,
              @SaleType ,
              @Price_Market ,
              @Price ,
              @TruePrice ,
              @Amount ,
              @SubTotal ,
              @BeginDate ,
              @ServiceTerm ,
              @ServiceTermUnit ,
              @Remark ,
              @PresentMoney ,
              @PresentPoint ,
              @PresentExp ,
              @ProductCharacter ,
              @ProductName ,
              @Unit ,
              @Weight ,
              @TrueAmount ,
              @TrueSubTotal
            )
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_OrderItem_UpdateByProductId]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_OrderItem_UpdateByProductId]
    (
      @ProductId INT ,
      @ProductName NVARCHAR(255)
    )
AS 
    UPDATE  PE_ORDERITEM
    SET     productname = @productname
    WHERE   productid = @productid
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_Service_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_Service_Update]
    (
      @ItemId INT ,    --明细ID
      @ClientId INT ,    --客户ID
      @ContacterId INT ,    --联系人ID
      @OrderId INT ,    --订单ID
      @ServiceTime DATETIME ,    --服务时间
      @ServiceType NVARCHAR(50) ,    --服务类型
      @ServiceMode NVARCHAR(50) ,    --服务方式
      @ServiceTitle NVARCHAR(50) ,    --服务主题
      @ServiceContent NTEXT ,    --服务内容
      @ServiceResult INT ,    --服务结果，0为未完成，1为完成
      @TakeTime INT ,    --花费时间
      @Processor NVARCHAR(50) ,    --执行人
      @Inputer NVARCHAR(50) ,    --录入者
      @Feedback NTEXT ,    --客户反馈
      @ConfirmTime DATETIME ,    --回访时间
      @ConfirmCaller NVARCHAR(50) ,    --回访人
      @ConfirmScore INT ,    --客户评价
      @ConfirmFeedback NTEXT ,    --客户反馈
      @Remark NTEXT ,    --备注
      @ServicePoint INT --服务积分
    )
AS 
    UPDATE  [PE_ServiceItem]
    SET     
            [ClientId] = @ClientId ,    --客户ID
            [ContacterId] = @ContacterId ,    --联系人ID
            [OrderId] = @OrderId ,    --订单ID
            [ServiceTime] = @ServiceTime ,    --服务时间
            [ServiceType] = @ServiceType ,    --服务类型
            [ServiceMode] = @ServiceMode ,    --服务方式
            [ServiceTitle] = @ServiceTitle ,    --服务主题
            [ServiceContent] = @ServiceContent ,    --服务内容
            [ServiceResult] = @ServiceResult ,    --服务结果，0为未完成，1为完成
            [TakeTime] = @TakeTime ,    --花费时间
            [Processor] = @Processor ,    --执行人
            [Inputer] = @Inputer ,    --录入者
            [Feedback] = @Feedback ,    --客户反馈
            [ConfirmTime] = @ConfirmTime ,    --回访时间
            [ConfirmCaller] = @ConfirmCaller ,    --回访人
            [ConfirmScore] = @ConfirmScore ,    --客户评价
            [ConfirmFeedback] = @ConfirmFeedback ,    --客户反馈
            [Remark] = @Remark ,    --备注
            [ServicePoint] = @ServicePoint
    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_AD_Advertisement_Passed]'
GO
ALTER PROCEDURE [dbo].[PR_AD_Advertisement_Passed] ( @strAdId VARCHAR(8000) )
AS
    DECLARE @passed BIT
    DECLARE @adId INT
    DECLARE @split VARCHAR(1)
	
    SET @split = ','
    SET @strAdId = @strAdId + ','
	
    WHILE CHARINDEX(@split, @strAdId) > 0 
        BEGIN
            SET @adId = CONVERT(INT, LEFT(@strAdId,
                                          CHARINDEX(@split, @strAdId) - 1))
            SET @strAdId = STUFF(@strAdId, 1, CHARINDEX(@split, @strAdId), '')
		
            SELECT  @passed = Passed
            FROM    PE_Advertisement
            WHERE   AdID = @adId
            IF @passed = 0 
                SET @passed = 1
            UPDATE  PE_Advertisement
            SET     passed = @passed
            WHERE   ADId = @adId
        END
    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_Crm_Service_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_Service_Add]
    (
      @ClientId INT ,    --客户ID
      @ContacterId INT ,    --联系人ID
      @OrderId INT ,    --订单ID
      @ServiceTime DATETIME ,    --服务时间
      @ServiceType NVARCHAR(50) ,    --服务类型
      @ServiceMode NVARCHAR(50) ,    --服务方式
      @ServiceTitle NVARCHAR(50) ,    --服务主题
      @ServiceContent NTEXT ,    --服务内容
      @ServiceResult INT ,    --服务结果，0为未完成，1为完成
      @TakeTime INT ,    --花费时间
      @Processor NVARCHAR(50) ,    --执行人
      @Inputer NVARCHAR(50) ,    --录入者
      @Feedback NTEXT ,    --客户反馈
      @ConfirmTime DATETIME ,    --回访时间
      @ConfirmCaller NVARCHAR(50) ,    --回访人
      @ConfirmScore INT ,    --客户评价
      @ConfirmFeedback NTEXT ,    --客户反馈
      @Remark NTEXT ,    --备注
      @ServicePoint INT --服务积分
    )
AS 
    INSERT  INTO [PE_ServiceItem]
            ( 
              [ClientId] ,    --客户ID
              [ContacterId] ,    --联系人ID
              [OrderId] ,    --订单ID
              [ServiceTime] ,    --服务时间
              [ServiceType] ,    --服务类型
              [ServiceMode] ,    --服务方式
              [ServiceTitle] ,    --服务主题
              [ServiceContent] ,    --服务内容
              [ServiceResult] ,    --服务结果，0为未完成，1为完成
              [TakeTime] ,    --花费时间
              [Processor] ,    --执行人
              [Inputer] ,    --录入者
              [Feedback] ,    --客户反馈
              [ConfirmTime] ,    --回访时间
              [ConfirmCaller] ,    --回访人
              [ConfirmScore] ,    --客户评价
              [ConfirmFeedback] ,    --客户反馈
              [Remark] ,    --备注
              [ServicePoint] --服务积分
		)
    VALUES  ( --@ItemId,
              @ClientId ,
              @ContacterId ,
              @OrderId ,
              @ServiceTime ,
              @ServiceType ,
              @ServiceMode ,
              @ServiceTitle ,
              @ServiceContent ,
              @ServiceResult ,
              @TakeTime ,
              @Processor ,
              @Inputer ,
              @Feedback ,
              @ConfirmTime ,
              @ConfirmCaller ,
              @ConfirmScore ,
              @ConfirmFeedback ,
              @Remark ,
              @ServicePoint
	      )
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_AD_Advertisement_CancelPassed]'
GO
ALTER PROCEDURE [dbo].[PR_AD_Advertisement_CancelPassed]
    (
      @strAdId NVARCHAR(4000)
    )
AS 
    SET NOCOUNT OFF
    DECLARE @passed BIT
    DECLARE @adId INT
    DECLARE @split VARCHAR(1)
	
    SET @split = ','
    SET @strAdId = @strAdId + ','
    WHILE CHARINDEX(@split, @strAdId) > 0 
        BEGIN
            SET @adId = CONVERT(INT, LEFT(@strAdId,
                                          CHARINDEX(@split, @strAdId) - 1))
            SET @strAdId = STUFF(@strAdId, 1, CHARINDEX(@split, @strAdId), '')
		
            SELECT  @passed = Passed
            FROM    PE_Advertisement
            WHERE   AdID = @adId
            IF @passed = 1 
                SET @passed = 0
            UPDATE  PE_Advertisement
            SET     passed = @passed
            WHERE   ADId = @adId
        END
    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_Shop_DeliverType_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_DeliverType_Add]
    (
      @typeId INT ,
      @typeName NVARCHAR(50) ,
      @intro NVARCHAR(50) ,
      @chargeType INT ,
      @isDefault BIT = 0 ,
      @isDisabled BIT = 0 ,
      @releaseType INT = 0 ,
      @minMoney1 MONEY = 0 ,
      @releaseCharge MONEY = 0 ,
      @minmoney2 MONEY = 0 ,
      @maxCharge MONEY = 0 ,
      @minMoney3 MONEY = 0 ,
      @charge_Min MONEY = 0 ,
      @charge_Max MONEY = 0 ,
      @includeTax INT = 0 ,
      @taxRate FLOAT = 0 ,
      @charge_Percent SMALLINT = 0
     
    )
AS 
    DECLARE @orderId INT
    SELECT  @orderId = ( ISNULL(MAX(OrderId), 0) + 1 )
    FROM    PE_DeliverType
	
    IF ( @typeId = 1 ) 
        BEGIN
			
            SET @isDefault = 1
        END
    ELSE 
        BEGIN
            IF ( @isDefault = 1 ) 
                BEGIN
                    UPDATE  PE_DeliverType
                    SET     IsDefault = 0
                    WHERE   IsDefault = 1
                END
        END

    INSERT  INTO [PE_DeliverType]
            ( [TypeId] ,
              [TypeName] ,
              [Intro] ,
              [ChargeType] ,
              [IsDefault] ,
              [IsDisabled] ,
              [OrderId] ,
              [ReleaseType] ,
              [MinMoney1] ,
              [ReleaseCharge] ,
              [Minmoney2] ,
              [MaxCharge] ,
              [MinMoney3] ,
              [Charge_Min] ,
              [Charge_Max] ,
              [IncludeTax] ,
              [TaxRate] ,
              [Charge_Percent]
		   
            )
    VALUES  ( @typeId ,
              @typeName ,
              @intro ,
              @chargeType ,
              @isDefault ,
              @isDisabled ,
              @orderId ,
              @releaseType ,
              @minMoney1 ,
              @releaseCharge ,
              @minmoney2 ,
              @maxCharge ,
              @minMoney3 ,
              @charge_Min ,
              @charge_Max ,
              @includeTax ,
              @taxRate ,
              @charge_Percent
            )
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_AD_Advertisement_Add]'
GO
ALTER PROCEDURE [dbo].[PR_AD_Advertisement_Add]
    (
      @ADID INT ,
      @strZoneId NVARCHAR(4000) ,
      @UserID INT ,
      @ADType INT ,
      @ADName NVARCHAR(100) ,
      @ImgUrl NVARCHAR(255) ,
      @ImgWidth INT ,
      @ImgHeight INT ,
      @FlashWmode INT ,
      @ADIntro NTEXT ,
      @LinkUrl NVARCHAR(255) ,
      @LinkTarget INT ,
      @LinkAlt NVARCHAR(255) ,
      @Priority INT ,
      @Setting NTEXT ,
      @CountView BIT ,
      @Views INT ,
      @CountClick BIT ,
      @Clicks INT ,
      @IsAbsolutePath BIT ,
      @Passed BIT ,
      @OverdueDate DATETIME 
    )
AS 
    SET NOCOUNT OFF
 
    DECLARE @active BIT
    DECLARE @zoneId INT
    DECLARE @split VARCHAR(1)
	
    SET @split = ','
    IF @strZoneId != '' 
        BEGIN
            SET @strZoneId = @strZoneId + ','
            WHILE CHARINDEX(@split, @strZoneId) > 0 
                BEGIN
                    SET @zoneId = CONVERT(INT, LEFT(@strZoneId,
                                                    CHARINDEX(@split,
                                                              @strZoneId) - 1))
                    SET @strZoneId = STUFF(@strZoneId, 1,
                                           CHARINDEX(@split, @strZoneId), '')
                    INSERT  INTO PE_Zone_Advertisement
                            ( ADID, ZoneID )
                    VALUES  ( @ADID, @ZoneId )
                END
        END
    BEGIN 
        INSERT  INTO [PE_Advertisement]
                ( [ADID] ,
                  [UserID] ,
                  [ADType] ,
                  [ADName] ,
                  [ImgUrl] ,
                  [ImgWidth] ,
                  [ImgHeight] ,
                  [FlashWmode] ,
                  [ADIntro] ,
                  [LinkUrl] ,
                  [LinkTarget] ,
                  [LinkAlt] ,
                  [Priority] ,
                  [Setting] ,
                  [CountView] ,
                  [Views] ,
                  [CountClick] ,
                  [Clicks] ,
                  [IsAbsolutePath] ,
                  [Passed] ,
                  [OverdueDate]
                )
        VALUES  ( @ADID ,
                  @UserID ,
                  @ADType ,
                  @ADName ,
                  @ImgUrl ,
                  @ImgWidth ,
                  @ImgHeight ,
                  @FlashWmode ,
                  @ADIntro ,
                  @LinkUrl ,
                  @LinkTarget ,
                  @LinkAlt ,
                  @Priority ,
                  @Setting ,
                  @CountView ,
                  @Views ,
                  @CountClick ,
                  @Clicks ,
                  @IsAbsolutePath ,
                  @Passed ,
                  @OverdueDate
                )
    END
    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_Shop_DeliverType_GetById]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_DeliverType_GetById] ( @typeId INT )
AS 
    SELECT  *
    FROM    [PE_DeliverType]
    WHERE   TypeId = @typeId
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_AD_Advertisement_Update]'
GO
ALTER PROCEDURE [dbo].[PR_AD_Advertisement_Update]
    (
      @ADId INT ,
      @strZoneId NVARCHAR(4000) ,
      @UserId INT ,
      @ADType INT ,
      @ADName NVARCHAR(100) ,
      @ImgUrl NVARCHAR(255) ,
      @ImgWidth INT ,
      @ImgHeight INT ,
      @FlashWmode INT ,
      @ADIntro NTEXT ,
      @LinkUrl NVARCHAR(255) ,
      @LinkTarget INT ,
      @LinkAlt NVARCHAR(255) ,
      @Priority INT ,
      @Setting NTEXT ,
      @CountView BIT ,
      @Views INT ,
      @CountClick BIT ,
      @Clicks INT ,
      @IsAbsolutePath BIT ,
      @Passed BIT ,
      @OverdueDate DATETIME 
    )
AS 
    SET NOCOUNT OFF

    DECLARE @active BIT
    DECLARE @zoneId INT
    DECLARE @split VARCHAR(1)
	
    SET @split = ','
    IF @strZoneId != '' 
        BEGIN
            SET @strZoneId = @strZoneId + ','
            DELETE  FROM PE_Zone_Advertisement
            WHERE   ADID = @ADID
            WHILE CHARINDEX(@split, @strZoneId) > 0 
                BEGIN
                    SET @zoneId = CONVERT(INT, LEFT(@strZoneId,
                                                    CHARINDEX(@split,
                                                              @strZoneId) - 1))
                    SET @strZoneId = STUFF(@strZoneId, 1,
                                           CHARINDEX(@split, @strZoneId), '')
                    INSERT  INTO PE_Zone_Advertisement
                            ( ADID, ZoneID )
                    VALUES  ( @ADID, @ZoneId )
                END
        END
    BEGIN


        UPDATE  [PE_Advertisement]
        SET     [UserId] = @UserId ,
                [ADType] = @ADType ,
                [ADName] = @ADName ,
                [ImgUrl] = @ImgUrl ,
                [ImgWidth] = @ImgWidth ,
                [ImgHeight] = @ImgHeight ,
                [FlashWmode] = @FlashWmode ,
                [ADIntro] = @ADIntro ,
                [LinkUrl] = @LinkUrl ,
                [LinkTarget] = @LinkTarget ,
                [LinkAlt] = @LinkAlt ,
                [Priority] = @Priority ,
                [Setting] = @Setting ,
                [CountView] = @CountView ,
                [Views] = @Views ,
                [CountClick] = @CountClick ,
                [Clicks] = @Clicks ,
                [IsAbsolutePath] = @IsAbsolutePath ,
                [Passed] = @Passed ,
                [OverdueDate] = @OverdueDate
        WHERE   [ADId] = @ADId
    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_DeliverType_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_DeliverType_Update]
    (
      @typeId INT ,
      @typeName NVARCHAR(50) ,
      @intro NVARCHAR(50) ,
      @chargeType INT ,
      @isDefault BIT ,
      @isDisabled BIT ,
      @orderId INT ,
      @releaseType INT = 0 ,
      @minMoney1 MONEY = 0 ,
      @releaseCharge MONEY = 0 ,
      @minmoney2 MONEY = 0 ,
      @maxCharge MONEY = 0 ,
      @minMoney3 MONEY = 0 ,
      @charge_Min MONEY = 0 ,
      @charge_Max MONEY = 0 ,
      @includeTax INT = 0 ,
      @taxRate FLOAT = 0 ,
      @charge_Percent SMALLINT = 0
	
    )
AS --处理 默认值
    IF ( @isDefault = 1 ) 
        BEGIN
            UPDATE  [PE_DeliverType]
            SET     IsDefault = 0
            WHERE   IsDefault = 1
        END
	
	--处理 排序
    DECLARE @oldOrderId INT
    DECLARE @max INT
    DECLARE @min INT
    DECLARE @count INT
    DECLARE @icount INT
    DECLARE @offset1 INT
    DECLARE @offset2 INT

    SET @icount = 1
    DECLARE @tmp TABLE
        (
          Row INT IDENTITY(1, 1)
                  PRIMARY KEY ,
          id INT ,
          oid INT
        )
    SELECT  @oldOrderId = OrderId
    FROM    [PE_DeliverType]
    WHERE   TypeID = @typeId
    IF ( @orderId <> @oldOrderId ) 
        BEGIN
            IF ( @orderId > @oldOrderId ) 
                BEGIN
                    SET @max = @orderId
                    SET @min = @oldOrderId
                    SET @offset1 = 0
                    SET @offset2 = 1
                END
            ELSE 
                BEGIN
                    SET @max = @oldOrderId
                    SET @min = @orderId
                    SET @offset1 = 1
                    SET @offset2 = 0
                END
            INSERT  INTO @tmp
                    ( id ,
                      oid
                    )
                    SELECT  TypeId ,
                            OrderId
                    FROM    [PE_DeliverType]
                    WHERE   OrderId BETWEEN @min AND @max
                    ORDER BY orderId
            SELECT  @count = COUNT(*)
            FROM    @tmp

            WHILE ( @icount < @count ) 
                BEGIN
                    UPDATE  [PE_DeliverType]
                    SET     OrderId = ( SELECT  oid
                                        FROM    @tmp
                                        WHERE   Row = @icount + @offset1
                                      )
                    WHERE   TypeId = ( SELECT   Id
                                       FROM     @tmp
                                       WHERE    Row = @icount + @offset2
                                     )
                    SET @icount = @icount + 1
                END 
        END
	
    UPDATE  [PE_DeliverType]
    SET     [TypeName] = @typeName ,
            [Intro] = @intro ,
            [ChargeType] = @chargeType ,
            [IsDefault] = @isDefault ,
            [IsDisabled] = @isDisabled ,
            [OrderId] = @orderId ,
            [ReleaseType] = @releaseType ,
            [MinMoney1] = @minMoney1 ,
            [ReleaseCharge] = @releaseCharge ,
            [Minmoney2] = @minmoney2 ,
            [MaxCharge] = @maxCharge ,
            [MinMoney3] = @minMoney3 ,
            [Charge_Min] = @charge_Min ,
            [Charge_Max] = @charge_Max ,
            [IncludeTax] = @includeTax ,
            [TaxRate] = @taxRate ,
            [Charge_Percent] = @charge_Percent
    WHERE   TypeId = @typeId
    IF NOT ( @@error > 0 ) 
        BEGIN
            RETURN 1
        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_GetByOrderId]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Order_GetByOrderId] ( @OrderId INT )
AS 
    BEGIN
        SELECT  O.* ,
                C.ClientName
        FROM    PE_Orders O
                LEFT JOIN PE_Client C ON O.ClientID = C.ClientID
        WHERE   OrderID = @OrderId 

    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_ShoppingCarts_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_ShoppingCarts_Add]
    (
      @UserName NVARCHAR(50) ,
      @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_Shop_Order_AgentPayment]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Order_AgentPayment]
    (
      @OrderID INT ,
      @Money MONEY ,
      @RefundType INT
    )
AS 
    DECLARE @AgentBalance MONEY ,
        @AgentClientID INT ,
        @UserName NVARCHAR(4000)
    SELECT  AgentName ,
            MoneyReceipt ,
            MoneyTotal
    FROM    PE_Orders
    WHERE   OrderID = @OrderID
            AND MoneyReceipt < MoneyTotal
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(50) ,
      @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_Order_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Order_Add]
    (
      @OrderID INT ,
      @OrderNum NVARCHAR(20) ,
      @UserName NVARCHAR(50) ,
      @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_GetByOrderId]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Invoice_GetByOrderId] ( @OrderId INT )
AS 
    SELECT  O.* ,
            C.ShortedForm AS ClientName
    FROM    PE_Orders O
            LEFT JOIN PE_Client C ON O.ClientID = C.ClientID
    WHERE   OrderID = @OrderId
            AND NeedInvoice = 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_Accessories_WordReplace_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_WordReplace_Add]
    (
      @ItemID INT ,
      @SourceWord NVARCHAR(250) ,
      @TargetWord NVARCHAR(250) ,
      @ReplaceType INT ,
      @ScopesType INT ,
      @Priority INT ,
      @ReplaceTimes INT ,
      @OpenType BIT ,
      @IsEnabled BIT ,
      @Title NVARCHAR(255)
    )
AS 
    INSERT  INTO [PE_WordReplaceItem]
            ( [SourceWord] ,
              [TargetWord] ,
              [ReplaceType] ,
              [ScopesType] ,
              [Priority] ,
              [ReplaceTimes] ,
              [OpenType] ,
              [IsEnabled] ,
              [Title]    
		)
    VALUES  ( @SourceWord ,
              @TargetWord ,
              @ReplaceType ,
              @ScopesType ,
              @Priority ,
              @ReplaceTimes ,
              @OpenType ,
              @IsEnabled ,
              @Title
	      )
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_WordReplace_GetById]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_WordReplace_GetById] ( @ItemID INT )
AS 
    SELECT  *
    FROM    PE_WordReplaceItem
    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_Shop_Invoice_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Invoice_Add]
    (
      @ClientId INT ,
      @UserName NVARCHAR(50) ,
      @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].[PR_Accessories_WordReplace_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_WordReplace_Update]
    (
      @ItemID INT ,
      @SourceWord NVARCHAR(250) ,
      @TargetWord NVARCHAR(250) ,
      @ReplaceType INT ,
      @ScopesType INT ,
      @Priority INT ,
      @ReplaceTimes INT ,
      @OpenType BIT ,
      @IsEnabled BIT ,
      @Title NVARCHAR(255)
    )
AS 
    UPDATE  PE_WordReplaceItem
    SET     SourceWord = @SourceWord ,
            TargetWord = @TargetWord ,
            ReplaceType = @ReplaceType ,
            ScopesType = @ScopesType ,
            Priority = @Priority ,
            ReplaceTimes = @ReplaceTimes ,
            OpenType = @OpenType ,
            IsEnabled = @IsEnabled ,
            Title = @Title
    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_Shop_DeliverItem_SignIn]'
GO
ALTER PROC [dbo].[PR_Shop_DeliverItem_SignIn]
    (
      @DeliverID INT ,
      @OrderID INT
    )
AS 
    DECLARE @noReceivedCount INT
    UPDATE  pe_deliverItem
    SET     Received = 1
    WHERE   DeliverId = @DeliverID
    SELECT  @noReceivedCount = COUNT(OrderID)
    FROM    Pe_deliveritem
    WHERE   orderid = @OrderID
            AND Received = 0
    IF ( @noReceivedCount = 0 ) 
        BEGIN
            UPDATE  Pe_orders
            SET     DeliverStatus = 5
            WHERE   orderid = @OrderID
                    AND DeliverStatus = 4
        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_Accessories_WordReplace_Exists]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_WordReplace_Exists]
    (
      @SourceWord NVARCHAR(250)
    )
AS 
    SELECT  COUNT(*)
    FROM    PE_WordReplaceItem
    WHERE   SourceWord = @SourceWord
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_DeliverItem_GetByOrderId]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_DeliverItem_GetByOrderId] ( @OrderId INT )
AS 
    SELECT  O.OrderNum ,
            O.ContacterName ,
            O.Email ,
            O.UserName ,
            C.ClientName ,
            O.Status ,
            O.MoneyTotal ,
            O.MoneyReceipt ,
            O.DeliverType ,
            O.Memo ,
            D.TypeName AS DeliverTypeName
    FROM    PE_Orders O
            LEFT JOIN PE_DeliverType D ON O.DeliverType = D.TypeID
            LEFT JOIN PE_Client C ON O.ClientID = C.ClientID
    WHERE   O.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_Crm_Client_Merge]'
GO
ALTER PROC [dbo].[PR_Crm_Client_Merge]
    (
      @PrincipalClientID INT ,
      @SubordinateClientID INT
    )
AS 
    DECLARE @clienttype1 INT
    DECLARE @clienttype2 INT
    DECLARE @LastContactedTime1 DATETIME
    DECLARE @LastContactedTime2 DATETIME
    SELECT  @clienttype1 = clienttype ,
            @LastContactedTime1 = LastContactedTime
    FROM    pe_client
    WHERE   clientid = @PrincipalClientID
    SELECT  @clienttype2 = clienttype ,
            @LastContactedTime2 = LastContactedTime
    FROM    pe_client
    WHERE   clientid = @SubordinateClientID
    IF @clienttype1 = 0--主客户为企业客户
        BEGIN
            DECLARE ContacterCursor CURSOR 
            FOR
            SELECT contacterid,truename FROM Pe_contacter WHERE clientid=@SubordinateClientID
            DECLARE @contacterid INT
            DECLARE @truename VARCHAR(20)
            OPEN ContacterCursor
            FETCH NEXT FROM ContacterCursor INTO @contacterid,@truename 
            WHILE ( @@fetch_status = 0 ) 
                BEGIN--处理联系信息
                    DECLARE @PrincipalClientcontacterid INT
                    SELECT  @PrincipalClientcontacterid = contacterid
                    FROM    pe_contacter
                    WHERE   truename = @truename
                            AND clientid = @PrincipalClientID
                    IF ( @PrincipalClientcontacterid > 0 ) --判断是否有同名的
                        BEGIN
			--和主客户的联系人的资料同名的联系人id 删除 并更改相应的联系记录内容
                            UPDATE  pe_clientannal
                            SET     clientid = @PrincipalClientID ,
                                    contacter = @PrincipalClientcontacterid
                            WHERE   clientid = @SubordinateClientID
                                    AND contacter = @contacterid
                            UPDATE  pe_reminditem
                            SET     CorrelativeClient = @PrincipalClientID ,
                                    CorrelativeContacter = @PrincipalClientcontacterid
                            WHERE   CorrelativeClient = @SubordinateClientID
                                    AND CorrelativeContacter = @contacterid
                            UPDATE  pe_ComplainItem
                            SET     clientid = @PrincipalClientID ,
                                    contacterid = @PrincipalClientcontacterid
                            WHERE   clientid = @SubordinateClientID
                                    AND contacterid = @contacterid
                            UPDATE  pe_serviceItem
                            SET     clientid = @PrincipalClientID ,
                                    contacterid = @PrincipalClientcontacterid
                            WHERE   clientid = @SubordinateClientID
                                    AND contacterid = @contacterid
                            DELETE  pe_contacter
                            WHERE   contacterid = @contacterid 
                        END
                    ELSE--没有和主客户同名的联系人则只需更改记录的客户ID
                        BEGIN
                            UPDATE  pe_clientannal
                            SET     clientid = @PrincipalClientID
                            WHERE   clientid = @SubordinateClientID
                            UPDATE  pe_reminditem
                            SET     CorrelativeClient = @PrincipalClientID
                            WHERE   CorrelativeClient = @SubordinateClientID 
                            UPDATE  pe_serviceItem
                            SET     clientid = @PrincipalClientID
                            WHERE   clientid = @SubordinateClientID 
                            UPDATE  pe_ComplainItem
                            SET     clientid = @PrincipalClientID
                            WHERE   clientid = @SubordinateClientID
                            UPDATE  pe_contacter
                            SET     clientid = @PrincipalClientID
                            WHERE   contacterid = @contacterid
                        END
                    FETCH NEXT FROM ContacterCursor INTO @contacterid ,@truename
                END
            CLOSE ContacterCursor
            DEALLOCATE ContacterCursor
        END
    IF @clienttype2 = 1--从客户为个人客户
        BEGIN
            UPDATE  pe_contacter
            SET     usertype = 2
            WHERE   clientid = @PrincipalClientID
                    AND usertype = 0
        END
    IF ( @clienttype2 = 1
         AND @clienttype1 = 1
       )--主从客户都是个人客户
        BEGIN
            SET nocount ON
            DECLARE @contacterid_z INT 
            DECLARE @contacterid_c INT
            SELECT  @contacterid_z = contacterid
            FROM    pe_contacter
            WHERE   clientid = @PrincipalClientID
            SELECT  @contacterid_c = contacterid
            FROM    pe_contacter
            WHERE   clientid = @SubordinateClientID
            UPDATE  pe_clientannal
            SET     clientid = @PrincipalClientID ,
                    contacter = @contacterid_z
            WHERE   clientid = @SubordinateClientID
                    AND contacter = @contacterid_c
            UPDATE  pe_reminditem
            SET     CorrelativeClient = @PrincipalClientID ,
                    CorrelativeContacter = @contacterid_z
            WHERE   CorrelativeClient = @SubordinateClientID
                    AND CorrelativeContacter = @contacterid_c
            UPDATE  pe_ComplainItem
            SET     clientid = @PrincipalClientID ,
                    contacterid = @contacterid_z
            WHERE   clientid = @SubordinateClientID
                    AND contacterid = @contacterid_c
            UPDATE  pe_serviceItem
            SET     clientid = @PrincipalClientID ,
                    contacterid = @contacterid_z
            WHERE   clientid = @SubordinateClientID
                    AND contacterid = @contacterid_c
            DELETE  pe_contacter
            WHERE   contacterid = @contacterid_c 
        END

    UPDATE  PE_Orders
    SET     ClientID = @PrincipalClientID
    WHERE   ClientID = @SubordinateClientID--从客户的订单改为主客户的
    UPDATE  PE_BankrollItem
    SET     ClientID = @PrincipalClientID
    WHERE   ClientID = @SubordinateClientID--资金明细记录
    UPDATE  PE_Client
    SET     Balance = ( SELECT  SUM(Balance)
                        FROM    pe_client
                        WHERE   clientid IN ( @PrincipalClientID,
                                              @SubordinateClientID )
                      )
    WHERE   ClientID = @PrincipalClientID--资金余额相加
    DELETE  PE_Client
    WHERE   ClientID = @SubordinateClientID--删除从客户

    IF @LastContactedTime2 > @LastContactedTime1 
        BEGIN
            UPDATE  PE_Client
            SET     LastContactedTime = @LastContactedTime2
            WHERE   ClientID = @PrincipalClientID
        END

    DECLARE @SubUserID INT
    SELECT  @SubUserID = UserID
    FROM    PE_Users
    WHERE   ClientID = @SubordinateClientID

    IF ( @SubUserID IS NOT NULL ) 
        BEGIN
            UPDATE  PE_Users
            SET     ClientID = @PrincipalClientID
            WHERE   UserID = @SubUserID
        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_Refund_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Refund_Add]
    (
      @OrderID INT ,
      @Money MONEY ,
      @HandlingCharge MONEY ,
      @RefundType INT
	
    )
AS 
    DECLARE @ClientID INT ,
        @UserName NVARCHAR(50)
    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_Remittance_GetByOrderId]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Remittance_GetByOrderId] ( @OrderId INT )
AS 
    SELECT  O.ClientID ,
            O.OrderID ,
            O.OrderNum ,
            O.UserName ,
            O.Email ,
            C.ShortedForm AS ClientName ,
            O.MoneyTotal ,
            O.MoneyReceipt
    FROM    PE_Orders O
            LEFT JOIN PE_Client C ON O.ClientID = C.ClientID
    WHERE   O.OrderID = @OrderId
            AND MoneyTotal > MoneyReceipt
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_Remittance_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Remittance_Add]
    (
      @OrderID INT ,
      @Money MONEY
	
    )
AS 
    UPDATE  PE_Orders
    SET     Status = 1 ,
            MoneyReceipt = MoneyReceipt + @Money
    WHERE   OrderID = @OrderID
            AND MoneyReceipt < MoneyTotal
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_GetByOrderId]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Refund_GetByOrderId] ( @OrderId INT )
AS 
    SELECT  O.OrderNum ,
            O.Email ,
            O.UserName ,
            C.ClientID ,
            C.ClientName ,
            O.MoneyTotal ,
            O.MoneyReceipt
    FROM    PE_Orders O
            LEFT JOIN PE_Client C ON O.ClientID = C.ClientID
    WHERE   O.OrderID = @OrderId
            AND MoneyReceipt > 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_AD_ADZone_Active]'
GO
ALTER PROCEDURE [dbo].[PR_AD_ADZone_Active]
    (
      @strZoneId VARCHAR(8000)
    )
AS 
    DECLARE @active BIT
    DECLARE @zoneId INT
    DECLARE @split VARCHAR(1)
	
    SET @split = ','
    SET @strZoneId = @strZoneId + ','
	
    WHILE CHARINDEX(@split, @strZoneId) > 0 
        BEGIN
            SET @zoneId = CONVERT(INT, LEFT(@strZoneId,
                                            CHARINDEX(@split, @strZoneId) - 1))
            SET @strZoneId = STUFF(@strZoneId, 1,
                                   CHARINDEX(@split, @strZoneId), '')
	
            SELECT  @active = Active
            FROM    PE_ADZone
            WHERE   ZoneId = @zoneId
            IF @active = 0 
                SET @active = 1
            UPDATE  PE_ADZone
            SET     Active = @active
            WHERE   ZoneId = @zoneId
        END
    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_Source_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Source_Update]
    (
      @ID INT ,
      @Type VARCHAR(50) ,
      @Name VARCHAR(50) ,
      @Passed BIT ,
      @onTop BIT ,
      @IsElite BIT ,
      @Hits INT ,
      @LastUseTime DATETIME ,
      @Photo VARCHAR(50) ,
      @Intro NVARCHAR(255) ,
      @Address VARCHAR(50) ,
      @Tel VARCHAR(50) ,
      @Fax VARCHAR(50) ,
      @Mail VARCHAR(50) ,
      @Email VARCHAR(50) ,
      @ZipCode NVARCHAR(50) ,
      @HomePage VARCHAR(50) ,
      @Im VARCHAR(50) ,
      @ContacterName VARCHAR(50)
    )
AS 
    IF @ID > 0 
        BEGIN
            UPDATE  PE_Source
            SET     Type = @Type ,
                    [Name] = @Name ,
                    Passed = @Passed ,
                    onTop = @onTop ,
                    IsElite = @IsElite ,
                    Hits = @Hits ,
                    LastUseTime = @LastUseTime ,
                    Photo = @Photo ,
                    Intro = @Intro ,
                    Address = @Address ,
                    Tel = @Tel ,
                    Fax = @Fax ,
                    Mail = @Mail ,
                    Email = @Email ,
                    ZipCode = @ZipCode ,
                    HomePage = @HomePage ,
                    Im = @Im ,
                    Contacter = @ContacterName
            WHERE   ID = @ID
        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_AD_ADZone_Add]'
GO
ALTER PROCEDURE [dbo].[PR_AD_ADZone_Add]
    (
      @ZoneId INT ,
      @ZoneName NVARCHAR(100) ,
      @ZoneJSName NVARCHAR(100) ,
      @ZoneIntro NVARCHAR(255) ,
      @ZoneType INT ,
      @DefaultSetting BIT ,
      @ZoneSetting NVARCHAR(255) ,
      @ZoneWidth INT ,
      @ZoneHeight INT ,
      @Active BIT ,
      @ShowType INT ,
      @UpdateTime DATETIME
	
    )
AS 
    SET NOCOUNT OFF 
    BEGIN
        INSERT  INTO PE_AdZone
                ( ZoneId ,
                  ZoneName ,
                  ZoneJSName ,
                  ZoneIntro ,
                  ZoneType ,
                  DefaultSetting ,
                  ZoneSetting ,
                  ZoneWidth ,
                  ZoneHeight ,
                  Active ,
                  ShowType ,
                  UpdateTime
                )
        VALUES  ( @ZoneId ,
                  @ZoneName ,
                  @ZoneJSName ,
                  @ZoneIntro ,
                  @ZoneType ,
                  @DefaultSetting ,
                  @ZoneSetting ,
                  @ZoneWidth ,
                  @ZoneHeight ,
                  @Active ,
                  @ShowType ,
                  @UpdateTime
                )
    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_Accessories_Source_Insert]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Source_Insert]
    (
      @Type VARCHAR(50) ,
      @Name VARCHAR(50) ,
      @Passed BIT ,
      @onTop BIT ,
      @IsElite BIT ,
      @Hits INT ,
      @LastUseTime DATETIME ,
      @Photo VARCHAR(50) ,
      @Intro NVARCHAR(255) ,
      @Address VARCHAR(50) ,
      @Tel VARCHAR(50) ,
      @Fax VARCHAR(50) ,
      @Mail VARCHAR(50) ,
      @Email VARCHAR(50) ,
      @ZipCode NVARCHAR(50) ,
      @HomePage VARCHAR(50) ,
      @Im VARCHAR(50) ,
      @ContacterName VARCHAR(50)
    )
AS 
    INSERT  INTO PE_Source
            ( Type ,
              Name ,
              Passed ,
              onTop ,
              IsElite ,
              Hits ,
              LastUseTime ,
              Photo ,
              Intro ,
              Address ,
              Tel ,
              Fax ,
              Mail ,
              Email ,
              ZipCode ,
              HomePage ,
              Im ,
              Contacter
            )
    VALUES  ( @Type ,
              @Name ,
              @Passed ,
              @onTop ,
              @IsElite ,
              @Hits ,
              @LastUseTime ,
              @Photo ,
              @Intro ,
              @Address ,
              @Tel ,
              @Fax ,
              @Mail ,
              @Email ,
              @ZipCode ,
              @HomePage ,
              @Im ,
              @ContacterName
            )
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_AD_ADZone_Pause]'
GO
ALTER PROCEDURE [dbo].[PR_AD_ADZone_Pause]
    (
      @strZoneId VARCHAR(8000)
    )
AS
    DECLARE @active BIT
    DECLARE @zoneId INT
    DECLARE @split VARCHAR(1)
	
    SET @split = ','
    SET @strZoneId = @strZoneId + ','
	
    WHILE CHARINDEX(@split, @strZoneId) > 0 
        BEGIN
            SET @zoneId = CONVERT(INT, LEFT(@strZoneId,
                                            CHARINDEX(@split, @strZoneId) - 1))
            SET @strZoneId = STUFF(@strZoneId, 1,
                                   CHARINDEX(@split, @strZoneId), '')
	
            SELECT  @active = Active
            FROM    PE_ADZone
            WHERE   ZoneId = @zoneId
            IF @active = 1 
                SET @active = 0
            UPDATE  PE_ADZone
            SET     Active = @active
            WHERE   ZoneId = @zoneId
        END
	
    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_Source_GetSourceInfoByType]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Source_GetSourceInfoByType] ( @Type VARCHAR(1000) )
AS 
    SELECT  ID ,
            Type ,
            [Name] ,
            Passed ,
            onTop ,
            IsElite ,
            Hits ,
            LastUseTime ,
            Photo ,
            Intro ,
            Address ,
            Tel ,
            Fax ,
            Mail ,
            Email ,
            ZipCode ,
            HomePage ,
            Im ,
            Contacter
    FROM    PE_Source
    WHERE   Type = @Type
    ORDER BY ID DESC 	
    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_AD_ADZone_Update]'
GO
ALTER PROCEDURE [dbo].[PR_AD_ADZone_Update]
    (
      @ZoneId INT ,
      @ZoneName NVARCHAR(100) ,
      @ZoneJSName NVARCHAR(100) ,
      @ZoneIntro NVARCHAR(255) ,
      @ZoneType INT ,
      @DefaultSetting BIT ,
      @ZoneSetting NVARCHAR(255) ,
      @ZoneWidth INT ,
      @ZoneHeight INT ,
      @Active BIT ,
      @ShowType INT ,
      @UpdateTime DATETIME
    )
AS 
    SET NOCOUNT OFF
    BEGIN
        UPDATE  PE_AdZone
        SET     ZoneName = @ZoneName ,
                ZoneJSName = @ZoneJSName ,
                ZoneIntro = @ZoneIntro ,
                ZoneType = @ZoneType ,
                DefaultSetting = @DefaultSetting ,
                ZoneSetting = @ZoneSetting ,
                ZoneWidth = @ZoneWidth ,
                ZoneHeight = @ZoneHeight ,
                Active = @Active ,
                ShowType = @ShowType
        WHERE   ZoneId = @ZoneId
	
    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_Accessories_Source_GetSourceInfoByID]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Source_GetSourceInfoByID] ( @ID INT )
AS 
    IF @ID > 0 
        BEGIN
            SELECT  ID ,
                    Type ,
                    [Name] ,
                    Passed ,
                    onTop ,
                    IsElite ,
                    Hits ,
                    LastUseTime ,
                    Photo ,
                    Intro ,
                    Address ,
                    Tel ,
                    Fax ,
                    Mail ,
                    Email ,
                    ZipCode ,
                    HomePage ,
                    Im ,
                    Contacter
            FROM    PE_Source
            WHERE   ID = @ID
        END
    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_Author_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Author_Update]
    (
      @ID INT ,
      @UserId INT ,
      @Name VARCHAR(50) ,
      @Type VARCHAR(50) ,
      @Passed BIT ,
      @onTop BIT ,
      @IsElite BIT ,
      @Hits INT ,
      @LastUseTime DATETIME ,
      @TemplateID INT ,
      @Photo VARCHAR(255) ,
      @Intro NTEXT ,
      @Address VARCHAR(50) ,
      @Tel VARCHAR(50) ,
      @Fax VARCHAR(50) ,
      @Mail VARCHAR(50) ,
      @Email VARCHAR(50) ,
      @ZipCode NVARCHAR(50) ,
      @HomePage VARCHAR(50) ,
      @Im VARCHAR(50) ,
      @Sex SMALLINT ,
      @BirthDay DATETIME ,
      @Company VARCHAR(50) ,
      @Department VARCHAR(50)
    )
AS 
    IF @ID > 0 
        BEGIN

            UPDATE  PE_Author
            SET     UserID = @UserId ,
                    Type = @Type ,
                    [Name] = @Name ,
                    Passed = @Passed ,
                    onTop = @onTop ,
                    IsElite = @IsElite ,
                    Hits = @Hits ,
                    LastUseTime = @LastUseTime ,
                    TemplateID = @TemplateID ,
                    Photo = @Photo ,
                    Intro = @Intro ,
                    Address = @Address ,
                    Tel = @Tel ,
                    Fax = @Fax ,
                    Mail = @Mail ,
                    Email = @Email ,
                    ZipCode = @ZipCode ,
                    HomePage = @HomePage ,
                    Im = @Im ,
                    Sex = @Sex ,
                    BirthDay = @BirthDay ,
                    Company = @Company ,
                    Department = @Department
            WHERE   ID = @ID
		
        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_Accessories_Author_GetAuthorInfoByID]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Author_GetAuthorInfoByID] ( @ID INT )
AS 
    IF @ID > 0 
        BEGIN
            SELECT  ID ,
                    UserID ,
                    Type ,
                    [Name] ,
                    Passed ,
                    onTop ,
                    IsElite ,
                    Hits ,
                    LastUseTime ,
                    TemplateID ,
                    Photo ,
                    Intro ,
                    Address ,
                    Tel ,
                    Fax ,
                    Mail ,
                    Email ,
                    ZipCode ,
                    HomePage ,
                    Im ,
                    Sex ,
                    BirthDay ,
                    Company ,
                    Department
            FROM    PE_Author
            WHERE   ID = @ID
        END
	
    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_Source_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Source_Delete] ( @ID NVARCHAR(100) )
AS 
    IF @ID IS NOT NULL 
        BEGIN
            DELETE  FROM PE_Source
            WHERE   ID IN ( @ID )
        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_Accessories_Author_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Author_Delete] ( @ID NVARCHAR(100) )
AS 
    IF @ID IS NOT NULL 
        BEGIN
            DELETE  FROM PE_Author
            WHERE   ID IN ( @ID ) 
        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_Accessories_Author_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Author_Add]
    (
      @UserId INT ,
      @Name VARCHAR(50) ,
      @Type VARCHAR(50) ,
      @Passed BIT ,
      @onTop BIT ,
      @IsElite BIT ,
      @Hits INT ,
      @LastUseTime DATETIME ,
      @TemplateID INT ,
      @Photo VARCHAR(255) ,
      @Intro NTEXT ,
      @Address VARCHAR(50) ,
      @Tel VARCHAR(50) ,
      @Fax VARCHAR(50) ,
      @Mail VARCHAR(50) ,
      @Email VARCHAR(50) ,
      @ZipCode NVARCHAR(50) ,
      @HomePage VARCHAR(50) ,
      @Im VARCHAR(50) ,
      @Sex SMALLINT ,
      @BirthDay DATETIME ,
      @Company VARCHAR(50) ,
      @Department VARCHAR(50)
    )
AS 
    INSERT  INTO PE_Author
            ( UserID ,
              Type ,
              [Name] ,
              Passed ,
              onTop ,
              IsElite ,
              Hits ,
              LastUseTime ,
              TemplateID ,
              Photo ,
              Intro ,
              Address ,
              Tel ,
              Fax ,
              Mail ,
              Email ,
              ZipCode ,
              HomePage ,
              Im ,
              Sex ,
              BirthDay ,
              Company ,
              Department
            )
    VALUES  ( @UserId ,
              @Type ,
              @Name ,
              @Passed ,
              @onTop ,
              @IsElite ,
              @Hits ,
              @LastUseTime ,
              @TemplateID ,
              @Photo ,
              @Intro ,
              @Address ,
              @Tel ,
              @Fax ,
              @Mail ,
              @Email ,
              @ZipCode ,
              @HomePage ,
              @Im ,
              @Sex ,
              @BirthDay ,
              @Company ,
              @Department
            )
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_Bank_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Bank_Update]
    (
      @BankID INT ,
      @BankShortName NVARCHAR(100) ,
      @BankName NVARCHAR(100) ,
      @Accounts NVARCHAR(100) ,
      @CardNum NVARCHAR(100) ,
      @HolderName NVARCHAR(100) ,
      @BankIntro NVARCHAR(510) ,
      @BankPic NVARCHAR(400) ,
      @IsDefault BIT ,
      @IsDisabled BIT ,
      @OrderID INT
    )
AS 
    IF ( @IsDisabled = 1 ) 
        BEGIN
            UPDATE  [PE_Bank]
            SET     IsDefault = 0
            WHERE   IsDefault = 1
        END
    IF @orderId <> 0 
        BEGIN
            DECLARE @oldOrderId INT
            DECLARE @max INT
            DECLARE @min INT
            DECLARE @count INT
            DECLARE @icount INT
            DECLARE @offset1 INT
            DECLARE @offset2 INT

            SET @icount = 1
            DECLARE @tmp TABLE
                (
                  Row INT IDENTITY(1, 1)
                          PRIMARY KEY ,
                  id INT ,
                  oid INT
                )
            SELECT  @oldOrderId = OrderId
            FROM    [PE_Bank]
            WHERE   BankID = @BankId
            IF ( @orderId <> @oldOrderId ) 
                BEGIN
                    IF ( @orderId > @oldOrderId ) 
                        BEGIN
                            SET @max = @orderId
                            SET @min = @oldOrderId
                            SET @offset1 = 0
                            SET @offset2 = 1
                        END
                    ELSE 
                        BEGIN
                            SET @max = @oldOrderId
                            SET @min = @orderId
                            SET @offset1 = 1
                            SET @offset2 = 0
                        END
                    INSERT  INTO @tmp
                            ( id ,
                              oid
                            )
                            SELECT  BankId ,
                                    OrderId
                            FROM    [PE_Bank]
                            WHERE   OrderId BETWEEN @min AND @max
                            ORDER BY orderId
                    SELECT  @count = COUNT(*)
                    FROM    @tmp

                    WHILE ( @icount < @count ) 
                        BEGIN
                            UPDATE  [PE_Bank]
                            SET     OrderId = ( SELECT  oid
                                                FROM    @tmp
                                                WHERE   Row = @icount
                                                        + @offset1
                                              )
                            WHERE   BankId = ( SELECT   Id
                                               FROM     @tmp
                                               WHERE    Row = @icount
                                                        + @offset2
                                             )
                            SET @icount = @icount + 1
                        END 
                END
            UPDATE  [PE_Bank]
            SET     [BankShortName] = @BankShortName ,
                    [BankName] = @BankName ,
                    [Accounts] = @Accounts ,
                    [CardNum] = @CardNum ,
                    [HolderName] = @HolderName ,
                    [BankIntro] = @BankIntro ,
                    [BankPic] = @BankPic ,
                    [IsDefault] = @IsDefault ,
                    [IsDisabled] = @IsDisabled ,
                    [OrderID] = @OrderID
            WHERE   [BankID] = @BankID
        END
    ELSE 
        BEGIN
            UPDATE  [PE_Bank]
            SET     [BankShortName] = @BankShortName ,
                    [BankName] = @BankName ,
                    [Accounts] = @Accounts ,
                    [CardNum] = @CardNum ,
                    [HolderName] = @HolderName ,
                    [BankIntro] = @BankIntro ,
                    [BankPic] = @BankPic ,
                    [IsDefault] = @IsDefault ,
                    [IsDisabled] = @IsDisabled ,
                    [OrderID] = @OrderID
            WHERE   [BankID] = @BankID
        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_Accessories_Bank_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Bank_Delete] ( @BankID INT )
AS 
    SET NOCOUNT OFF
    DELETE  PE_Bank
    WHERE   [BankID] = @BankID
    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_Bank_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Bank_Add]
    (
      @BankID INT ,
      @BankShortName NVARCHAR(100) ,
      @BankName NVARCHAR(100) ,
      @Accounts NVARCHAR(100) ,
      @CardNum NVARCHAR(100) ,
      @HolderName NVARCHAR(100) ,
      @BankIntro NVARCHAR(510) ,
      @BankPic NVARCHAR(400) ,
      @IsDefault BIT ,
      @IsDisabled BIT 
    )
AS 
    SET NOCOUNT OFF 
    DECLARE @TempID INT

    SELECT  @TempID = ( ISNULL(MAX(OrderId), 0) + 1 )
    FROM    PE_Bank

    INSERT  INTO PE_Bank
            ( [BankID] ,
              [BankShortName] ,
              [BankName] ,
              [Accounts] ,
              [CardNum] ,
              [HolderName] ,
              [BankIntro] ,
              [BankPic] ,
              [IsDefault] ,
              [IsDisabled] ,
              [OrderID]
	      )
    VALUES  ( @BankID ,
              @BankShortName ,
              @BankName ,
              @Accounts ,
              @CardNum ,
              @HolderName ,
              @BankIntro ,
              @BankPic ,
              @IsDefault ,
              @IsDisabled ,
              @TempID
	      )
    IF @IsDefault = 1 
        UPDATE  PE_Bank
        SET     IsDefault = 0
        WHERE   BankShortName != @BankShortName
    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_Bank_GetBankInfoByID]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Bank_GetBankInfoByID] ( @BankID INT )
AS 
    SET NOCOUNT OFF
    SELECT  [BankID] ,
            [BankShortName] ,
            [BankName] ,
            [Accounts] ,
            [CardNum] ,
            [HolderName] ,
            [BankIntro] ,
            [BankPic] ,
            [IsDefault] ,
            [IsDisabled] ,
            [OrderID]
    FROM    PE_Bank
    WHERE   [BankID] = @BankID
    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_Bank_SetOrderID]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Bank_SetOrderID]
    (
      @BankID INT ,
      @OrderID INT
    )
AS 
    IF NOT EXISTS ( SELECT  OrderID
                    FROM    PE_Bank
                    WHERE   OrderID = @OrderID
                            AND BankID <> @BankID ) 
        BEGIN
            UPDATE  PE_Bank
            SET     [OrderID] = @OrderID
            WHERE   [BankID] = @BankID
        END	
    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_Bank_SetDisabled]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Bank_SetDisabled]
    (
      @BankID INT ,
      @IsDisabled BIT
    )
AS 
    SET NOCOUNT OFF
    UPDATE  PE_Bank
    SET     [IsDisabled] = @IsDisabled
    WHERE   [BankID] = @BankID
    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_Bank_SetDefault]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Bank_SetDefault] ( @BankID INT )
AS 
    UPDATE  PE_Bank
    SET     [IsDefault] = 0
		
    UPDATE  PE_Bank
    SET     [IsDefault] = 1
    WHERE   [BankID] = @BankID
    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].[PE_StatVisitor]'
GO
ALTER TABLE [dbo].[PE_StatVisitor] ALTER COLUMN [Ip] [varchar] (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_StatIp]'
GO
ALTER TABLE [dbo].[PE_StatIp] ALTER COLUMN [TIp] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT 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_StatIp] on [dbo].[PE_StatIp]'
GO
ALTER TABLE [dbo].[PE_StatIp] ADD CONSTRAINT [PK_PE_StatIp] PRIMARY KEY CLUSTERED  ([TIp])
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 index [TIp] on [dbo].[PE_StatIp]'
GO
CREATE NONCLUSTERED INDEX [TIp] ON [dbo].[PE_StatIp] ([TIp])
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_PaymentType_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_PaymentType_Add]
    (
      @typeName NVARCHAR(50) ,
      @intro NVARCHAR(50) = NULL ,
      @discount FLOAT = 100.0 ,
      @isDefault BIT ,
      @isDisabled BIT ,
      @category INT = 0
	
    )
AS 
    DECLARE @typeId INT
    DECLARE @orderId INT
	
    SELECT  @typeId = ISNULL(MAX(TypeId), 0) + 1 ,
            @orderId = ISNULL(MAX(OrderId), 0) + 1
    FROM    PE_PaymentType
	
    IF ( @typeID = 1 ) 
        BEGIN
            SET @isDefault = 1
        END
    ELSE 
        BEGIN
            IF ( @isDefault = 1 ) 
                BEGIN
                    UPDATE  PE_PaymentType
                    SET     IsDefault = 0
                    WHERE   IsDefault = 1
                END
        END
	
    INSERT  INTO PE_PaymentType
            ( TypeId ,
              TypeName ,
              Intro ,
              Discount ,
              OrderId ,
              IsDefault ,
              IsDisabled ,
              Category
            )
    VALUES  ( @typeId ,
              @typeName ,
              @intro ,
              @discount ,
              @orderId ,
              @isDefault ,
              @isDisabled ,
              @category
            )
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_Analytics_SaveConfig]'
GO
ALTER PROCEDURE [dbo].[PR_Analytics_SaveConfig]
    (
      @MasterTimeZone INT ,
      @Interval INT ,
      @IntervalNum INT ,
      @OnlineTime INT ,
      @VisitRecord INT ,
      @KillRefresh INT ,
      @RegFields_Fill NVARCHAR(255) ,
      @OldTotalNum INT ,
      @OldTotalView INT ,
      @StartDate NVARCHAR(10)
    )
AS 
    IF EXISTS ( SELECT  *
                FROM    PE_statInfoList ) 
        BEGIN
	
            UPDATE  PE_StatInfoList
            SET     [MasterTimeZone] = @MasterTimeZone ,
                    [Interval] = @Interval ,
                    [IntervalNum] = @IntervalNum ,
                    [OnlineTime] = @OnlineTime ,
                    [VisitRecord] = @VisitRecord ,
                    [KillRefresh] = @KillRefresh ,
                    [RegFields_Fill] = @RegFields_Fill ,
                    [OldTotalNum] = @OldTotalNum ,
                    [OldTotalView] = @OldTotalView ,
                    [StartDate] = @StartDate

        END
    ELSE 
        BEGIN
            INSERT  INTO [PE_StatInfoList]
                    ( [StartDate] ,
                      [TotalNum] ,
                      [TotalView] ,
                      [MonthNum] ,
                      [MonthMaxNum] ,
                      [OldMonth] ,
                      [MonthMaxDate] ,
                      [DayNum] ,
                      [DayMaxNum] ,
                      [OldDay] ,
                      [DayMaxDate] ,
                      [HourNum] ,
                      [HourMaxNum] ,
                      [OldHour] ,
                      [HourMaxTime] ,
                      [ChinaNum] ,
                      [OtherNum] ,
                      [MasterTimeZone] ,
                      [Interval] ,
                      [IntervalNum] ,
                      [OnlineTime] ,
                      [VisitRecord] ,
                      [KillRefresh] ,
                      [RegFields_Fill] ,
                      [OldTotalNum] ,
                      [OldTotalView]
                    )
            VALUES  ( @StartDate ,
                      0 ,
                      0 ,
                      0 ,
                      0 ,
                      '' ,
                      '' ,
                      0 ,
                      0 ,
                      CONVERT(CHAR(10), GETDATE(), 120) ,
                      '' ,
                      0 ,
                      0 ,
                      '' ,
                      0 ,
                      0 ,
                      0 ,
                      @MasterTimeZone ,
                      @Interval ,
                      @IntervalNum ,
                      @OnlineTime ,
                      @VisitRecord ,
                      @KillRefresh ,
                      @RegFields_Fill ,
                      @OldTotalNum ,
                      @OldTotalView
                    )

        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_PaymentType_GetById]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_PaymentType_GetById] ( @id INT = 0 )
AS
    SELECT  *
    FROM    PE_PaymentType
    WHERE   TypeId = @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'Altering [dbo].[PR_Analytics_GetOnlineByIP]'
GO
ALTER PROCEDURE [dbo].[PR_Analytics_GetOnlineByIP] @IP VARCHAR(50)
AS 
    BEGIN
        DECLARE @onlinetime INT
        SELECT  @onlinetime = ISNULL(OnlineTime, 100)
        FROM    PE_StatInfoList

        SELECT  *
        FROM    PE_Statonline
        WHERE   LastTime > DATEADD(s, -@onlinetime, GETDATE())
                AND UserIP = @IP

    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_PaymentType_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_PaymentType_Delete] ( @typeId INT )
AS 
    DECLARE @Oid INT
    SELECT  @Oid = OrderId
    FROM    PE_PaymentType
    WHERE   [TypeId] = @typeId
	
    DELETE  PE_PaymentType
    WHERE   TypeId = @typeId

    UPDATE  PE_PaymentType
    SET     OrderId = OrderId - 1
    WHERE   OrderId > @Oid
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_PaymentType_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_PaymentType_Update]
    @typeId INT ,
    @typeName NVARCHAR(50) ,
    @intro NVARCHAR(50) = NULL ,
    @discount FLOAT = 100.0 ,
    @orderId INT = 0 ,
    @category INT = 0 ,
    @isDefault BIT ,
    @isDisabled BIT
AS
    IF ( @isDefault = 1 ) 
        BEGIN
            UPDATE  PE_PaymentType
            SET     IsDefault = 0
            WHERE   IsDefault = 1
        END
    IF @orderId <> 0 
        BEGIN
            DECLARE @oldOrderId INT
            DECLARE @max INT
            DECLARE @min INT
            DECLARE @count INT
            DECLARE @icount INT
            DECLARE @offset1 INT
            DECLARE @offset2 INT

            SET @icount = 1
            DECLARE @tmp TABLE
                (
                  Row INT IDENTITY(1, 1)
                          PRIMARY KEY ,
                  id INT ,
                  oid INT
                )
            SELECT  @oldOrderId = OrderId
            FROM    PE_PaymentType
            WHERE   TypeID = @typeId
            IF ( @orderId <> @oldOrderId ) 
                BEGIN
                    IF ( @orderId > @oldOrderId ) 
                        BEGIN
                            SET @max = @orderId
                            SET @min = @oldOrderId
                            SET @offset1 = 0
                            SET @offset2 = 1
                        END
                    ELSE 
                        BEGIN
                            SET @max = @oldOrderId
                            SET @min = @orderId
                            SET @offset1 = 1
                            SET @offset2 = 0
                        END
                    INSERT  INTO @tmp
                            ( id ,
                              oid
                            )
                            SELECT  TypeId ,
                                    OrderId
                            FROM    PE_PaymentType
                            WHERE   OrderId BETWEEN @min AND @max
                            ORDER BY orderId
                    SELECT  @count = COUNT(*)
                    FROM    @tmp

                    WHILE ( @icount < @count ) 
                        BEGIN
                            UPDATE  PE_PaymentType
                            SET     OrderId = ( SELECT  oid
                                                FROM    @tmp
                                                WHERE   Row = @icount
                                                        + @offset1
                                              )
                            WHERE   TypeId = ( SELECT   Id
                                               FROM     @tmp
                                               WHERE    Row = @icount
                                                        + @offset2
                                             )
                            SET @icount = @icount + 1
                        END 
                END
            UPDATE  PE_PaymentType
            SET     TypeName = @typeName ,
                    Intro = @intro ,
                    Discount = @discount ,
                    OrderId = @orderId ,
                    IsDefault = @isDefault ,
                    IsDisabled = @isDisabled ,
                    Category = @category
            WHERE   TypeId = @typeId
        END
    ELSE 
        BEGIN
		--不排序的更新
            UPDATE  PE_PaymentType
            SET     TypeName = @typeName ,
                    Intro = @intro ,
                    Discount = @discount ,
                    IsDefault = @isDefault ,
                    IsDisabled = @isDisabled ,
                    Category = @category
            WHERE   TypeId = @typeId
        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_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(50) ,    --使用者
      @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_Analytics_StatOnline_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Analytics_StatOnline_Add]
    (
      @UserIP VARCHAR(50) ,
      @UserAgent VARCHAR(255) ,
      @UserPage VARCHAR(4000)
    )
AS 
    BEGIN

        DECLARE @onlinetime INT
        DECLARE @onNowTime DATETIME
        DECLARE @id INT

        SELECT  @onlinetime = ISNULL(OnlineTime, 0)
        FROM    PE_StatInfoList
        SET @onNowTime = DATEADD(s, -@onlinetime, GETDATE())

        IF EXISTS ( SELECT  *
                    FROM    PE_Statonline
                    WHERE   LastTime > @onNowTime
                            AND UserIP = @UserIP ) 
            BEGIN
                UPDATE  PE_StatOnline
                SET     LastTime = GETDATE() ,
                        UserPage = @UserPage
                WHERE   LastTime > @onNowTime
                        AND UserIP = @UserIP
            END
        ELSE 
            BEGIN
                SELECT TOP 1
                        @id = id
                FROM    PE_StatOnline
                WHERE   LastTime < @onNowTime
                ORDER BY LastTime
                IF @id IS NOT NULL 
                    BEGIN
                        UPDATE  PE_StatOnline
                        SET     UserIP = @UserIP ,
                                UserAgent = @UserAgent ,
                                UserPage = @UserPage ,
                                Ontime = GETDATE() ,
                                LastTime = GETDATE()
                        WHERE   id = @id
                    END
                ELSE 
                    BEGIN
                        INSERT  INTO PE_StatOnline
                                ( UserIP ,
                                  UserAgent ,
                                  UserPage ,
                                  OnTime ,
                                  LastTime
                                )
                        VALUES  ( @UserIP ,
                                  @UserAgent ,
                                  @UserPage ,
                                  GETDATE() ,
                                  GETDATE()
                                )
                    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_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(50) ,
      @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].[PR_Accessories_Keywords_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Keywords_Update]
    (
      @KeywordID INT ,
      @KeywordText NVARCHAR(200) ,
      @KeywordType INT ,
      @Priority INT ,
      @LastUseTime DATETIME ,
      @QuoteTimes INT
    )
AS 
    UPDATE  PE_Keywords
    SET     KeywordText = @KeywordText ,
            KeywordType = @KeywordType ,
            Priority = @Priority ,
            LastUseTime = @LastUseTime ,
            QuoteTimes = @QuoteTimes
    WHERE   KeywordID = @KeywordID
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_Keywords_GetById]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Keywords_GetById] ( @KeywordID INT )
AS 
    SELECT  KeywordID ,
            KeywordText ,
            KeywordType ,
            Priority ,
            Hits ,
            LastUseTime ,
            QuoteTimes
    FROM    PE_Keywords
    WHERE   KeywordID = @KeywordID
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_Keywords_Exists]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Keywords_Exists]
    (
      @KeywordText NVARCHAR(200)
    )
AS 
    SELECT  COUNT(*)
    FROM    PE_Keywords
    WHERE   KeywordText = @KeywordText
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_Client_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_Client_Update]
    (
      @ClientID INT ,
      @ParentID INT ,
      @ClientNum NVARCHAR(20) ,
      @ClientType INT ,
      @ClientName NVARCHAR(100) ,
      @ShortedForm NVARCHAR(20) ,
      @Area INT ,
      @ClientField INT ,
      @ValueLevel INT ,
      @CreditLevel INT ,
      @Importance INT ,
      @ConnectionLevel INT ,
      @GroupID INT ,
      @SourceType INT ,
      @PhaseType INT ,
      @Remark NTEXT ,
      @UpdateTime DATETIME ,
      @Owner NVARCHAR(50) ,
      @IsBusiness BIT
    )
AS 
    UPDATE  [PE_Client]
    SET     ParentID = @ParentID ,
            ClientNum = @ClientNum ,
            ClientType = @ClientType ,
            ClientName = @ClientName ,
            ShortedForm = @ShortedForm ,
            Area = @Area ,
            ClientField = @ClientField ,
            ValueLevel = @ValueLevel ,
            CreditLevel = @CreditLevel ,
            Importance = @Importance ,
            ConnectionLevel = @ConnectionLevel ,
            GroupID = @GroupID ,
            SourceType = @SourceType ,
            PhaseType = @PhaseType ,
            Remark = @Remark ,
            UpdateTime = @UpdateTime ,
            Owner = @Owner ,
            IsBusiness = @IsBusiness
    WHERE   ClientID = @ClientID
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_Keywords_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Keywords_Add]
    (
      @KeywordText NVARCHAR(200) ,
      @KeywordType INT ,
      @Priority INT ,
      @QuoteTimes INT
    )
AS 
    INSERT  INTO PE_Keywords
            ( KeywordText ,
              KeywordType ,
              Priority ,
              QuoteTimes
            )
    VALUES  ( @KeywordText ,
              @KeywordType ,
              @Priority ,
              @QuoteTimes
            )
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_Client_GetById]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_Client_GetById] ( @ClientID INT )
AS 
    SELECT  *
    FROM    PE_Client
    WHERE   ClientID = @ClientID
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_Log]'
GO
ALTER TABLE [dbo].[PE_Log] ALTER COLUMN [UserIP] [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].[PR_Accessories_Log_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Log_Update]
    (
      @LogID INT ,
      @Category INT ,
      @Priority INT ,
      @Title NVARCHAR(255) ,
      @Message TEXT ,
      @Timestamp DATETIME ,
      @UserName NVARCHAR(255) ,
      @UserIP NVARCHAR(50) ,
      @Source TEXT ,
      @ScriptName NVARCHAR(255) ,
      @PostString TEXT    
    )
AS 
    UPDATE  [PE_Log]
    SET     [Category] = @Category ,
            [Priority] = @Priority ,
            [Title] = @Title ,
            [Message] = @Message ,
            [Timestamp] = @Timestamp ,
            [UserName] = @UserName ,
            [UserIP] = @UserIP ,
            [Source] = @Source ,
            [ScriptName] = @ScriptName ,
            [PostString] = @PostString
    WHERE   LogID = @LogID
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_Client_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_Client_Add]
    (
      @ClientID INT ,
      @ParentID INT ,
      @ClientNum NVARCHAR(20) ,
      @ClientType INT ,
      @ClientName NVARCHAR(100) ,
      @ShortedForm NVARCHAR(20) ,
      @Area INT ,
      @ClientField INT ,
      @ValueLevel INT ,
      @CreditLevel INT ,
      @Importance INT ,
      @ConnectionLevel INT ,
      @GroupID INT ,
      @SourceType INT ,
      @PhaseType INT ,
      @Remark NTEXT ,
      @CreateTime DATETIME ,
      @UpdateTime DATETIME ,
      @Owner NVARCHAR(50) ,
      @IsBusiness BIT
	
    )
AS 
    INSERT  INTO [PE_Client]
            ( [ClientID] ,
              [ParentID] ,
              [ClientNum] ,
              [ClientType] ,
              [ClientName] ,
              [ShortedForm] ,
              [Area] ,
              [ClientField] ,
              [ValueLevel] ,
              [CreditLevel] ,
              [Importance] ,
              [ConnectionLevel] ,
              [GroupID] ,
              [SourceType] ,
              [PhaseType] ,
              [Remark] ,
              [CreateTime] ,
              [UpdateTime] ,
              [Owner] ,
              [LastContactedTime] ,
              [IsBusiness] 
            )
    VALUES  ( @ClientID ,
              @ParentID ,
              @ClientNum ,
              @ClientType ,
              @ClientName ,
              @ShortedForm ,
              @Area ,
              @ClientField ,
              @ValueLevel ,
              @CreditLevel ,
              @Importance ,
              @ConnectionLevel ,
              @GroupID ,
              @SourceType ,
              @PhaseType ,
              @Remark ,
              @CreateTime ,
              @UpdateTime ,
              @Owner ,
              GETDATE() ,
              @IsBusiness
            )
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_Log_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Log_Add]
    (
      @Category INT ,
      @Priority INT ,
      @Title NVARCHAR(255) ,
      @Message TEXT ,
      @Timestamp DATETIME ,
      @UserName NVARCHAR(255) ,
      @UserIP NVARCHAR(50) ,
      @Source TEXT ,
      @ScriptName NVARCHAR(255) ,
      @PostString TEXT    
    )
AS 
    INSERT  INTO [PE_Log]
            ( Category ,
              Priority ,
              Title ,
              [Message] ,
              [Timestamp] ,
              UserName ,
              UserIP ,
              Source ,
              ScriptName ,
              PostString    
		)
    VALUES  ( @Category ,
              @Priority ,
              @Title ,
              @Message ,
              @Timestamp ,
              @UserName ,
              @UserIP ,
              @Source ,
              @ScriptName ,
              @PostString     
	      )
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
[HtmlPageName] [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_Analytics_StatVisitor_Add]'
GO
ALTER PROCEDURE [dbo].[PR_Analytics_StatVisitor_Add]
    (
      @Ip NVARCHAR(50) ,
      @Address NVARCHAR(50) ,
      @System NVARCHAR(20) ,
      @Browser NVARCHAR(20) ,
      @Screen NVARCHAR(10) ,
      @Color NVARCHAR(16) ,
      @Referer NVARCHAR(100) ,
      @Timezone INT
    )
AS 
    BEGIN
        DECLARE @VisitCount INT
        DECLARE @VisitRecord INT
        SELECT  @VisitCount = COUNT(ID)
        FROM    PE_StatVisitor
        SELECT TOP 1
                @VisitRecord = VisitRecord
        FROM    PE_StatInfoList
        IF @VisitCount >= @VisitRecord 
            BEGIN
                DECLARE @id INT
                SELECT TOP 1
                        @id = id
                FROM    PE_StatVisitor
                ORDER BY VTime ASC
                UPDATE  [PE_StatVisitor]
                SET     [VTime] = GETDATE() ,
                        [Ip] = @Ip ,
                        [Address] = @Address ,
                        [System] = @System ,
                        [Browser] = @Browser ,
                        [Screen] = @Screen ,
                        [Color] = @Color ,
                        [Referer] = @Referer ,
                        [Timezone] = @Timezone
                WHERE   id = @id
            END
        ELSE 
            BEGIN
                INSERT  INTO [PE_StatVisitor]
                        ( [VTime] ,
                          [Ip] ,
                          [Address] ,
                          [System] ,
                          [Browser] ,
                          [Screen] ,
                          [Color] ,
                          [Referer] ,
                          [Timezone]
                        )
                VALUES  ( GETDATE() ,
                          @Ip ,
                          @Address ,
                          @System ,
                          @Browser ,
                          @Screen ,
                          @Color ,
                          @Referer ,
                          @Timezone
                        )
            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_Crm_RemindItem_GetInfoByTime]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_RemindItem_GetInfoByTime] ( @time INT )
AS 
    BEGIN
        SELECT  R.CREATER ,
                R.REMINDCONTENT ,
                R.REMINDBYEMAIL ,
                R.REMINDBYSMS ,
                U.EMAIL ,
                C.PHS ,
                C.MOBILE
        FROM    PE_REMINDITEM R
                INNER JOIN PE_ADMIN A ON R.CREATER = A.ADMINNAME ,
                PE_USERS U ,
                PE_CONTACTER C
        WHERE   U.USERNAME = A.USERNAME
                AND C.USERNAME = A.USERNAME
                AND r.status = 0
                AND r.remindtime BETWEEN GETDATE()
                                 AND     DATEADD(n, @time, GETDATE())
        SELECT  creater
        FROM    pe_reminditem
        WHERE   status = 0
                AND remindtime BETWEEN GETDATE()
                               AND     DATEADD(n, @time, GETDATE())
        GROUP BY creater 
    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_DeliverType_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_DeliverType_Delete] ( @typeId INT )
AS 
    DECLARE @Oid INT
    SELECT  @Oid = OrderId
    FROM    PE_DeliverType
    WHERE   TypeId = @typeId

    DELETE  PE_DeliverType
    WHERE   TypeId = @typeId

    UPDATE  PE_DeliverType
    SET     OrderId = OrderId - 1
    WHERE   OrderId > @Oid
	
    DELETE  PE_DeliverCharge
    WHERE   DeliverTypeId = @typeId
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_Product_DeleteModel]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Product_DeleteModel]
    (
      @TableName NVARCHAR(255)
    )
AS 
    BEGIN

        IF NOT EXISTS ( SELECT  *
                        FROM    PE_OrderItem
                        WHERE   TableName = @TableName ) 
            BEGIN
                DELETE  PE_CommonProduct
                WHERE   TableName = @TableName
                DELETE  PE_ProductData
                WHERE   TableName = @TableName
                DELETE  PE_ProductPrice
                WHERE   TableName = @TableName
                IF EXISTS ( SELECT  *
                            FROM    PE_Cards
                            WHERE   TableName = @TableName ) 
                    BEGIN
                        UPDATE  PE_Cards
                        SET     ProductID = 0 ,
                                TableName = ''
                        WHERE   TableName = @TableName
                    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_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Order_Delete]
    (
      @OrderId NVARCHAR(4000) ,
      @ErrOrderNum NVARCHAR(4000) OUTPUT 
    )
AS 
    BEGIN

        DECLARE @UserName NVARCHAR(50) ,
            @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'Altering [dbo].[PR_Shop_Invoice_GetById]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Invoice_GetById] ( @InvoiceId INT )
AS 
    SELECT  I.* ,
            O.OrderNum ,
            O.MoneyTotal ,
            O.MoneyReceipt ,
            C.ShortedForm AS ClientName
    FROM    PE_InvoiceItem I
            LEFT JOIN ( PE_Orders O
                        LEFT JOIN PE_Client C ON O.ClientID = C.ClientID
                      ) ON I.OrderID = O.OrderID
    WHERE   InvoiceID = @InvoiceID
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_Analytics_StatUpdate]'
GO
ALTER PROCEDURE [dbo].[PR_Analytics_StatUpdate]
    (
      @_visit INT ,
      @_IP NVARCHAR(50) = '' ,
      @_address NVARCHAR(50) = '' ,
      @_system NVARCHAR(20) = '' ,
      @_browser NVARCHAR(20) = '' ,
      @_screen NVARCHAR(10) = '' ,
      @_color NVARCHAR(16) = '' ,
      @_referer NVARCHAR(100) = '' ,
      @_weburl NVARCHAR(50) = '' ,
      @_timezone NVARCHAR(50) = '' ,
      @_visitTimezone INT ,
      @_keyword NVARCHAR(50) = '' ,
      @_mozilla NVARCHAR(255) = ''
    )
AS 
    BEGIN
        DECLARE @RegFields_Fill NVARCHAR(4000)
        DECLARE @_scope NVARCHAR(20)
        DECLARE @Province NVARCHAR(4000)
        DECLARE @dayLong NVARCHAR(10)
        DECLARE @monthLong NVARCHAR(7)
        DECLARE @hour NVARCHAR(2)
        DECLARE @year NVARCHAR(4)
        DECLARE @month NVARCHAR(2)
        DECLARE @day NVARCHAR(2)
        DECLARE @week NVARCHAR(1)

        SET @monthLong = CONVERT(NVARCHAR(7), GETDATE(), 120)
        SET @dayLong = CONVERT(NVARCHAR(10), GETDATE(), 120)
        SET @year = DATENAME(yyyy, GETDATE())
        SET @month = CONVERT(NVARCHAR(2), DATEPART(mm, GETDATE()))
        SET @day = DATENAME(dd, GETDATE())
        SET @hour = DATENAME(hh, GETDATE())
        SET @week = CONVERT(NVARCHAR(1), DATEPART(dw, GETDATE()))
        SET @Province = '北京天津上海重庆黑龙江吉林辽宁江苏浙江安徽河南河北湖南湖北山东山西内蒙古陕西甘肃宁夏青海新疆西藏云南贵州四川广东广西福建江西海南香港澳门台湾内部网络未知保留本地本机'

        IF CHARINDEX(LEFT(@_address, 2), @Province, 0) > 0 
            SET @_scope = 'ChinaNum'
        ELSE 
            SET @_scope = 'OtherNum'

        SELECT  @RegFields_Fill = RegFields_Fill
        FROM    pe_statInfolist

	--更新PE_StatVisit表
        IF CHARINDEX('FVisit', @RegFields_Fill, 0) > 0 
            EXEC [PR_Analytics_StatVisit_Update] @visit = @_visit

	--更新PE_StatVisitor表
        EXEC [PR_Analytics_StatVisitor_Add] @Ip = @_IP, @Address = @_address,
            @System = @_system, @Browser = @_browser, @Screen = @_screen,
            @Color = @_color, @Referer = @_referer,
            @Timezone = @_visitTimezone

	--更新PE_StatInfoList表
        IF NOT EXISTS ( SELECT TOP 1
                                *
                        FROM    PE_StatInfoList ) 
            BEGIN
                INSERT  INTO PE_StatInfoList
                        ( [StartDate] ,
                          [TotalNum] ,
                          [TotalView] ,
                          [MonthNum] ,
                          [MonthMaxNum] ,
                          [OldMonth] ,
                          [MonthMaxDate] ,
                          [DayNum] ,
                          [DayMaxNum] ,
                          [OldDay] ,
                          [DayMaxDate] ,
                          [HourNum] ,
                          [HourMaxNum] ,
                          [OldHour] ,
                          [HourMaxTime] ,
                          [ChinaNum] ,
                          [OtherNum] ,
                          [MasterTimeZone] ,
                          [Interval] ,
                          [IntervalNum] ,
                          [OnlineTime] ,
                          [VisitRecord] ,
                          [KillRefresh] ,
                          [RegFields_Fill] ,
                          [OldTotalNum] ,
                          [OldTotalView]
                        )
                VALUES  ( @dayLong ,
                          0 ,
                          0 ,
                          0 ,
                          0 ,
                          0 ,
                          '' ,
                          0 ,
                          0 ,
                          @dayLong ,
                          '' ,
                          0 ,
                          0 ,
                          '' ,
                          '' ,
                          0 ,
                          0 ,
                          0 ,
                          60 ,
                          10 ,
                          120 ,
                          0 ,
                          0 ,
                          '' ,
                          0 ,
                          0
                        )
            END
        EXEC ('Update PE_StatInfoList set TotalNum = TotalNum +1,TotalView = TotalView +1,'+@_scope+'='+@_scope+'+1')
        DECLARE @StartDate NVARCHAR(10)
        DECLARE @OldDay NVARCHAR(10)
        SELECT TOP 1
                @Startdate = startDate ,
                @OldDay = OldDay
        FROM    PE_StatInfoList
        IF @StartDate IS NULL 
            UPDATE  PE_StatInfoList
            SET     startDate = @dayLong
        IF @OldDay IS NULL 
            BEGIN
                UPDATE  PE_StatInfoList
                SET     OldDay = @dayLong
                SET @OldDay = @dayLong
            END
        EXEC [PR_Analytics_ModiMaxNum]
        IF @_keyword <> ''
            AND CHARINDEX('FKeyword', @RegFields_Fill, 0) > 0 
            EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatKeyword',
                @CompareField = 'Tkeyword', @AddField = 'TkeywordNum',
                @Data = @_keyword

        IF CHARINDEX('FSystem', @RegFields_Fill, 0) > 0 
            EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatSystem',
                @CompareField = 'TSystem', @AddField = 'TSysNum',
                @Data = @_system

        IF CHARINDEX('FBrowser', @RegFields_Fill, 0) > 0 
            EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatBrowser',
                @CompareField = 'TBrowser', @AddField = 'TBrwNum',
                @Data = @_browser

        IF CHARINDEX('FMozilla', @RegFields_Fill, 0) > 0 
            EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatMozilla',
                @CompareField = 'TMozilla', @AddField = 'TMozNum',
                @Data = @_mozilla

        IF CHARINDEX('FScreen', @RegFields_Fill, 0) > 0 
            EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatScreen',
                @CompareField = 'TScreen', @AddField = 'TScrNum',
                @Data = @_screen

        IF CHARINDEX('FColor', @RegFields_Fill, 0) > 0 
            EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatColor',
                @CompareField = 'TColor', @AddField = 'TColNum',
                @Data = @_color

        IF CHARINDEX('FTimezone', @RegFields_Fill, 0) > 0 
            EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatTimezone',
                @CompareField = 'TTimezone', @AddField = 'TTimNum',
                @Data = @_timezone

        IF CHARINDEX('FRefer', @RegFields_Fill, 0) > 0 
            EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatRefer',
                @CompareField = 'TRefer', @AddField = 'TRefNum',
                @Data = @_referer

        IF CHARINDEX('FWeburl', @RegFields_Fill, 0) > 0 
            EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatWeburl',
                @CompareField = 'TWeburl', @AddField = 'TWebNum',
                @Data = @_weburl

        IF CHARINDEX('FAddress', @RegFields_Fill, 0) > 0 
            EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatAddress',
                @CompareField = 'TAddress', @AddField = 'TAddNum',
                @Data = @_address

        IF CHARINDEX('FIP', @RegFields_Fill, 0) > 0 
            EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatIp',
                @CompareField = 'TIp', @AddField = 'TIpNum', @Data = @_IP

        EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatDay',
            @CompareField = 'TDay', @AddField = @hour, @Data = @dayLong
        EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatDay',
            @CompareField = 'TDay', @AddField = @hour, @Data = 'Total'
        EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatYear',
            @CompareField = 'TYear', @AddField = @month, @Data = @year
        EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatYear',
            @CompareField = 'TYear', @AddField = @month, @Data = 'Total'
        EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatMonth',
            @CompareField = 'TMonth', @AddField = @day, @Data = @monthLong
        EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatMonth',
            @CompareField = 'TMonth', @AddField = @day, @Data = 'Total'
        EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatWeek',
            @CompareField = 'TWeek', @AddField = @week, @Data = 'Total'
        IF DATEDIFF(week, @OldDay, GETDATE()) > 0 
            DELETE  FROM PE_StatWeek
            WHERE   TWeek = 'Current'
        EXEC [PR_Analytics_AddNum] @TableName = 'PE_StatWeek',
            @CompareField = 'TWeek', @AddField = @week, @Data = 'Current'

    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_IncludeFile]'
GO
ALTER TABLE [dbo].[PE_IncludeFile] ADD
[CallType] [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].[PE_Specials]'
GO
ALTER TABLE [dbo].[PE_Specials] ADD
[CreateTime] [datetime] NULL,
[IsPigeonhole] [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'Creating [dbo].[PE_UserHitHistory]'
GO
CREATE TABLE [dbo].[PE_UserHitHistory]
(
[ID] [int] NOT NULL,
[GeneralID] [int] NULL,
[ScanIP] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[UserName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[Hits] [int] NULL,
[HitTime] [datetime] NULL,
[LastHitTime] [datetime] 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_UserHitHistory] on [dbo].[PE_UserHitHistory]'
GO
ALTER TABLE [dbo].[PE_UserHitHistory] ADD CONSTRAINT [PK_PE_UserHitHistory] 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 [dbo].[PE_Wap_Article]'
GO
CREATE TABLE [dbo].[PE_Wap_Article]
(
[WapArticleId] [int] NOT NULL,
[WapCategoryID] [int] NULL,
[GeneralId] [int] NULL,
[Title] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL,
[Inputer] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[UpdateTime] [datetime] NULL,
[Status] [int] NULL,
[Content] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
[InputTime] [datetime] NULL,
[Author] [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'Creating primary key [PK_PE_Wap_Article] on [dbo].[PE_Wap_Article]'
GO
ALTER TABLE [dbo].[PE_Wap_Article] ADD CONSTRAINT [PK_PE_Wap_Article] PRIMARY KEY CLUSTERED  ([WapArticleId])
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_Wap_Category]'
GO
CREATE TABLE [dbo].[PE_Wap_Category]
(
[WapCategoryId] [int] NOT NULL,
[WapCategoryName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[TemplatePath] [nvarchar] (500) COLLATE Chinese_PRC_CI_AS NULL,
[OrderID] [int] NULL,
[CommentPurviewType] [int] NULL,
[CommentNeedCheck] [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'Creating primary key [PK_PE_Wap_Categroy] on [dbo].[PE_Wap_Category]'
GO
ALTER TABLE [dbo].[PE_Wap_Category] ADD CONSTRAINT [PK_PE_Wap_Categroy] PRIMARY KEY CLUSTERED  ([WapCategoryId])
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_Wap_Token]'
GO
CREATE TABLE [dbo].[PE_Wap_Token]
(
[LoginID] [int] NOT NULL,
[UserName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[LoginTime] [datetime] NULL,
[Identification] [nvarchar] (200) COLLATE Chinese_PRC_CI_AS NULL,
[UserCookie] [ntext] 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'Creating primary key [PK_PE_Wap_ Token] on [dbo].[PE_Wap_Token]'
GO
ALTER TABLE [dbo].[PE_Wap_Token] ADD CONSTRAINT [PK_PE_Wap_ Token] PRIMARY KEY CLUSTERED  ([LoginID])
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_BankroolItem_GetBill]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_BankroolItem_GetBill]
    (
      @StartRows INT ,
      @PageSize INT ,
      @SortColumn VARCHAR(1000) ,
      @StrColumn VARCHAR(1000) ,
      @Sorts VARCHAR(100) ,
      @Filter VARCHAR(1000) ,
      @TableName VARCHAR(1000) ,
      @Total INT OUTPUT
    )
AS 
    SET NOCOUNT ON

    BEGIN
        IF @StartRows <= 0 
            SET @StartRows = 0

        DECLARE @equalOperator CHAR(2)
        IF @StartRows = 0 
            BEGIN
                SET @equalOperator = '='
                SET @StartRows = 1
            END
        ELSE 
            SET @equalOperator = ''

/*Set sorting variables.*/
        DECLARE @operator CHAR(2)
        DECLARE @strSortColumn VARCHAR(10)
	
        IF CHARINDEX('DESC', @Sorts) > 0 
            BEGIN
                SET @strSortColumn = REPLACE(@Sorts, 'DESC', '')
                SET @operator = '<' + @equalOperator
            END
        ELSE 
            BEGIN
                IF CHARINDEX('ASC', @Sorts) = 0 
                    SET @strSortColumn = REPLACE(@Sorts, 'ASC', '')
                SET @operator = '>' + @equalOperator
            END

        DECLARE @strFilter VARCHAR(1000)
        DECLARE @strSimpleFilter VARCHAR(1000)

        IF @Filter IS NOT NULL
            AND @Filter != '' 
            BEGIN
                SET @strFilter = ' WHERE ' + @Filter + ' '
                SET @strSimpleFilter = ' AND ' + @Filter + ' '
            END
        ELSE 
            BEGIN	
                SET @strFilter = ''
                SET @strSimpleFilter = ''
            END

        DECLARE @Sql NVARCHAR(4000)
        SET @Sql = N'SELECT @Total=Count(Distinct B.OrderID) FROM '
            + @TableName + @strFilter
        EXEC sp_executesql @Sql, N'@Total Int Out', @Total OUT 

        IF @PageSize <= 0 
            SET @PageSize = @Total

        EXEC(
        'DECLARE @SortId int ' +
        'DECLARE @SortCol DateTime '+
        'SET ROWCOUNT '+ @StartRows + 
        ' SELECT @SortId = '+ @SortColumn + ',@SortCol=Max(B.DateAndTime) FROM '+ @TableName + @strFilter +' Group By B.OrderID Order by Max(B.DateAndTime) ' +@Sorts+ ', B.OrderID ' +@Sorts + 
        ' SET ROWCOUNT '+ @PageSize +
        ' SELECT '+ @StrColumn +' FROM '+ @TableName +  
        ' WHERE (B.DateAndTime '+@operator+' @SortCol OR (B.DateAndTime = @SortCol AND B.OrderID '+@operator+' @SortId )) '+ @strSimpleFilter + ' Group By B.OrderID ORDER BY Max(B.DateAndTime) ' +@Sorts+ ',B.OrderID '+ @Sorts + ''
        )


        RETURN @Total
    END

    SET ROWCOUNT 0

    SET NOCOUNT OFF
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_Keywords_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Keywords_Delete]
    (
      @KeywordId NVARCHAR(4000)
    )
AS 
    DECLARE @sql NVARCHAR(4000)
    SET @sql = 'DELETE PE_Keywords WHERE KeywordId IN (' + @KeywordId + ')'
    EXEC(@sql)
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_Message_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_Message_Delete]
    (
      @DeleteType INT ,
      @DeleteValue NVARCHAR(100)
    )
AS 
    SET NOCOUNT OFF
    BEGIN
        DECLARE @Sql NVARCHAR(4000)
		
        IF @DeleteType = 0 
            BEGIN
                SET @Sql = N'DELETE PE_Message WHERE MessageID in ('
                    + @DeleteValue + ')'
            END
        ELSE 
            IF @DeleteType = 1 
                BEGIN
                    SET @Sql = N'DELETE PE_Message WHERE Sender in ('
                        + @DeleteValue + ')'
                END
            ELSE 
                BEGIN
                    IF @DeleteValue = 0 
                        SET @Sql = N'DELETE PE_Message'
                    ELSE 
                        SET @Sql = N'DELETE PE_Message WHERE  datediff(day,SendTime,getdate()) >='
                            + @DeleteValue
                END

        EXEC sp_executesql @Sql
    END
    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_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_PaymentLog_Delete]
    (
      @paymentLogId NVARCHAR(4000)
    )
AS 
    DECLARE @sql NVARCHAR(4000)
    SET @sql = 'DELETE [PE_PaymentLog] WHERE [Status]=1 AND [PaymentLogID] IN('
        + @paymentLogId + ')'
    EXEC(@sql)
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_WordReplace_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_WordReplace_Delete] ( @ItemID NVARCHAR(4000) )
AS 
    DECLARE @sql NVARCHAR(4000)
    SET @sql = 'DELETE PE_WordReplaceItem WHERE ItemID IN (' + @ItemID + ')'
    EXEC(@sql)
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_WordReplace_IsEnabled]'
GO
ALTER PROCEDURE [dbo].[PR_Accessories_WordReplace_IsEnabled]
    (
      @ItemID NVARCHAR(4000) ,
      @IsEnabled NVARCHAR(10)
    )
AS 
    DECLARE @sql NVARCHAR(4000)
    SET @sql = 'UPDATE PE_WordReplaceItem SET IsEnabled = ' + @IsEnabled
        + ' WHERE ItemID IN (' + @ItemID + ')'
    EXEC(@sql)
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_AD_ADZone_GetExportList]'
GO
ALTER PROCEDURE [dbo].[PR_AD_ADZone_GetExportList] ( @ZoneID NVARCHAR(250) )
AS  
    EXEC(' SELECT ZoneID,ZoneName,ZoneJSName,ZoneIntro,ZoneType,DefaultSetting,ZoneSetting,ZoneWidth,ZoneHeight,ShowType,Active,UpdateTime FROM PE_AdZone WHERE ZoneID in ('+@ZoneID+')  ORDER BY ZoneID ASC')
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_GetList]'
GO
ALTER PROCEDURE [dbo].[PR_Common_GetList]
    (
      @StartRows INT ,
      @PageSize INT ,
      @SortColumn VARCHAR(1000) ,
      @StrColumn VARCHAR(1000) ,
      @Sorts VARCHAR(100) ,
      @Filter VARCHAR(4000) ,
      @TableName VARCHAR(1000) ,
      @Total INT OUTPUT
    )
AS 
    SET NOCOUNT ON

    BEGIN
        IF @StartRows <= 0 
            SET @StartRows = 0

        DECLARE @equalOperator CHAR(2)
        IF @StartRows = 0 
            BEGIN
                SET @equalOperator = '='
                SET @StartRows = 1
            END
        ELSE 
            SET @equalOperator = ''

/*Set sorting variables.*/
        DECLARE @operator CHAR(2)
        DECLARE @strSortColumn VARCHAR(10)
	
        IF CHARINDEX('DESC', @Sorts) > 0 
            BEGIN
                SET @strSortColumn = REPLACE(@Sorts, 'DESC', '')
                SET @operator = '<' + @equalOperator
            END
        ELSE 
            BEGIN
                IF CHARINDEX('ASC', @Sorts) = 0 
                    SET @strSortColumn = REPLACE(@Sorts, 'ASC', '')
                SET @operator = '>' + @equalOperator
            END

        DECLARE @strFilter VARCHAR(4000)
        DECLARE @strSimpleFilter VARCHAR(4000)

        IF @Filter IS NOT NULL
            AND @Filter != '' 
            BEGIN
                SET @strFilter = ' WHERE ' + @Filter + ' '
                SET @strSimpleFilter = ' AND ' + @Filter + ' '
            END
        ELSE 
            BEGIN	
                SET @strFilter = ''
                SET @strSimpleFilter = ''
            END

        IF @Total <= 0
            OR @Total IS NULL 
            BEGIN	
                DECLARE @Sql NVARCHAR(4000)
                SET @Sql = N'SELECT @Total=Count(*) FROM ' + @TableName
                    + @strFilter
                EXEC sp_executesql @Sql, N'@Total Int Out', @Total OUT 
            END

        IF @PageSize <= 0 
            SET @PageSize = @Total

        EXEC(
        'DECLARE @SortId int ' +
        'SET ROWCOUNT '+ @StartRows + '
        SELECT @SortId = '+ @SortColumn + ' FROM '+ @TableName + @strFilter +' ORDER BY '+ @SortColumn + ' ' +@Sorts +'
        SET ROWCOUNT '+ @PageSize  + '
        SELECT '+ @StrColumn +' FROM '+ @TableName + ' 
        WHERE '+ @SortColumn  + @operator +' @SortId '+ @strSimpleFilter + ' ORDER BY '+ @SortColumn + ' '+ @Sorts + ''
        )


        RETURN @Total
    END

    SET ROWCOUNT 0

    SET NOCOUNT OFF
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_CommonPage]'
GO
ALTER PROCEDURE [dbo].[PR_CommonPage]
    @StrSql NTEXT , --查询字符串
    @CurrentPage INT , --第N页
    @PageSize INT , --每页行数
    @Total INT OUTPUT
AS 
    SET NOCOUNT ON

    DECLARE @P1 INT , --P1是游标的id
        @Rowcount INT
    EXEC sp_cursoropen @P1 OUTPUT, @StrSql, @scrollopt = 1, @ccopt = 1,
        @Rowcount = @Total OUTPUT
    SET @CurrentPage = ( @CurrentPage - 1 ) * @PageSize + 1
    EXEC sp_cursorfetch @P1, 16, @CurrentPage, @PageSize 
    EXEC sp_cursorclose @P1
    RETURN @Total

    SET NOCOUNT OFF
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_ModelTemplates_GetExportList]'
GO
ALTER PROCEDURE [dbo].[PR_Contents_ModelTemplates_GetExportList]
    (
      @TemplateID NVARCHAR(250)
    )
AS 
    EXEC(' SELECT * FROM PE_ModelTemplates WHERE TemplateID IN ('+@TemplateID+')  ORDER BY TemplateID ASC')
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_ClientAnnal_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_ClientAnnal_Delete]
    (
      @condition VARCHAR(1000)
    )
AS 
    DECLARE @sql VARCHAR(1100)
    SET @sql = 'delete from pe_clientannal where clientannalid in('
        + @condition + ')'
    EXEC (@sql)
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_RemindItem_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Crm_RemindItem_Delete]
    (
      @condition VARCHAR(1000)
    )
AS 
    BEGIN
        DECLARE @sql VARCHAR(1100)
        SET @sql = 'delete from pe_remindItem where remindid in(' + @condition
            + ')'
        EXEC (@sql)
    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_Producer_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Producer_Delete]
    (
      @ProducerId NVARCHAR(4000)
    )
AS 
    DECLARE @sql NVARCHAR(4000)
    SET @sql = 'DELETE PE_Producer WHERE ProducerID IN (' + @ProducerId + ')'
    EXEC(@sql)
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_Product_Update]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Product_Update]
    (
      @Unit NVARCHAR(20) ,
      @Stocks INT ,
      @Weight FLOAT ,
      @SalePromotionType INT ,
      @MinNumber INT ,
      @PresentNumber INT ,
      @PresentId INT ,
      @PresentExp INT ,
      @PresentMoney DECIMAL ,
      @PresentPoint INT ,
      @EliteLevel INT ,
      @UpdateTime DATETIME ,
      @TemplateFile NVARCHAR(255) = '' ,
      @TaxRate FLOAT ,
      @GeneralIdList NVARCHAR(1000) ,
      @NodeIdList NVARCHAR(1000) ,
      @ModelIdList NVARCHAR(1000)
    )
AS 
    BEGIN
        DECLARE @sql NVARCHAR(1000)
        DECLARE @sqlChild NVARCHAR(1000)
        DECLARE @FieldStr NVARCHAR(20)
	
        SET @sql = 'Select P.ID From PE_CommonModel M inner join PE_CommonProduct P on M.ItemID=P.ProductID and M.TableName=P.TableName'
        IF @GeneralIdList != '' 
            BEGIN
                SET @sql = @sql + ' where M.GeneralID IN(' + @GeneralIdList
                    + ')'
                SET @sqlChild = ' (' + @GeneralIdList + ')'
                SET @FieldStr = 'GeneralID'
            END
        ELSE 
            BEGIN
                SET @FieldStr = 'NodeID'
                SET @sql = @sql + ' Where M.NodeID IN(' + @NodeIdList + ')'
                SET @sqlChild = ' (' + @NodeIdList + ')'
                IF @ModelIdList != '' 
                    BEGIN
                        SET @sql = @sql + ' And ModelID IN(' + @ModelIdList
                            + ')'
                        SET @sqlChild = @sqlChild + ' And ModelID IN('
                            + @ModelIdList + ')'
                    END
                ELSE 
                    BEGIN
                        SET @sql = @sql
                            + ' And ModelID IN(Select ModelID From PE_ContentModel Where IsEShop = 1)'
                        SET @sqlChild = @sqlChild
                            + ' And ModelID IN(Select ModelID From PE_ContentModel Where IsEShop = 1)'
                    END				
            END
		--declare @t table(id int)
		--insert into @t exec(@sql)
        DECLARE @UpdateSql NVARCHAR(1000)
        SET @UpdateSql = 'Update PE_CommonProduct Set ID = ID'
		
        IF @Unit != '' 
            SET @UpdateSql = @UpdateSql + ',Unit=''' + @Unit + ''''
        IF @Stocks != 0 
            SET @UpdateSql = @UpdateSql + ',Stocks='
                + CONVERT(CHAR(10), @Stocks)
        IF @Weight != 0 
            SET @UpdateSql = @UpdateSql + ',Weight='
                + CONVERT(CHAR(10), @Weight)
        IF @SalePromotionType != 0 
            SET @UpdateSql = @UpdateSql + ',SalePromotionType='
                + CONVERT(CHAR(10), @SalePromotionType)
        IF @MinNumber != 0 
            SET @UpdateSql = @UpdateSql + ',MinNumber='
                + CONVERT(CHAR(10), @MinNumber)
        IF @PresentId != 0 
            SET @UpdateSql = @UpdateSql + ',PresentId='
                + CONVERT(CHAR(10), @PresentId)
        IF @PresentExp != 0 
            SET @UpdateSql = @UpdateSql + ',PresentExp='
                + CONVERT(CHAR(10), @PresentExp)
        IF @PresentMoney != 0 
            SET @UpdateSql = @UpdateSql + ',PresentMoney='
                + CONVERT(CHAR(10), @PresentMoney)
        IF @PresentPoint != 0 
            SET @UpdateSql = @UpdateSql + ',PresentPoint='
                + CONVERT(CHAR(10), @PresentPoint)
        IF @TaxRate != 0 
            SET @UpdateSql = @UpdateSql + ',TaxRate='
                + CONVERT(CHAR(10), @TaxRate)

        SET @UpdateSql = @UpdateSql + 'Where ID IN (' + @sql + ')'
		
        EXEC(@UpdateSql)
		
        DECLARE @UpdateSql1 NVARCHAR(1000)
        SET @UpdateSql1 = 'Update PE_CommonModel Set GeneralID = GeneralID'
        IF @EliteLevel != 0 
            SET @UpdateSql1 = @UpdateSql1 + ',EliteLevel='
                + CONVERT(CHAR(10), @EliteLevel)
        IF @UpdateTime != '' 
            SET @UpdateSql1 = @UpdateSql1 + ',UpdateTime='''
                + CONVERT(CHAR(10), @UpdateTime) + ''''
        IF @TemplateFile != '' 
            SET @UpdateSql1 = @UpdateSql1 + ',TemplateFile=''' + @TemplateFile
                + ''''
			
        EXEC(@UpdateSql1+' WHERE	'+@FieldStr+' IN'+@sqlChild)
    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_SaleCount_GetList]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_SaleCount_GetList]
    (
      @StartRows INT ,
      @PageSize INT ,
      @InfoType VARCHAR(100) ,
      @SortColumn VARCHAR(1000) ,
      @StrColumn VARCHAR(1000) ,
      @Sorts VARCHAR(100) ,
      @Filter VARCHAR(1000) ,
      @TableName VARCHAR(1000) ,
      @Total INT OUTPUT
    )
AS 
    SET NOCOUNT ON

    BEGIN
        IF @StartRows <= 0 
            SET @StartRows = 0
        SET @StartRows = @StartRows + 1
        IF @PageSize < 1 
            SET @PageSize = 10
        DECLARE @operator CHAR(2)
        DECLARE @strSortColumn VARCHAR(10)
	
        IF CHARINDEX('DESC', @Sorts) > 0 
            BEGIN
                SET @strSortColumn = REPLACE(@Sorts, 'DESC', '')
                SET @operator = '<='
            END
        ELSE 
            BEGIN
                IF CHARINDEX('ASC', @Sorts) = 0 
                    SET @strSortColumn = REPLACE(@Sorts, 'ASC', '')
                SET @operator = '>='
            END

        DECLARE @strFilter VARCHAR(1000)
        DECLARE @strSimpleFilter VARCHAR(1000)

        IF @InfoType = 'NoDeliver' 
            SET @Filter = ' I.OrderID in (select OrderID from PE_Orders where (MoneyReceipt>=MoneyTotal or MoneyReceipt>0) And DeliverStatus<>-1 AND DeliverStatus<=4) '
                + @Filter
        ELSE 
            SET @Filter = ' I.OrderID in (select OrderID from PE_Orders where MoneyReceipt>=MoneyTotal) '
                + @Filter

        IF @Filter IS NOT NULL
            AND @Filter != '' 
            BEGIN
                SET @strFilter = ' WHERE ' + @Filter + ' '
                SET @strSimpleFilter = ' AND ' + @Filter + ' '
            END
        ELSE 
            BEGIN	
                SET @strFilter = ''
                SET @strSimpleFilter = ''
            END


        EXEC(
        'DECLARE @SortId int ' +
        'SET ROWCOUNT '+ @StartRows + '
        SELECT @SortId = '+ @SortColumn + ' FROM '+ @TableName + @strFilter +' GROUP BY I.ProductID,I.TableName ORDER BY '+ @SortColumn + ' ' +@Sorts +'
        SET ROWCOUNT '+ @PageSize  + '
        SELECT '+ @StrColumn +' FROM '+ @TableName + @strFilter + ' 
        GROUP BY I.ProductID,I.TableName Having ' + @SortColumn  + @operator +' @SortId  ORDER BY '+ @SortColumn +' '+ @Sorts + ''
        )


        DECLARE @Sql NVARCHAR(4000)

        SET @Sql = N'SELECT @Total=Count(DISTINCT I.ProductID,I.TableName) FROM '
            + @TableName + @strFilter
        EXEC sp_executesql @Sql, N'@Total Int Out', @Total OUT 
        RETURN @Total
    END

    SET ROWCOUNT 0

    SET NOCOUNT OFF
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_ShoppingCart_GetList]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_ShoppingCart_GetList]
    (
      @StartRows INT ,
      @PageSize INT ,
      @SortColumnDbType VARCHAR(100) , --排序字段的数值类型 如 Int ,DateTime等
      @SortColumn VARCHAR(1000) ,
      @StrColumn VARCHAR(1000) ,
      @Sorts VARCHAR(100) ,
      @Filter VARCHAR(1000) ,
      @TableName VARCHAR(1000) ,
      @Total INT OUTPUT
    )
AS 
    SET NOCOUNT ON

    BEGIN
        IF @StartRows <= 0 
            SET @StartRows = 0

        IF @PageSize < 1 
            SET @PageSize = 10

        DECLARE @equalOperator CHAR(2)
        IF @StartRows = 0 
            BEGIN
                SET @equalOperator = '='
                SET @StartRows = 1
            END
        ELSE 
            SET @equalOperator = ''
        DECLARE @operator CHAR(2)
        DECLARE @strSortColumn VARCHAR(10)
	
        IF CHARINDEX('DESC', @Sorts) > 0 
            BEGIN
                SET @strSortColumn = REPLACE(@Sorts, 'DESC', '')
                SET @operator = '<' + @equalOperator
            END
        ELSE 
            BEGIN
                IF CHARINDEX('ASC', @Sorts) = 0 
                    SET @strSortColumn = REPLACE(@Sorts, 'ASC', '')
                SET @operator = '>' + @equalOperator
            END

        DECLARE @strFilter VARCHAR(1000)
        DECLARE @strSimpleFilter VARCHAR(1000)

        IF @Filter IS NOT NULL
            AND @Filter != '' 
            BEGIN
                SET @strFilter = ' WHERE ' + @Filter + ' '
                SET @strSimpleFilter = ' AND ' + @Filter + ' '
            END
        ELSE 
            BEGIN	
                SET @strFilter = ''
                SET @strSimpleFilter = ''
            END

        DECLARE @Sql NVARCHAR(4000)
        SET @Sql = N'SELECT @Total=Count(DISTINCT CartID) FROM ' + @TableName
            + @strFilter
        EXEC sp_executesql @Sql, N'@Total Int Out', @Total OUT 

        EXEC(
        'DECLARE @SortId '+ @SortColumnDbType +
        'DECLARE @StartId int '+
        'SET ROWCOUNT '+ @StartRows + '
        SELECT @SortId = '+ @SortColumn + ' FROM '+ @TableName + @strFilter +' GROUP BY CartID ORDER BY MIN(CartItemID) ' +@Sorts +'
        SELECT @StartId = CartItemID FROM '+ @TableName + ' WHERE CartID = @SortId
        SET ROWCOUNT '+ @PageSize  + '
        SELECT '+ @StrColumn +' FROM '+ @TableName + ' 
        WHERE CartItemID '+ @operator +' @StartId '+ @strSimpleFilter + ' GROUP BY CartID,CartItemID ORDER BY MIN(CartItemID) '+ @Sorts + ''
        )

        RETURN @Total
    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_Statistics_GetList]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Statistics_GetList]
    (
      @StartRows INT ,
      @PageSize INT ,
      @SortColumn VARCHAR(1000) ,
      @StrColumn VARCHAR(1000) ,
      @Sorts VARCHAR(100) ,
      @Filter VARCHAR(1000) ,
      @TableName VARCHAR(1000) ,
      @ID VARCHAR(255) , --需要排序的不重复的ID号
      @Group VARCHAR(255) ,
      @Total INT OUTPUT
    )
AS 
    IF @PageSize = 0 
        BEGIN
            SET @PageSize = 20
        END

    DECLARE @strFilter VARCHAR(1000)
    DECLARE @strSimpleFilter VARCHAR(1000)

    IF @Filter IS NOT NULL
        AND @Filter != '' 
        BEGIN
            SET @strFilter = ' WHERE ' + @Filter + ' '
            SET @strSimpleFilter = ' AND ' + @Filter + ' '
        END
    ELSE 
        BEGIN	
            SET @strFilter = ''
            SET @strSimpleFilter = ''
        END


    DECLARE @Sql NVARCHAR(4000)

    SET @Sql = N'SELECT @Total=(Count(DISTINCT ' + @ID + ')) FROM '
        + @TableName + @strFilter 
    EXEC sp_executesql @Sql, N'@Total Int Out', @Total OUT 

    DECLARE @Str NVARCHAR(4000)
    IF @StartRows = 0 
        BEGIN
            SET @Str = 'SELECT TOP ' + CAST(@PageSize AS VARCHAR(20)) + ' '
                + @StrColumn + ' FROM 
		' + @TableName + @strFilter + ' ' + @Group + ' ORDER BY '
                + @SortColumn + ' ' + @Sorts
        END
    ELSE 
        BEGIN
            SET @Str = 'SELECT TOP ' + CAST(@PageSize AS VARCHAR(20)) + ' '
                + @StrColumn + ' FROM 
		' + @TableName + '  WHERE ' + @ID + ' NOT IN (SELECT TOP '
                + CAST(( @StartRows ) AS VARCHAR(20)) + ' ' + @ID + ' FROM '
                + @TableName + @strFilter + ' ' + @Group + ' ORDER BY '
                + @SortColumn + ' ' + @Sorts + ') ' + @strSimpleFilter + ' '
                + @Group + ' ORDER BY ' + @SortColumn + ' ' + @Sorts
        END
    EXEC sp_ExecuteSql @Str

    RETURN @Total
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_Trademark_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_Shop_Trademark_Delete]
    (
      @TrademarkId NVARCHAR(4000)
    )
AS 
    DECLARE @sql NVARCHAR(4000)
    SET @sql = 'DELETE PE_Trademark WHERE TrademarkID IN (' + @TrademarkId
        + ')'
    EXEC(@sql)
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_BatchMove]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_Users_BatchMove]
    (
      @UserType INT = 1 ,
      @GroupId NVARCHAR(500) = '' ,
      @UserId NVARCHAR(4000) = '' ,
      @UserName NVARCHAR(255) = '' ,
      @StartUserId INT = 0 ,
      @EndUserId INT = 0 ,
      @BatchUserGroupId NVARCHAR(500) = ''
    )
AS 
    BEGIN
        SET NOCOUNT OFF
        IF ( @UserType = 1 ) 
            BEGIN
                EXEC('Update PE_Users set GroupID= ' + @GroupId +' Where UserID  in (' + @UserId + ')')
            END
        ELSE 
            IF ( @UserType = 2 ) 
                BEGIN
                    EXEC('Update PE_Users set GroupID= ' + @GroupId +' Where UserName  in (''' + @UserName + ''')')	
                END
            ELSE 
                IF ( @UserType = 3 ) 
                    BEGIN
                        EXEC('Update PE_Users set GroupID= ' + @GroupId +' Where UserId between ' + @StartUserId + ' and ' + @EndUserId)	
                    END
                ELSE 
                    IF ( @UserType = 4 ) 
                        BEGIN
                            EXEC('Update PE_Users set GroupID= ' + @GroupId +' Where GroupID  in (' + @BatchUserGroupId + ')')	
                        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_UserManage_Users_Delete]'
GO
ALTER PROCEDURE [dbo].[PR_UserManage_Users_Delete] ( @UserId NVARCHAR(4000) )
AS 
    DECLARE @sql NVARCHAR(4000)
    SET @sql = 'DELETE PE_Users WHERE UserID IN (' + @UserId + ')'
    EXEC(@sql)
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
