Skip to content

Add support for OUTPUT Clause in TSQL (MS SQL) Merge statements #1789

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

Closed
dilovancelik opened this issue Apr 1, 2025 · 1 comment · Fixed by #1790
Closed

Add support for OUTPUT Clause in TSQL (MS SQL) Merge statements #1789

dilovancelik opened this issue Apr 1, 2025 · 1 comment · Fixed by #1790

Comments

@dilovancelik
Copy link
Contributor

dilovancelik commented Apr 1, 2025

Add the end of a merge statement in T-SQL we can use an OUTPUT clause to write the changed rows to a table variable. This is often used in Data Warehouse workloads were we would like to track slowly changing dimensions.

A merge statement with the output clause could look like this:

MERGE dso.products AS t
USING dsi.products AS s
	ON s.ProductID = t.ProductID
WHEN MATCHED 
	AND NOT (t.[ProductName] = s.[ProductName] OR (ISNULL(t.[ProductName], s.[ProductName]) IS NULL))
THEN
	UPDATE SET t.[ProductName] = s.[ProductName]

WHEN NOT MATCHED BY TARGET THEN
INSERT ([ProductID]
           ,[ProductName])
     VALUES
           (s.[ProductID]
           ,s.[ProductName])

WHEN NOT MATCHED BY SOURCE  THEN
	DELETE

OUTPUT $action, deleted.* INTO dsi.temp_products;

Currently this fails with the error ParserError("Expected: WHEN, found: OUTPUT at Line: 112, Column: 1").

I will try to create a PR solving this issue, but any pointers on a good approach would be highly appreciated.

Thanks for the awesome crate btw.

@dilovancelik
Copy link
Contributor Author

I have added PR 1789 which add's the functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant