Solved

db2 sp and "with"

Posted on 2013-02-06
6
540 Views
Last Modified: 2013-02-07
Hi all,

I have an SP, and this part of it:

if exists (
      With a as ( select timestamp, row_number() over (order by timestamp desc) as rown,
      mjid,
      level from dbo.fm_fuel order by Timestamp fetch first 2 rows only)
      Select t1.timestamp, t1.level, t2.timestamp, t2.level
      from a t1 inner join a t2 on t1.rown=t2.rown+1 where t1.mjid=3199 and (t2.level - t1.level) > 9;
) then
INSERT INTO DBO.FM_REFUEL (TIMESTAMP, MJID, CHID, LEVEL, ODOMETER)
                                                                VALUES  
                                                                (CDTWHEN, IMJID, CCHID, iLEVEL, iODOMETER);
end if;

gives me this error:

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

DB2
SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=as;if
exists (
      With a;JOIN, DRIVER=3.57.82
Error Code: -104

The statament works by itself, but how do I make it work inside a stored procedure?

Any help is appreciated, thank you
0
Comment
Question by:darrgyas
6 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 38861180
Hi  darrgyas ,

You can't use the *with* structure this way.  :(

Either drop the with construct (using just select ... from ...) or write the query as a MERGE ... WHEN NOT MATCHED INSERT ....



Kent
0
 
LVL 18

Expert Comment

by:daveslash
ID: 38861183
I wonder if it's throwing an error because TIMESTAMP is a reserved word.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 38862280
I like both answers :-) but I guess fm_fuel.timestamp is a field (so I presume it was allowed) - however I would certainly always nominate the table or alias against that field!

the "with a as" is then self-joined so the intention is good (to re-use) but you might use a cursor instead.
0
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

 
LVL 45

Expert Comment

by:Kdo
ID: 38862466
Hi darrgyas ,

There are subtle differences between client submitted SQL and the SQL used in Stored Procedures, as well as the major differences of objects allowed only in one or the other.  I've long been a supporter of good formatting, and have taken the liberty of modifying yours for easier (to me) readability.  It occurs to me that if the timestamp field is indexed AND it is guaranteed unique (not necessarily true if the table contains a transaction log) that a small rewrite gets you the answers a lot faster.

if exists
(
  With a as
  (
    select timestamp, row_number() over (order by timestamp desc) as rown, mjid, level
    from dbo.fm_fuel
    order by Timestamp
    fetch first 2 rows only
  )
  Select t1.timestamp, t1.level, t2.timestamp, t2.level
  from a t1
  inner join a t2
    on t1.rown=t2.rown+1
  where t1.mjid=3199
    and (t2.level - t1.level) > 9;
) then
  INSERT INTO DBO.FM_REFUEL (TIMESTAMP, MJID, CHID, LEVEL, ODOMETER)
  VALUES  (CDTWHEN, IMJID, CCHID, iLEVEL, iODOMETER);
end if;

Try this small change:

DECLARE row_count INTEGER;

With a as
(
  select timestamp, row_number() over (order by timestamp desc) as rown, mjid, level
  from dbo.fm_fuel
  order by Timestamp
  fetch first 2 rows only
)
SELECT count(*)  INTO row_count
FROM a t1
  INNER JOIN a t2
    ON t1.rown=t2.rown+1
  WHERE t1.mjid=3199
    AND (t2.level - t1.level) > 9;

IF (row_count > 0) THEN
  INSERT INTO DBO.FM_REFUEL (TIMESTAMP, MJID, CHID, LEVEL, ODOMETER)
  VALUES  (CDTWHEN, IMJID, CCHID, iLEVEL, iODOMETER);
end if;


I'm still not sure if that WITH syntax will work in the stored procedure.  With so many code bases and versions for DB2, it's dependent on what you have.



Kent
0
 

Author Comment

by:darrgyas
ID: 38863658
Kent,
You're right, it still doesn't work:

ERROR: A character, token, or clause is invalid or missing.
DB2
SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=With a
as
(
  select timestamp, ro;M.SYSDUMMY1;
0
 

Author Closing Comment

by:darrgyas
ID: 38863659
Thank you!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

20 Experts available now in Live!

Get 1:1 Help Now