简单的informix选择存储过程

时间:2012-06-01 13:51:09

标签: sql stored-procedures informix

我正在尝试编写一个执行select语句的简单存储过程但是它一直给我一个语法错误而没有任何其他帮助告诉我错误是什么

create procedure _this_is_it(this CHAR(3), that CHAR(4), other CHAR(3))
foreach select * from table 
where column1 = this and
column2 = that and
column3 = other
end foreach
end procedure;

我应该收到语法错误的原因是什么?

2 个答案:

答案 0 :(得分:3)

如果要在foreach循环中执行某些处理,则必须 SELECT INTO 变量。所以你的代码将成为:

drop procedure if exists _this_is_it ;
drop table if exists this_table ;
create temp table this_table(column1 CHAR(3), column2 CHAR(4), column3 CHAR(3))
with no log ;
insert into this_table values("A","B","C") ;

create procedure _this_is_it(this CHAR(3), that CHAR(4), other CHAR(3))
returns CHAR(10) AS something

define f_this CHAR(3) ;
define f_that CHAR(4) ;
define f_other CHAR(3) ;

foreach
select * into f_this, f_that, f_other
from this_table
where column1 = this and
column2 = that and
column3 = other

return f_this||f_that||f_other WITH RESUME ;

end foreach
end procedure;

grant execute on _this_is_it to public;
execute procedure _this_is_it("A","B","C") ;

drop procedure if exists _this_is_it ;
drop table if exists this_table ;

FOREACH语句的语法可从IBM的Infocenter获得。

答案 1 :(得分:2)

删除foreach_作为第一个字母,同时在选择的末尾添加;

create procedure this_is_it(this CHAR(3), that CHAR(4), other CHAR(3))

select * from table 
where column1 = this and
column2 = that and
column3 = other;

end procedure;