猿问

如何从函数(存储过程)中获取 INOUT 值?与GOLAN

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 = 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)

 }

}

这是恐慌,所以在这里:


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


qq_花开花谢_0
浏览 187回答 1
1回答

一只斗牛犬

您的函数不知道返回什么结果。并且最好在函数中添加新变量。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 VOLATILECOST 100;
随时随地看视频慕课网APP

相关分类

Go
我要回答