 |
SoftTree Technologies
Technical Support Forums
|
|
Author |
Message |
gemisigo
Joined: 11 Mar 2010 Posts: 2165
|
|
Formatting MERGE |
|
I've create a snippet for MERGE, based on judahr's one here (by the way by, thanks for it):
 |
 |
MERGE INTO $OBJECT(ins_qualname, table, view)$ AS t
USING (
SELECT
FROM
) AS s
ON "AND s."$COLUMNS(vertical, keys)$" = t."$COLUMNS(vertical, keys)$
WHEN MATCHED THEN
UPDATE
SET "t."$COLUMNS(vertical,updatable)$" = s."$COLUMNS(vertical,updatable)$
WHEN NOT MATCHED BY TARGET THEN
INSERT
(
$COLUMNS(vertical)$
)
VALUES
(
s.$COLUMNS(vertical)$
)
WHEN NOT MATCHED BY SOURCE THEN
DELETE
; |
I've also created a formatting rule for MERGE:
 |
 |
MERGE INTO ... AS t
USING (
...
) AS s
ON ... = ...
AND ...
OR ...
WHEN MATCHED THEN
UPDATE ...
SET ... = ...
WHEN NOT MATCHED BY TARGET THEN
INSERT ...
(
...
,...
)
VALUES
(
...
,...
)
WHEN NOT MATCHED BY SOURCE THEN
DELETE
; |
Triggering a snippet for table1 (and adding table2 with relevant parts) results in:
 |
 |
MERGE INTO dbo.table1 AS t
USING (
SELECT
t2.id1_table2
,t2.id2_table2
,t2.t2column1
,t2.t2column2
,t2.t2column3
FROM
dbo.table2 AS t2
) AS s
ON s.id1_table2 = t.id1_table1
AND s.id2_table2 = t.id2_table1
WHEN MATCHED THEN
UPDATE
SET t.id1_table1 = s.id1_table1,
t.id2_table1 = s.id2_table1,
t.t2column1 = s.t2column1,
t.t2column2 = s.t2column2,
t.t2column3 = s.t2column3
WHEN NOT MATCHED BY TARGET THEN
INSERT
(
id1_table1,
id2_table1,
t2column1,
t2column2,
t2column3
)
VALUES
(
s.id1_table1,
s.id2_table1,
s.t2column1,
s.t2column2,
s.t2column3
)
WHEN NOT MATCHED BY SOURCE THEN
DELETE
; |
Now there must be some errors in my formatting rule, because after applying the rule to the code, the merge search condition is a bit weird:
 |
 |
MERGE INTO dbo.table1 AS t
USING (
SELECT
t2.id1_table2
,t2.id2_table2
,t2.t2column1
,t2.t2column2
,t2.t2column3
FROM
dbo.table2 AS t2
) AS s
ON s
.id1_table2 = t.id1_table1
AND s
.id2_table2 = t.id2_table1
WHEN MATCHED THEN
UPDATE
SET t.id1_table1 = s.id1_table1
,t.id2_table1 = s.id2_table1
,t.t2column1 = s.t2column1
,t.t2column2 = s.t2column2
,t.t2column3 = s.t2column3
WHEN NOT MATCHED BY TARGET THEN
INSERT
(
id1_table1
,id2_table1
,t2column1
,t2column2
,t2column3
)
VALUES
(
s.id1_table1
,s.id2_table1
,s.t2column1
,s.t2column2
,s.t2column3
)
WHEN NOT MATCHED BY SOURCE THEN
DELETE
; |
Any hints on how to correct it?
|
|
Fri Nov 23, 2012 5:17 am |
|
 |
gemisigo
Joined: 11 Mar 2010 Posts: 2165
|
|
|
|
None? :(
|
|
Mon Feb 04, 2013 9:10 am |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
Thank you. I'm not sure why you're getting such result, it seems like other formatting rules get in the way
I will need to check with our development team on what can be done to get the required result.
|
|
Mon Feb 04, 2013 12:03 pm |
|
 |
gemisigo
Joined: 11 Mar 2010 Posts: 2165
|
|
|
|
Provided that this is going to be solved sometime, it is still an issue in 6.4.199 preview.
|
|
Wed May 22, 2013 3:39 am |
|
 |
gemisigo
Joined: 11 Mar 2010 Posts: 2165
|
|
|
|
Hi,
Any progress on this one?
|
|
Tue Apr 21, 2015 6:28 am |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
The issue is in the formatting rule that contains "AS s" which is non-keyword and syntax-only element. Keywords and syntax elements are used as anchors. Anything extra could throw off the formatting.
Please change formatting rule to the following
 |
 |
MERGE INTO ... AS t
USING (
...
) AS ...
ON ... = ...
AND ...
OR ...
WHEN MATCHED THEN
UPDATE ...
SET ... = ...
WHEN NOT MATCHED BY TARGET THEN
INSERT ...
(
...
,...
)
VALUES
(
...
,...
)
WHEN NOT MATCHED BY SOURCE THEN
DELETE
; |
|
|
Wed Apr 22, 2015 9:26 am |
|
 |
gemisigo
Joined: 11 Mar 2010 Posts: 2165
|
|
|
|
Interesting. I'll give it a try. But won't "AS t" get in the way as well?
|
|
Wed Apr 22, 2015 10:00 am |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
You are correct. Please fix that one too
|
|
Wed Apr 22, 2015 11:00 am |
|
 |
gemisigo
Joined: 11 Mar 2010 Posts: 2165
|
|
|
|
Yes, it works now, thank you very much.
|
|
Wed Apr 22, 2015 11:25 am |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
FYI, Version 7.2 now includes predefined MERGE code formatting template
|
|
Wed May 06, 2015 12:45 am |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|