SoftTree Technologies SoftTree Technologies
Technical Support Forums
RegisterSearchFAQMemberlistUsergroupsLog in
Formatting MERGE

 
Reply to topic    SoftTree Technologies Forum Index » SQL Assistant View previous topic
View next topic
Formatting MERGE
Author Message
gemisigo



Joined: 11 Mar 2010
Posts: 2108

Post Formatting MERGE Reply with quote
I've create a snippet for MERGE, based on judahr's one here (by the way by, thanks for it):
Code:

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:
Code:

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:
Code:

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:
Code:

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 View user's profile Send private message
gemisigo



Joined: 11 Mar 2010
Posts: 2108

Post Reply with quote
None? :(
Mon Feb 04, 2013 9:10 am View user's profile Send private message
SysOp
Site Admin


Joined: 26 Nov 2006
Posts: 7847

Post Reply with quote
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 View user's profile Send private message
gemisigo



Joined: 11 Mar 2010
Posts: 2108

Post Reply with quote
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 View user's profile Send private message
gemisigo



Joined: 11 Mar 2010
Posts: 2108

Post Reply with quote
Hi,

Any progress on this one?
Tue Apr 21, 2015 6:28 am View user's profile Send private message
SysOp
Site Admin


Joined: 26 Nov 2006
Posts: 7847

Post Reply with quote
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
Code:
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 View user's profile Send private message
gemisigo



Joined: 11 Mar 2010
Posts: 2108

Post Reply with quote
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 View user's profile Send private message
SysOp
Site Admin


Joined: 26 Nov 2006
Posts: 7847

Post Reply with quote
You are correct. Please fix that one too
Wed Apr 22, 2015 11:00 am View user's profile Send private message
gemisigo



Joined: 11 Mar 2010
Posts: 2108

Post Reply with quote
Yes, it works now, thank you very much.
Wed Apr 22, 2015 11:25 am View user's profile Send private message
SysOp
Site Admin


Joined: 26 Nov 2006
Posts: 7847

Post Reply with quote
FYI, Version 7.2 now includes predefined MERGE code formatting template
Wed May 06, 2015 12:45 am View user's profile Send private message
Display posts from previous:    
Reply to topic    SoftTree Technologies Forum Index » SQL Assistant All times are GMT - 4 Hours
Page 1 of 1

 
Jump to: 
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


 

 

Powered by phpBB © 2001, 2005 phpBB Group
Design by Freestyle XL / Flowers Online.