SET NUMERIC_ROUNDABORT OFF
GO
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors')) DROP TABLE #tmpErrors
GO
CREATE TABLE #tmpErrors (Error int)
GO
SET XACT_ABORT ON
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO
BEGIN TRANSACTION
GO
PRINT N'Altering [dbo].[SplitID]'
GO
ALTER  FUNCTION  [dbo].[SplitID] 
(  
     @String  nvarchar(max),  --格式：“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].[PE_Client]'
GO
ALTER TABLE [dbo].[PE_Client] ADD
[MaxOrderMoney] [money] NULL
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[PR_Crm_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 INT,
	  @UpdateOwnerTime DATETIME,
	  @MaxOrderMoney MONEY
    )
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,
			UpdateOwnerTime = @UpdateOwnerTime,
			MaxOrderMoney=@MaxOrderMoney
    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_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 INT,
	  @UpdateOwnerTime DATETIME,
	  @MaxOrderMoney MONEY
	
    )
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] ,
			  [UpdateOwnerTime],
			  [MaxOrderMoney]
            )
    VALUES  ( @ClientID ,
              @ParentID ,
              @ClientNum ,
              @ClientType ,
              @ClientName ,
              @ShortedForm ,
              @Area ,
              @ClientField ,
              @ValueLevel ,
              @CreditLevel ,
              @Importance ,
              @ConnectionLevel ,
              @GroupID ,
              @SourceType ,
              @PhaseType ,
              @Remark ,
              @CreateTime ,
              @UpdateTime ,
              @Owner ,
              GETDATE() ,
              @IsBusiness,
			  @UpdateOwnerTime,
			  @MaxOrderMoney
            )
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
