我有这个存储过程:
CREATE or replace PROCEDURE TESTx()
BEGIN
DECLARE tableOneCount INTEGER;
DECLARE tableTwoCount INTEGER;
DECLARE strCmd VARCHAR(500);
SET tableOneCount = (SELECT COUNT(*) FROM proj);
SET tableTwoCount = (SELECT COUNT(*) FROM proj2);
SET msg = tableOneCount + tableTwoCount;
Create table tempa(name varchar(50), counter integer);
Insert into tempa(name, counter) values ('counter1', tableOneCount);
Insert into tempa(name, counter) values ('counter2', tableTwoCount);
Insert into tempa(name, counter) values ('counter_all', msg);
SET strCmd=(SELECT * FROM tempa);
EXECUTE IMMEDIATE(strCmd);
drop table tempa;
END @
它应该只计算2个表添加它们然后返回一个包含结果的表。由于我还没有找到另一种方法,我创建了一个临时表并在那里插入所有内容然后只返回一个select语句。不知怎的,虽然这不起作用,因为select语句遇到了错误。
我尝试使用
declare global temporary table session.tempa
(name varchar(50), counter integer)
on commit preserve rows not logged;
但是这给了我错误,行和变量的数量不匹配(SQL0117N)
有关如何使其发挥作用的任何想法吗?
答案 0 :(得分:1)
你最后的陈述(动态查询形成不正确)抛出错误,应该如下所示
SET strCmd="SELECT * FROM tempa";
EXECUTE IMMEDIATE(strCmd);
虽然您根本不需要在此处形成动态查询语句。您只需返回select
,如SELECT * FROM tempa
。
您的程序可以缩短,如
CREATE or replace PROCEDURE TESTx()
BEGIN
DECLARE tableOneCount INTEGER;
SET tableOneCount = (SELECT COUNT(*) FROM proj) + (SELECT COUNT(*) FROM proj2);
select 'counter1', count(*) from proj
union all
select 'counter2', COUNT(*) FROM proj2
union all
select 'counter_all', tableOneCount from dual
END @