3

我正在尝试编写一个执行 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;

我应该得到语法错误的任何理由?

4

2 回答 2

3

删除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;
于 2012-06-01T14:09:54.520 回答
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获得。

于 2012-10-26T11:41:47.587 回答