如何在使用Golang的Oracle包(”github.com/sijms/go-ora/v2″)中获取REFCURSOR输出?

huangapple go评论91阅读模式
英文:

How to get output REFCURSOR in package oracle with golang ("github.com/sijms/go-ora/v2")

问题

我在Oracle中有一个包。

CREATE OR REPLACE PACKAGE PKG_TEST IS
    TYPE REFCURSOR IS REF CURSOR;
    PROCEDURE PROC_GET_DUAL(P_CUR OUT REFCURSOR);
END PKG_TEST;
CREATE OR REPLACE PACKAGE BODY PKG_TEST IS
    PROCEDURE PROC_GET_DUAL(P_CUR OUT REFCURSOR) IS
    BEGIN
        OPEN P_CUR FOR
            SELECT * FROM dual;
    END PROC_GET_DUAL;
END PKG_TEST;

我使用Golang("github.com/sijms/go-ora/v2")连接到Oracle并成功进行了ping。我调用了这个包:

var cursor go_ora.RefCursor
fmt.Println(db.Ping())
statement := `BEGIN :x := PKG_TEST.PROC_GET_DUAL(); END;`
_, err := db.Exec(statement, sql.Out{Dest: &cursor})
// 检查错误

defer cursor.Close()
rows, err := cursor.Query()
// 检查错误

var (
    var1 string
)
for rows.Next_() {
    err = rows.Scan(&var1)
    // 检查错误
    fmt.Println(var1)
}

我按照https://github.com/sijms/go-ora/blob/master/README.md中的说明进行操作,并得到了以下结果:

如何在使用Golang的Oracle包(”github.com/sijms/go-ora/v2″)中获取REFCURSOR输出?

如何在使用Golang的Oracle包(”github.com/sijms/go-ora/v2″)中获取REFCURSOR输出?

谢谢你的帮助,对不起,我的英语不是我的母语。

英文:

i have a package in oracle

    CREATE OR REPLACE PACKAGE PKG_TEST IS
    TYPE REFCURSOR IS REF CURSOR;
    PROCEDURE PROC_GET_DUAL(P_CUR OUT REFCURSOR);
end PKG_TEST;
CREATE OR REPLACE PACKAGE BODY PKG_TEST is
    PROCEDURE PROC_GET_DUAL(
        P_CUR OUT REFCURSOR)
        IS
    BEGIN
        OPEN P_CUR FOR
            select * from dual;
    END PROC_GET_DUAL;
end PKG_TEST;

and i use golang ("github.com/sijms/go-ora/v2") to connect oracle and ping success. I call package such as

var cursor go_ora.RefCursor
fmt.Println(db.Ping())
statement := `begin :x := PKG_TEST.PROC_GET_DUAL(); end;`
_, err := db.Exec(statement, sql.Out{Dest: &cursor})
//check errors

defer cursor.Close()
rows, err := cursor.Query()
// check for error

var (
	var1 string
)
for rows.Next_() {
	err = rows.Scan(&var1)
	// check for error
	fmt.Println(var1)
}

I followed the instructions in https://github.com/sijms/go-ora/blob/master/README.md and got this

如何在使用Golang的Oracle包(”github.com/sijms/go-ora/v2″)中获取REFCURSOR输出?

如何在使用Golang的Oracle包(”github.com/sijms/go-ora/v2″)中获取REFCURSOR输出?

Thank you for your help and sorry my English because English is not my native language

答案1

得分: 1

你还需要将 out param 传递给该过程。

statement := `begin PKG_TEST.PROC_GET_DUAL(:1); end;`
_, err := db.Exec(statement, sql.Out{Dest: &cursor})
英文:

You also need to pass the out param to the procedure.

statement := `begin PKG_TEST.PROC_GET_DUAL(:1); end;`
_, err := db.Exec(statement, sql.Out{Dest: &cursor})

huangapple
  • 本文由 发表于 2022年7月14日 16:42:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/72977700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定