-
Notifications
You must be signed in to change notification settings - Fork 252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merge temp table as source table #959
Comments
Hey 👋 AFAIK, the temporary table has to be defined first, either inside the same SQL procedure/function OR within the same request to database ( Then you can add the temporary table to database context before invoking the 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"),
})) Lemme know if |
When trying to insert value into temp table it throws this error RequestError: Invalid object name '#temp_table'.
|
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: