Help with DB2 Create store procedure query

Hi,

I am trying to create a store procedure for a select statement. When i try to execute the query I am getting below error message. Can someone please let me know what am I missing here, because when I try to run syntax check it doesn't through any error.

Error:   " "An unexpected token "select logtime, servername from was found following D HH24:MI:SSSSS');".Expected tokens may include <delete>."

Create Procedure read(IN starttime TIMESTAMP,
                                         IN  ENDTIME TIMESTAMP,
                                         IN  NAME VARCHAR(128), 
                                         IN  COMPONENTNAME VARCHAR(128), 
                                         IN  TXTMESSAGE VARCHAR(512)
                                         IN  MESSAGE VARCHAR(1024))

LANGUAGE SQL EXTERNAL ACTION

BEGIN

declare s_time, e_time timestamp; 

set s_time = to_date(starttime, 'YYYY-MM-DD HH24:MI:SSSSS');
set E_time = to_date(endtime, 'YYYY-MM-DD HH24:MI:SSSSS');

select logtime, servername, component, txtmessage, custommessage, userid from schema1.table1
where 
           (servername is NULL OR (servername =  NAME)) and
           (component is NULL OR (component = COMPONENTNAME)) AND
           (txtmessage IS NULL OR (txtmessage =TXTMESSAGE)) AND
           (MESSAGE IS NULL OR xmlexists('$doc/LogMessage[@*=' + MESSAGE + ']' passing MESSAGE as "doc")) AND
           LOGTIME between s_time and e_time
           order by logtime with ur; 

end
;
            

Open in new window


Thanks,
AG
LVL 18
Anil GolamariAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave FordSoftware Developer / Database AdministratorCommented:
It doesn't really make sense to put a SELECT statement as the only contents of a stored procedure. What are you trying to accomplish? Are you just trying to return a result-set? If that's the case, you'd define a cursor with your select and return that.

give me a minute, and I'll whip up an example

HTH,
DaveSalash
Dave FordSoftware Developer / Database AdministratorCommented:
This should work:

create procedure MySchema.EEExample
result sets 1
language sql
Begin

  DECLARE x CURSOR for
  select distinct
         firstname
    from SomeSchema.SomeTable
   where lastName = 'BIDEN'
  ;

  OPEN x;
  SET RESULT SETS CURSOR x;
End;

call MySchema.EEExample;

Open in new window


HTH,
DaveSlash

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anil GolamariAuthor Commented:
Thanks for your response. When I try to use sample you have provided above I am getting syntax error "Encountered unexpected token "SETS"". Can you please let me know if this is something to do with the version (9.7) of DB2 I am using?
Dave FordSoftware Developer / Database AdministratorCommented:
Since I don't use version 9.7, I cannot definitively say what the problem is, but try commenting out the SET RESULT SETS line, and let us know how it behaves.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DB2

From novice to tech pro — start learning today.