Solved

close MERGE statement in stored procedure

Posted on 2013-01-16
9
806 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
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 142

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 142

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:Kdo
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 142

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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…

816 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

11 Experts available now in Live!

Get 1:1 Help Now