Its possibleSee below for mockup of your scenario and solution using TVPs and MERGEfirst create a table type for use by TVPCREATE TYPE [dbo].[tblStats] AS TABLE([ID] [int] IDENTITY(1,1) NOT NULL,[OwnerID] [int] NOT NULL,[StatID] [int] NOT NULL,[StatValue] [int] NOT NULL)Now create your procedure based on thisCREATE PROCEDURE dbo.UpdateOrInsertStats@InputTable tblStats READONLYAS BEGINSET NOCOUNT ON; MERGE DestinationTable AS tUSING @InputTable AS sON s.OwnerID=t.OwnerIDAND s.StatID=t.StatIDWHEN MATCHED THEN UPDATE SET t.StatValue=s.StatValueWHEN NOT MATCHED THEN INSERT (OwnerID,StatID,StatValue) VALUES (s.OwnerID,s.StatID,s.StatValue);END;GONow lets test the procedure with some sample data --Your destination table created with some test valuesCREATE TABLE DestinationTable([ID] [int] IDENTITY(1,1) NOT NULL,[OwnerID] [int] NOT NULL,[StatID] [int] NOT NULL,[StatValue] [int] NOT NULL)GOINSERT INTO DestinationTableVALUES (1022,121,300),(1025,113,430),(1028,118,332),(1014,129,123),(1022,231,456)SELECT * FROM DestinationTableoutput--------------------------------ID OwnerID StatID StatValue1 1022 121 3002 1025 113 4303 1028 118 3324 1014 129 1235 1022 231 456Now lets create an input tableDECLARE @InpTable tblStatsINSERT INTO @InpTableVALUES (1022,121,344),(1031,114,324),(1028,118,400),(1009,110,566),(1022,231,332)SELECT * FROM @InpTableoutput--------------------------------ID OwnerID StatID StatValue1 1022 121 3442 1031 114 3243 1028 118 4004 1009 110 5665 1022 231 332please note i've included 3 already existing values pairs with different statvalues as well as two new pairsso once we do merge we get two new rows in destination and three others would have updated stat valuenow lets call procedureDECLARE @InpTable tblStatsINSERT INTO @InpTableVALUES (1022,121,344),(1031,114,324),(1028,118,400),(1009,110,566),(1022,231,332)SELECT * FROM DestinationTableEXEC UpdateOrInsertStats @InpTableSELECT * FROM DestinationTableoutput-----------------------------before mergeID OwnerID StatID StatValue1 1022 121 3002 1025 113 4303 1028 118 3324 1014 129 1235 1022 231 456after merge1 1022 121 3442 1025 113 4303 1028 118 4004 1014 129 1235 1022 231 3326 1031 114 3247 1009 110 566
the ones in blue have updated values and last two rows have been newly inserted------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/