Solved

close MERGE statement in stored procedure

Posted on 2013-01-16
9
821 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 38782712
Howdy...


'ELSE' should be 'WHEN NOT MATCHED'


Otherwise, it looks pretty good.  :)


Kent
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 38782719
<somewhat redundant>

WHEN MATCHED
WHEN NOT MATCHED
WHEN NOT MATCHED BY SOURCE

ELSE no worky in MERGE statements..
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38782736
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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 38782741
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
 

Author Comment

by:darrgyas
ID: 38782770
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:Kent Olsen
ID: 38782787
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 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38782801
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:
Kent Olsen earned 250 total points
ID: 38782830
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
ID: 38782972
Thank you all
0

Featured Post

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
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…

739 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