sql - DB2 Stored Procedure IF statement -



sql - DB2 Stored Procedure IF statement -

i experimenting stored procedures , seek implement simple if/else statement. using db2 , trying select records if procedure parameter null , if parameter not null, query database.

my stored procedure code follows:

drop procedure lwilson.ifquery@ create procedure lwilson.ifquery ( in p_type varchar(15) ) dynamic result sets 1 language sql begin declare c_result cursor homecoming if p_type null select * lwilson."animals"; open c_result; else select id,type,name,weight, age lwilson."animals" animalresults animalresults.type = p_type; open c_result; end if; end@

(i using @ symbol command separator). error message receive when trying execute procedure follows...

any help appreciated. thanks.

you can preparing statement execute. not need 2 cursors in case:

create or replace procedure lwilson.ifquery ( in p_type varchar(15) ) dynamic result sets 1 language sql begin declare stmt varchar(120); declare c_result cursor homecoming res_set; if (p_type null) set stmt = "select * lwilson.animals"; else set stmt = "select id, type, name, weight, age " || "from lwilson.animals animalresults " || "where animalresults.type = " || p_type; end if; prepare res_set stmt open c_result; end@

sql stored-procedures db2

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -