首页 > 解决方案 > golang sql package: return INOUT from postgres procedure

问题描述

I am trying to get the value from an INOUT parameter into the invoking go code.

I see some discussion here about oracle drivers supporting this. However, I am using PostgreSQL, and I want to access my INOUT parameter.

I have a postgres procedure that has an INOUT parameter like so:

CREATE OR REPLACE PROCEDURE foo(INOUT my_variable text) LANGUAGE plpgsql ...

I am trying to get the variable by following the docs here. I am writing something like this

import "database/sql"
db, _ := sql.Open(...some connection...)

tx, err := db.Begin()
defer tx.Rollback()

var myVariable string
if _, err := tx.Exec(`call foo(?);`, sql.Named("my_variable", sql.Named("my_variable", sql.Out{Dest: &myVariable, In: true}))

This returns

sql: converting argument with name "my_variable" type: unsupported type sql.Out, a struct

Has anyone been able to make this work with PostgreSQL It seems like this should work, given that it is documented with the package. So, do the postgresql drivers not yet support this, or am I just missing some package import?

标签: postgresqlgo

解决方案


推荐阅读