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

htan
  • 79
  • 6
  • Hi, Did you see this: https://github.com/lib/pq/issues/434#issuecomment-182360684 – hmmftg Jul 19 '22 at 05: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 – htan Jul 19 '22 at 07:30
  • I use a := tx.QueryRow("SELECT find_all_by_user_id($1)", userId).Scan(&cursor) but cusor: "" – htan Jul 19 '22 at 07:33

0 Answers0