Solved

close MERGE statement in stored procedure

Posted on 2013-01-16
9
799 Views
Last Modified: 2013-01-16
Hi all

I have the following SP:

MERGE INTO DBO.FM_ALERT t1
USING (select max(timestamp) as ts from  DBO.FM_ALERT where active=1 and MJID = IMJID and typeid=itypeid) t2
on
t1.TIMESTAMP=t2.ts and
t1.MJID=IMJID and t1.active=1
WHEN MATCHED THEN
       UPDATE SET t1.NEXTTIMESTAMP=TIMESTAMP(CDTWHEN);
ELSE
      INSERT INTO DBO.FM_ALERT (TIMESTAMP, nextTIMESTAMP, MJID,CHID,
            LOCALX, LOCALY, MESSAGE, DETAILS, ACTIVE, TYPEID)
      VALUES (CDTWHEN, CDTWHEN, IMJID,
            CCHID, ILOCALX, ILOCALY, CMESSAGE, CDETAILS, 1, ITYPEID);

There's a number of statements after that, but they all are only executed if WHEN MATCHED is ELSE.
I want them to be always executed, not depending on the outcome of MATCHED.

How? How do I "close" the "WHEN MATCHED THEN" - "ELSE"?

All help is appreciated, thank you.
0
Comment
Question by:darrgyas
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
Howdy...


'ELSE' should be 'WHEN NOT MATCHED'


Otherwise, it looks pretty good.  :)


Kent
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
<somewhat redundant>

WHEN MATCHED
WHEN NOT MATCHED
WHEN NOT MATCHED BY SOURCE

ELSE no worky in MERGE statements..
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
remove the ";" before the ELSE... ?
MERGE INTO DBO.FM_ALERT t1
USING (select max(timestamp) as ts from  DBO.FM_ALERT where active=1 and MJID = IMJID and typeid=itypeid) t2
on
t1.TIMESTAMP=t2.ts and
t1.MJID=IMJID and t1.active=1
WHEN MATCHED THEN
       UPDATE SET t1.NEXTTIMESTAMP=TIMESTAMP(CDTWHEN)
ELSE
      INSERT INTO DBO.FM_ALERT (TIMESTAMP, nextTIMESTAMP, MJID,CHID,
            LOCALX, LOCALY, MESSAGE, DETAILS, ACTIVE, TYPEID)
      VALUES (CDTWHEN, CDTWHEN, IMJID,
            CCHID, ILOCALX, ILOCALY, CMESSAGE, CDETAILS, 1, ITYPEID);

Open in new window

0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
Comment Utility
and indeed, as jimhorn indicates, it's not ELSE, but WHEN NOT MATCHED ...
I presume you have added the ";" to fix the syntax issue

MERGE INTO DBO.FM_ALERT t1
USING (select max(timestamp) as ts from  DBO.FM_ALERT where active=1 and MJID = IMJID and typeid=itypeid) t2
on
t1.TIMESTAMP=t2.ts and
t1.MJID=IMJID and t1.active=1
WHEN MATCHED THEN
       UPDATE SET t1.NEXTTIMESTAMP=TIMESTAMP(CDTWHEN)
WHEN NOT MATCHED THEN
      INSERT INTO DBO.FM_ALERT (TIMESTAMP, nextTIMESTAMP, MJID,CHID,
            LOCALX, LOCALY, MESSAGE, DETAILS, ACTIVE, TYPEID)
      VALUES (CDTWHEN, CDTWHEN, IMJID,
            CCHID, ILOCALX, ILOCALY, CMESSAGE, CDETAILS, 1, ITYPEID);

Open in new window

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:darrgyas
Comment Utility
Changing the statement to this:

MERGE INTO DBO.FM_ALERT t1
USING (select max(timestamp) as ts from  DBO.FM_ALERT where active=1 and MJID = IMJID and typeid=itypeid) t2
on
t1.TIMESTAMP=t2.ts and
t1.MJID=IMJID and t1.active=1
WHEN MATCHED THEN
       UPDATE SET t1.NEXTTIMESTAMP=TIMESTAMP(CDTWHEN)
WHEN NOT MATCHED THEN
      INSERT INTO DBO.FM_ALERT (TIMESTAMP, nextTIMESTAMP, MJID,CHID,
            LOCALX, LOCALY, MESSAGE, DETAILS, ACTIVE, TYPEID)
      VALUES (CDTWHEN, CDTWHEN, IMJID,
            CCHID, ILOCALX, ILOCALY, CMESSAGE, CDETAILS, 1, ITYPEID);

returns the following error:

ERROR: A character, token, or clause is invalid or missing.

DB2
SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=INTO;MATCHED
THEN
      INSERT;<merge_values>, DRIVER=3.57.82
Error Code:
-104
0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
The table name in the INSERT clause is redundant


WHEN NOT MATCHED THEN
      INSERT (TIMESTAMP, nextTIMESTAMP, MJID,CHID,
            LOCALX, LOCALY, MESSAGE, DETAILS, ACTIVE, TYPEID)
      VALUES (CDTWHEN, CDTWHEN, IMJID,
            CCHID, ILOCALX, ILOCALY, CMESSAGE, CDETAILS, 1, ITYPEID);
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
please use () around the ON condition.
also:
http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=%2Fcom.ibm.db2.udb.admin.doc%2Fdoc%2Fr0010873.htm
expression
    Indicates the new value of the column. The expression must not include a column function (SQLSTATE 42903).  

=> TIMESTAMP(CDTWHEN) is invalid, please fix that as needed.


MERGE INTO DBO.FM_ALERT t1
USING (select max(timestamp) as ts from  DBO.FM_ALERT where active=1 and MJID = IMJID and typeid=itypeid) t2
on ( t1.TIMESTAMP=t2.ts and t1.MJID=IMJID and t1.active=1 )
WHEN MATCHED THEN
       UPDATE SET t1.NEXTTIMESTAMP=TIMESTAMP(CDTWHEN)
WHEN NOT MATCHED THEN
      INSERT INTO DBO.FM_ALERT (TIMESTAMP, nextTIMESTAMP, MJID,CHID,
            LOCALX, LOCALY, MESSAGE, DETAILS, ACTIVE, TYPEID)
      VALUES (CDTWHEN, CDTWHEN, IMJID,
            CCHID, ILOCALX, ILOCALY, CMESSAGE, CDETAILS, 1, ITYPEID);

Open in new window

0
 
LVL 45

Accepted Solution

by:
Kdo earned 250 total points
Comment Utility
Parentheses around the join condition (ON clause) are not required in DB2 or SQL Server.

However, the affected table is declared in the initial MERGE INTO xxx clause.  That is the ONLY place where the insert can occur.  Remove the reference to the table name from from the INSERT statement (as noted earlier).


Kent
0
 

Author Closing Comment

by:darrgyas
Comment Utility
Thank you all
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now