2015-12-21 60 views
2

ALL 如何從一個功能(存儲過程)一個INOUT參數值,從而,我想v_id值:如何從函數(存儲過程)獲取INOUT值?與GOLANG

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,and我的程序有恐慌:

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 

回答

0

你的功能不知道返回的結果。最好在函數中添加新的變量。

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; 
相關問題