oracle在执行立即返回ID时给出“缺少关键字”错误

时间:2015-12-01 10:33:36

标签: oracle stored-procedures plsql sql-returning

以下是oracle程序

mlogit

和我正在使用的Pl / SQL

mlogit

这给了我以下错误

create or replace 
PROCEDURE INSERT_COMMON(
ENTITY_NAME IN VARCHAR2  
, INSERT_QUERY IN varchar2
) 
AS
NEW_ID NUMBER;
BEGIN
    -- execute insert
    DBMS_OUTPUT.PUT_LINE('INSERT_QUERY: ' || INSERT_QUERY);
    -- execute IMMEDIATE INSERT_QUERY returning ID into NEW_ID;
    -- above gives me a syntax error so using as below
    execute IMMEDIATE INSERT_QUERY || ' returning ID into NEW_ID';
    DBMS_OUTPUT.PUT_LINE('NEW_ID: ' || NEW_ID);

END INSERT_COMMON;

但是,我已经测试过并且工作正常

DECLARE
  ENTITY_NAME VARCHAR2(200);
  INSERT_QUERY VARCHAR2(200);
BEGIN
  ENTITY_NAME := 'company';
  INSERT_QUERY := 'INSERT INTO COMPANY (NAME) VALUES (''A  Company 2'')';

  INSERT_COMMON(ENTITY_NAME,INSERT_QUERY);
END;

1 个答案:

答案 0 :(得分:1)

您需要指定一个在动态sql语句中返回的绑定变量,但是您还需要将returning into <variable>子句添加到execute immediate语句中。

我相信(未经测试,因为您没有提供设置表格和关联触发器的语句),以下内容应该对您的问题进行排序:

create or replace procedure insert_common (entity_name in varchar2,
                                           insert_query in varchar2)
as
  new_id number;
begin
  -- execute insert
  dbms_output.put_line ('INSERT_QUERY: ' || insert_query);

  -- above gives me a syntax error so using as below
  execute immediate insert_query || ' returning ID into :NEW_ID' returning into new_id;

  dbms_output.put_line ('NEW_ID: ' || new_id);
end insert_common;
/