我创建了一个函数abcd_insert(),它将数据插入到表abcd中,该表有8列。函数内部的代码类似于下面的代码:
BEGIN
INSERT INTO abcd
VALUES
(
x ,
y ,
select sum(count) from (select count(*) from a where a1 = x and a2 = y and a3 = 1 union all select count(*) from b where b1 = x and b2 = y and b3 = 1 ) as n1,
select sum(count) from (select count(*) from a where a1 = x and a2 = y and a2 = 2 union all select count(*) from b where b1 = x and b2 = y and b3 = 2 ) as n2 ,
select sum(count) from (select count(*) from a where a1 = x and a2 = y and a2 = 3 union all select count(*) from b where b1 = x and b2 = y and b3 = 3 ) as n3 ,
select sum(count) from (select count(*) from a where a1 = x and a2 = y and a2 = 4 union all select count(*) from b where b1 = x and b2 = y and b3 = 4 ) as n4 ,
select sum(count) from (select count(*) from a where a1 = x and a2 = y and a2 = 5 union all select count(*) from b where b1 = x and b2 = y and b3 = 5 ) as n5 ,
SELECT sum(q1) from
(SELECT CASE WHEN COUNT(1) > 0 THEN 1 ELSE 0 END as q1 FROM p1 where p11 = x and p12 = y union all
SELECT CASE WHEN COUNT(1) > 0 THEN 1 ELSE 0 END as q1 FROM p2 where p21 = x and p22 = y ) as q1
);
END;
'x'和'y'是我的输入参数,其值将传递给函数abcd_insert()。 a,b,p1和p2是同一模式中的表。 当我在运行时将'x'和'y'传递给函数时,它会抛出错误。 有人可以告诉我这里我做错了什么。
答案 0 :(得分:0)
您需要为列命名,而不仅仅是派生表:
SELECT SUM(foo) as foo
FROM ((SELECT CASE WHEN COUNT(1) > 0 THEN 1 ELSE 0 END) as foo FROM a.a1 where p1=100) UNION ALL
. . .
) x