Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 590
  • Last Modified:

db2 sp and "with"

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
darrgyas
Asked:
darrgyas
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
Dave FordSoftware Developer / Database AdministratorCommented:
I wonder if it's throwing an error because TIMESTAMP is a reserved word.
0
 
PortletPaulCommented:
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
darrgyasAuthor Commented:
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
 
darrgyasAuthor Commented:
Thank you!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now