doucan957495
2018-09-02 21:00
浏览 128

使用Golang返回mysql过程

I'm having a problem getting the return of a procedure using golang. If I remove the parameters from the procedure the procedure is executed normally, however, when I need to get the return (OUT parameter) I can not get it and the procedure is not executed. To make the tests simpler and to get help, I created a simple procedure as below and I want to get the return of it.

Follow procedure below:


drop procedure if exists PESSOA_TESTE;

delimiter $$


create procedure PESSOA_TESTE(out psaida int)

begin

set psaida = 2;

end

$$

Now it follows the section of golang code that I'm using to try to get the value 2 specified in the procedure.

var GerenciaBD GERENCIABD
var PontoExecucao int

GerenciaBD.F_GERENCIABD_ABRIR_CONEXAO_MYSQL()

GerenciaBD.DataBase.ExecContext(context.TODO(),"call PESSOA_TESTE", sql.Named("psaida", sql.Out{Dest:&PontoExecucao}))

println(PontoExecucao)

When executing I have as answer 0 and not 2, which is the one specified in the procedure.

My golang version is at 1.10.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douduoting8408 2018-09-04 08:08
    已采纳

    You can see here that usage of out parameter is not yet implemented for https://github.com/go-sql-driver/mysql library.

    I'm not sure what you want to do with your code, but if you want to return only one value, you can do something like this:

    Stored procedure:

    drop procedure if exists PESSOA_TESTE;
    
    delimiter $$
    
    
    create procedure PESSOA_TESTE(IN psaida int)
    
    begin
    SET psaida = 2;
    select psaida;
    
    end
    
    $$
    

    Code:

       var GerenciaBD GERENCIABD
        var PontoExecucao int
    
        GerenciaBD.F_GERENCIABD_ABRIR_CONEXAO_MYSQL()
    
        GerenciaBD.DataBase.QueryRowContext(context.TODO(),"CALL PESSOA_TESTE(?)", PontoExecucao).Scan(&PontoExecucao)
    
        println(PontoExecucao)
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题