【问题标题】:PLS-00201: identifier when running oracle stored procedure through railsPLS-00201:通过 rails 运行 oracle 存储过程时的标识符
【发布时间】:2015-05-26 13:43:11
【问题描述】:

我正在运行 Rails 中的 oracle 存储过程,但出现以下错误:

ActionView::Template::Error (undefined method `parse' for
#<Mysql2::Client:0x00000008ef4310>):

在下面一行:

   cursor = connection.parse(sql)

这是我的 database.yml 文件

development:
  adapter:  mysql2
  database: db1
  username: user1
  password: *****
  host:     *****
  port:     3306

db1_development:
  adapter: mysql2
  username: user2
  password: ****
  database: ****
  host: *****
  port: 3306

db2_development:
  adapter: mysql2
  database: user3
  username: ******
  password: ******
  host: *****
  port: 3309

db3_development:
  adapter: oracle_enhanced
  database: user3
  username: *****
  password: *****

这是我的 2 个模型类:

module Sts

  class StsLtd < Sts::Base
    def number
        errormsg = nil
        errorcode = nil
        sperrormsg = nil
        vpan = nil

        sql =
      "BEGIN #{Pkgltd::PKG_LTD}.GET_PAN('
        8042049440330819','32', 'TEST', '0',vpan, errormsg, errorcode, sperrormsg);
       END;"

       connection = self.connection.raw_connection
       cursor = connection.parse(sql)

         cursor.bind_param(:errormsg, nil, String, 1000)
         cursor.bind_param(:errorcode, nil, String, 1000)
         cursor.bind_param(:sperrormsg, nil, String, 1000
             cursor.bind_param(:vpan, nil, String, 1000)

       cursor.exec

       vpan, errormsg, errorcode, sperrormsg, vpan = cursor[:vpan], cursor[:errormsg], cursor[:errorcode], cursor[:sperrormsg]
       cursor.close
       vpan
    end
  end
end

sts.rb:

module Sts

  PKG_LTD ="PKG_LTD"

  class Base < ActiveRecord::Base
    self.abstract_class = true
    establish_connection = "db3_#{Rails.env}"

  end
end

当特定的代码集只是尝试连接到 oracle 数据库并运行 oracle 存储过程时,我不确定它为什么会引发 mysql 解析错误。

编辑:我能够通过从以下行中删除“=”来修复解析错误:

establish_connection = "db3_#{Rails.env}"

但是,我收到以下错误:

ActionView::Template::Error(ORA-06550:第 1 行,第 53 列:PLS-00201: 标识符 'VAULT' 必须声明 ORA-06550:第 1 行,第 7 列: PL/SQL:语句被忽略):

如果我将“VAULT”硬编码如下,我的存储过程可以正常工作:

    sql =
  "BEGIN #{Pkgltd::PKG_LTD}.GET_PAN('
    8042049440330819','32', 'VAULT', '0',vpan, errormsg, errorcode, sperrormsg);

但是如果我将它作为函数参数传递并调用它,我会得到上述错误:

    sql =
  "BEGIN #{Pkgltd::PKG_LTD}.GET_PAN('
    8042049440330819','32', #{vault_cd}, '0',vpan, errormsg, errorcode, sperrormsg);

【问题讨论】:

  • 什么时候会抛出错误?一旦你启动服务器?仅当您触发存储过程时?有堆栈跟踪吗?
  • 是的,当我执行 binding.pry 并从上面的代码到达 cursor = connection.parse(sql) 时,它会抛出错误。
  • 但是如果你不做任何绑定,它仍然会出错吗?
  • 是的,我得到同样的错误并跟踪到同一行:ActionView::Template::Error (undefined method parse' for #<:client:0x0000000c6d88d0>):`
  • 是的,但是堆栈跟踪是有原因的,而不仅仅是一个错误行。包括更多的堆栈跟踪,以帮助人们帮助你。发布您的堆栈跟踪,或者至少 5 或 10 行,如果它真的很长。网络服务器日志说明了什么?

标签: ruby-on-rails ruby oracle ruby-on-rails-3 activerecord


【解决方案1】:

好的。所以我在运行存储过程时遇到了 3 个不同的错误。

第一个错误是:

ActionView::Template::Error (undefined method `parse' for
#<Mysql2::Client:0x00000008ef4310>):

已通过更改以下代码修复:

establish_connection = "db3_#{Rails.env}" 

establish_connection  "db3_#{Rails.env}"

第二个错误是:

OCIError: ORA-01036: 非法变量名/编号

通过从以下位置更新 sql 已修复:

  sql =
  "BEGIN #{Pkgltd::PKG_LTD}.GET_PAN('
    8042049440330819','32', 'VAULT', '0',vpan, errormsg, errorcode, sperrormsg);

添加符号

  sql =
  "BEGIN #{Pkgltd::PKG_LTD}.GET_PAN('
    8042049440330819','32', 'VAULT', '0',:vpan, :errormsg, :errorcode, sperrormsg);

最后的错误是:

ActionView::Template::Error(ORA-06550:第 1 行,第 53 列:PLS-00201: 标识符 'VAULT' 必须声明 ORA-06550:第 1 行,第 7 列: PL/SQL:语句被忽略):

由于某种原因,它没有将其解释为字符串。

所以我必须为 #{vault_cd} 添加单引号才能使其工作:

 sql =

      "BEGIN #{Pkgltd::PKG_LTD}.GET_PAN('
        8042049440330819','32', '#{vault_cd}', '0',vpan, errormsg, errorcode, sperrormsg);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-09
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 2017-01-22
    • 2014-06-24
    相关资源
    最近更新 更多