将Select query的结果获取到变量中,并将其作为列值插入另一个表中

时间:2014-05-12 10:15:13

标签: oracle plsql toad

DECLARE count NUMBER;

SELECT max(to_number(param_code))+1 INTO count from abc where interface_code='PQR' ;

INSERT INTO abc
( INTERFACE_CODE
, PARAM_CODE
, DSCR
, PARAM_CHAR
, PARAM_NUMBER
, PARAM_DATE
, TRGA_CODE
)
VALUES
(  'PQR'
,   count 
,   'xyz'
,   ''     
,  ''        
,   ''         
,   '*'
)

我想在Oracle中将这3个语句作为脚本执行。 但这给了我错误。

请帮忙

1 个答案:

答案 0 :(得分:3)

DECLARE
  MY_COUNT NUMBER;
BEGIN
  SELECT MAX(to_number(param_code))+1
  INTO MY_COUNT
  FROM abc
  WHERE interface_code='PQR' ;

  INSERT
  INTO abc
    (
      INTERFACE_CODE ,
      PARAM_CODE ,
      DSCR ,
      PARAM_CHAR ,
      PARAM_NUMBER ,
      PARAM_DATE ,
      TRGA_CODE
    )
    VALUES
    (
      'PQR' ,
      MY_COUNT ,
      'xyz' ,
      '' ,
      '' ,
      '' ,
      '*'
    );
EXCEPTION
WHEN OTHERS THEN
 DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
/