我正在尝试使用 godror 在 golang 中检索 PL/SQL 函数的游标结果。
https://github.com/godror/godror
Using cursors returned by stored procedures Use ExecContext and an interface{} or a database/sql/driver.Rows as the sql.Out destination, then either use the driver.Rows interface, or transform it into a regular *sql.Rows with godror.WrapRows, or (since Go 1.12) just Scan into *sql.Rows.
db, err := sql.Open("godror", "api/user@localhost/DEV")
if err != nil {
fmt.Println(err)
panic(err)
}
defer db.Close()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
const query = `
DECLARE
BEGIN
:cursor := pkg_api.my_func(pstr_logn_nm => 'my_name');
END;
`
stmt, err := db.PrepareContext(ctx, query)
if err != nil {
fmt.Println(err)
panic(err)
}
var rows driver.Rows
_, err = stmt.ExecContext(ctx, sql.Out{Dest: &rows})
if err != nil {
fmt.Println(err)
}
var r []driver.Value
err = rows.Next(r)
if err != nil {
fmt.Println(err) // column count mismatch: we have 10 columns, but given 0 destination
}
defer rows.Close()
fmt.Println(rows.Columns()) // [COL_1 COL_2 COL_3 COL_4 COL_5 COL_6 COL_7 COL_8 COL_9 COL_10]
我有一个错误:
column count mismatch: we have 10 columns, but given 0 destination
在我看来,这是因为我必须定义一个包含一些列的接口(interface)。如果我尝试改变
driver.Rows
通过 sql.Rows
我有这个错误:arg: unknown type sql.Rows
如果我尝试创建一个界面,我:
type I interface {
NM() string
}
var r []I
err = rows.Next(r)
我有这个错误:
cannot use r (type []I) as type []driver.Value in argument to rows.Next
如果有人有想法,我也是 Golang 的新手 :)
非常感谢!
最佳答案
这解决了我的问题!
r := make([]driver.Value, len(rows.Columns()))
err = rows.Next(r)
if err != nil {
fmt.Println(err) // column count mismatch: we have 10 columns, but given 0 destination
}
defer rows.Close()
https://github.com/godror/godror/issues/62
关于oracle - Golang godror - 执行一个 PL/SQL 游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62329230/