You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SQL Statement MERGE INTO MainTable AS Target USING #TempData AS Source ON Target.ID = Source.ID WHEN MATCHED THEN UPDATE SET Target.Name = Source.Name WHEN NOT MATCHED THEN INSERT (ID, Name) VALUES (Source.ID, Source.Name);
Dialect: MSSQL
The text was updated successfully, but these errors were encountered:
AFAIK, the temporary table has to be defined first, either inside the same SQL procedure/function OR within the same request to database (${query that defines the temp table}; ${merge into query}).
Then you can add the temporary table to database context before invoking the merge query as follows:
db.withTables<{"#tempData": {ID: string;itemCode: string|null;Name: string;};}>().mergeInto("MainTable as Target").using("#tempData as Source","Target.ID","Source.ID").whenMatched().thenUpdateSet((eb)=>({Name: eb.ref("Source.Name"),})).whenNotMatched().thenInsertValues((eb)=>({ID: eb.ref("Source.ID"),Name: eb.ref("Source.Name"),}))
Is there a way to use temp table as the source table in merge statement something like this?
await db.mergeInto('mainTable').using('tempData', 'mainTable.itemCode', 'tempData.itemCode').whenMatched()......
SQL Statement
MERGE INTO MainTable AS Target USING #TempData AS Source ON Target.ID = Source.ID WHEN MATCHED THEN UPDATE SET Target.Name = Source.Name WHEN NOT MATCHED THEN INSERT (ID, Name) VALUES (Source.ID, Source.Name);
Dialect: MSSQL
The text was updated successfully, but these errors were encountered: