CREATE TRIGGER [dbo].[tgUser_Error] ON [dbo].[DrugH]
    FOR UPDATE
AS
    IF ( SELECT @@RowCount
       ) > 1
        RETURN
    DECLARE @strTmp_New VARCHAR(20)
    DECLARE @strTmp_Old VARCHAR(20)
    SELECT  @strTmp_Old = ISNULL(User_Error, REPLICATE('0', 20))
    FROM    Deleted
    SET @strTmp_New = REPLICATE('0', 20) 

    SELECT  @strTmp_New = CASE WHEN Ins.Sazman_Code != del.Sazman_Code
                               THEN '1'
                               ELSE SUBSTRING(@strTmp_Old, 1, 1)
                          END ,
            @strTmp_New = @strTmp_New
            + CASE WHEN Ins.PT_Code != del.PT_Code THEN '1'
                   ELSE SUBSTRING(@strTmp_Old, 2, 1)
              END ,
            @strTmp_New = @strTmp_New
            + CASE WHEN Ins.Note_Code != del.Note_Code THEN '1'
                   ELSE SUBSTRING(@strTmp_Old, 3, 1)
              END ,
            @strTmp_New = @strTmp_New
            + CASE WHEN Ins.Resive_Name != del.Resive_Name THEN '1'
                   ELSE SUBSTRING(@strTmp_Old, 4, 1)
              END ,
            @strTmp_New = @strTmp_New
            + CASE WHEN Ins.Page_No != del.Page_No THEN '1'
                   ELSE SUBSTRING(@strTmp_Old, 5, 1)
              END ,
            @strTmp_New = @strTmp_New
            + CASE WHEN Ins.Tarikh_Noskheh != del.Tarikh_Noskheh THEN '1'
                   ELSE SUBSTRING(@strTmp_Old, 6, 1)
              END ,
            @strTmp_New = @strTmp_New
            + CASE WHEN Ins.Tarikh_Etebar != del.Tarikh_Etebar THEN '1'
                   ELSE SUBSTRING(@strTmp_Old, 7, 1)
              END ,
            @strTmp_New = @strTmp_New
            + CASE WHEN Ins.Tarikh_Paziresh != del.Tarikh_Paziresh THEN '1'
                   ELSE SUBSTRING(@strTmp_Old, 8, 1)
              END ,
            @strTmp_New = @strTmp_New
            + CASE WHEN Ins.Nezam_No != del.Nezam_No THEN '1'
                   ELSE SUBSTRING(@strTmp_Old, 9, 1)
              END ,
            @strTmp_New = @strTmp_New
            + CASE WHEN Ins.Del_Flag != del.Del_Flag THEN '1'
                   ELSE SUBSTRING(@strTmp_Old, 10, 1)
              END
    FROM    Inserted Ins ,
            Deleted del

    SET @strTmp_New = @strTmp_New + REPLICATE('0', 10)
    IF @strTmp_New != REPLICATE('0', 20)
        BEGIN
            SET @strTmp_New = STUFF(@strTmp_New, 18, 3,
                                    CAST(Manager.dbo.SessionUser(@@SPID,1) AS VARCHAR(3)))
            UPDATE  DrugH
            SET     User_Error = @strTmp_New
            WHERE   Id_Havaleh = ( SELECT   Id_Havaleh
                                   FROM     Inserted
                                 )
        END

