Capture Source count,update count,inserted count in variable using merge

  • Hi ,

    Below sp is working fine updating and inserting record properly.

    But now I want to track the source record , updated record for logging in my variable.

    please suggest me the same

    alter Procedure SP_Archive_using_merge

    AS

    BEGIN

    SET NOCOUNT ON

    Declare @Source_RowCount int

    Declare @New_RowCount int

    Declare @updated_Rowcount int

    DECLARE @TimeIn SMALLDATETIME

    DECLARE @LatestVersion INT

    SET NOCOUNT ON

    ---BBxKey and Hash value of all the source columns are derived in source query itself--

    select @TimeIn=getdate()

    Select @LatestVersion=1

    MERGE Archive.dbo.ArchiveBBxCemxr as TARGET

    USING

    (select *,cast(SUBSTRING(Col001,1,10) as varchar(100)) BBxKey,

    HashBytes('MD5', CAST(CHECKSUM(Col001,Col002,Col003,Col004,Col005,Col006,Col007) AS VARCHAR(MAX))) RowChecksum

    from dbo.ImportBBxCemxr

    where Col001 IS NOT NULL)as SOURCE

    ON (SOURCE.Col001 = target.BBxKey )

    AND (target.LatestVersion = 1 OR target.LatestVersion IS NULL)

    WHEN MATCHED AND (source.RowChecksum <> TARGET.RowChecksum) THEN

    UPDATE SET

    TARGET.TimeIn = @TimeIn,

    TARGET.BBXKey=SOURCE.BBXKey,

    TARGET.RowChecksum=SOURCE.RowChecksum,

    TARGET.Col001=SOURCE.Col001,

    TARGET.Col002=SOURCE.Col002,

    TARGET.Col003=SOURCE.Col003,

    TARGET.Col004=SOURCE.Col004,

    TARGET.Col005=SOURCE.Col005,

    TARGET.Col006=SOURCE.Col006,

    TARGET.Col007=SOURCE.Col007,

    TARGET.LatestVersion=@LatestVersion

    WHEN NOT MATCHED THEN --For New record

    Insert (TimeIn,BBXKey,RowChecksum,Col001,Col002,Col003,Col004,Col005,Col006,Col007,LatestVersion)

    values(getdate(),SOURCE.BBXKey,SOURCE.RowChecksum,SOURCE.Col001,SOURCE.Col002,SOURCE.Col003,SOURCE.Col004,SOURCE.Col005,SOURCE.Col006,SOURCE.Col007,@LatestVersion);

    Select @New_RowCount=@@ROWCOUNT

    end

  • Use the OUTPUT clause and the $action column https://msdn.microsoft.com/en-us/library/ms177564.aspx you can then aggregate over that.

    Note: I have found that it is a real performance hit and only do this if you really really really need to know 🙂



    Clear Sky SQL
    My Blog[/url]

  • Dave Ballantyne (2/12/2015)


    Use the OUTPUT clause and the $action column https://msdn.microsoft.com/en-us/library/ms177564.aspx you can then aggregate over that.

    Note: I have found that it is a real performance hit and only do this if you really really really need to know 🙂

    I agree about the performance hit. If you really do need the counts and performance is an issue for you, I suggest that you consider doing the inserts and updates separately and using the old-fashioned @@RowCount method.

    Use a transaction to keep things atomic, if your needs require it. Performance will still be better – in my experience.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Quick thought, consider changing the hashing part of the statement as it will not produce unique hash values for every unique combination of input values.

    😎

    More details here.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply