PL/SQL 块查找每个国家的供应商数量

PL/SQL Block Finding number of suppliers for each nation

我还是 PLSQL 的新手,目前正在使用 TPCH 数据集进行练习。我尝试这个已经有一段时间了,但我似乎无法理解它并且可以使用一些建议。数据集的粗略概述 here.

到目前为止,这是我的代码

DECLARE
countNationkey number (5);

BEGIN
    FOR QRow IN (   SELECT r_name, n_name, s_nationkey, r_regionkey, count(s_nationkey) INTO countNationkey
            FROM region, nation, supplier
            WHERE r_regionkey = n_regionkey
            AND n_nationkey = s_nationkey
            GROUP BY r_name, n_name, s_nationkey, r_regionkey
            HAVING count(s_nationkey) > 130 
            ORDER BY r_name )

    LOOP
            dbms_output.put_line( rpad('R_NAME', 15) || rpad('N_NAME', 15) || 
                            rpad('COUNT(S_NATIONKEY)', 20) || chr(10) );
            dbms_output.put_line('----------------------------------------------------------'); 
        dbms_output.put_line( rpad(QRow.r_name, 15) || rpad(QRow.n_name, 15) || rpad(countNationkey, 15) );
    END LOOP;
END;

但是,当我只尝试 select 查询时,出现错误

    SELECT r_name, n_name, s_nationkey, r_regionkey, count(s_nationkey) INTO countNationkey
                                                                         *
ERROR at line 1:
ORA-00905: missing keyword

如果我删除 INTO countNationkey,我知道 countNationkey 没有值,因此我将得到如下所示的输出。

R_NAME         N_NAME         COUNT(S_NATIONKEY)  

----------------------------------------------------------
ASIA           CHINA
R_NAME         N_NAME         COUNT(S_NATIONKEY)  

----------------------------------------------------------
ASIA           INDONESIA
R_NAME         N_NAME         COUNT(S_NATIONKEY)  

----------------------------------------------------------
EUROPE         GERMANY
R_NAME         N_NAME         COUNT(S_NATIONKEY)  

----------------------------------------------------------
MIDDLE EAST    SAUDI ARABIA

这是我想要的预期结果

R_NAME                     N_NAME                    COUNT(S_NATIONKEY)
------------------------- ------------------------- ------------------
ASIA                       INDONESIA                  131
ASIA                       CHINA                      145
MIDDLE                     EAST SAUDI ARABIA          132
EUROPE                     GERMANY                    132

真的需要一些帮助!提前致谢!

只需删除 INTO。它 在 PL/SQL 中是 所必需的,但当 select 是游标的一部分时则不是(在您的情况下,这是一个游标 FOR 循环)。

此外,您随后将使用游标变量的名称 (QROW.countNationkey) 引用 countNationkey,这也意味着您不需要局部变量。

所以:

BEGIN
   FOR QRow IN (  SELECT r_name,
                         n_name,
                         s_nationkey,
                         r_regionkey,
                         COUNT (s_nationkey) countNationkey
                    FROM region, nation, supplier
                   WHERE     r_regionkey = n_regionkey
                         AND n_nationkey = s_nationkey
                GROUP BY r_name,
                         n_name,
                         s_nationkey,
                         r_regionkey
                  HAVING COUNT (s_nationkey) > 130
                ORDER BY r_name)
   LOOP
      DBMS_OUTPUT.put_line (
            RPAD ('R_NAME', 15)
         || RPAD ('N_NAME', 15)
         || RPAD ('COUNT(S_NATIONKEY)', 20)
         || CHR (10));
      DBMS_OUTPUT.put_line (
         '----------------------------------------------------------');
      DBMS_OUTPUT.put_line (
            RPAD (QRow.r_name, 15)
         || RPAD (QRow.n_name, 15)
         || RPAD (QROW.countNationkey, 15));
   END LOOP;
END;