我可以将 table 中的列与游标类型的变量进行比较吗?

Can i compare a column in a table to a variable of type cursor?

我声明了一个游标来迭代我的员工 ID。

DECLARE
    r_employee employee.id%type;
c_employee CURSOR FOR
    SELECT
        distinct employee_id
    FROM
        employee;

然后我将此光标用于另一个 table“分支”中的 select 字段,其中“分支”table 中的 employee_id 与光标匹配。

SELECT
    COUNT(*),
    employee_id
FROM
    branch
WHERE
    branch.employee_id = r_employee 

GROUP BY
    employee_id

出现以下错误:

ERROR: column "r_employee" does not exist.

上面的代码在一个循环中,在每个循环中从 c_employee 中获取 r_employee。 任何线索如何解决这个问题? (使用 postgresql)

不,你不能。您必须引用游标中的列而不是游标本身。游标包含查询的结果,无论查询 returns 是单列还是多列。在这种情况下,您的 r_employee 声明本质上是一个包含单个变量的记录。但是单列或多列您必须引用游标中的列名,或者您将游标提取到的记录。所以

SELECT
    COUNT(*),
    employee_id
FROM
    branch
WHERE
    branch.employee_id = r_employee.id   --<< reference the variable within the record >>

GROUP BY
    employee_id;