merge-statement

Performance issue in merge statement

空扰寡人 提交于 2021-01-28 06:05:33
问题 I have a merge statement like below MERGE DESTINATION AS DST USING ( SELECT <Some_Columns> FROM TABLEA WITH(NOLOCK) INNER JOIN TableB ..... ) AS SRC ON( <some conditions> ) WHEN MATCHED THEN UPDATE SET column1 = src.column1 ............... ,Modified_By = @PackageName ,Modified_Date = GETDATE() WHEN NOT MATCHED THEN INSERT (<Some_Columns>) VALUES(<Some_Columns>) OUTPUT $action, inserted.key'inserted' INTO @tableVar ; For the first set of records (around 300,000) it is working perfectly and

Performance issue in merge statement

五迷三道 提交于 2021-01-28 05:57:52
问题 I have a merge statement like below MERGE DESTINATION AS DST USING ( SELECT <Some_Columns> FROM TABLEA WITH(NOLOCK) INNER JOIN TableB ..... ) AS SRC ON( <some conditions> ) WHEN MATCHED THEN UPDATE SET column1 = src.column1 ............... ,Modified_By = @PackageName ,Modified_Date = GETDATE() WHEN NOT MATCHED THEN INSERT (<Some_Columns>) VALUES(<Some_Columns>) OUTPUT $action, inserted.key'inserted' INTO @tableVar ; For the first set of records (around 300,000) it is working perfectly and

Updating inserted record within MERGE statement in SQL Server 2008 R2

时光毁灭记忆、已成空白 提交于 2019-12-25 08:49:29
问题 I have following code in my SQL Server 2008 R2 stored procedure. In that stored procedure, I am copying one city to another city with it's family and persons. Here I maintain family's source and target id in @FamilyIdMap . left column indicates the codes line no. -- Copy Person 1> DECLARE @PersonIdMap table (TargetId int, SourceId int) 2> MERGE Person as PersonTargetTable 3> USING (SELECT PersonID, FamilyID, PersonName, ParentID FROM Person 4> WHERE FamilyID in (SELECT FamilyID from Family

SQL Server MERGE statement and ORDER BY clause

自古美人都是妖i 提交于 2019-12-12 11:34:25
问题 I would like to write a MERGE statement to pick TOP 10 rows from a large table by using ORDER BY clause and update it’s one of the column values. MERGE statement allows me to pick TOP 10 rows but I could not put ORDER BY clause anywhere. MERGE TOP(10) StudentAllocation AS SA USING (SELECT @sub_id AS subId) AS TSA ON SA.sub_id = TSA.subId WHEN MATCHED THEN UPDATE SET SA.exam_batch = 1); 回答1: You can use a table expression as both the source and target for the MERGE . WITH SA AS ( SELECT TOP(10