0

再会,

我有一个如下的 PL-SQL 查询。我正在尝试执行查询并将结果存储到变量中。到目前为止,我有这个有效的查询:

    SELECT DECODE(COUNT(*), 0, 'N', 'Y') REC_EXISTS
    FROM
         (SELECT 
                COUNT(*) AS TOTAL_COUNT
           FROM DEV.BASEOBJECT INNER JOIN
                DEV.ANIMAL ON DEV.BASEOBJECT.ID = DEV.ANIMAL.BASEOBJECT_ID
       GROUP BY DEV.BASEOBJECT.ID, 
                DEV.BASEOBJECT.FIRST_NAME, 
                DEV.BASEOBJECT.LAST_NAME, 
                DEV.BASEOBJECT.CITY,
                DEV.BASEOBJECT.STATE,
                DEV.BASEOBJECT.ZIP,
                DEV.ANIMAL.ID,
                DEV.ANIMAL.NAME,
                DEV.ANIMAL.BREED, 
                DEV.ANIMAL.DATE_OF_BIRTH, 
                DEV.ANIMAL.GENDER,
                DEV.ANIMAL.SPECIES
         HAVING (COUNT(*) > 1));

但是,当我尝试使用此查询将结果保存到变量中时:

DECLARE
    v_name  VARCHAR2(2);
BEGIN
    SELECT DECODE(COUNT(*), 0, 'N', 'Y') REC_EXISTS
        INTO v_name
    FROM
         (SELECT 
                COUNT(*) AS TOTAL_COUNT
           FROM DEV.BASEOBJECT INNER JOIN
                DEV.ANIMAL ON DEV.BASEOBJECT.ID = DEV.ANIMAL.BASEOBJECT_ID
       GROUP BY DEV.BASEOBJECT.ID, 
                DEV.BASEOBJECT.FIRST_NAME, 
                DEV.BASEOBJECT.LAST_NAME, 
                DEV.BASEOBJECT.CITY,
                DEV.BASEOBJECT.STATE,
                DEV.BASEOBJECT.ZIP,
                DEV.ANIMAL.ID,
                DEV.ANIMAL.NAME,
                DEV.ANIMAL.BREED, 
                DEV.ANIMAL.DATE_OF_BIRTH, 
                DEV.ANIMAL.GENDER,
                DEV.ANIMAL.SPECIES
         HAVING (COUNT(*) > 1));
END

我收到一个错误:

ERROR
ORA-06550: line 1, column 8:
PLS-00103: Encountered the symbol "" when expecting one of the following:

   begin function package pragma procedure subtype type use
   <an identifier> <a double-quoted delimited-identifier> form
   current cursor
The symbol "" was ignored.    

最终,这将是一个存储过程,但我还没有准备好那部分,因为我想验证我所拥有的到目前为止是否有效。

TIA,

科森

4

1 回答 1

0

你怎么运行这个?你使用什么工具?我认为问题在于您的工具/IDE 正在尝试将其编译为函数/过程。这段代码看起来不错,如果您在 SQL*Plus 中将其作为脚本运行,它可能会毫无问题地运行。如果将其作为脚本运行,则应dbmbs_output.put_line(v_name)在查询后添加一个以查看结果。

于 2012-10-09T19:11:00.830 回答