0

我有一个表 emp(eid, ename, bossid, eloc) 我的要求是编写一个程序来显示根据给定参数(给定参数)位于同一位置的所有员工的所有员工及其老板姓名员工 ID)。使用“with”子句可以简单地解决上述查询(对于 eid=10),如下所示

with t (id, name, loc, bsid, bsname) as 
    (select a.eid, a.ename, a.eloc, b.eid, b.ename
     from emp a, emp b 
     where a.bossid = b.eid)
select y.id, y.bsname
from t x, t y
where x.loc = y.loc and  <b> x.id = 10;

但是当我编写如下 DB2 过程时,该过程无法编译,错误为“SQL0104N 在“bsid,bsname)”之后发现了意外的标记“as”。

create or replace procedure test with (
    in argid varchar(100),
)
LANGUAGE SQL
COMMIT ON RETURN NO
BEGIN
    for rec as (
        with t (id, name, loc, bsid, bsname)  as
                (select a.eid, a.ename, a.eloc, b.eid, b.ename
                 from emp a, emp b
                 where a.bossid = b.eid)
        select y.id, y.bsname
        from t x, t y
        where x.loc = y.loc and x.id = **argid**)
    do
        call dbms_output.put_line('ID = ' || rec.id || ', Name = ' || rec.bsname );
    end for;
END
4

1 回答 1

0

我不确定这是否是您的实际程序代码:procedure test with (并且**argid**看起来很可疑。

如果是,那么您不应该在 FOR 中的 SELECT 语句周围使用括号:

BEGIN
  for rec as -- parenthesis removed (
    with t (id, name, loc, bsid, bsname)  as
    ...
    where x.loc = y.loc and x.id = **argid** -- parenthesis removed)
于 2013-09-26T15:35:41.277 回答