檢查手冊 「與簡歷返回」,他們做的魔術......
例1)
這是簡單...
CREATE FUNCTION teste_p2(pParam CHAR(100) DEFAULT '%') RETURNING CHAR(20) AS tabname
DEFINE vSql CHAR(500);
DEFINE vTab CHAR(20);
FOREACH select tabname into vTab
from systables where tabname like pParam
RETURN vTab WITH RESUME;
END FOREACH;
END FUNCTION;
例2)
更復雜
CREATE FUNCTION teste_p1(pParam CHAR(100) DEFAULT '%')
RETURNING CHAR(20) AS tabname
DEFINE vSql CHAR(500);
DEFINE vTab CHAR(20);
LET vSql='select tabname from systables where tabname like ? ';
LET vTab='';
PREPARE sts1 FROM vSql;
DECLARE cur1 CURSOR FOR sts1;
OPEN cur1 USING pParam;
LOOP
FETCH cur1 INTO vTab ;
IF SQLCODE = 100 THEN exit; END IF;
RETURN vTab WITH RESUME;
END LOOP;
CLOSE cur1;
FREE cur1;
FREE sts1;
END FUNCTION
然後調用這樣的:
select * from table(teste_p2('systab%')) as t(column);
如果您需要返回更多字段......只需在RETURNING聲明和RETURN WITH RESUME中添加它們即可。
這是關於MySQL服務器的問題嗎?和女巫版的DBMS? –
[爲什麼此SQL存儲過程要求創建臨時表才能使其工作(返回結果)?](http://stackoverflow.com/questions/7343187/why-does-this-sql-storage -procedure-require-that-a-temp-table-be-created-for-it-t) –
@juergend:我試着回答這個問題,但我得到這個錯誤:'一個子查詢不是eaxctly返回一行' –