Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to get the rowcount for rows inserted and updated in merge procedure

$
0
0
Hi All,

I have a stored procedure which is below :-

if we see in the end , I am setting the rows inserted = row count which gives me the row count for any rows inserted and as this being a merge procedure i want to have the row count for updated as well.
Is there any way i have both the row counts for any inserts and updates in a single merge?


ALTER PROCEDURE [JJill].[pODS_Mrg_tODS_StoreTrafficException]
    @RowsProcessed INT OUTPUT,
    @RowsInserted INT OUTPUT,
    @RowsUpdated INT OUTPUT
AS
BEGIN
    BEGIN TRANSACTION
        SET @RowsUpdated = 0
        SET @RowsProcessed = ( SELECT COUNT(0) FROM ODS.JJill.tODS_StoreTraffic)

        MERGE  JJill.tODS_StoreTrafficException AS tODS_StoreTrafficException
        USING (
                SELECT
                LocationNum                                       AS LocationNum
                FROM    JJill.tODS_StoreTraffic
                ) AS tODS_StoreTraffic                
ON             tODS_StoreTraffic.LocationNum                                       =tODS_StoreTrafficException.LocationNu
WHEN MATCHED  
THEN UPDATE SET SoftDeleteFlag = 1        
WHEN NOT MATCHED
THEN INSERT (
                    LocationNum,
                    SoftDeleteFlag
                )
        VALUES (
                    LocationNum,
                    NumVisits
                );

        SET @RowsInserted = @@ROWCOUNT    
    COMMIT TRANSACTION
END


Thanks

Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>