1

i don't know how to call function with output REFCURSOR in golang My function

create function find_all_by_user_id(OUT rc_out refcursor, p_user_id bigint) returns refcursor
    language plpgsql
as
$$
BEGIN
    open rc_out for
        select *
        from users u
        where u.id = p_user_id;
END;
$$;

My connect DB

func createDBConn() {
    config := GetDbYmlConfig()
    datasource := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
        config.Host, config.Port, config.Username, config.Password, config.Database,
    )
    Db, err = sql.Open(config.DriverName, datasource)
    fmt.Println(Db.Ping())
    Db.SetMaxOpenConns(config.MaximumPoolSize)
    Db.SetMaxIdleConns(config.MaximumIdle)
    if err != nil {
        panic(err.Error())
    }
}

but i don't know import param and call function

db := database_config.NewConnection()
    defer db.Close()
    tx, err := db.Begin()
    if err != nil {
        return 0, errors.New(constant.Exception)
    }
    sql := fmt.Sprintf(`SELECT %s($1, $2)`, Config.Procedure.FindAllByUsers)

Thank you for your help

3
  • Hi, Did you see this: github.com/lib/pq/issues/434#issuecomment-182360684 Commented Jul 19, 2022 at 5:10
  • @hmmftg I have 1 input param and 1 output param. I try the way you guide but I don't know how to fill in the param Commented Jul 19, 2022 at 7:30
  • I use a := tx.QueryRow("SELECT find_all_by_user_id($1)", userId).Scan(&cursor) but cusor: "<unnamed portal 1>" Commented Jul 19, 2022 at 7:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.