Skip to content
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

BUG: MERGE statement not parsing correctly with target alias #129

Open
SGStino opened this issue Jul 3, 2023 · 3 comments
Open

BUG: MERGE statement not parsing correctly with target alias #129

SGStino opened this issue Jul 3, 2023 · 3 comments

Comments

@SGStino
Copy link

SGStino commented Jul 3, 2023

This fails to parse:

MERGE [testDb].[dbo].[testTbl] AS [target]
USING (VALUES (@key1, @key2, @key1, @key2 ))
  AS [source] ([key1], [key2], [key1], [key2])
 ON [key1] = @key1 AND [key2] = @key2
WHEN MATCHED THEN
  UPDATE SET [source].[data1] = [target].[data1] , [source].[data2] = [target].[data2]
WHEN NOT MATCHED BY TARGET
  THEN INSERT ([key1], [key2], [data1], [data2]) VALUES ([source].[key1], [source].[key2], [source].[data1], [source].[data2]);

while

MERGE [testDb].[dbo].[testTbl] [target]
USING (VALUES (@key1, @key2, @key1, @key2 ))
  AS [source] ([key1], [key2], [key1], [key2])
 ON [key1] = @key1 AND [key2] = @key2
WHEN MATCHED THEN
  UPDATE SET [source].[data1] = [target].[data1] , [source].[data2] = [target].[data2]
WHEN NOT MATCHED BY TARGET
  THEN INSERT ([key1], [key2], [data1], [data2]) VALUES ([source].[key1], [source].[key2], [source].[data1], [source].[data2]);

parses correctly.

I'm assuming that the AS isn't expected after the column name of the MERGE while it should be?

@bruce-dunwiddie
Copy link
Owner

Thank you for reporting the issue.

I'll look into it.

@SGStino
Copy link
Author

SGStino commented Jul 4, 2023

It seems like there are more edge cases in the parsing of a MERGE:

MERGE [testDb]. [dbo]. [testTbl] [target]
USING ( VALUES ( @key1, @key2, @data1, @data2 ))
    AS [source] ( [key1], [key2], [data1], [data2])
  ON [target]. [key1] = [source]. [key1] ,  [target]. [key2] = [source]. [key2]
WHEN MATCHED THEN
    UPDATE SET [target]. [data1] = [source]. [data1] ,  [target]. [data2] = [source]. [data2]
WHEN NOT MATCHED BY TARGET
    THEN INSERT ( [key1], [key2], [data1], [data2]) VALUES ( [source]. [key1], [source]. [key2], [source]. [data1], [source]. [data2]) OUTPUT cast(1 as bit) AS RowExists, 1 as RowsUpdatedOrInserted, @@ERROR as ErrorCode, [INSERTED]. [key1], [INSERTED]. [key2], [INSERTED]. [data1], [INSERTED]. [data2];

It doesn't seem to like the CAST(1 as bit) AS RowExists, but here it doesn't even parse the implicit alias syntax, so even 1 RowExists doesn't get parsed correctly here.

It just outputs a merge expression and an unknown expression, with CAST being the first token of the unknown expression.

@bruce-dunwiddie
Copy link
Owner

ok, I'll add this to the tests also.

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

No branches or pull requests

2 participants