无法在Oracle过程中绑定变量

时间:2012-10-28 18:45:52

标签: php sql oracle stored-procedures oracle11g

这是我正在尝试运行的程序。它插入两个表 - table1和关联表table2

create or replace procedure Insert_Row (summary IN VARCHAR2
   description IN VARCHAR2, status IN NUMBER, 
   date_submitted IN DATE, last_updated IN DATE,
   owner_id IN NUMBER, reporter_id IN NUMBER,
   foo IN VARCHAR2, bar IN VARCHAR2,
   first_assignee IN NUMBER) 
is
   l_cur_id number;
begin
   insert into table1 
      (summary, description, status, date_submitted, 
       last_updated, owner_id, reporter_id, 
       foo, bar)
    values( :summary, :description, :status, 
            to_date(:date_submitted,'YYYY-MM-DD'), 
            to_date(:last_updated,'YYYY-MM-DD'), :owner_id, 
            :reporter_id, :foo, :bar)
            returning action_id into l_cur_id; 
    insert into table2(action_id, assignee_id) 
    values(l_cur_id,:test);
    commit;
 end;"

没有参数,查询工作正常。即如果我删除所有:somethings并对值进行硬编码,那么当我运行exec Insert_Row;

时,它会运行而没有错误

使用参数(:somethings),有错误:

show errors procedure Insert_Row;

Errors: check compiler log
2/4            PLS-00103: Encountered the symbol "DESCRIPTION" when expecting one of the following:

   := . ) , @ % default character
The symbol "," was substituted for "DESCRIPTION" to continue.

14/13          PLS-00049: bad bind variable 'SUMMARY'
14/23          PLS-00049: bad bind variable 'DESCRIPTION'
14/37          PLS-00049: bad bind variable 'STATUS'
15/21          PLS-00049: bad bind variable 'DATE_SUBMITTED'
16/21          PLS-00049: bad bind variable 'LAST_UPDATED'
16/50          PLS-00049: bad bind variable 'OWNER_ID'
17/13          PLS-00049: bad bind variable 'REPORTER_ID'
17/27          PLS-00049: bad bind variable 'FOO'
17/33          PLS-00049: bad bind variable 'BAR'
20/21          PLS-00049: bad bind variable 'TEST'

我做错了什么?

修改

在@ron tornambe的解决方案之后查询:

create or replace procedure Insert_Row (summary IN VARCHAR2,
   description IN VARCHAR2, status IN NUMBER, 
   date_submitted IN DATE, last_updated IN DATE,
   owner_id IN NUMBER, reporter_id IN NUMBER,
   foo IN VARCHAR2, bar IN VARCHAR2,
   first_assignee IN NUMBER) 
is
   l_cur_id number;
begin
   insert into table1 
      (summary, description, status, date_submitted, 
       last_updated, owner_id, reporter_id, 
       foo, bar)
    values( :summary, :description, :status, 
            to_date(:date_submitted,'YYYY-MM-DD'), 
            to_date(:last_updated,'YYYY-MM-DD'), :owner_id, 
            :reporter_id, :foo, :bar)
            returning action_id into l_cur_id; 
    insert into table2(action_id, assignee_id) 
    values(l_cur_id,:test);
    commit;
 end;"

此修改后的错误是:

14/13          PLS-00049: bad bind variable 'SUMMARY'
14/23          PLS-00049: bad bind variable 'DESCRIPTION'
14/37          PLS-00049: bad bind variable 'STATUS'
15/21          PLS-00049: bad bind variable 'DATE_SUBMITTED'
16/21          PLS-00049: bad bind variable 'LAST_UPDATED'
16/50          PLS-00049: bad bind variable 'OWNER_ID'
17/13          PLS-00049: bad bind variable 'REPORTER_ID'
17/27          PLS-00049: bad bind variable 'FOO'
17/33          PLS-00049: bad bind variable 'BAR'
20/21          PLS-00049: bad bind variable 'TEST'

非常感谢。

2 个答案:

答案 0 :(得分:2)

试试这个

create or replace procedure Insert_Row (summary IN VARCHAR2,
   description IN VARCHAR2, status IN NUMBER, 
   date_submitted IN DATE, last_updated IN DATE,
   owner_id IN NUMBER, reporter_id IN NUMBER,
   foo IN VARCHAR2, bar IN VARCHAR2,
   first_assignee IN NUMBER) 
is
   l_cur_id number;
begin
   insert into table1 
      (summary, description, status, date_submitted, 
       last_updated, owner_id, reporter_id, 
       foo, bar)
    values( summary, description, status, 
            to_date(date_submitted,'YYYY-MM-DD'), 
            to_date(last_updated,'YYYY-MM-DD'), owner_id, 
            reporter_id, foo, bar)
            returning action_id into l_cur_id; 
    insert into table2(action_id, assignee_id) 
    values(l_cur_id,test);
    commit;
 end;

如果要传递参数,则不需要冒号,可以直接指定参数名称。另一点是,如果您从客户端或前端调用此过程,最好从您的过程中删除提交并使用来自您的调用客户端的提交。

答案 1 :(得分:1)

在“摘要IN VARCHAR2”之后只需要一个逗号。此外,关键字“IN”是默认值。