title: Get last inserted id (go + oracle)
body: <p>I need to insert two records in Oracle database from my go application using sqlx (<a href="https://github.com/jmoiron/sqlx" rel="nofollow">https://github.com/jmoiron/sqlx</a>) with go-oci8 (<a href="https://github.com/mattn/go-oci8" rel="nofollow">https://github.com/mattn/go-oci8</a>) driver.
  Second record references previous one by foreign key.
  So I need to have primary key of first record (it is assigned from sequence with before insert trigger on first table) before I can insert second record.</p>
  
  <p>So I experimented to get last inserted id:</p>
  
  <pre><code>create table t(x int primary key);
  create sequence x_seq;
  </code></pre>
  
  <p>LastInsertId failed for me:</p>
  
  <pre><code>import(
      "fmt"
      "github.com/jmoiron/sqlx"
      _ "github.com/mattn/go-oci8"
  )
  
  func main(){
      db, err := sqlx.Connect("oci8", "integr/integr@localhost:49161/xe")
      if err != nil {
         fmt.Println(err)
      }
      sql := "insert into t values(x_seq.nextval)"
      r, err := db.Exec(sql)
      if err != nil {
         fmt.Println(err)
      }
      fmt.Println(r.RowsAffected())
      fmt.Println(r.LastInsertId())
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>1 &lt;nil&gt;
  0 LastInsertId not supported
  </code></pre>
  
  <p>Then I tried to make stored function that inserts record and returns primary key.</p>
  
  <pre><code>create function f(x int) return int as
  v int;
  begin
      insert into t values(x)
      returning x into v;
      return v;
  end;
  </code></pre>
  
  <p>But I failed to find a way to get its result. PostgreSQL style select fails:</p>
  
  <pre><code>SQL&gt; select f(9) from dual;
  select f(9) from dual
         *
  ERROR at line 1:
  ORA-14551: cannot perform a DML operation inside a query
  ORA-06512: at "INTEGR.F", line 1
  </code></pre>
  
  <p>And OCI8 style variable binding does not work:</p>
  
  <pre><code>package main
  
  import(
      "fmt"
      "github.com/jmoiron/sqlx"
      _ "github.com/mattn/go-oci8"
  )
  
  func main(){
      db, err := sqlx.Connect("oci8", "integr/integr@localhost:49161/xe")
      if err != nil {
         fmt.Println(err)
      }
      sql := sqlx.Rebind(sqlx.NAMED,"begin ? := f(?); end;")
      var a int
      _, err = db.Exec(sql, a, 333)
      if err != nil {
         fmt.Println(err)
      }
      fmt.Println(sql)
      fmt.Println(a)
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>begin :arg1 := f(:arg2); end;
  0
  </code></pre>
  
  <p>How to get last inserted ID, or how to get value from stored funcion in Go from Oracle?</p>