遍历游标的数据并比较另一个 table 的值 sql

loop through cursor's data and compare for another table's value sql

我有两张桌子。我想在执行以下步骤后从 Table 2 获取 desc:
1. Select 来自 Table1 的名称,其中类型 = 'animal';
2. 遍历 1. 中的每个名称并检查 Table2 即 o_name = name;
3. 然后检查 o_name.
的 desc 是否存在 4. 如果 desc 不存在,则在 Table2.

上为该记录插入 'pet'

我该怎么做?现在我有一个名称来自 Table1 的游标。我正在考虑遍历光标的记录,但除此之外我无法做到。请建议我:

DECLARE
    CURSOR DATA is
        SELECT name 
        FROM Table1
        where type='animal';    
BEGIN
     FOR C IN DATA LOOP
        // After this what can I do?? I cannot do select into because there will be
        // multiple rows
     END LOOP;   
END;

/

Table1:
id | name  | type
---| ----  | -----
  1| Apple | food
  2| Ball  | game
  3| Cat   | animal
  4| Cow   | animal
  5| Ball  | game

   Table2:
o_name | desc
    ---| ----
  Apple| eat  
    Cat| pet
    Cow|  

您仍然可以将其作为查询执行,并且不需要游标。请注意,数据库针对处理记录集进行了优化,这就是 SQL 查询所做的。只有在其他策略不起作用时才应使用游标。

UPDATE Table2
SET "desc" = 'pet'
WHERE
    "desc" IS NULL AND
    o_name IN (SELECT name FROM Table1 WHERE "type" = 'animal')

请注意,DESCTYPEreserved words in Oracle,因此我将它们括在双引号中。单引号用于在 Oracle 中包含文本文字(字符串)。