在动态sql 里面怎样返回一个值

[复制链接]
查看11 | 回复6 | 2016-2-2 09:36:33 | 显示全部楼层 |阅读模式
declare tab_count
set stmt = ' select count(*)from mm.temp ';
prepare s1 from stmt;
execute s1;
怎样把一个返回count(*) 的值,返回出来?传递给tab_count
回复

使用道具 举报

千问 | 2016-2-2 09:36:33 | 显示全部楼层
set stmt = ' select count(*) into tab_countfrom mm.temp ';
行么?
回复

使用道具 举报

千问 | 2016-2-2 09:36:33 | 显示全部楼层
SQL0206N"TAB_COUNT" is not valid in the context where it is used.
SQLSTATE=42703
这样好像不行,我没有试成功过。
你能否写一个例子出来看一下呢?
[ 本帖最后由 foryuling 于 2009-3-6 15:08 编辑 ]
回复

使用道具 举报

千问 | 2016-2-2 09:36:33 | 显示全部楼层
好象直接写是不行的....
通常处理方法:
创建一个临时表,表中包含一个字段,在动态sql中将值更新到临时表中
再查询这个临时表获取结果.
回复

使用道具 举报

千问 | 2016-2-2 09:36:33 | 显示全部楼层
DB2的SQL PL,跟Oracle的PL/SQL真的是没法比啊
回复

使用道具 举报

千问 | 2016-2-2 09:36:33 | 显示全部楼层
这个应该可以的,楼主试试:
declare tab_count
select count(*)into tab_count from mm.temp;
回复

使用道具 举报

千问 | 2016-2-2 09:36:33 | 显示全部楼层
...
declare tab_count int default 0;
declare stmt varchar(1024);
declare c1 cursor for s1;
set stmt = ' select count(*)from mm.temp ';
prepare s1 from stmt;
open c1;
fetch c1 into tab_count ;
close c1;
...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行