将多个命令插入准备好的语句栏

Ank*_*a.P 2 postgresql execute prepared-statement prepare ruby-on-rails-3

我正在使用 rails 3 并且我需要在我的迁移之一中执行原始 sql 并且我需要使用准备好的语句来执行它,因为它是避免由于单个语句等引起的问题的最佳方法。有没有一种方法可以在单个准备语句中执行多个 sql 语句。我的数据库使用 PostgreSQL

这是我尝试过的代码

  CONN = ActiveRecord::Base.connection.raw_connection  
  sql = %Q[
            INSERT INTO table1 
              (
                name,
                email,
                phone,
                created_at,
                updated_at
               ) 
              VALUES 
              (
                $1, 
                $2, 
                $3,
                current_timestamp, 
                current_timestamp
              );
            UPDATE table2 
              SET column_1 = $1
              WHERE id = $4;
            UPDATE contacts SET 
              column_2 = $2
              WHERE id = $4
          ]

  CONN.prepare('insert_and_update', sql)
  CONN.exe_prepared('insert_and_update', [
      name,
      email,
      phone,
      customer.id
    ])
Run Code Online (Sandbox Code Playgroud)

但我收到错误

 cannot insert multiple commands into a prepared statement
Run Code Online (Sandbox Code Playgroud)

Clo*_*eto 5

将其转换为单个命令:

with i as (
    insert into table1 (
        name,
        email,
        phone,
        created_at,
        updated_at
    ) values (
        $1, 
        $2, 
        $3,
        current_timestamp, 
        current_timestamp
), u as (
    update table2 
    set column_1 = $1
    where id = $4
)
update contacts 
set column_2 = $2
where id = $4
;
Run Code Online (Sandbox Code Playgroud)