【问题标题】:execute_update pl/sql in jruby failsjruby 中的 execute_update pl/sql 失败
【发布时间】:2014-01-13 23:32:16
【问题描述】:

我正在尝试使用 jdbc 驱动程序连接到 oracle db,并且该部分工作正常。之后我想执行一些 pl/sql bolck,这就是我遇到的问题,这似乎是语法问题。有人可以帮我解决这个问题吗?我试图弄清楚,但不能。下面是代码sn-p。

   userlist = ['John', 'Sam', 'Lucia']

   $userlist.each do|usr|
   puts "Working on #{usr}"
   stmt = <<-EOF
       DECLARE
            CURSOR cur IS
                   SELECT sid, serial#
                   FROM v$session WHERE username = upper('#{usr}');

       BEGIN
                   FOR rec IN cur
                     LOOP
                       EXECUTE IMMEDIATE 'ALTER USER #{usr} IDENTIFIED BY chng';
                       dbms_output.put_line('Killing sessions which belong to #{usr}...');
                       EXECUTE IMMEDIATE 'ALTER SYSTEM KILL SESSION '''||session_rec.sid||','||session_rec.serial#'''';
                     END LOOP;
                   END;
       EOF

       #in the initialize method I established the connection and it is working fine.
       plsql_stmt = @conn.create_statement 
       plsql_stmt.execute_update(stmt)
   end

错误信息:

   Working on John
   NativeException: java.sql.SQLException: ORA-06550: line 10, column 100:
   PLS-00103: Encountered the symbol "'" when expecting one of the following:

   . ( * @ % & = - + ; < / > at in is mod remainder not rem
   return returning <an exponent (**)> <> or != or ~= >= <= <>
   and or like like2 like4 likec between into using || bulk
   member submultiset
   The symbol "*" was substituted for "'" to continue.

谢谢。

【问题讨论】:

  • 你试过在sqldeveloper中执行代码吗?
  • 我会puts stmt 并检查它。错误消息表明您有一个放错位置的撇号。
  • 我正在使用 putty 在 linux box 上执行代码。
  • @Shepmaster:感谢您打印查询的建议。我错过了'||'在“Alter system kill”查询结束时。更正为EXECUTE IMMEDIATE 'ALTER SYSTEM KILL SESSION '''||session_rec.sid||','||session_rec.serial#||'''';

标签: oracle plsql jruby oracle-call-interface


【解决方案1】:

将大坏的plsql重构为plsql过程,并使用https://github.com/rsim/ruby-plsql从ruby调用它

【讨论】:

    【解决方案2】:

    该错误消息表明您有 SQL 语法错误,特别是在撇号周围。我会puts stmt 并检查它。

    【讨论】:

      猜你喜欢
      • 2012-11-22
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      • 2020-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多