gpt4 book ai didi

postgresql - 如何从函数(存储过程)中获取 INPUT 值?与 golang

转载 作者:IT王子 更新时间:2023-10-29 01:48:01 25 4
gpt4 key购买 nike

全部如何从函数(存储过程)获取 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 中的库,我的程序出现了 panic:

  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 = 0
var s string
a, 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)
}
}

这是 panic ,所以在这里:

D:\Go>fs_con.exe
Error 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 +0x29
main.main()
D:/Go/fs_con.go:35 +0x48d

goroutine 5 [chan receive]:
database/sql.(*DB).connectionOpener(0xc0820683c0)
c:/go/src/database/sql/sql.go:634 +0x4c
created by database/sql.Open
c:/go/src/database/sql/sql.go:481 +0x33d

最佳答案

您的函数不知道要返回什么结果。最好在函数中添加新变量。

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;

关于postgresql - 如何从函数(存储过程)中获取 INPUT 值?与 golang ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34396986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com