SELECT INTO和UPDATE的多个变量

时间:2012-12-12 15:25:48

标签: oracle plsql oracle-sqldeveloper

我是Oracle和PL / SQL的初学者;我在选择几个变量时遇到了一些问题。我正在尝试做的是在一个表中搜索多个项目(SA_SPECIFICATION_DETAILS)并将它们连接在一起到另一个表中的字段(SA_ASSET)。 SA_SPECIFICATION_DETAILS表为每个SA_ASSET.ASSET_ID保存多行属性。我几天来一直在研究这个问题并且一直有几个错误。这是我的示例代码:

DECLARE 
manuf VARCHAR(50);
mods VARCHAR(50);
mvs VARCHAR(50);
yeara VARCHAR(4);
assid VARCHAR(50):= &assid;

BEGIN
SELECT TRIM(ATTRIBUTE_VALUE) INTO manuf      
    FROM
      SA_SPECIFICATION_DETAILS d,
      SA_ASSET a
    WHERE
      d.SPECIFICATION_NO = a.SPECIFICATION_NO
    AND d.ATTRIBUTE_DESC = 'MANUFACTURER'
    AND ASSET_ID = assid;

SELECT TRIM(ATTRIBUTE_VALUE) INTO mods
    FROM
      SA_SPECIFICATION_DETAILS d,
      SA_ASSET a
    WHERE
      d.SPECIFICATION_NO = a.SPECIFICATION_NO
    AND d.ATTRIBUTE_DESC = 'MODEL'
    AND ASSET_ID = assid;

SELECT TRIM(ATTRIBUTE_VALUE) INTO mvs
    FROM
      SA_SPECIFICATION_DETAILS d,
      SA_ASSET a
   WHERE
      d.SPECIFICATION_NO = a.SPECIFICATION_NO
    AND d.ATTRIBUTE_DESC = 'MAIN VALVE SIZE'
    AND ASSET_ID = assid;

SELECT TRIM(ATTRIBUTE_VALUE) INTO yeara
    FROM
      SA_SPECIFICATION_DETAILS d,
      SA_ASSET a
    WHERE
      d.SPECIFICATION_NO = a.SPECIFICATION_NO
    AND d.ATTRIBUTE_DESC = 'YEAR MANUFACTURED'
    AND ASSET_ID = assid;

dbms_output.ENABLE(buffer_size => NULL);
DBMS_OUTPUT.PUT_LINE ('Variables:');
dbms_output.put_line (manuf);
dbms_output.put_line (mods);
dbms_output.put_line (mvs);
dbms_output.put_line (yeara);
dbms_output.put_line (assid);
END;

BEGIN
UPDATE
  SA_ASSET
SET
  ASSET_DESC = TRIM(ATTRIBUTE1)
  || ' - Service Type: PW, Manf: '
  || manuf
  || ', Model: '
  || mods
  || ', Main Valve Size: '
  || mvs
  || ', Year Manf: '
  || yearm
  || ', Location: '
  || TRIM(SA_ASSET.STREET_NUMBER_CHAR)
  || ' '
  || TRIM(SA_ASSET.STREET_NAME)
WHERE
  ASSET_TYPE     = 'HYDRANT'
AND ASSET_STATUS = 'ACTIVE'
AND UPPER(ASSET_DESC) NOT LIKE '%LOCATION:%'
AND UPPER(ASSET_DESC) NOT LIKE '%HYDRANT%'
AND SA_ASSET.ASSET_ID = assid
END;

我已经测试了这个,它失败了: 错误报告: ORA-06550:第73行第1栏: PLS-00103:遇到符号“BEGIN” ORA-06550:第96行,第1栏: PLS-00103:遇到以下任何一种情况时遇到符号“END”:

所以我注释掉了更新部分并尝试了这个:

    declare 
    manuf sa_specification_details.attribute_value%type;
    mods sa_specification_details.attribute_value%type;
    mvs sa_specification_details.attribute_value%type;
    yeara sa_specification_details.attribute_value%type;
    assid varchar2(15):= &assid;

    --dbms_output.ENABLE(buffer_size => NULL);

    BEGIN
    SELECT ATTRIBUTE_VALUE INTO manuf      
        FROM
          SA_SPECIFICATION_DETAILS d,
          SA_ASSET a
        WHERE
          d.SPECIFICATION_NO = a.SPECIFICATION_NO
        AND d.ATTRIBUTE_DESC = 'MANUFACTURER'
        AND ASSET_ID = assid;
    dbms_output.put_line (manuf);

   SELECT ATTRIBUTE_VALUE INTO mods
        FROM
          SA_SPECIFICATION_DETAILS d,
          SA_ASSET a
        WHERE
          d.SPECIFICATION_NO = a.SPECIFICATION_NO
        AND d.ATTRIBUTE_DESC = 'MODEL'
        AND ASSET_ID = assid;
    dbms_output.put_line (mods);

    SELECT ATTRIBUTE_VALUE INTO mvs
        FROM
          SA_SPECIFICATION_DETAILS d,
          SA_ASSET a
       WHERE
          d.SPECIFICATION_NO = a.SPECIFICATION_NO
        AND d.ATTRIBUTE_DESC = 'MAIN VALVE SIZE'
        AND ASSET_ID = assid;
    dbms_output.put_line (mvs);

    SELECT ATTRIBUTE_VALUE INTO yeara
        FROM
          SA_SPECIFICATION_DETAILS d,
          SA_ASSET a
        WHERE
          d.SPECIFICATION_NO = a.SPECIFICATION_NO
        AND d.ATTRIBUTE_DESC = 'YEAR MANUFACTURED'
        AND ASSET_ID = assid;
    dbms_output.put_line (yeara);  

    SYS.dbms_output.ENABLE;
    DBMS_OUTPUT.PUT_LINE ('Variables:');
    dbms_output.put_line (manuf);
    dbms_output.put_line (mods);
    dbms_output.put_line (mvs);
    dbms_output.put_line (yeara);
    dbms_output.put_line (assid); 
    END;

这让我错误: 错误报告: ORA-01403:未找到任何数据 ORA-06512:第11行 01403. 00000 - “找不到数据” *原因:
*操作:

所以我在没有选择的情况下运行查询:

SELECT
  ATTRIBUTE_VALUE
FROM
  SA_SPECIFICATION_DETAILS d,
  SA_ASSET a
WHERE
  d.SPECIFICATION_NO = a.SPECIFICATION_NO
AND d.attribute_desc = 'MANUFACTURER'
AND asset_id = '001722';

它返回单行/单列(也就是说它有效)。那么我做错了什么?我已经声明了变量,我已经选择正确运行但它没有传递变量(我可以告诉 - 没有得到dbms_output.put_line(manuf);工作)。我完全关闭或走错了轨道吗?

1 个答案:

答案 0 :(得分:9)

在您的代码中,您需要在此处添加单引号:

assid VARCHAR2(50):= '&assid';

也删除结束/从这里开始:

dbms_output.put_line (assid);
END;

BEGIN

因为您希望将其作为一个块(因为您正在使用更新中第一个选择的变量)。

最后我认为你的意思是年龄而不是年龄

  || ', Year Manf: '
  || yearm <-- yeara?

您也可以进行优化,将4个选项替换为:

select max(case d.attribute_desc
             when 'MANUFACTURER' then trim(attribute_value)
           end) manuf,
       max(case d.attribute_desc
             when 'MODEL' then trim(attribute_value)
           end) mods,
       max(case d.attribute_desc
             when 'MAIN VALVE SIZE' then trim(attribute_value)
           end) mvs,
       max(case d.attribute_desc
             when 'YEAR MANUFACTURED' then trim(attribute_value)
           end) yeara
  into manuf, mods, mvs, yeara
  from sa_specification_details d,
       sa_asset a
 where d.specification_no = a.specification_no
   and d.attribute_desc in ( 'MANUFACTURER', 'MODEL', 'MAIN VALVE SIZE',
                             'YEAR MANUFACTURED' )
   and asset_id = assid;