ALL 如何从函数(存储过程)中获取 INOUT 参数值,因此,我想获取 v_id 的值:CREATE OR REPLACE FUNCTION mytest( IN v_name character varying, INOUT v_id bigint) RETURNS bigint AS$BODY$ Begin v_id := 99; select v_name; return ; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100;当我调用这个 func 时,看看这个,我使用了 github.com/lib/pq 中的库,我的程序出现了恐慌: func main() { dbstring := "user=postgres dbname=xxx password=xxx host=127.0.0.1 port=5432 sslmode=disable" db, err := sql.Open("postgres", dbstring) db.SetMaxIdleConns(4) db.SetMaxOpenConns(10) if err != nil { panic(err) }var mynum int64 = 0var s stringa, err2 := db.Query("SELECT mytest($1::TEXT,$2::bigint)","lihao",mynum)if err2 != nil { fmt.Fprintln(os.Stderr, "Error sending mytest:", err) //os.Exit(1)}else{ fmt.Fprintln(os.Stdout, "a is:",a) fmt.Fprintln(os.Stdout, "mynum is:",mynum)}for a.Next() { err = a.Scan(&s, &mynum) if err != nil { panic(err) } fmt.Fprintln(os.Stdout, "mynum is:",mynum) }}这是恐慌,所以在这里:D:\Go>fs_con.exeError sending mytest: panic: runtime error: invalid memory address or nil pointer dereference[signal 0xc0000005 code=0x0 addr=0x20 pc=0x48d4b9]goroutine 1 [running]:database/sql.(*Rows).Next(0x0, 0xc082028018)c:/go/src/database/sql/sql.go:1633 +0x29main.main()D:/Go/fs_con.go:35 +0x48dgoroutine 5 [chan receive]:database/sql.(*DB).connectionOpener(0xc0820683c0)c:/go/src/database/sql/sql.go:634 +0x4ccreated by database/sql.Openc:/go/src/database/sql/sql.go:481 +0x33d
1 回答
一只斗牛犬
TA贡献1784条经验 获得超2个赞
您的函数不知道返回什么结果。并且最好在函数中添加新变量。
CREATE OR REPLACE FUNCTION mytest(
IN v_name character varying,
IN v_id bigint)
RETURNS bigint AS
$BODY$
DECLARE
result bigint;
BEGIN
result := v_id + 1;
return result;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
- 1 回答
- 0 关注
- 186 浏览
添加回答
举报
0/150
提交
取消